001/**
002 * DisplayCasesTableMethod.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 * 25/10/2007
008 */
009package es.ucm.fdi.gaia.jcolibri.extensions.recommendation.casesDisplay;
010
011import java.awt.BorderLayout;
012import java.awt.event.ActionEvent;
013import java.awt.event.ActionListener;
014import java.util.Collection;
015import java.util.List;
016import java.util.Vector;
017
018import javax.swing.Box;
019import javax.swing.BoxLayout;
020import javax.swing.ButtonGroup;
021import javax.swing.JButton;
022import javax.swing.JCheckBox;
023import javax.swing.JDialog;
024import javax.swing.JOptionPane;
025import javax.swing.JPanel;
026import javax.swing.JRadioButton;
027import javax.swing.JScrollPane;
028import javax.swing.JTable;
029import javax.swing.event.TableModelEvent;
030import javax.swing.table.TableModel;
031
032import es.ucm.fdi.gaia.jcolibri.cbrcore.Attribute;
033import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
034import es.ucm.fdi.gaia.jcolibri.cbrcore.CaseComponent;
035import es.ucm.fdi.gaia.jcolibri.extensions.recommendation.casesDisplay.utils.RadioButtonEditor;
036import es.ucm.fdi.gaia.jcolibri.extensions.recommendation.casesDisplay.utils.RadioButtonTableRenderer;
037import es.ucm.fdi.gaia.jcolibri.method.gui.utils.WindowUtils;
038import es.ucm.fdi.gaia.jcolibri.util.AttributeUtils;
039
040/**
041 * Shows cases in a table allowing to select one.<br>
042 * This method is not very suitable for composed cases
043 * (cases with nested CaseComponents) because it doesn't
044 * reflects that structure.<br>
045 * It allways shows an "Add to Basket" and a "Quit" buttons. Optionally it
046 * shows an "Edit Query" button for conversational recommenders (not useful 
047 * in one-shot recommenders).<br>
048 * The methods of this class return an UserChoice object.
049 * 
050 * @author Juan A. Recio-Garcia
051 * @author Developed at University College Cork (Ireland) in collaboration with Derek Bridge.
052 * @version 1.0
053 */
054public class DisplayCasesTableMethod
055{
056    private static JDialog dialog;
057    
058    private static ButtonGroup group;
059    private static int returnCode = UserChoice.QUIT;
060    private static CBRCase selectedCase = null;
061    private static JTable table;
062    
063    /** Shown cases */
064    private static CBRCase[] _cases;
065    
066    /**
067     * Display options for the methods.
068     * In BASIC mode the dialog only shows the buy and quit buttons.
069     * The EDIT_QUERY option returns a UserChoice.EDIT_QUERY value without any case selected.
070     * The SELECT_CASE option returns a UserChoice.EDIT_QUERY value but with a case selected from the list.
071     * @author Juan A. Recio-Garcia
072     * @version 1.0
073     *
074     */
075    public enum DisplayOption {BASIC, EDIT_QUERY, SELECT_CASE};
076    
077    /**
078     * Shows the dialog without the "Edit Query" option
079     * @param cases to display
080     * @return UserChoice object
081     */
082    public static UserChoice displayCasesInTableBasic(Collection<CBRCase> cases)
083    {
084        return displayCasesInTable(cases, DisplayOption.BASIC, null);
085    }
086
087    /**
088     * Shows the dialog without the "Edit Query" option
089     * @param cases to display
090     * @return UserChoice object
091     */
092    public static UserChoice displayCasesInTableEditQuery(Collection<CBRCase> cases)
093    {
094        return displayCasesInTable(cases, DisplayOption.EDIT_QUERY, "Refine Query");
095    }
096    
097    /**
098     * Shows the dialog without the "Edit Query" option
099     * @param cases to display
100     * @return UserChoice object
101     */
102    public static UserChoice displayCasesInTableSelectCase(Collection<CBRCase> cases)
103    {
104        return displayCasesInTable(cases, DisplayOption.SELECT_CASE, "Something like this");
105    }
106    
107    
108    /**
109     * Shows the dialog and allows to choose if show the "Edit Query" option.
110     * @param cases to display.
111     * @return UserChoice object.
112     */
113    static UserChoice displayCasesInTable(Collection<CBRCase> cases, DisplayOption displayOption, String optionLabel)
114    {
115        _cases = new CBRCase[cases.size()];
116        cases.toArray(_cases);
117        
118        dialog = new JDialog();
119        dialog.setTitle(cases.size()+" Retrieved cases");
120        dialog.setModal(true);
121        
122        if(cases.size()==0)
123            return new UserChoice(UserChoice.REFINE_QUERY, selectedCase);
124
125        Vector<Object> columnNames = extractColumnNames(cases.iterator().next());
126        
127
128        Vector<Object> rows = new Vector<Object>();
129        for(CBRCase c: cases)
130            rows.add(getAttributes(c));
131        
132        table = new JTable(rows, columnNames){
133
134            private static final long serialVersionUID = 1L;
135
136            public void tableChanged(TableModelEvent e) {
137                        super.tableChanged(e);
138                        repaint();
139              }
140
141        };
142        
143        table.getColumn("Select").setCellRenderer(
144                new RadioButtonTableRenderer());
145        table.getColumn("Select").setCellEditor(
146                new RadioButtonEditor(new JCheckBox()));
147        
148        group = new ButtonGroup();
149        TableModel tm = table.getModel();
150        for(int i=0; i<tm.getRowCount();i++)
151            group.add((JRadioButton) tm.getValueAt(i, 0));
152        
153        
154        JScrollPane scrollPane = new JScrollPane(table);
155        table.setFillsViewportHeight(true);
156        
157        JPanel mainPanel = new JPanel();
158        mainPanel.setLayout(new BorderLayout());
159        mainPanel.add(scrollPane,BorderLayout.CENTER);
160        
161        JPanel actionsPanel = new JPanel();
162        actionsPanel.setLayout(new BoxLayout(actionsPanel,BoxLayout.X_AXIS));
163        
164        JButton ok = new JButton("Add to Basket");
165        ok.addActionListener(new ActionListener(){
166            public void actionPerformed(ActionEvent arg0)
167            {
168                
169                if(table.getSelectedRow() == -1)
170                    JOptionPane.showMessageDialog(dialog, "You should choose one item", "Error", JOptionPane.ERROR_MESSAGE);
171                else
172                {
173                    returnCode = UserChoice.BUY;
174                    selectedCase = _cases[table.getSelectedRow()];
175                    dialog.setVisible(false);
176                }
177            } 
178        });
179        JButton quit = new JButton("Quit");
180        quit.addActionListener(new ActionListener(){
181            public void actionPerformed(ActionEvent arg0)
182            {
183                returnCode = UserChoice.QUIT;
184                dialog.setVisible(false);
185            } 
186        });
187        JButton refine = new JButton(optionLabel);
188        refine.addActionListener(new ActionListener(){
189            public void actionPerformed(ActionEvent arg0)
190            {
191                
192                returnCode = UserChoice.REFINE_QUERY;
193                dialog.setVisible(false);
194            } 
195        });
196        JButton select = new JButton(optionLabel);
197        select.addActionListener(new ActionListener(){
198            public void actionPerformed(ActionEvent arg0)
199            {
200                if(table.getSelectedRow() == -1)
201                    JOptionPane.showMessageDialog(dialog, "You should choose one item", "Error", JOptionPane.ERROR_MESSAGE);
202                else
203                {
204                    returnCode = UserChoice.REFINE_QUERY;
205                    selectedCase = _cases[table.getSelectedRow()];
206                    dialog.setVisible(false);
207                }
208            } 
209        });     
210        actionsPanel.add(Box.createHorizontalGlue());
211        actionsPanel.add(ok);
212        actionsPanel.add(quit);
213        if(displayOption == DisplayOption.EDIT_QUERY)
214            actionsPanel.add(refine);
215        if(displayOption == DisplayOption.SELECT_CASE)
216            actionsPanel.add(select);
217        actionsPanel.add(Box.createHorizontalGlue());
218
219        
220        mainPanel.add(actionsPanel, BorderLayout.SOUTH);
221        
222        dialog.getContentPane().add(mainPanel);
223        dialog.setSize(800, 200);
224        WindowUtils.centerWindow(dialog);
225        dialog.setVisible(true);
226
227        
228        return new UserChoice(returnCode, selectedCase);
229    }
230    
231    /**
232     * Returns a list with the values of the attributes of a case.
233     * @param c the case
234     * @return Vector of Objects
235     */
236    private static Vector<Object> getAttributes(CBRCase c)
237    {
238        Vector<Object> res = new Vector<Object>();
239        
240        JRadioButton rb = new JRadioButton(c.getID().toString());
241        res.add(rb);
242        
243        getAttributes(c.getDescription(), res);
244        getAttributes(c.getSolution(), res);
245        getAttributes(c.getJustificationOfSolution(), res);
246        getAttributes(c.getResult(), res);
247        
248        return res;
249    }
250    
251    /**
252     * Fills the list with the values of the attributes of the CaseComponent.
253     * @param cc CaseComponent
254     * @param res List to fill
255     */
256    private static void getAttributes(CaseComponent cc, Vector<Object> res)
257    {
258        List<Attribute> atts = AttributeUtils.getAttributes(cc);
259        if(atts == null)
260            return;
261
262        Attribute id = cc.getIdAttribute();
263        for(Attribute a: atts)
264        {
265            if(!a.equals(id))
266                res.add(AttributeUtils.findValue(a, cc));
267        }
268    }
269    
270    /**
271     * Obtains the column names of the tables from a case.
272     * (It obtains the names of the attributes)
273     * @param c is any case.
274     * @return a list of objects
275     */
276    private static Vector<Object> extractColumnNames(CBRCase c)
277    {
278        Vector<Object> res = new Vector<Object>();
279        res.add("Select");
280        extractColumnNames(c.getDescription(),res);
281        extractColumnNames(c.getSolution(),res);
282        extractColumnNames(c.getJustificationOfSolution(),res);
283        extractColumnNames(c.getResult(),res);
284        return res;
285    }
286    
287    /**
288     * Extracts the column names (names of the attributes) from
289     * a CaseComponent.
290     * @param cc is the CaseComponent.
291     * @param res List to fill.
292     */
293    private static void extractColumnNames(CaseComponent cc, Vector<Object> res)
294    {
295        List<Attribute> atts = AttributeUtils.getAttributes(cc);
296        if(atts == null)
297            return;
298        Attribute id = cc.getIdAttribute();
299        for(Attribute a: atts)
300        {
301            if(!a.equals(id))
302                res.add(a.getName());
303        }
304    }
305    
306}