View Javadoc

1   /*
2    * $Id: RestrictionEditorProviderFactory.java,v 1.7 2005/05/31 19:37:20 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.util.Collection;
12  
13  import org.eclipse.jface.viewers.IFontProvider;
14  import org.eclipse.jface.viewers.ITableLabelProvider;
15  import org.eclipse.jface.viewers.LabelProvider;
16  import org.eclipse.swt.graphics.Font;
17  import org.eclipse.swt.graphics.Image;
18  import org.eclipse.ui.PlatformUI;
19  import org.eclipse.ui.model.BaseWorkbenchContentProvider;
20  
21  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
22  import com.bbn.swede.core.dom.OAST;
23  import com.bbn.swede.core.dom.OASTNode;
24  import com.bbn.swede.core.dom.owl.Klass;
25  import com.bbn.swede.core.dom.owl.OnProperty;
26  import com.bbn.swede.core.dom.owl.Restriction;
27  import com.bbn.swede.ui.OWLImages;
28  import com.hp.hpl.jena.ontology.OntClass;
29  import com.hp.hpl.jena.rdf.model.Resource;
30  
31  /***
32   * Returns a ILabelProvider and IContentProvider objects for the
33   * RestrictionEditor. The ILabelProvider implements ILabelProvider,
34   * ITableLabelProvider, and IFontProvider.
35   * 
36   * The label and content providers are capable of supporting both Jena and OAST
37   * objects.
38   * 
39   * @author aperezlo
40   */
41  public class RestrictionEditorProviderFactory
42  {
43  
44     /***
45      * <code>NOT_DEFINED</code>- text used by all label providers for a null
46      * value.
47      */
48     public static final String NOT_DEFINED = "*None Defined*";
49  
50     /***
51      * The singleton instance.
52      */
53     protected static RestrictionEditorProviderFactory _me;
54  
55     /***
56      * Returns, creating if neccesary, a singleton instance of the provider
57      * factory.
58      * 
59      * @return the singleton instances
60      */
61     public static RestrictionEditorProviderFactory getFactory()
62     {
63        if(_me == null)
64        {
65           _me = new RestrictionEditorProviderFactory();
66        }
67        return _me;
68     }
69  
70     /***
71      * Retrieves a label provider for use by the restiction editor.
72      * @param detective the RestrictionEditorInspector used to support the label
73      *        provider
74      * @param oast the OAST of the current document
75      * @return a ILabelProvider suitable for the RestrictionEditor
76      */
77     public RestrictionLabelProvider getLabelProvider(
78        RestrictionEditorInspector detective, IOWLAbstractSyntaxTree oast)
79     {
80        return new RestrictionLabelProvider(detective, oast);
81     }
82  
83     /***
84      * Retrieves a content provider for use in the restriction editor.
85      * @param detective the RestrictionEditorInspector used to support the
86      *        content provider
87      * @param oast the OAST of the current document
88      * @return a IContentProvider suitable for the RestrictionEditor
89      */
90     public RestrictionContentProvider getContentProvider(
91        RestrictionEditorInspector detective, IOWLAbstractSyntaxTree oast)
92     {
93        return new RestrictionContentProvider(detective, oast);
94     }
95  
96     protected final class DefaultContentProvider extends
97        BaseWorkbenchContentProvider
98     {
99        private IOWLAbstractSyntaxTree _oast;
100 
101       private RestrictionEditorInspector _inspector;
102 
103       public DefaultContentProvider(RestrictionEditorInspector inspector,
104          IOWLAbstractSyntaxTree oast)
105       {
106          _inspector = inspector;
107          _oast = oast;
108       }
109 
110       /*
111        *  (non-Javadoc)
112        * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
113        */
114       public Object[] getElements(Object element)
115       {
116          Object[] toReturn = super.getElements(element);
117          if(element instanceof Collection)
118          {
119             toReturn = ((Collection)element).toArray();
120          }
121          return toReturn;
122       }
123 
124       /*
125        *  (non-Javadoc)
126        * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
127        */
128       public Object[] getChildren(Object element)
129       {
130          Object[] toReturn = super.getChildren(element);
131          String sUri = null;
132          if(element instanceof String)
133          {
134             sUri = (String)element;
135             //            QueryJob qj = _inspector.getRestrictionsAsQueryJob(URI, _oast);
136             //            qj.setSystem(true);
137             //            qj.schedule();
138             //            try
139             //            {
140             //               qj.join();
141             //            }
142             //            catch (InterruptedException e)
143             //            {
144             //               e.printStackTrace();
145             //            }
146             //            toReturn = ((List) qj.getQueryResult()).toArray();
147 
148             toReturn = _inspector.getRestrictions(sUri, _oast).toArray();
149          }
150 
151          return toReturn;
152       }
153    }
154 
155    protected final class DefaultTableLabelProvider extends LabelProvider
156       implements ITableLabelProvider, IFontProvider
157    {
158       public Font getFont(Object element)
159       {
160          return PlatformUI.getWorkbench().getThemeManager().getCurrentTheme()
161             .getFontRegistry().defaultFont();
162       }
163 
164       public Image getColumnImage(Object element, int columnIndex)
165       {
166          return getImage(element);
167       }
168 
169       public String getColumnText(Object element, int columnIndex)
170       {
171          return getText(element);
172       }
173    }
174 
175    protected class RestrictionLabelProvider extends LabelProvider implements
176       ITableLabelProvider, IFontProvider
177    {
178       private RestrictionEditorInspector _detective;
179 
180       private LabelProvider _defaultProvider;
181 
182       private LabelProvider _swedeProvider;
183 
184       private LabelProvider _jenaProvider;
185 
186       private IOWLAbstractSyntaxTree _oast;
187 
188       public RestrictionLabelProvider(RestrictionEditorInspector detective,
189          IOWLAbstractSyntaxTree oast)
190       {
191          _detective = detective;
192          _swedeProvider = new SWeDELabelProvider(_detective);
193          _jenaProvider = new JenaLabelProvider(_detective);
194          _defaultProvider = new DefaultTableLabelProvider();
195          _oast = oast;
196       }
197 
198       public Font getFont(Object element)
199       {
200          return ((IFontProvider)route(element)).getFont(element);
201       }
202 
203       private ITableLabelProvider route(Object element)
204       {
205          ITableLabelProvider toReturn = (ITableLabelProvider)_defaultProvider;
206          if(element instanceof Resource)
207          {
208             toReturn = (ITableLabelProvider)_jenaProvider;
209          }
210          if((element instanceof Klass) || (element instanceof Restriction)
211             || (element instanceof OnProperty) || (element instanceof String)
212             || (element instanceof java.net.URI))
213          {
214             toReturn = (ITableLabelProvider)_swedeProvider;
215          }
216          return (ITableLabelProvider)toReturn;
217       }
218 
219       public Image getColumnImage(Object element, int columnIndex)
220       {
221          return route(element).getColumnImage(element, columnIndex);
222       }
223 
224       public String getColumnText(Object element, int columnIndex)
225       {
226          String temp = route(element).getColumnText(element, columnIndex);
227          if((element instanceof String)
228             && (columnIndex != RestrictionEditor.CLASS_NAME_COLUMN))
229          {
230             temp = null;
231          }
232          temp = (temp == null ? "" : ((OAST)_oast).getQNameForURI(temp));
233          return temp.trim();
234       }
235 
236       public String getText(Object element)
237       {
238          String toReturn = ((LabelProvider)route(element)).getText(element);
239          return toReturn;
240       }
241 
242       public Image getImage(Object element)
243       {
244          return ((LabelProvider)route(element)).getImage(element);
245       }
246    }
247 
248    protected class RestrictionContentProvider extends
249       BaseWorkbenchContentProvider
250    {
251       private RestrictionEditorInspector _detective;
252 
253       private BaseWorkbenchContentProvider _defaultProvider;
254 
255       private BaseWorkbenchContentProvider _swedeProvider;
256 
257       private BaseWorkbenchContentProvider _jenaProvider;
258 
259       public RestrictionContentProvider(RestrictionEditorInspector detective,
260          IOWLAbstractSyntaxTree oast)
261       {
262          _detective = detective;
263          _defaultProvider = new DefaultContentProvider(_detective, oast);
264          _swedeProvider = new SWeDEContentProvider(_detective);
265          _jenaProvider = new JenaContentProvider(_detective);
266       }
267 
268       private BaseWorkbenchContentProvider route(Object element)
269       {
270          BaseWorkbenchContentProvider toReturn = _defaultProvider;
271          if(element instanceof Resource)
272          {
273             toReturn = _jenaProvider;
274          }
275          if((element instanceof Klass) || (element instanceof Restriction)
276             || (element instanceof IOWLAbstractSyntaxTree))
277          {
278             toReturn = _swedeProvider;
279          }
280          return toReturn;
281       }
282 
283       public Object[] getElements(Object element)
284       {
285          return route(element).getElements(element);
286       }
287 
288       public Object[] getChildren(Object element)
289       {
290          return route(element).getChildren(element);
291       }
292 
293       public boolean hasChildren(Object element)
294       {
295          return route(element).hasChildren(element);
296       }
297 
298       public Object getParent(Object element)
299       {
300          return route(element).getParent(element);
301       }
302 
303    }
304 
305    /***
306     * Label provider for OAST-based restrictions in the table editor.
307     * @author aperezlo
308     */
309    protected final class SWeDELabelProvider extends LabelProvider implements
310       ITableLabelProvider, IFontProvider
311    {
312       private RestrictionEditorInspector _detective;
313 
314       public SWeDELabelProvider(RestrictionEditorInspector detective)
315       {
316          _detective = detective;
317       }
318 
319       public Font getFont(Object element)
320       {
321          return PlatformUI.getWorkbench().getThemeManager().getCurrentTheme()
322             .getFontRegistry().defaultFont();
323       }
324 
325       public String getText(Object element)
326       {
327          String toReturn = super.getText(element);
328          if(element == null)
329          {
330             toReturn = NOT_DEFINED;
331          }
332          if(element instanceof Klass)
333          {
334             Klass klass = (Klass)element;
335             toReturn = _detective.oastGetRDF(klass.getOWLAbstractSyntaxTree(),
336                klass, "ID");
337             toReturn = ((OAST)klass.getOWLAbstractSyntaxTree())
338                .getURIForQName(toReturn);
339          }
340          else if(element instanceof OnProperty)
341          {
342             OnProperty onProp = (OnProperty)element;
343             toReturn = _detective.oastGetRDF(onProp.getOWLAbstractSyntaxTree(),
344                onProp, "resource");
345             toReturn = ((OAST)onProp.getOWLAbstractSyntaxTree())
346                .getURIForQName(toReturn);
347          }
348          else if(element instanceof java.net.URI)
349          {
350             java.net.URI uri = (java.net.URI)element;
351             toReturn = uri.toString();
352             //            toReturn = ((OAST)
353             // onProp.getOWLAbstractSyntaxTree()).getURIForQName(toReturn);
354          }
355 
356          return toReturn;
357       }
358 
359       public Image getImage(Object element)
360       {
361          Image toReturn = super.getImage(element);
362          OASTNode node = null;
363          if(element instanceof Klass)
364          {
365             Klass klass = (Klass)element;
366             toReturn = OWLImages.getSharedImage(OWLImages.IMG_CLASS_OBJ);
367          } // if
368          else if(element instanceof OASTNode)
369          {
370             node = (OASTNode)element;
371             if(node != null)
372             {
373                if((node.getNodeType() == OASTNode.OWL_DATATYPEPROPERTY)
374                   || (node.getNodeType() == OASTNode.OWL_OBJECTPROPERTY)
375                   || (node.getNodeType() == OASTNode.OWL_ONPROPERTY))
376                {
377                   toReturn = OWLImages.getSharedImage(OWLImages.IMG_PROP_OBJ);
378                } // if
379             } // if
380          } // else
381 
382          return toReturn;
383       }
384 
385       public Image getColumnImage(Object element, int columnIndex)
386       {
387          Image toReturn = null;
388          //         Image toReturn = getImage(element);
389          if(element instanceof Restriction)
390          {
391             if(columnIndex == RestrictionEditor.RESTRICTION_ORIGIN_COLUMN)
392             {
393                toReturn = OWLImages.getSharedImage(OWLImages.IMG_CLASS_OBJ);
394             }
395             if(columnIndex == RestrictionEditor.PROPERTY_NAME_COLUMN)
396             {
397                toReturn = OWLImages.getSharedImage(OWLImages.IMG_PROP_OBJ);
398             }
399             //            toReturn =
400             // getImage(_detective.getObjectFromRestriction((Restriction)
401             // element, columnIndex));
402          }
403          if(element instanceof String)
404          {
405             if(columnIndex != RestrictionEditor.CLASS_NAME_COLUMN)
406             {
407                toReturn = null;
408             }
409             else
410             {
411                toReturn = OWLImages.getSharedImage(OWLImages.IMG_CLASS_OBJ);
412             }
413          }
414          return toReturn;
415       }
416 
417       public String getColumnText(Object element, int columnIndex)
418       {
419          String toReturn = getText(element);
420          Restriction restriction = null;
421          if(element instanceof String)
422          {
423             toReturn = (String)element;
424          }
425          if(element instanceof Restriction)
426          {
427             restriction = (Restriction)element;
428             if(columnIndex != RestrictionEditor.CLASS_NAME_COLUMN)
429             {
430                toReturn = getText(_detective.getObjectFromRestriction(
431                   restriction, columnIndex));
432             }
433             else
434             {
435                toReturn = "";
436             }
437          }
438          if((element instanceof Klass)
439             && (columnIndex != RestrictionEditor.CLASS_NAME_COLUMN))
440          {
441             toReturn = "";
442          }
443 
444          return toReturn;
445       }
446    }
447 
448    /***
449     * Content provider for Jena-based restrictions in the table editor.
450     * @author aperezlo
451     */
452    protected final class JenaContentProvider extends
453       BaseWorkbenchContentProvider
454    {
455       private RestrictionEditorInspector _inspector;
456 
457       public JenaContentProvider(RestrictionEditorInspector insp)
458       {
459          _inspector = insp;
460       }
461 
462       public Object getParent(Object element)
463       {
464          return super.getParent(element);
465       }
466 
467       public Object[] getElements(Object element)
468       {
469          return super.getElements(element);
470       }
471 
472       public Object[] getChildren(Object element)
473       {
474          Object[] toReturn = new Object[0];
475          toReturn = super.getChildren(element);
476          return toReturn;
477       }
478    }
479 
480    /***
481     * Label provider for Jena-based restrictions in the table editor.
482     * @author aperezlo
483     */
484    protected final class JenaLabelProvider extends LabelProvider implements
485       ITableLabelProvider, IFontProvider
486    {
487       private RestrictionEditorInspector _detective;
488 
489       public JenaLabelProvider(RestrictionEditorInspector detective)
490       {
491          _detective = detective;
492       }
493 
494       public Font getFont(Object element)
495       {
496          Font currentFont = PlatformUI.getWorkbench().getThemeManager()
497             .getCurrentTheme().getFontRegistry().defaultFont();
498          Font italics = PlatformUI.getWorkbench().getThemeManager()
499             .getCurrentTheme().getFontRegistry().getItalic(
500                currentFont.toString());
501          return italics;
502       }
503 
504       public Image getImage(Object element)
505       {
506          Resource res = null;
507          Image toReturn = null;
508          if(element instanceof Resource)
509          {
510             res = (Resource)element;
511             if(res.canAs(OntClass.class))
512             {
513                toReturn = OWLImages.getSharedImage(OWLImages.IMG_CLASS_OBJ);
514             }
515          }
516 
517          return toReturn;
518       }
519 
520       public String getText(Object element)
521       {
522          String toReturn = "";
523          return toReturn;
524       }
525 
526       public Image getColumnImage(Object element, int columnIndex)
527       {
528          Image toReturn = null;
529 
530          switch(columnIndex)
531          {
532             case RestrictionEditor.CLASS_NAME_COLUMN:
533                toReturn = getImage(element);
534                break;
535             case RestrictionEditor.PROPERTY_NAME_COLUMN:
536                toReturn = OWLImages.getSharedImage(OWLImages.IMG_PROP_OBJ);
537                break;
538             default:
539                break;
540          }
541 
542          return toReturn;
543       }
544 
545       public String getColumnText(Object element, int columnIndex)
546       {
547          String toReturn = getText(element);
548          com.hp.hpl.jena.ontology.OntClass restrict = null;
549          Resource resource = null;
550          RestrictionWrapper wrapper = null;
551          if(element instanceof Resource)
552          {
553             resource = (Resource)element;
554             if(resource.canAs(com.hp.hpl.jena.ontology.OntClass.class))
555             {
556                restrict = (com.hp.hpl.jena.ontology.OntClass)((Resource)element)
557                   .as(com.hp.hpl.jena.ontology.OntClass.class);
558             }
559          }
560          if(restrict != null)
561          {
562             wrapper = new RestrictionWrapper(restrict.asRestriction());
563             switch(columnIndex)
564             {
565                case RestrictionEditor.RESTRICTION_ORIGIN_COLUMN:
566                   toReturn = _detective.getOrigin(restrict.asRestriction());
567                   break;
568                case RestrictionEditor.RESTRICTION_TYPE_COLUMN:
569                   toReturn = wrapper.getRestrictionType();
570                   break;
571                case RestrictionEditor.RESTRICTION_VALUE_COLUMN:
572                   java.net.URI tempURI = wrapper.getRestrictionValueURI();
573                   if(tempURI == null)
574                   {
575                      toReturn = wrapper.getRestrictionValueText();
576                   }
577                   else
578                   {
579                      toReturn = tempURI.toString();
580                   }
581                   if(toReturn == null)
582                   {
583                      toReturn = NOT_DEFINED;
584                   }
585                   break;
586                case RestrictionEditor.PROPERTY_NAME_COLUMN:
587                   toReturn = wrapper.getOnPropertyURI();
588                   break;
589                default:
590                   break;
591             }
592          }
593          //         _detective.convertURIToFragment(toReturn);
594          return toReturn;
595       }
596    }
597 
598    /***
599     * Content provider for OAST-based restrictions in the table editor.
600     * @author aperezlo
601     */
602    protected final class SWeDEContentProvider extends
603       BaseWorkbenchContentProvider
604    {
605       private RestrictionEditorInspector _detective;
606 
607       public SWeDEContentProvider(RestrictionEditorInspector detective)
608       {
609          _detective = detective;
610       }
611 
612       public Object[] getElements(Object element)
613       {
614          Object[] toReturn = new Object[0];
615          QueryJob qj = null;
616          if(element instanceof IOWLAbstractSyntaxTree)
617          {
618             //            qj = _detective.getClassesAsQueryJob((IOWLAbstractSyntaxTree)
619             // element);
620             //            qj.setUser(true);
621             //            qj.schedule();
622             //            try
623             //            {
624             //               qj.join();
625             //            }
626             //            catch (InterruptedException e)
627             //            {
628             //               e.printStackTrace();
629             //            }
630             //            toReturn = ((List) qj.getQueryResult()).toArray();
631             toReturn = _detective.getClasses((IOWLAbstractSyntaxTree)element)
632                .toArray();
633          }
634          return toReturn;
635       }
636 
637       public Object[] getChildren(Object element)
638       {
639          return new Object[0];
640       }
641 
642       public Object getParent(Object element)
643       {
644          Object toReturn = null;
645          return toReturn;
646       }
647    }
648 }