001    package jcolibri.extensions.recommendation.navigationByAsking;
002    
003    import java.awt.BorderLayout;
004    import java.awt.event.ActionEvent;
005    import java.awt.event.ActionListener;
006    import java.util.Collection;
007    import java.util.HashSet;
008    import java.util.Map;
009    
010    import javax.swing.JButton;
011    import javax.swing.JDialog;
012    import javax.swing.JLabel;
013    import javax.swing.JPanel;
014    import javax.swing.SpringLayout;
015    
016    import jcolibri.cbrcore.Attribute;
017    import jcolibri.cbrcore.CBRCase;
018    import jcolibri.cbrcore.CBRQuery;
019    import jcolibri.method.gui.editors.ParameterEditor;
020    import jcolibri.method.gui.editors.ParameterEditorFactory;
021    import jcolibri.method.gui.utils.LayoutUtils;
022    import jcolibri.method.gui.utils.WindowUtils;
023    import jcolibri.util.AttributeUtils;
024    
025    /**
026     * Obtains the query asking for the value of an attribute.
027     * 
028     * @author Juan A. Recio-Garcia
029     * @author Developed at University College Cork (Ireland) in collaboration with Derek Bridge.
030     * @version 1.0
031     *
032     */
033    public class ObtainQueryWithAttributeQuestionMethod
034    {
035        private static JDialog dialog;
036        
037        /**
038         * Modifies the query with value of an attribute. It shows a dialog
039         * to obtain that value .<br>
040         * If the attribute parameter is null, this method does nothing and returns false.
041         * It returns true in a.o.c. This servers to know when there are not more paramethers
042         * to be asked and the conversation must finish. The condition of finishing a conversation
043         * is calculated by the SelectAttributeMethod executed before. The Attribute
044         * returned by these methods is the input "attribute" here. If a SelectAttributMethod
045         * returns null, it means that there are not any other attributes to ask.
046         *  
047         * @param query to modify.
048         * @param attribute to ask.
049         * @param labels for the attribute.
050         * @param cases used to find the available values presented to the user.
051         * @return true 
052         */
053        public static boolean obtainQueryWithAttributeQuestion(CBRQuery query, Attribute attribute, Map<Attribute, String> labels, Collection<CBRCase> cases)
054        {
055            if(attribute == null)
056                return false;
057            
058            dialog = new JDialog();
059            dialog.setModal(true);
060            dialog.setTitle("Enter query value");
061            
062            String info = "Please specify following property to focus the retrieval: ";
063            Object currentValue = AttributeUtils.findValue(attribute, query);
064            if(currentValue != null)
065                info+="(current value = "+currentValue+")";
066            JLabel infoLabel = new JLabel(info);
067            
068            JPanel main = new JPanel();
069            main.setLayout(new SpringLayout());
070            String label = labels.get(attribute);
071            if(label==null)
072                label = attribute.getName();
073            main.add(new JLabel(label));
074            
075            HashSet<Object> values = new HashSet<Object>();
076            for(CBRCase c: cases)
077                values.add(AttributeUtils.findValue(attribute, c));
078            
079            ParameterEditor pe = ParameterEditorFactory.getEditor(attribute.getType(), values);
080            main.add(pe.getJComponent());
081            
082    
083            JButton ok = new JButton("OK");
084            ok.addActionListener(new ActionListener(){
085                public void actionPerformed(ActionEvent arg0)
086                {
087                    dialog.setVisible(false);
088                }});
089            JPanel southPanel = new JPanel();
090            southPanel.add(ok);
091            
092            dialog.getContentPane().setLayout(new BorderLayout());
093            dialog.getContentPane().add(infoLabel,BorderLayout.NORTH);
094            dialog.getContentPane().add(main, BorderLayout.CENTER);
095            dialog.getContentPane().add(southPanel, BorderLayout.SOUTH);
096    
097            
098            //dialog.setSize(new Dimension(500,170));
099            LayoutUtils.makeCompactGrid(main, 1, 2, 5, 5, 5, 5);
100            dialog.pack();
101            WindowUtils.centerWindow(dialog);
102            dialog.setVisible(true);
103            
104            AttributeUtils.setValue(attribute, query, pe.getEditorValue());
105            return true;
106        }
107    }