View Javadoc

1   /*
2    * $Id: DumpOntContentProvider.java,v 1.4 2005/06/01 14:36:00 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.library.dumpont;
10  
11  import java.util.ArrayList;
12  import java.util.Iterator;
13  import java.util.List;
14  
15  import org.daml.rdf.dumpont.core.Class;
16  import org.daml.rdf.dumpont.core.Ontology;
17  import org.daml.rdf.dumpont.core.Property;
18  import org.eclipse.jface.viewers.ITreeContentProvider;
19  import org.eclipse.jface.viewers.Viewer;
20  
21  /***
22   * Content provider for the TreeViewer that powers the DumpOnt visualization
23   * page.
24   * @author jlerner
25   */
26  public class DumpOntContentProvider implements ITreeContentProvider
27  {
28     private class ClassHierarchy
29     {
30        /*
31         *  (non-Javadoc)
32         * @see java.lang.Object#toString()
33         */
34        public String toString() 
35        {
36           return "Class Hierarchy";
37        }
38     }
39     private class PropertyHierarchy
40     {
41        /*
42         *  (non-Javadoc)
43         * @see java.lang.Object#toString()
44         */
45        public String toString()
46        {
47           return "Property Hierarchy";
48        }
49     }
50     /***
51      * The ontology to display.
52      */
53     protected Ontology _ont;
54     /* (non-Javadoc)
55      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
56      */
57     public Object[] getChildren(Object parentElement)
58     {
59        if (parentElement == _ont)
60        {
61           return getElements(parentElement);
62        }
63        if (parentElement instanceof ClassHierarchy)
64        {
65           List lClasses = new ArrayList();
66           ArrayList al = new ArrayList(_ont.getClasses().values());
67           Iterator it = al.iterator();
68           while (it.hasNext())
69           {
70              Class klass = (Class)it.next();
71              if (!klass.isRestriction() && !klass.getNode().isAnonymous()
72                  && klass.getSuperClasses().size() == 0)
73              {
74                 lClasses.add(klass);
75              }
76           }
77           return lClasses.toArray();
78        }
79        if (parentElement instanceof PropertyHierarchy)
80        {
81           List lProperties = new ArrayList();
82           Iterator it = _ont.getProperties().values().iterator();
83           while (it.hasNext())
84           {
85              Property prop = (Property)it.next();
86              if (prop.getSuperProperties().size() == 0)
87              {
88                 lProperties.add(prop);
89              }
90           }
91           return lProperties.toArray();
92        }
93        if (parentElement instanceof Class)
94        {
95           Class klass = (Class)parentElement;
96           return klass.getSubClasses().toArray();
97        }
98        if (parentElement instanceof Property)
99        {
100          Property prop = (Property)parentElement;
101          return prop.getSubProperties().toArray();
102       }
103       return null;
104    }
105 
106    /* (non-Javadoc)
107     * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
108     */
109    public Object getParent(Object element)
110    {
111       if (element instanceof ClassHierarchy
112           || element instanceof PropertyHierarchy)
113       {
114          return _ont;
115       }
116       if (element instanceof Class)
117       {
118          Class klass = (Class)element;
119          ArrayList al = new ArrayList(klass.getSuperClasses());
120          if (al.size() > 0)
121          {
122             return al.get(0);
123          }
124       }
125       if (element instanceof Property)
126       {
127          Property prop = (Property)element;
128          ArrayList al = new ArrayList(prop.getSuperProperties());
129          if (al.size() > 0)
130          {
131             return al.get(0);
132          }
133       }
134       return null;
135    }
136 
137    /* (non-Javadoc)
138     * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
139     */
140    public boolean hasChildren(Object element)
141    {
142       if (element == _ont)
143       {
144          return true;
145       }
146       if (element instanceof ClassHierarchy)
147       {
148          return _ont.getClasses().values().size() > 0;
149       }
150       if (element instanceof PropertyHierarchy)
151       {
152          return _ont.getProperties().values().size() > 0;
153       }
154       if (element instanceof Class)
155       {
156          Class klass = (Class)element;
157          return klass.getSubClasses().size() > 0;
158       }
159       if (element instanceof Property)
160       {
161          Property prop = (Property)element;
162          return prop.getSubProperties().size() > 0;
163       }
164       return false;
165    }
166 
167    /* (non-Javadoc)
168     * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
169     */
170    public Object[] getElements(Object inputElement)
171    {
172       return new Object[]{new ClassHierarchy(), new PropertyHierarchy()};
173    }
174 
175    /* (non-Javadoc)
176     * @see org.eclipse.jface.viewers.IContentProvider#dispose()
177     */
178    public void dispose()
179    {
180    }
181 
182    /* (non-Javadoc)
183     * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(
184     *       org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
185     */
186    public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
187    {
188       _ont = (Ontology)newInput;
189    }
190 
191 }