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