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