KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > bridge > SrcElementImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.bridge;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.api.mdr.MDRepository;
29 import org.netbeans.jmi.javamodel.Resource;
30 import org.netbeans.modules.java.JavaDataObject;
31 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
32 import org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl;
33 import org.openide.filesystems.FileObject;
34 import org.openide.loaders.DataObject;
35 import org.openide.nodes.Node;
36 import org.openide.src.*;
37 import org.openide.util.Task;
38
39 public class SrcElementImpl implements SourceElement.Impl, Node.Cookie, PropertyChangeListener JavaDoc {
40         
41     private static final ClassElement[] NO_CLASSES = new ClassElement[0];
42     
43     /**
44      * PropertyChangeListeners attached to the SourceElement.
45      */

46     LinkedList JavaDoc listeners;
47     
48     /**
49      * The Element representing this proxy/impl. Once set, the element cannot be
50      * changed.
51      */

52     SourceElement srcElement = null;
53         
54     private transient int status = SourceElement.STATUS_OK;
55     
56     private transient DefaultLangModel model;
57     
58     private transient WeakReference JavaDoc delegate;
59     
60     private JavaDataObject javaDataObject;
61     
62     private static final long serialVersionUID = 3120888788645227532L;
63     
64     
65     // init .....................................................................
66

67     public SrcElementImpl(JavaDataObject javaObject) {
68         javaDataObject = javaObject;
69         model = javaDataObject.getModel ();
70     }
71
72     private SourceElementImpl getDelegate() {
73         SourceElementImpl impl = (delegate != null) ? (SourceElementImpl) delegate.get() : null;
74         if (impl == null || !impl.isResourceValid()) {
75             MDRepository repo = JavaMetamodel.getDefaultRepository();
76             repo.beginTrans(false);
77             try {
78                 FileObject fileObj = javaDataObject.getPrimaryFile();
79                 Resource resource = JavaMetamodel.getManager().getResource (fileObj);
80                 if (resource != null) {
81                     status = SourceElement.STATUS_OK;
82                     impl = new SourceElementImpl (model, resource, javaDataObject);
83                     impl.attachedToElement(srcElement);
84                     delegate = new WeakReference JavaDoc (impl);
85                 }
86             } finally {
87                 repo.endTrans(false);
88             }
89         }
90         return impl;
91     }
92     
93     public void invalidateDelegate() {
94         delegate = null;
95     }
96     
97     /*
98     String getRelativeName(FileObject root, JavaDataObject resource) {
99        String rootName = root.getPackageNameExt('/', '.');
100        String resName = resource.getPrimaryFile().getPackageNameExt('/', '.');
101        if ("".equals(rootName))
102            return resName;
103         return resName.substring(rootName.length() + 1).replace('/', '.');
104     }
105      */

106     
107     /**
108      * Returns the SourceElement representing the hierarchy.
109      */

110     protected SourceElement getElement() {
111         return this.srcElement;
112     }
113     
114     /**
115      * OpenAPI callback; called when the SourceElement is constructed. This implementation
116      * only records the reference
117      */

118     public void attachedToElement(Element el) {
119         this.srcElement = (SourceElement)el;
120     }
121
122     /**
123      * Retrieves the status of the parsing operation. Delegates to ParsingSupport.
124      */

125     public int getStatus() {
126         MDRepository repo = JavaMetamodel.getDefaultRepository();
127         repo.beginTrans(false);
128         try {
129             FileObject fileObj = javaDataObject.getPrimaryFile();
130             Resource resource = JavaMetamodel.getManager().getResource(fileObj);
131             if (resource != null) {
132                 if (resource.getStatus() == ResourceImpl.HAS_SYNTAX_ERROR) {
133                     return SourceElement.STATUS_ERROR;
134                 }
135             }
136         } finally {
137             repo.endTrans(false);
138         }
139         return status;
140     }
141     
142     /**
143      * Implementation of OpenAPI prepare() call.
144      */

145     public Task prepare() {
146         return Task.EMPTY;
147     }
148     
149     /**
150      * Implementation of setPackage(). Retrieves the real implementation and
151      * delegates to it. Throws SourceException if the source cannot be parsed.
152      */

153     public void setPackage(Identifier id) throws SourceException {
154         SourceElementImpl del = getDelegate();
155         if (del != null)
156             del.setPackage(id);
157     }
158     
159     public Identifier getPackage() {
160         SourceElementImpl del = getDelegate();
161         if (del != null)
162             return del.getPackage();
163         else
164             return Identifier.create ("");
165     }
166     
167     public Import[] getImports() {
168         SourceElementImpl del = getDelegate();
169         if (del != null)
170             return del.getImports();
171         else
172             return new Import[0];
173     }
174     
175     public void changeImports(Import[] elems, int action) throws SourceException {
176         SourceElementImpl del = getDelegate();
177         if (del != null)
178             del.changeImports(elems, action);
179     }
180     
181     public void changeClasses(ClassElement[] elems, int action) throws SourceException {
182         SourceElementImpl del = getDelegate();
183         if (del != null)
184             del.changeClasses(elems, action);
185     }
186     
187     public ClassElement[] getClasses() {
188         SourceElementImpl del = getDelegate();
189         if (del != null)
190             return del.getClasses();
191         else
192             return NO_CLASSES;
193     }
194     
195     public ClassElement getClass(Identifier name) {
196         SourceElementImpl del = getDelegate();
197         if (del != null)
198             return del.getClass(name);
199         else
200             return null;
201     }
202     
203     public ClassElement[] getAllClasses() {
204         SourceElementImpl del = getDelegate();
205         if (del != null)
206             return del.getAllClasses();
207         else
208             return NO_CLASSES;
209     }
210     
211     /**
212      * Implementation of getCookie(). The implementation fakes cookies of all interfaces
213      * on this object; if the cookie is not available, it asks the environment to
214      * provide the cookie and IF the cookie descends from the implementation hierarchy
215      * passes the request on to the real SourceElement's implementation, optionally
216      * parsing it from the source.
217      */

218     public Node.Cookie getCookie(Class JavaDoc type) {
219         if (type.isAssignableFrom(getClass()))
220             return this;
221         if (type.isAssignableFrom(DataObject.class))
222             return javaDataObject.getCookie (type);
223         SourceElementImpl del = getDelegate();
224         if (del != null)
225             return del.getCookie(type);
226         else
227             return javaDataObject.getCookie (type);
228     }
229     
230     /**
231      * No effect on SourceElements, there's only one of them in a source :-)
232      */

233     public void markCurrent(boolean beforeAfter) {
234     }
235     
236     /**
237      * Exclusively locks the model for the execution of the Runnable object. Note that
238      * the implementation does *NOT* lock the document! Also, do *NOT* call the
239      * method if you have already locked the document for writing unless you
240      * _REALLY_ know what you are doing.
241      */

242     public void runAtomic(final Runnable JavaDoc run) {
243         getDelegate().runAtomic (run);
244         /*
245         final StyledDocument doc;
246         try {
247             doc = supp.docBinding.getEditorSupport().openDocument();
248         } catch (java.io.IOException ex) {
249             return;
250         }
251         try {
252             model.runAtomic(new Runnable() {
253                 public void run() {
254                     try {
255                         supp.docBinding.enableAtomicAsUser(true);
256                         NbDocument.runAtomic(doc, run);
257                     } finally {
258                         supp.docBinding.enableAtomicAsUser(false);
259                     }
260                 }
261             });
262         } catch (SourceException ex) {
263         }
264          */

265     }
266     /**
267      * Creates an atomic transaction, that respects the guarded sections inside
268      * the document, over the model. Again, this does *NOT* lock the document.
269      */

270     public void runAtomicAsUser(final Runnable JavaDoc run) throws SourceException {
271         getDelegate().runAtomicAsUser (run);
272         /*
273         final StyledDocument doc;
274         try {
275             doc = supp.docBinding.getEditorSupport().openDocument();
276         } catch (java.io.IOException ex) {
277             throw new SourceException.IO(ex);
278         }
279         model.runAtomic(new Runnable() {
280             public void run() {
281                 try {
282                     supp.docBinding.enableAtomicAsUser(true);
283                     NbDocument.runAtomic(doc, run);
284                 } finally {
285                     supp.docBinding.enableAtomicAsUser(false);
286                 }
287             }
288         });
289          */

290     }
291
292     /**
293      * Adds a PropertyChangeListener
294      */

295     public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
296         if (listeners == null) {
297             listeners = new LinkedList JavaDoc();
298         }
299         boolean attach = listeners.isEmpty();
300         listeners.add(l);
301                 
302         if (attach) {
303             SourceElementImpl del = getDelegate();
304             if (del != null)
305                 del.addPropertyChangeListener(this);
306         }
307     }
308     
309     public synchronized void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
310         if (listeners == null)
311             return;
312         listeners.remove(l);
313         if (listeners.isEmpty()) {
314             SourceElementImpl del = getDelegate();
315             if (del != null)
316                 del.removePropertyChangeListener(this);
317         }
318     }
319     
320     public Object JavaDoc readResolve() {
321         return null;
322     }
323     
324     /**
325      * PropertyChangeListener implementation that refires all property changes
326      * to the listeners attached to a SourceElement.
327      */

328     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
329         List JavaDoc l;
330         synchronized (this) {
331             if (listeners == null)
332                 return;
333             l = new java.util.ArrayList JavaDoc(listeners);
334         }
335         // remap the event so that it appears to be fired from the source element.
336
evt = new PropertyChangeEvent JavaDoc(
337             srcElement, evt.getPropertyName(),
338             evt.getOldValue(), evt.getNewValue());
339         
340         for (Iterator JavaDoc it = l.iterator(); it.hasNext(); ) {
341             PropertyChangeListener JavaDoc ll = (PropertyChangeListener JavaDoc)it.next();
342             ll.propertyChange(evt);
343         }
344     }
345     
346 }
347
Popular Tags