View Javadoc

1   /*
2    * $Id: OWLProject.java,v 1.9 2005/05/31 16:26:41 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.core.resources;
9   
10  
11  import org.eclipse.core.resources.IContainer;
12  import org.eclipse.core.resources.IProject;
13  import org.eclipse.core.resources.IResource;
14  
15  import com.bbn.swede.core.IOWLDocument;
16  import com.bbn.swede.core.IOWLElement;
17  import com.bbn.swede.core.IOWLFolder;
18  import com.bbn.swede.core.IOWLLibrary;
19  import com.bbn.swede.core.IOWLProject;
20  import com.bbn.swede.core.libraries.ILibraryChangeListener;
21  import com.bbn.swede.core.libraries.LibraryChangeEvent;
22  import com.bbn.swede.core.libraries.LibraryChangeSupport;
23  
24  /***
25   * Implementation for OWL project elements.
26   * @author jlerner
27   */
28  public class OWLProject extends OWLElement implements IOWLProject, ILibraryChangeListener
29  {
30     /***
31      * Delegate object for library change notifications.
32      */   
33     protected LibraryChangeSupport _libraryChangeSupport;
34     
35     /***
36      * Creates an OWL project element based on an Eclipse project resource.
37      * @param project The Eclipse project
38      */
39     OWLProject(IProject project)
40     {
41        _resource = project;
42        _libraryChangeSupport = new LibraryChangeSupport();
43        setElementName(project.getName());
44     }
45  
46     /*
47      *  (non-Javadoc)
48      * @see com.bbn.swede.core.IOWLElement#contains(org.eclipse.core.resources.IResource)
49      */
50     public boolean contains(IResource resource)
51     {
52        if (this.getResource().equals(resource))
53        {
54           return true;
55        }
56        IOWLElement[] elems = getElementInfo().getChildren();
57        for (int i = 0; i < elems.length; i++)
58        {
59           if (resource.equals(elems[i].getResource()))
60           {
61              return true;
62           }
63           else if (
64              elems[i] instanceof IOWLFolder
65                 && ((IOWLFolder) elems[i]).contains(resource))
66           {
67              return true;
68           }
69  
70        }
71        return false;
72     }
73  
74     /*
75      *  (non-Javadoc)
76      * @see com.bbn.swede.core.IOWLElement#getCorrespondingElement(org.eclipse.core.resources.IResource)
77      */
78     public IOWLElement getCorrespondingElement(IResource resource)
79     {
80        if (_resource.equals(resource))
81        {
82           return this;
83        }
84  
85        IOWLElement[] elems = getElementInfo().getChildren();
86        for (int i = 0; i < elems.length; i++)
87        {
88           IOWLElement elem = elems[i].getCorrespondingElement(resource);
89           if (elem != null)
90           {
91              return elem;
92           }
93        }
94        return null;
95     }
96     
97     /***
98      * Registers an object for notifications of changes to the project's
99      * referenced libraries.  If the object is already registered for change
100     * notifications, this method has no effect.
101     * @param lcl The listener to register
102     */
103    public void addLibraryChangeListener(ILibraryChangeListener lcl)
104    {
105       _libraryChangeSupport.addLibraryChangeListener(lcl);
106    }
107    /***
108     * Unregisters an object from notifications of changes to the project's
109     * referenced libraries.  If the object is not registered for change
110     * notifications, this method has no effect.
111     * @param lcl The listener to unregister
112     */
113    public void removeLibraryChangeListener(ILibraryChangeListener lcl)
114    {
115       _libraryChangeSupport.removeLibraryChangeListener(lcl);
116    }
117    /*
118     *  (non-Javadoc)
119     * @see com.bbn.swede.core.libraries.ILibraryChangeListener#libraryChanged(
120     *    com.bbn.swede.core.libraries.LibraryChangeEvent)
121     */
122    public void libraryChanged(LibraryChangeEvent lce)
123    {
124       _libraryChangeSupport.fireLibraryChange(lce);
125    }
126    
127    /***
128     * Retrieves the Eclipse project the OWL project element is based on.  This
129     * is a convenience method, fully equivalent to (IProject)getResource().
130     * @return The Eclipse project resource.
131     */
132    public IProject getProject()
133    {
134       if (getResource() instanceof IProject)
135       {
136          return (IProject) getResource();
137       }
138       return null;
139    }
140    
141    /*
142     *  (non-Javadoc)
143     * @see com.bbn.swede.core.IOWLProject#getContainer()
144     */
145    public IContainer getContainer()
146    {
147       if (getResource() instanceof IContainer)
148       {
149          return (IContainer) getResource();
150       }
151       return null;
152    }
153    
154    /*
155     *  (non-Javadoc)
156     * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
157     */
158    public Object getAdapter(Class adapter)
159    {
160       if (adapter.equals(IProject.class))
161       {
162          return getProject();
163       }
164       if (adapter.equals(IContainer.class))
165       {
166          return getContainer();
167       }
168       return super.getAdapter(adapter);
169    }
170 
171    /* (non-Javadoc)
172     * @see com.bbn.swede.core.IOWLProject#getDocument(java.lang.String)
173     */
174    public IOWLDocument getDocument(String sURI)
175    {
176       return findDocument(this, sURI);
177    }
178    
179    /***
180     * Helper method for getDocument.  Performs the recursion necessary to search
181     * the project and its subfolders for the requested URI.
182     * @param elem Current level of recursion
183     * @param sURI URI of the document to locate
184     * @return the document if it is found, <code>null</code> if not.
185     */
186    private IOWLDocument findDocument(IOWLElement elem, String sURI)
187    {
188       IOWLElement[] children = elem.getElementInfo().getChildren();
189       for (int i = 0; i < children.length; i++)
190       {
191          if (children[i] instanceof IOWLDocument)
192          {
193             IOWLDocument doc = (IOWLDocument) children[i];
194             if (doc.getURI().equals(sURI))
195             {
196                return doc;
197             }
198          }
199          else if ((children[i] instanceof IOWLFolder) 
200             || (children[i] instanceof IOWLLibrary))
201          {
202             IOWLDocument doc = findDocument(children[i], sURI);
203             if (doc != null)
204             {
205                return doc;
206             }
207          }
208       }
209 
210       return null;
211    }
212 }