View Javadoc

1   /*
2    * $Id: URIExplorerContentProvider.java,v 1.19 2005/05/31 22:04:32 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   package com.bbn.swede.view.uriexplorer.core;
9   
10  import java.util.ArrayList;
11  import java.util.List;
12  
13  import org.eclipse.core.resources.IContainer;
14  import org.eclipse.core.resources.IFile;
15  import org.eclipse.core.resources.IFolder;
16  import org.eclipse.core.resources.IProject;
17  import org.eclipse.core.resources.IResource;
18  import org.eclipse.core.resources.ResourcesPlugin;
19  import org.eclipse.core.runtime.CoreException;
20  import org.eclipse.jface.viewers.ITreeContentProvider;
21  import org.eclipse.jface.viewers.Viewer;
22  
23  import com.bbn.swede.core.IOWLClass;
24  import com.bbn.swede.core.IOWLDocument;
25  import com.bbn.swede.core.IOWLElement;
26  import com.bbn.swede.core.IOWLFolder;
27  import com.bbn.swede.core.IOWLLibrary;
28  import com.bbn.swede.core.IOWLModel;
29  import com.bbn.swede.core.IOWLProject;
30  import com.bbn.swede.core.IOWLProperty;
31  import com.bbn.swede.core.OWLCore;
32  import com.bbn.swede.core.OWLElementInfo;
33  import com.bbn.swede.core.resources.SWResourceManager;
34  import com.bbn.swede.view.ViewPlugin;
35  
36  /***
37   * Builds the Semantic Web view of the Eclipse workspace for display in URI 
38   * Explorer.  Much of the structure
39   * returned by this class is taken directly from the OWLModel for a document.
40   * Resources that are not included in the model are returned as well, as
41   * instances of IFile, IFolder, and IProject.
42   */
43  public class URIExplorerContentProvider implements ITreeContentProvider
44  {
45     private Viewer _viewer;
46     private IOWLModel _model;
47  
48     /*
49      *  (non-Javadoc)
50      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
51      */
52     public Object[] getChildren(Object parent)
53     {
54        if (parent instanceof IOWLModel)
55        {
56           Object[] owlStuff = ((IOWLModel) parent).getOWLProjects();
57           Object[] projects = getNonOWLProjects();
58           Object[] children = new Object[owlStuff.length + projects.length];
59           System.arraycopy(owlStuff, 0, children, 0, owlStuff.length);
60           System.arraycopy(
61              projects,
62              0,
63              children,
64              owlStuff.length,
65              projects.length);
66           return children;
67        }
68        if (parent instanceof IOWLProject || parent instanceof IOWLFolder)
69        {
70           OWLElementInfo info = ((IOWLElement) parent).getElementInfo();
71           Object[] owlStuff = info.getChildren();
72           Object[] folders = getNonOWLFolders((IOWLElement) parent);
73           Object[] files = getNonOWLFiles((IOWLElement) parent);
74           Object[] children =
75              new Object[owlStuff.length + folders.length + files.length];
76           System.arraycopy(owlStuff, 0, children, 0, owlStuff.length);
77           System.arraycopy(
78              folders,
79              0,
80              children,
81              owlStuff.length,
82              folders.length);
83           System.arraycopy(
84              files,
85              0,
86              children,
87              owlStuff.length + folders.length,
88              files.length);
89           return children;
90        }
91        if (parent instanceof IProject)
92        {
93           try
94           {
95              return ((IProject) parent).members();
96           }
97           catch (CoreException e)
98           {
99              OWLCore.logWarning(
100                ViewPlugin.getID(),
101                "Unable to enumerate members of project "
102                   + ((IProject) parent).getName(),
103                e);
104          }
105       }
106       if (parent instanceof IOWLDocument)
107       {
108          List toReturn = new ArrayList();
109          Object[] temp = null;
110          
111          int numAnon = 0;
112          temp = ((IOWLDocument) parent).getElementInfo().getChildren();
113          for(int i = 0; i < temp.length; i++)
114          {
115             if(temp[i] instanceof IOWLClass)
116             {
117                if(!((IOWLClass) temp[i]).isAnonymous())
118                {
119                   toReturn.add(temp[i]);
120                }
121             }
122             else if(temp[i] instanceof IOWLProperty)
123             {
124                if(!((IOWLProperty) temp[i]).isAnonymous())
125                {
126                   toReturn.add(temp[i]);
127                }
128             }
129          }
130          
131          return toReturn.toArray(); 
132       }
133       if(parent instanceof IOWLLibrary)
134       {
135          return ((IOWLLibrary) parent).getElementInfo().getChildren();
136       }
137       if (parent instanceof IFolder)
138       {
139          try
140          {
141             return ((IFolder) parent).members();
142          }
143          catch (CoreException e)
144          {
145             //do nothing
146          }
147       }
148       return null;
149    }
150 
151    /*
152     *  (non-Javadoc)
153     * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
154     */
155    public Object getParent(Object element)
156    {
157       if (element instanceof IOWLElement)
158       {
159          return ((IOWLElement) element).getParent();
160       }
161       if (element instanceof IResource)
162       {
163          return ((IResource) element).getParent();
164       }
165       return null;
166    }
167 
168    /*
169     *  (non-Javadoc)
170     * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
171     */
172    public boolean hasChildren(Object element)
173    {
174       if (element instanceof IProject)
175       {
176          IProject proj = (IProject)element;
177          return proj.isOpen();
178       }
179       else if (element instanceof IOWLProject
180                || element instanceof IOWLFolder
181                || element instanceof IOWLDocument
182                || element instanceof IFolder
183                || element instanceof IProject
184                || element instanceof IOWLLibrary)
185       {
186          return true;
187       }
188       return false;
189    }
190 
191    /*
192     *  (non-Javadoc)
193     * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
194     */
195    public Object[] getElements(Object parent)
196    {
197       return getChildren(parent);
198    }
199 
200    /*
201     *  (non-Javadoc)
202     * @see org.eclipse.jface.viewers.IContentProvider#dispose()
203     */
204    public void dispose()
205    {
206 
207    }
208 
209    /*
210     *  (non-Javadoc)
211     * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(
212     *       org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
213     */
214    public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
215    {
216       _viewer = viewer;
217       _model = (IOWLModel) newInput;
218    }
219 
220    private static final Object[] EMPTY_ARRAY = new Object[0];
221    /***
222     * Builds a list of folders that are parented to a specific element but not
223     * in the OWL model.
224     * @param elem The element to check for non-OWL subfolders
225     * @return An array containing all child resources of <code>elem</code> that
226     *         are folders and are not in the OWL model.  
227     *         This may be an empty array.
228     */
229    protected Object[] getNonOWLFolders(IOWLElement elem)
230    {
231       IContainer container = null;
232       try
233       {
234          container = (IContainer) elem.getResource();
235       }
236       catch (ClassCastException e)
237       {
238          return EMPTY_ARRAY;
239       }
240 
241       try
242       {
243          IResource[] resources = container.members();
244          List lFolders = new ArrayList();
245          for (int i = 0; i < resources.length; i++)
246          {
247             if (resources[i] instanceof IFolder)
248             {
249                if (!elem.contains(resources[i]))
250                {
251                   lFolders.add(resources[i]);
252                }
253             }
254          }
255          return lFolders.toArray();
256       }
257       catch (CoreException e)
258       {
259          return EMPTY_ARRAY;
260       }
261    }
262 
263    /***
264     * Builds a list of projects that do not have the semantic web nature.
265     * @return An array of all project resources in the Eclipse worksapce that
266     *         do not have the semantic web nature.
267     */
268    protected Object[] getNonOWLProjects()
269    {
270       IProject[] projects =
271          ResourcesPlugin.getWorkspace().getRoot().getProjects();
272       IOWLModel model = SWResourceManager.getModel();
273       ArrayList alProjects = new ArrayList();
274       for (int i = 0; i < projects.length; i++)
275       {
276          if (!model.contains(projects[i]))
277          {
278             alProjects.add(projects[i]);
279          }
280       }
281       return alProjects.toArray();
282    }
283 
284    /***
285     * Builds a list of files that are parented to a specified element and are
286     * not in the OWL model.
287     * @param elem The element to check for non-OWL files
288     * @return An array containing all child resources of <code>elem</code> that
289     *         are files and are not in the OWL model.  
290     *         This may be an empty array.
291     */
292    protected Object[] getNonOWLFiles(IOWLElement elem)
293    {
294       IContainer container = null;
295       try
296       {
297          container = (IContainer) elem.getResource();
298       }
299       catch (ClassCastException e)
300       {
301          return EMPTY_ARRAY;
302       }
303 
304       try
305       {
306          IResource[] resources = container.members();
307          List lNonOWLFiles = new ArrayList();
308          for (int i = 0; i < resources.length; i++)
309          {
310             if (resources[i] instanceof IFile)
311             {
312                if (!elem.contains(resources[i]))
313                {
314                   lNonOWLFiles.add(resources[i]);
315                }
316             }
317          }
318          return lNonOWLFiles.toArray();
319       }
320       catch (CoreException e)
321       {
322          return EMPTY_ARRAY;
323       }
324    }
325 }