001/**
002 * DisplayCasesMethod.java
003 * jCOLIBRI2 framework. 
004 * @author Juan A. Recio-Garc�a.
005 * GAIA - Group for Artificial Intelligence Applications
006 * http://gaia.fdi.ucm.es
007 * 24/10/2007
008 */
009package es.ucm.fdi.gaia.jcolibri.extensions.recommendation.casesDisplay;
010
011import org.apache.logging.log4j.LogManager;
012
013import es.ucm.fdi.gaia.jcolibri.cbrcore.Attribute;
014import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
015import es.ucm.fdi.gaia.jcolibri.cbrcore.CaseComponent;
016import es.ucm.fdi.gaia.jcolibri.exception.AttributeAccessException;
017import es.ucm.fdi.gaia.jcolibri.method.gui.utils.LayoutUtils;
018import es.ucm.fdi.gaia.jcolibri.method.gui.utils.WindowUtils;
019import es.ucm.fdi.gaia.jcolibri.util.AttributeUtils;
020
021import java.awt.BorderLayout;
022import java.awt.Dimension;
023import java.awt.event.ActionEvent;
024import java.awt.event.ActionListener;
025import java.util.ArrayList;
026import java.util.List;
027
028import javax.swing.Box;
029import javax.swing.BoxLayout;
030import javax.swing.JButton;
031import javax.swing.JDialog;
032import javax.swing.JLabel;
033import javax.swing.JPanel;
034import javax.swing.SpringLayout;
035
036/**
037 * Displays the cases in a panel with a "previous" and "next" buttons to 
038 * move through the cases.<br> 
039 * This method is useful when showing composed cases.<br> 
040 * It allways shows an "Add to Basket" and a "Quit" buttons. Optionally it
041 * shows an "Edit Query" button for conversational recommenders (not useful 
042 * in one-shot recommenders).<br>
043 * The methods of this class return an UserChoice object.
044 * 
045 * @author Juan A. Recio-Garcia
046 * @author Developed at University College Cork (Ireland) in collaboration with Derek Bridge.
047 * @version 1.0
048 */
049public class DisplayCasesMethod
050{
051    /** Shown cases */
052    private static CBRCase[] _cases;
053    /** ith case currently shown */
054    private static int i = 0;
055    
056    /** Main panel */
057    private static JPanel casesPanel;
058    /** Dialog object */
059    private static JDialog dialog;
060    /** Option to return */
061    private static int returnCode = UserChoice.QUIT;
062    /** Selected case to return */
063    private static CBRCase selectedCase = null;
064    
065    /**
066     * Shows the dialog without the "Edit Query" option
067     * @param cases to display
068     * @return UserChoice object
069     */
070    public static UserChoice displayCases(List<CBRCase> cases)
071    {
072        return displayCases(cases, false);
073    }
074    
075    /**
076     * Shows the dialog without the "Edit Query" option
077     * @param cases to display
078     * @return UserChoice object
079     */
080    public static UserChoice displayCasesWithEditOption(List<CBRCase> cases)
081    {
082        return displayCases(cases, true);
083    }
084    
085    /**
086     * Shows the dialog and allows to choose if show the "Edit Query" option.
087     * @param cases to display.
088     * @param editQueryEnabled decides if show the "Edit Query" option.
089     * @return UserChoice object.
090     */
091    public static UserChoice displayCases(List<CBRCase> cases, boolean editQueryEnabled)
092    {
093        _cases = new CBRCase[cases.size()];
094        cases.toArray(_cases);
095        dialog = new JDialog();
096        dialog.setModal(true);
097        
098        JPanel main = new JPanel();
099        main.setLayout(new BorderLayout());
100        
101        casesPanel = new JPanel();
102        casesPanel.setLayout(new BoxLayout(casesPanel, BoxLayout.Y_AXIS));
103        displayCase();
104        main.add(casesPanel, BorderLayout.CENTER);
105        
106        JPanel actionsPanel = new JPanel();
107        actionsPanel.setLayout(new BoxLayout(actionsPanel,BoxLayout.X_AXIS));
108        
109        JButton next = new JButton("Next >>");
110        next.addActionListener(new ActionListener(){
111            public void actionPerformed(ActionEvent arg0)
112            {
113                i = (i+1)%_cases.length;
114                displayCase();
115            } 
116        });
117        JButton prev = new JButton("<< Previous");
118        prev.addActionListener(new ActionListener(){
119            public void actionPerformed(ActionEvent arg0)
120            {
121                i = (i-1)%_cases.length;
122                displayCase();
123            } 
124        }); 
125        JButton ok = new JButton("Add to Basket");
126        ok.addActionListener(new ActionListener(){
127            public void actionPerformed(ActionEvent arg0)
128            {
129                returnCode = UserChoice.BUY;
130                selectedCase = _cases[i];
131                dialog.setVisible(false);
132            } 
133        });
134        JButton quit = new JButton("Quit");
135        quit.addActionListener(new ActionListener(){
136            public void actionPerformed(ActionEvent arg0)
137            {
138                returnCode = UserChoice.QUIT;
139                dialog.setVisible(false);
140            } 
141        });
142        JButton refine = new JButton("Edit Query");
143        refine.addActionListener(new ActionListener(){
144            public void actionPerformed(ActionEvent arg0)
145            {
146                returnCode = UserChoice.REFINE_QUERY;
147                dialog.setVisible(false);
148            } 
149        });
150        refine.setEnabled(editQueryEnabled);
151        
152        actionsPanel.add(ok);
153        actionsPanel.add(quit);
154        if(editQueryEnabled)
155            actionsPanel.add(refine);
156        actionsPanel.add(Box.createHorizontalGlue());
157        actionsPanel.add(prev);
158        actionsPanel.add(next);
159        
160        main.add(actionsPanel, BorderLayout.SOUTH);
161        
162        dialog.getContentPane().add(main);
163        dialog.pack();
164        dialog.setSize(new Dimension((int)dialog.getSize().getWidth()+50, (int)dialog.getSize().getHeight()));
165        WindowUtils.centerWindow(dialog);
166        dialog.setTitle(cases.size()+" Retrieved cases");
167        dialog.setVisible(true);
168        
169        return new UserChoice(returnCode, selectedCase);
170    }
171
172    /**
173     * Displays a case
174     */
175    private static void displayCase()
176    {
177        casesPanel.removeAll();
178        CBRCase c = _cases[i];
179        
180        displayCaseComponent(c.getDescription(),"Description",casesPanel);
181        displayCaseComponent(c.getSolution(),"Solution",casesPanel);
182        displayCaseComponent(c.getJustificationOfSolution(),"Justification of Solution",casesPanel);
183        displayCaseComponent(c.getResult(),"Result",casesPanel);
184        
185        casesPanel.validate();
186        casesPanel.repaint();
187
188        dialog.setTitle("Case "+(i+1)+"/"+_cases.length);
189    }
190    
191    /**
192     * Displays a case component
193     * @param cc is the case component to display
194     * @param title of the panel
195     * @param parentPanel is the parent panel. 
196     */
197    private static void displayCaseComponent(CaseComponent cc, String title, JPanel parentPanel)
198    {
199        if(cc==null)
200            return;
201        
202        JPanel panel = new JPanel();
203        panel.setBorder(javax.swing.BorderFactory.createTitledBorder(title));
204        
205        panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
206        JPanel simplePanel = new JPanel();
207        simplePanel.setLayout(new SpringLayout());
208        
209        JPanel compoundPanel = new JPanel();
210        compoundPanel.setLayout(new BoxLayout(compoundPanel,BoxLayout.Y_AXIS));
211
212        try
213        {
214            Attribute[] ats = AttributeUtils.getAttributes(cc.getClass());
215            
216            ArrayList<Attribute> compounds = new ArrayList<Attribute>();
217            int sAtts = 0;
218            for(Attribute a: ats)
219            {
220                if(a.getType().equals(CaseComponent.class))
221                {
222                    compounds.add(a);
223                }
224                else
225                {
226                    simplePanel.add(new JLabel(a.getName()));
227                    simplePanel.add(new JLabel(a.getValue(cc).toString()));
228                    sAtts++;
229                }
230            }
231            LayoutUtils.makeCompactGrid(simplePanel, sAtts,2,5,5,15,5);
232            
233            
234            //Now process compounds
235            for(Attribute comp: compounds)
236                displayCaseComponent((CaseComponent)comp.getValue(cc), comp.getName(),compoundPanel);
237            
238            
239            panel.add(simplePanel);
240            panel.add(compoundPanel);
241            
242            casesPanel.add(panel);
243        } catch (AttributeAccessException e)
244        {
245            LogManager.getLogger().error(e);
246        }
247    }
248
249}