View Javadoc

1   /*
2    * $Id: NewRestrictionWizard.java,v 1.14 2005/08/12 15:57:46 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8   
9   package com.bbn.swede.editor.visualeditors.restriction;
10  
11  import java.net.URI;
12  import java.net.URISyntaxException;
13  import java.util.List;
14  
15  import org.eclipse.jface.viewers.ISelectionChangedListener;
16  import org.eclipse.jface.viewers.IStructuredSelection;
17  import org.eclipse.jface.viewers.LabelProvider;
18  import org.eclipse.jface.viewers.ListViewer;
19  import org.eclipse.jface.viewers.SelectionChangedEvent;
20  import org.eclipse.jface.viewers.StructuredSelection;
21  import org.eclipse.jface.viewers.Viewer;
22  import org.eclipse.jface.viewers.ViewerFilter;
23  import org.eclipse.jface.viewers.ViewerSorter;
24  import org.eclipse.jface.wizard.Wizard;
25  import org.eclipse.jface.wizard.WizardDialog;
26  import org.eclipse.jface.wizard.WizardPage;
27  import org.eclipse.swt.SWT;
28  import org.eclipse.swt.events.SelectionAdapter;
29  import org.eclipse.swt.events.SelectionEvent;
30  import org.eclipse.swt.layout.GridData;
31  import org.eclipse.swt.layout.GridLayout;
32  import org.eclipse.swt.widgets.Button;
33  import org.eclipse.swt.widgets.Composite;
34  import org.eclipse.swt.widgets.Group;
35  import org.eclipse.swt.widgets.Label;
36  import org.eclipse.ui.internal.Workbench;
37  
38  import com.bbn.swede.core.OWLCore;
39  import com.bbn.swede.core.dom.ClassNode;
40  import com.bbn.swede.core.dom.DefaultOASTRunnable;
41  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
42  import com.bbn.swede.core.dom.OAST;
43  import com.bbn.swede.core.dom.OASTException;
44  import com.bbn.swede.core.dom.OASTNode;
45  import com.bbn.swede.core.dom.owl.Restriction;
46  import com.bbn.swede.core.dom.rdf.Description;
47  import com.bbn.swede.core.dom.rdf.Rdf;
48  import com.bbn.swede.editor.EditorPlugin;
49  import com.bbn.swede.ui.wizards.NewOWLClassWizard;
50  import com.bbn.swede.ui.wizards.NewOWLPropertyWizard;
51  
52  /***
53   * A dialog tied to the Restriction Editor used to create a new Restriction.
54   * 
55   * 
56   * @author aperezlo
57   */
58  public class NewRestrictionWizard extends Wizard
59  {
60     private Restriction _restriction;
61  
62     private List _properties;
63  
64     private List _classes;
65  
66     private RestrictionEditorInspector _inspector;
67  
68     private IOWLAbstractSyntaxTree _oast;
69  
70     private PageOne _pageOne;
71  
72     /***
73      * Comment for <code>THE_NAMESPACE_FILTER</code>
74      * 
75      * This filter accepts as input a URI or a String, and will allow through all
76      * URIs that do not begin with the OWL, RDF or RDFS namespaces.
77      *  
78      */
79     public static final ViewerFilter THE_LANGUAGE_NAMESPACE_FILTER = new ViewerFilter()
80     {
81        public boolean select(Viewer viewer, Object parentElement, Object element)
82        {
83           boolean toReturn = true;
84           String uri = null;
85           if(element instanceof java.net.URI)
86           {
87              uri = ((java.net.URI)element).toString();
88           }
89           if(element instanceof String)
90           {
91              uri = (String)element;
92           }
93           if(uri != null)
94           {
95              if((uri.startsWith(OASTNode.S_OWL_URI))
96                 || (uri.startsWith(OASTNode.S_RDF_URI))
97                 || (uri.startsWith(OASTNode.S_RDFS_URI)))
98              {
99                 toReturn = false;
100             }
101          }
102          return toReturn;
103       }
104    };
105 
106    /***
107     * A filter that identifies items in the XSD namespace.
108     */
109    public static final ViewerFilter THE_XSD_NAMESPACE_FILTER = new ViewerFilter()
110    {
111       public boolean select(Viewer viewer, Object parentElement, Object element)
112       {
113          boolean toReturn = true;
114          String uri = null;
115          if(element instanceof java.net.URI)
116          {
117             uri = ((java.net.URI)element).toString();
118          }
119          if(element instanceof String)
120          {
121             uri = (String)element;
122          }
123          if(uri != null)
124          {
125             if(uri.startsWith(OASTNode.S_XSD_URI))
126             {
127                toReturn = false;
128             }
129          }
130 
131          return toReturn;
132       }
133    };
134 
135    public NewRestrictionWizard(RestrictionEditorInspector inspector,
136       IOWLAbstractSyntaxTree oast, List classes, List properties)
137    {
138       _properties = properties;
139       _classes = classes;
140       _inspector = inspector;
141       _oast = oast;
142       _pageOne = new PageOne();
143       addPage(_pageOne);
144    }
145 
146    /***
147     * Allows for initialization and preselection of a particular class.
148     * 
149     * @param klass a URI specifying the class to select
150     */
151    public void setSelectedClass(URI klass)
152    {
153       _pageOne.setPreselectedClass(klass);
154    }
155 
156    /***
157     * Retrieves the URI of the currently selected class.
158     * @return the class selected by the user in this Wizard's dialog
159     */
160    public URI getSelectedClass()
161    {
162       return _pageOne.getSelectedClass();
163    }
164 
165    /***
166     * Retrieves the URI of the currently selected property.
167     * @return the property selected by the user in this Wizard's dialog
168     */
169    public URI getSelectedProperty()
170    {
171       return _pageOne.getSelectedProperty();
172    }
173 
174    /*
175     *  (non-Javadoc)
176     * @see org.eclipse.jface.wizard.IWizard#performFinish()
177     */
178    public boolean performFinish()
179    {
180       try
181       {
182          _oast.run(new DefaultOASTRunnable()
183          {
184             public void run() throws OASTException
185             {
186                String classURI = _pageOne.getSelectedClass().toString();
187                ClassNode parentNode = (ClassNode)_oast.getNode(classURI);
188                Restriction newRestriction = new Restriction();
189                newRestriction.addPropertyResource("owl:onProperty", _pageOne
190                   .getSelectedProperty().toString());
191                if(parentNode != null)
192                {
193                   parentNode.addPropertyTag("rdfs:subClassOf", newRestriction);
194                }
195                else
196                {
197                   Description newDesc = new Description();
198                   newDesc.setAttribute("rdf:about", classURI);
199                   newDesc.addPropertyTag("rdfs:subClassOf", newRestriction);
200                   OASTNode[] nodes = _oast.getRoot().getNodesOfType(
201                      OASTNode.RDF_RDF);
202                   if(nodes.length != 1)
203                   {
204                      //TODO: indicate error & cancel dialog?
205                   }
206                   else
207                   {
208                      Rdf rdf = (Rdf)nodes[0];
209                      rdf.appendChildTag(newDesc);
210                   }
211                }
212             }
213          });
214       }
215       catch (OASTException e)
216       {
217          //This should never happen because the restriction editor disables the
218          //edit controls for read-only docs, but make sure it gets reported if
219          //it does.
220          OWLCore.logWarning(EditorPlugin.getID(), e.getMessage(), e);
221       }
222 
223       return true;
224    }
225 
226    private final class PageOne extends WizardPage implements
227       ISelectionChangedListener
228    {
229       private ListViewer _propertiesViewer;
230 
231       private ListViewer _classesViewer;
232 
233       private java.net.URI _selectedClass;
234 
235       private java.net.URI _selectedProperty;
236 
237       private java.net.URI _preselectedClass;
238 
239       private final class URILabelProvider extends LabelProvider
240       {
241          private OAST _oast;
242 
243          /***
244           * Creates a label provider that displays URIs based on the namespaces
245           * defined in an OAST.
246           * @param oast The OAST
247           */
248          public URILabelProvider(IOWLAbstractSyntaxTree oast)
249          {
250             _oast = (OAST)oast;
251          }
252 
253          public String getText(Object element)
254          {
255             String toReturn = super.getText(element);
256             java.net.URI uri = null;
257             String uriString = null;
258             if(element instanceof String)
259             {
260                uriString = (String)element;
261             }
262             if(element instanceof java.net.URI)
263             {
264                uri = (java.net.URI)element;
265                uriString = uri.toString();
266             }
267             if(uriString != null)
268             {
269                toReturn = _oast.getQNameForURI(uriString);
270             }
271             return toReturn;
272          }
273       }
274 
275       /***
276        * Creates the first page of the new restriction dialog.
277        */
278       public PageOne()
279       {
280          super("pageOne");
281          setTitle("New Restriction");
282          setMessage("Use this page to create a new restriction.");
283       }
284 
285       /***
286        * Sets the class to be preselected - this must be called before the
287        * dialog is opened.
288        * 
289        * @param klass the class
290        */
291       public void setPreselectedClass(java.net.URI klass)
292       {
293          _preselectedClass = klass;
294       }
295 
296       /***
297        * Retrieves the URI of the currently selected class.
298        * @return the class selected by the user
299        */
300       public URI getSelectedClass()
301       {
302          return _selectedClass;
303       }
304 
305       /***
306        * Retrieves the URI fo the currently selected property.
307        * @return the property selected by the user
308        */
309       public URI getSelectedProperty()
310       {
311          return _selectedProperty;
312       }
313 
314       /*
315        *  (non-Javadoc)
316        * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
317        */
318       public void createControl(Composite parent)
319       {
320          GridData gd = null;
321          Composite mainComposite = new Composite(parent, parent.getStyle());
322          mainComposite.setFont(parent.getFont());
323          mainComposite.setLayout(new GridLayout());
324 
325          // class group construction and layout
326 
327          Group classGroup = new Group(mainComposite, mainComposite.getStyle());
328          classGroup.setFont(mainComposite.getFont());
329          classGroup.setLayout(new GridLayout(1, false));
330          gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL
331             | GridData.GRAB_HORIZONTAL);
332          classGroup.setLayoutData(gd);
333          classGroup.setText("Classes");
334 
335          Label classLabel = new Label(classGroup, classGroup.getStyle());
336          classLabel.setFont(classGroup.getFont());
337          classLabel
338             .setText("Select a class which will be a subclass of this restriction.");
339 
340          _classesViewer = new ListViewer(classGroup, mainComposite.getStyle()
341             | SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
342          _classesViewer.getControl().setFont(classGroup.getFont());
343          _classesViewer.addFilter(THE_LANGUAGE_NAMESPACE_FILTER);
344          _classesViewer.addFilter(THE_XSD_NAMESPACE_FILTER);
345 
346          gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL
347             | GridData.GRAB_HORIZONTAL);
348          gd.heightHint = _classesViewer.getList().getItemHeight() * 10;
349 
350          _classesViewer.getControl().setLayoutData(gd);
351 
352          gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
353          final Button addClassButton = new Button(classGroup, classGroup
354             .getStyle()
355             | SWT.PUSH);
356          addClassButton.setFont(classGroup.getFont());
357          addClassButton.setText("Create new class");
358          addClassButton.addSelectionListener(new SelectionAdapter()
359          {
360             public void widgetSelected(SelectionEvent e)
361             {
362                IStructuredSelection iss = new StructuredSelection(_oast
363                   .getRoot().getOWLDocument());
364                NewOWLClassWizard nocw = new NewOWLClassWizard();
365                WizardDialog wd = null;
366 
367                nocw.init(Workbench.getInstance(), iss);
368 
369                wd = new WizardDialog(getShell(), nocw);
370                wd.setBlockOnOpen(true);
371                if(wd.open() == WizardDialog.OK)
372                {
373                   _classes = _inspector.getAllClasses();
374                   //                  _classes = _inspector.getKlasses(_oast);
375                   _classesViewer.setInput(_classes);
376                   _classesViewer.refresh();
377                }
378             }
379          });
380 
381          //         final Button filterLanguageClasses = new Button(classGroup,
382          // classGroup.getStyle() | SWT.CHECK);
383          //         filterLanguageClasses.setFont(classGroup.getFont());
384          //         filterLanguageClasses.setText("Hide language classes");
385          //         filterLanguageClasses.setSelection(true);
386          //         filterLanguageClasses.addSelectionListener(new SelectionAdapter()
387          //         {
388          //            public void widgetSelected(SelectionEvent e)
389          //            {
390          //               if(filterLanguageClasses.getSelection() == true)
391          //               {
392          //                  _classesViewer.addFilter(THE_NAMESPACE_FILTER);
393          //               }
394          //               else
395          //               {
396          //                  _classesViewer.resetFilters();
397          //               }
398          //            }
399          //         });
400 
401          // property group construction and layout
402 
403          Group propertyGroup = new Group(mainComposite, mainComposite
404             .getStyle());
405          propertyGroup.setFont(mainComposite.getFont());
406          propertyGroup.setLayout(new GridLayout(2, false));
407          gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
408          propertyGroup.setLayoutData(gd);
409          propertyGroup.setText("Properties");
410          Label propertyLabel = new Label(propertyGroup, mainComposite
411             .getStyle());
412 
413          gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
414             | GridData.VERTICAL_ALIGN_BEGINNING);
415          gd.horizontalSpan = 2;
416          propertyLabel.setFont(propertyGroup.getFont());
417          propertyLabel
418             .setText("Select a property to which this restriction shall apply.");
419          propertyLabel.setLayoutData(gd);
420 
421          _propertiesViewer = new ListViewer(propertyGroup, mainComposite
422             .getStyle()
423             | SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
424          _propertiesViewer.getControl().setFont(propertyGroup.getFont());
425          _propertiesViewer.addFilter(THE_LANGUAGE_NAMESPACE_FILTER);
426 
427          gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL
428             | GridData.GRAB_HORIZONTAL);
429          gd.horizontalSpan = 2;
430          gd.heightHint = _propertiesViewer.getList().getItemHeight() * 10;
431          _propertiesViewer.getControl().setLayoutData(gd);
432 
433          gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
434          final Button addPropertyButton = new Button(propertyGroup,
435             propertyGroup.getStyle() | SWT.PUSH);
436          addPropertyButton.setFont(propertyGroup.getFont());
437          addPropertyButton.setText("Create new property");
438          addPropertyButton.addSelectionListener(new SelectionAdapter()
439          {
440             public void widgetSelected(SelectionEvent e)
441             {
442                IStructuredSelection iss = new StructuredSelection(_oast
443                   .getRoot().getOWLDocument());
444                NewOWLPropertyWizard nopw = new NewOWLPropertyWizard();
445                WizardDialog wd = null;
446 
447                nopw.init(Workbench.getInstance(), iss);
448 
449                wd = new WizardDialog(getShell(), nopw);
450                wd.setBlockOnOpen(true);
451                if(wd.open() == WizardDialog.OK)
452                {
453                   _properties = _inspector.getAllProperties();
454                   _propertiesViewer.setInput(_properties);
455                   _propertiesViewer.refresh();
456                }
457             }
458          });
459 
460          final Button filterLanguageProperties = new Button(propertyGroup,
461             propertyGroup.getStyle() | SWT.CHECK);
462          filterLanguageProperties.setFont(propertyGroup.getFont());
463          filterLanguageProperties.setText("Hide language properties");
464          filterLanguageProperties.setSelection(true);
465          filterLanguageProperties.addSelectionListener(new SelectionAdapter()
466          {
467             public void widgetSelected(SelectionEvent e)
468             {
469                if(filterLanguageProperties.getSelection())
470                {
471                   _propertiesViewer.addFilter(THE_LANGUAGE_NAMESPACE_FILTER);
472                }
473                else
474                {
475                   _propertiesViewer.resetFilters();
476                }
477             }
478          });
479 
480          //  Class and Property viewer data configuration
481 
482          _classesViewer.setContentProvider(RestrictionEditorProviderFactory
483             .getFactory().getContentProvider(_inspector, _oast));
484          _classesViewer.setLabelProvider(new URILabelProvider(_oast));
485 
486          _propertiesViewer.setContentProvider(RestrictionEditorProviderFactory
487             .getFactory().getContentProvider(_inspector, _oast));
488          _propertiesViewer.setLabelProvider(new URILabelProvider(_oast));
489 
490          _classesViewer.setInput(_classes);
491          _propertiesViewer.setInput(_properties);
492 
493          if(_preselectedClass != null)
494          {
495             _classesViewer.setSelection(new StructuredSelection(
496                _preselectedClass), true);
497          }
498 
499          _classesViewer.setSorter(new ViewerSorter());
500          _propertiesViewer.setSorter(new ViewerSorter());
501 
502          _classesViewer.addSelectionChangedListener(this);
503          _propertiesViewer.addSelectionChangedListener(this);
504 
505          setControl(mainComposite);
506          setPageComplete(false);
507       }
508 
509       /***
510        * Listener for clicks within the ListViewers. Will populate the populate
511        * the selected class and property attributes for calls by
512        * {@link #getSelectedClass() }and {@link #getSelectedProperty() }, and
513        * calls {@link #updatePageCompletion() }to ensure that the wizard's
514        * completion state is valid.
515        * 
516        * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(
517        *    org.eclipse.jface.viewers.SelectionChangedEvent)
518        */
519       public void selectionChanged(SelectionChangedEvent event)
520       {
521          IStructuredSelection classSelection = (IStructuredSelection)_classesViewer
522             .getSelection();
523          IStructuredSelection propSelection = (IStructuredSelection)_propertiesViewer
524             .getSelection();
525 
526          if(!classSelection.isEmpty())
527          {
528             _selectedClass = (java.net.URI)classSelection.getFirstElement();
529          }
530          if(!propSelection.isEmpty())
531          {
532             try
533             {
534                _selectedProperty = new java.net.URI((String)propSelection
535                   .getFirstElement());
536             }
537             catch (URISyntaxException e)
538             {
539                e.printStackTrace();
540             }
541          }
542          updatePageCompletion();
543       }
544 
545       /***
546        * Ensures that a property is selected and a class is selected.
547        */
548       public void updatePageCompletion()
549       {
550          setPageComplete(!(_classesViewer.getSelection().isEmpty() || _propertiesViewer
551             .getSelection().isEmpty()));
552          getContainer().updateButtons();
553       }
554 
555    }
556 
557 }