001/**
002 * ObtainQueryWithFormMethod.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 * 23/10/2007
008 */
009package es.ucm.fdi.gaia.jcolibri.method.gui.formFilling;
010
011import java.awt.BorderLayout;
012import java.awt.event.ActionEvent;
013import java.awt.event.ActionListener;
014import java.util.ArrayList;
015import java.util.Collection;
016import java.util.HashMap;
017import java.util.Hashtable;
018import java.util.Map;
019
020import javax.swing.BoxLayout;
021import javax.swing.JButton;
022import javax.swing.JDialog;
023import javax.swing.JLabel;
024import javax.swing.JPanel;
025import javax.swing.SpringLayout;
026
027import org.apache.logging.log4j.LogManager;
028
029import es.ucm.fdi.gaia.jcolibri.cbrcore.Attribute;
030import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRQuery;
031import es.ucm.fdi.gaia.jcolibri.cbrcore.CaseComponent;
032import es.ucm.fdi.gaia.jcolibri.exception.AttributeAccessException;
033import es.ucm.fdi.gaia.jcolibri.method.gui.editors.ParameterEditor;
034import es.ucm.fdi.gaia.jcolibri.method.gui.editors.ParameterEditorFactory;
035import es.ucm.fdi.gaia.jcolibri.method.gui.utils.LayoutUtils;
036import es.ucm.fdi.gaia.jcolibri.method.gui.utils.WindowUtils;
037import es.ucm.fdi.gaia.jcolibri.util.AttributeUtils;
038
039/**
040 * Shows a from to obtain the query.<br>
041 * The methods of this class allow to use default values (read from the query),
042 * hide some attributes and specify the label shown with each attribute.
043 * 
044 * @author Juan A. Recio-Garcia
045 * @version 1.0
046 *
047 */
048public class ObtainQueryWithFormMethod
049{
050    private static Hashtable<Attribute, ParameterEditor> editors;
051    private static JDialog dialog;
052
053    /**
054     * Obtains a query without showing initila values.
055     * It shows every attribute and uses the attribute name as the label.
056     * @param query to obtain.
057     */
058    public static void obtainQueryWithoutInitialValues(CBRQuery query, Collection<Attribute> hiddenAttributes, Map<Attribute, String> labels)
059    {
060        obtainQuery(query, false, hiddenAttributes, labels);
061    }
062    
063    /**
064     * Obtains a query showing the initial values of the received query object.
065     * It shows every attribute and uses the attribute name as the label.
066     * @param query with the initial values and where the user new values are stored
067     */
068    public static void obtainQueryWithInitialValues(CBRQuery query, Collection<Attribute> hiddenAttributes, Map<Attribute, String> labels)
069    {
070        obtainQuery(query, true,  hiddenAttributes, labels);
071    }
072    
073    /**
074     * Obtains the query showing a form
075     * @param query to fill with the obtained values and containing the initial values
076     * @param useQueryvalues indicates if the query values are shown by default
077     * @param hiddenAttributes is a list of not shown attributes
078     * @param labels for each attribute. If there is no label for an attribute, 
079     * the attribute name is used.
080     */
081    static void obtainQuery(CBRQuery query, boolean useQueryvalues, Collection<Attribute> hiddenAttributes, Map<Attribute, String> labels)
082    {
083        dialog = new JDialog();
084        dialog.setModal(true);
085        
086        editors = new Hashtable<Attribute, ParameterEditor>();
087        
088        JPanel panel = new JPanel();
089        
090        addAttributes(query.getDescription(), panel, hiddenAttributes, labels);
091        
092        if(useQueryvalues)
093        {
094            for(Attribute a: editors.keySet())
095            {
096                ParameterEditor editor = editors.get(a);
097                editor.setEditorValue(AttributeUtils.findValue(a, query));
098            }
099        }
100        
101        dialog.getContentPane().setLayout(new BorderLayout());
102        dialog.getContentPane().add(panel,BorderLayout.CENTER);
103        
104        JPanel okPanel = new JPanel();
105        JButton okButton = new JButton("Ok");
106        okButton.addActionListener(new ActionListener(){
107            public void actionPerformed(ActionEvent arg0)
108            {
109                dialog.setVisible(false);       
110            }
111        });
112        okPanel.add(okButton);
113        dialog.getContentPane().add(okPanel,BorderLayout.SOUTH);
114        
115        
116        dialog.pack();
117        WindowUtils.centerWindow(dialog);
118        dialog.setTitle("Query");
119        dialog.setVisible(true);
120        
121        for(Attribute a: editors.keySet())
122        {
123            Object value = editors.get(a).getEditorValue();
124            AttributeUtils.setValue(a, query, value);
125        }
126        System.out.println(query);
127    }
128    
129    /**
130     * Adds Parameter editor to the panel for each attribute in the CaseComponent
131     * @param cc CaseComponent with the attributes
132     * @param panel to add the ParameterEditor
133     * @param hiddenAttributes attributes not shown
134     * @param labels for the attributes
135     */
136    private static void addAttributes(CaseComponent cc, JPanel panel, Collection<Attribute> hiddenAttributes, Map<Attribute, String> labels)
137    {
138        panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
139        JPanel simplePanel = new JPanel();
140        simplePanel.setLayout(new SpringLayout());
141        
142        JPanel compoundPanel = new JPanel();
143        compoundPanel.setLayout(new BoxLayout(compoundPanel,BoxLayout.Y_AXIS));
144
145        try
146        {
147            if(hiddenAttributes == null)
148                hiddenAttributes = new ArrayList<>();
149            if(labels == null)
150                labels = new HashMap<>();
151            
152            Attribute[] ats = AttributeUtils.getAttributes(cc.getClass());
153            Attribute id = cc.getIdAttribute();
154            
155            ArrayList<Attribute> compounds = new ArrayList<Attribute>();
156            int sAtts = 0;
157            for(Attribute a: ats)
158            {
159                if(a.equals(id))
160                    continue;
161                else if(a.getType().equals(CaseComponent.class))
162                {
163                    compounds.add(a);
164                }
165                else if(hiddenAttributes.contains(a))
166                    continue;
167                else
168                {
169                    String label = labels.get(a);
170                    if(label==null)
171                        label = a.getName();
172                    simplePanel.add(new JLabel(label));
173                    ParameterEditor pe = ParameterEditorFactory.getEditor(a.getType());
174                    simplePanel.add(pe.getJComponent());
175                    editors.put(a,pe);
176                    sAtts++;
177                }
178            }
179            LayoutUtils.makeCompactGrid(simplePanel, sAtts,2,5,5,5,5);
180            
181            
182            //Now process compounds
183            for(Attribute comp: compounds)
184            {
185                JPanel subpanel = new JPanel();
186                subpanel.setBorder(javax.swing.BorderFactory.createTitledBorder(comp.getName()));
187                addAttributes((CaseComponent)comp.getValue(cc),subpanel,hiddenAttributes,labels);
188                compoundPanel.add(subpanel);
189            }
190            
191            panel.add(simplePanel);
192            panel.add(compoundPanel);
193        } catch (AttributeAccessException e)
194        {
195            LogManager.getLogger(AttributeUtils.class).error(e);
196        }
197    }
198    
199}