SearchAction.java
001 package gate.gui.ontology;
002 
003 import gate.creole.ontology.AnnotationProperty;
004 import gate.creole.ontology.DatatypeProperty;
005 import gate.creole.ontology.Literal;
006 import gate.creole.ontology.OClass;
007 import gate.creole.ontology.OInstance;
008 import gate.creole.ontology.OResource;
009 import gate.creole.ontology.ObjectProperty;
010 import gate.creole.ontology.RDFProperty;
011 import gate.creole.ontology.Restriction;
012 import gate.creole.ontology.SymmetricProperty;
013 import gate.creole.ontology.TransitiveProperty;
014 import gate.gui.MainFrame;
015 
016 import java.awt.BorderLayout;
017 import java.awt.Component;
018 import java.awt.Dimension;
019 import java.awt.FlowLayout;
020 import java.awt.event.ActionEvent;
021 import java.awt.event.KeyAdapter;
022 import java.awt.event.KeyEvent;
023 import java.util.ArrayList;
024 import java.util.Collections;
025 import java.util.HashSet;
026 import java.util.List;
027 import java.util.Set;
028 
029 import javax.swing.AbstractAction;
030 import javax.swing.BoxLayout;
031 import javax.swing.DefaultComboBoxModel;
032 import javax.swing.Icon;
033 import javax.swing.JCheckBox;
034 import javax.swing.JComboBox;
035 import javax.swing.JLabel;
036 import javax.swing.JList;
037 import javax.swing.JOptionPane;
038 import javax.swing.JPanel;
039 import javax.swing.ListCellRenderer;
040 import javax.swing.text.JTextComponent;
041 import javax.swing.tree.TreePath;
042 
043 /**
044  * A Class that provides a GUI to search for a resource in the ontology
045  * editor. It allows looking up for a resource with certain name or to
046  * locate a resource that has a certain value set on the specified
047  * property.
048  
049  @author niraj
050  
051  */
052 @SuppressWarnings("serial")
053 public class SearchAction extends AbstractAction {
054 
055   /**
056    * Constructor
057    
058    @param s - caption of the search button
059    @param icon - icon to be used for the search button
060    @param editor - instance of the ontology editor
061    */
062   public SearchAction(String s, Icon icon, OntologyEditor editor) {
063     super(s, icon);
064     this.ontologyEditor = editor;
065     guiPanel = new JPanel();
066     guiPanel.setLayout(new BoxLayout(guiPanel, BoxLayout.Y_AXIS));
067     JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
068     JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
069     panel1.add(new JLabel("Find what: "));
070 
071     resourcesBox = new JComboBox<OResource>();
072     resourcesBox.setRenderer(new ComboRenderer());
073     resourcesBox.setPrototypeDisplayValue(new RDFPropertyPrototype("this is just an example, not a value. OK?"));
074     
075     resourcesBox.setEditable(true);
076     resourcesBox.setEditable(true);
077     resourcesBox.getEditor().getEditorComponent().addKeyListener(
078             new KeyAdapter() {
079               @Override
080               public void keyReleased(KeyEvent keyevent) {
081                 String s = ((JTextComponent)resourcesBox.getEditor()
082                         .getEditorComponent()).getText();
083                 if(s != null) {
084                   if(keyevent.getKeyCode() != KeyEvent.VK_ENTER) {
085                     HashSet<OResource> set = new HashSet<OResource>();
086                     for(int i = 0; i < resourcesArray.length; i++) {
087                       String s1 = resourcesArray[i].getName();
088                       if(s1.toLowerCase().startsWith(s.toLowerCase())) {
089                         set.add(resourcesArray[i]);
090                       }
091                     }
092 
093                     if(searchInPropertyValues.isSelected()) {
094                       RDFProperty aProp = (RDFProperty)properties
095                               .getSelectedItem();
096                       List<OResource> toAdd = new ArrayList<OResource>();
097                       if(aProp instanceof ObjectProperty) {
098                         @SuppressWarnings("deprecation")
099                         OResource res = ontologyEditor.ontology
100                                 .getOResourceByName(s);
101                         if(res != null) {
102                           toAdd = ontologyEditor.ontology.getOResourcesWith(
103                                   aProp, res);
104                         }
105                       }
106                       else {
107                         toAdd = ontologyEditor.ontology.getOResourcesWith(
108                                 aProp, new Literal(s));
109                       }
110                       set.addAll(toAdd);
111                     }
112                     List<OResource> setList = new ArrayList<OResource>(set);
113                     Collections.sort(setList, new OntologyItemComparator());
114 
115                     DefaultComboBoxModel<OResource> defaultcomboboxmodel = new DefaultComboBoxModel<OResource>(
116                             setList.toArray(new OResource[setList.size()]));
117 
118                     resourcesBox.setModel(defaultcomboboxmodel);
119 
120                     try {
121                       if(!setList.isEmpty()) resourcesBox.showPopup();
122                     }
123                     catch(Exception exception) {
124                     }
125                   }
126                   ((JTextComponent)resourcesBox.getEditor()
127                           .getEditorComponent()).setText(s);
128                 }
129               }
130             });
131     panel1.add(resourcesBox);
132     properties = new JComboBox<RDFProperty>();
133     properties.setRenderer(new ComboRenderer());
134     properties.setEditable(true);
135     properties.setPrototypeDisplayValue(new RDFPropertyPrototype("this is just an example, not a value. OK?"));
136     properties.getEditor().getEditorComponent().addKeyListener(
137             new KeyAdapter() {
138               @Override
139               public void keyReleased(KeyEvent keyevent) {
140                 String s = ((JTextComponent)properties.getEditor()
141                         .getEditorComponent()).getText();
142                 if(s != null) {
143                   if(keyevent.getKeyCode() != KeyEvent.VK_ENTER) {
144                     ArrayList<RDFProperty> arraylist = new ArrayList<RDFProperty>();
145                     for(int i = 0; i < propertiesArray.length; i++) {
146                       String s1 = propertiesArray[i].getName();
147                       if(s1.toLowerCase().startsWith(s.toLowerCase())) {
148                         arraylist.add(propertiesArray[i]);
149                       }
150                     }
151                     Collections.sort(arraylist, new OntologyItemComparator());
152                     DefaultComboBoxModel<RDFProperty> defaultcomboboxmodel = new DefaultComboBoxModel<RDFProperty>(
153                             arraylist.toArray(new RDFProperty[arraylist.size()]));
154                     properties.setModel(defaultcomboboxmodel);
155 
156                     try {
157                       if(!arraylist.isEmpty()) properties.showPopup();
158                     }
159                     catch(Exception exception) {
160                     }
161                   }
162                   ((JTextComponent)properties.getEditor().getEditorComponent())
163                           .setText(s);
164                 }
165               }
166             });
167 
168     searchInPropertyValues = new JCheckBox("In the values of: ");
169     searchInPropertyValues.setSelected(false);
170     panel2.add(searchInPropertyValues);
171     panel2.add(properties);
172 
173     guiPanel.add(panel1);
174     guiPanel.add(panel2);
175     resourcesBox.setPreferredSize(new Dimension(300, resourcesBox.getPreferredSize().height));
176     properties.setPreferredSize(new Dimension(300, resourcesBox.getPreferredSize().height));
177   }
178 
179   /**
180    * Obtains a list of resources from the ontology being displayed in
181    * the ontology editor and invokes the search dialog.
182    */
183   @Override
184   public void actionPerformed(ActionEvent ae) {
185     @SuppressWarnings("deprecation")
186     List<OResource> resources = ontologyEditor.ontology.getAllResources();
187     Collections.sort(resources, new OntologyItemComparator());
188 
189     resourcesArray = new OResource[resources.size()];
190     resourcesArray = resources.toArray(resourcesArray);
191     DefaultComboBoxModel<OResource> defaultcomboboxmodel = new DefaultComboBoxModel<OResource>(
192             resources.toArray(new OResource[resources.size()]));
193     resourcesBox.setModel(defaultcomboboxmodel);
194 
195     Set<RDFProperty> props = ontologyEditor.ontology.getRDFProperties();
196     props.addAll(ontologyEditor.ontology.getAnnotationProperties());
197     props.addAll(ontologyEditor.ontology.getObjectProperties());
198     List<RDFProperty> propsList = new ArrayList<RDFProperty>(props);
199     Collections.sort(propsList, new OntologyItemComparator());
200 
201     propertiesArray = new RDFProperty[props.size()];
202     propertiesArray = props.toArray(propertiesArray);
203     DefaultComboBoxModel<RDFProperty> defaultcomboboxmodel1 = new DefaultComboBoxModel<RDFProperty>(
204             propsList.toArray(new RDFProperty[propsList.size()]));
205     properties.setModel(defaultcomboboxmodel1);
206 
207     resources = null;
208     props = null;
209     propsList = null;
210 
211     int returnValue = JOptionPane.showOptionDialog(MainFrame.getInstance(),
212             guiPanel, "Find Ontology Resource", JOptionPane.OK_CANCEL_OPTION,
213             JOptionPane.QUESTION_MESSAGE, MainFrame.getIcon("search"),
214             new String[] {"Find""Cancel"}"Find");
215     if(returnValue == JOptionPane.OK_OPTION) {
216       Object selectedItem = resourcesBox.getSelectedItem();
217       if(!(selectedItem instanceof OResource))
218         return;
219       
220       OResource selectedR = (OResourceselectedItem;
221       if(selectedR instanceof RDFProperty) {
222         ontologyEditor.propertyTree.setSelectionPath(new TreePath(
223                 ontologyEditor.uri2TreeNodesListMap.get(
224                         selectedR.getONodeID().toString()).get(0).getPath()));
225         ontologyEditor.propertyTree
226                 .scrollPathToVisible(ontologyEditor.propertyTree
227                         .getSelectionPath());
228         ontologyEditor.tabbedPane
229                 .setSelectedComponent(ontologyEditor.propertyScroller);
230       }
231       else {
232         ontologyEditor.tree.setSelectionPath(new TreePath(
233                 ontologyEditor.uri2TreeNodesListMap.get(
234                         selectedR.getONodeID().toString()).get(0).getPath()));
235         ontologyEditor.tree.scrollPathToVisible(ontologyEditor.tree
236                 .getSelectionPath());
237         ontologyEditor.tabbedPane.setSelectedComponent(ontologyEditor.scroller);
238       }
239     }
240   }
241 
242   /**
243    * Box to show the filtered resources based on the user's input in the
244    * find what box.
245    */
246   protected JComboBox<OResource> resourcesBox;
247 
248   /**
249    * main guiPanel that holds the search gui components.
250    */
251   protected JPanel guiPanel;
252 
253   /**
254    * The editor whose ontology is used for searching in.
255    */
256   protected OntologyEditor ontologyEditor;
257 
258   /**
259    * An array that contains a list of resources in which the search
260    * function searches in. This list is updated on each invocation of
261    * the search function.
262    */
263   protected OResource[] resourcesArray = new OResource[0];
264 
265   /**
266    * An array that contains a list of properties in which the search
267    * function searches in. This list is updated on each invocation of
268    * the search function.
269    */
270   protected RDFProperty[] propertiesArray = new RDFProperty[0];
271 
272   /**
273    * combobox that holds the filtered properties based on user's input.
274    */
275   protected JComboBox<RDFProperty> properties;
276 
277   /**
278    * Indicates if the search function should search for the find what
279    * string in the values of the specified property.
280    */
281   protected JCheckBox searchInPropertyValues;
282 
283 
284   /**
285    * Description: This class provides the renderer for the Search comboBox Nodes.
286    @author Niraj Aswani
287    @version 1.0
288    */
289   public class ComboRenderer extends JPanel implements ListCellRenderer<OResource> {
290 
291     /**
292      * Class label is shown using this label
293      */
294     private JLabel label;
295 
296     /**
297      * ICon label
298      */
299     private JLabel iconLabel;
300 
301     /**
302      * Label Panel
303      */
304     private JPanel labelPanel;
305     
306     public ComboRenderer() {
307       label = new JLabel();
308       iconLabel = new JLabel();
309       labelPanel = new JPanel(new BorderLayout(5,10));
310       ((BorderLayoutlabelPanel.getLayout()).setHgap(0);
311       labelPanel.add(label);
312       
313       setLayout(new BorderLayout(5,10));
314       ((BorderLayout)getLayout()).setHgap(1);
315       add(iconLabel, BorderLayout.WEST);
316       add(labelPanel, BorderLayout.CENTER);
317       this.setOpaque(true);
318     }
319 
320     /**
321      * Renderer method
322      */
323     @Override
324     public Component getListCellRendererComponent(JList<? extends OResource> list, OResource value,
325         int row, boolean isSelected, boolean hasFocus) {
326 
327      
328       /*if (!(value instanceof OResource)) {
329         label.setBackground(Color.white);
330         return this;
331       }*/
332       
333       javax.swing.Icon icon = null;
334       String conceptName = value.getName();
335       iconLabel.setVisible(true);
336       if(value instanceof Restriction) {
337         icon = MainFrame.getIcon("ontology-restriction");
338       }
339       else if(value instanceof OClass) {
340         icon = MainFrame.getIcon("ontology-class");
341       }
342       else if(value instanceof OInstance) {
343         icon = MainFrame.getIcon("ontology-instance");
344       }
345       else if(value instanceof AnnotationProperty) {
346         icon = MainFrame.getIcon("ontology-annotation-property");
347       }
348       else if(value instanceof DatatypeProperty) {
349         icon = MainFrame.getIcon("ontology-datatype-property");
350       }
351       else if(value instanceof SymmetricProperty) {
352         icon = MainFrame.getIcon("ontology-symmetric-property");
353       }
354       else if(value instanceof TransitiveProperty) {
355         icon = MainFrame.getIcon("ontology-transitive-property");
356       }
357       else if(value instanceof ObjectProperty) {
358         icon = MainFrame.getIcon("ontology-object-property");
359       }
360       else if(value instanceof RDFProperty) {
361         icon = MainFrame.getIcon("ontology-rdf-property");
362       }      
363       
364       iconLabel.setIcon(icon);
365       label.setText(conceptName);
366       label.setFont(list.getFont());
367       return this;
368     }
369   }  
370   
371 
372 }