View Javadoc

1   /*
2    * $Id: OWLLibrary.java,v 1.9 2005/06/01 19:55:51 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.core.libraries;
10  
11  import org.eclipse.core.resources.IProject;
12  import org.eclipse.core.resources.IResource;
13  
14  import com.bbn.swede.core.IOWLElement;
15  import com.bbn.swede.core.IOWLLibrary;
16  import com.bbn.swede.core.OWLElementInfo;
17  import com.bbn.swede.core.config.SWProjectInfo;
18  import com.bbn.swede.core.resources.OWLElement;
19  import com.bbn.swede.core.resources.SWResourceManager;
20  
21  /***
22   * This class is an implementation of the IOWLLibrary interface,
23   * providing SWeDE-type (i.e. consistent with IOWLElements, 'Documents,
24   * 'Projects, 'Classes, etc.) support for libraries. 
25   * 
26   * @author aperezlo
27   */
28  public class OWLLibrary extends OWLElement implements IOWLLibrary, ILibraryChangeListener
29  {
30     /***
31      * The library represented by this OWL library element.
32      */
33     protected ILibraryDescriptor _library;
34     /***
35      * Delegate object for library change notification handling.
36      */
37     protected LibraryChangeSupport _libraryChangeSupport;
38     
39     /***
40      * Creats an OWL library element representing a library.
41      * @param library the library
42      */
43     public OWLLibrary(ILibraryDescriptor library)
44     {
45        _library = library;
46        _libraryChangeSupport = new LibraryChangeSupport();
47        _library.addLibraryChangeListener(this);
48     }
49     
50     /* (non-Javadoc)
51      * @see com.bbn.swede.core.IOWLElement#getElementName()
52      */
53     public String getElementName()
54     {
55        return _library != null ? _library.getName() : "Library not found";
56     }
57     
58     /* (non-Javadoc)
59      * @see com.bbn.swede.core.IOWLLibrary#getLibrary()
60      */
61     public ILibraryDescriptor getLibrary()
62     {
63        return _library;
64     }
65  
66     /* (non-Javadoc)
67      * @see com.bbn.swede.core.libraries.ILibraryChangeListener#libraryChanged(
68      *    com.bbn.swede.core.libraries.LibraryChangeEvent)
69      */
70     public void libraryChanged(LibraryChangeEvent lce)
71     {
72        if((lce.getType() & LibraryChangeEvent.DELETED) == LibraryChangeEvent.DELETED)
73        {
74           getParent().getElementInfo().removeChild(this);
75           SWProjectInfo.refreshInfoFile((IProject)getParent().getCorrespondingResource());
76           _info = null; // now that our info is null, next time it is requested,
77           _libraryChangeSupport.fireLibraryChange(lce);
78        }
79        else if ((lce.getType() & (LibraryChangeEvent.REFRESHED | LibraryChangeEvent.CHANGED)) != 0)
80        {
81           //  keep this one commented out, as it will cause deadlock... :(
82  //         SWProjectInfo.refreshInfoFile((IProject)getParent().getCorrespondingResource());
83           _info = null; // now that our info is null, next time it is requested, 
84                         // it will be rebuilt, and will have the latest info...
85           _libraryChangeSupport.fireLibraryChange(lce);
86           // SWProjectInfo.refreshInfoFile((IProject)getParent().getCorrespondingResource());
87        }
88     }
89     
90     /***
91      * Registers an object for notifications of changes to this library.
92      * @param lcs The listener to register.  Duplicate registrations have no
93      *            effect.
94      */
95     public void addLibraryChangeListener(ILibraryChangeListener lcs)
96     {
97        _libraryChangeSupport.addLibraryChangeListener(lcs);
98     }
99  
100    /***
101     * Unregisters an object from notifications of changes to this library.
102     * @param lcs The listener to unregister.  This has no effect if
103     *            <code>lcs</code> is not registered for change notifications.
104     */
105    public void removeLibraryChangeListener(ILibraryChangeListener lcs)
106    {
107       _libraryChangeSupport.removeLibraryChangeListener(lcs);
108    }
109    /* (non-Javadoc)
110     * @see com.bbn.swede.core.IOWLElement#getElementInfo()
111     */
112    public OWLElementInfo getElementInfo()
113    {
114       if(_library != null)
115       {
116          String name = _library.getName();
117          if(Libraries.getLibrary(name) != null)
118          {
119             _library = Libraries.getLibrary(name);
120          }
121       }
122       if(_info == null)
123       {
124          _info = SWResourceManager.createElementInfo(this);
125       }
126       return _info;
127    }
128    
129    /* (non-Javadoc)
130     * @see com.bbn.swede.core.IOWLElement#getCorrespondingElement(org.eclipse.core.resources.IResource)
131     */
132    public IOWLElement getCorrespondingElement(IResource resource)
133    {
134       IOWLElement toReturn = null;
135       IOWLElement[] kids = getElementInfo().getChildren();
136       for(int i = 0; i < kids.length; i++)
137       {
138          toReturn = kids[i].getCorrespondingElement(resource);
139          if(toReturn != null)
140          {
141             break;
142          }
143       }
144       return toReturn;
145    }
146    
147 }