KickJava   Java API By Example, From Geeks To Geeks.

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


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

21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.lang.ref.*;
25 import java.util.*;
26
27 import org.netbeans.api.java.classpath.ClassPath;
28 import org.openide.ErrorManager;
29
30 import org.openide.src.*;
31
32 import org.netbeans.api.mdr.events.*;
33 import org.netbeans.jmi.javamodel.CallableFeature;
34 import org.netbeans.jmi.javamodel.Field;
35 import org.netbeans.modules.java.JavaDataObject;
36 import org.openide.nodes.Node;
37
38 import org.netbeans.jmi.javamodel.Resource;
39 import org.netbeans.jmi.javamodel.JavaPackage;
40 import org.netbeans.jmi.javamodel.JavaPackageClass;
41 import org.netbeans.jmi.javamodel.JavaModelPackage;
42 import org.netbeans.jmi.javamodel.JavaClass;
43 import org.netbeans.jmi.javamodel.Method;
44 import org.netbeans.jmi.javamodel.Parameter;
45 import org.netbeans.jmi.javamodel.ParameterizedType;
46 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
47 import org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl;
48 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
49 import org.openide.filesystems.FileObject;
50
51 public class SourceElementImpl extends ElementImpl implements SourceElement.Impl, ElementOrder,
52     PropertyChangeListener JavaDoc {
53
54     private transient FeaturesCollection members;
55     transient TopClassesCollection topClasses;
56     transient ImportsCollection imports;
57
58     private ElementImpl.ElementListener classesListener, sourceListener, importsListener;
59     
60     private IdentContextSupport identSupp;
61     
62     private transient Resource resource;
63     
64     private transient JavaDataObject javaObject;
65     
66     private static final long serialVersionUID = 8506642610861188475L;
67     
68     // init .....................................................................
69

70     public SourceElementImpl(DefaultLangModel model, Resource resource, JavaDataObject jdo) {
71         super(model, resource);
72         
73         this.resource = resource;
74         javaObject = jdo;
75         identSupp = new IdentContextSupport(11);
76         
77         members = new FeaturesCollection (this);
78         topClasses = new TopClassesCollection (members, resource, this);
79         imports = new ImportsCollection (members, (Resource) resource);
80     }
81     
82     public void connectListener () {
83         sourceListener = new SourceListener (this);
84         sourceListener.connect ();
85         classesListener = new TopClassesCollection.TopClassesListener (this);
86         classesListener.connect ();
87         org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl resImpl =
88             (org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl) resource;
89         if (resImpl.importsInited()) {
90             importsListener = new ImportsCollection.ImportsListener (this);
91             importsListener.connect ();
92         } else {
93             resImpl.addImportsListener(this);
94         }
95     }
96     
97     boolean isResourceValid() {
98         return resource.isValid();
99     }
100     
101     // a hack - listens on imports initialization fired by ResourceImpl
102
public void propertyChange (PropertyChangeEvent JavaDoc event) {
103         importsListener = new ImportsCollection.ImportsListener (this);
104         importsListener.connect ();
105     }
106     
107     protected void notifyElementCreated() {
108         // do nothing -- override ElementImpl's behaviour.
109
}
110     
111     protected void createFromModel(Element model) throws SourceException {
112     }
113     
114     public Element[] getElements() {
115         return getClasses();
116     }
117     
118     public Identifier getPackage() {
119         repository.beginTrans(false);
120         try {
121             if (resource.isValid()) {
122                 setClassPath();
123                 String JavaDoc javaPackage = resource.getPackageName();
124                 return javaPackage != null ? Identifier.create(javaPackage) : null;
125             } else {
126                 return null;
127             }
128         } finally {
129             repository.endTrans(false);
130         }
131     }
132     
133     public Import[] getImports() {
134         return imports.getImports ();
135     }
136     
137     public ImportElement[] getImportElements() {
138         return (ImportElement[]) imports.getElements ();
139     }
140     
141     public ClassElement getClass(Identifier id) {
142         return topClasses.getClass (id);
143     }
144     
145     public ClassElement[] getClasses() {
146         return topClasses.getClasses();
147     }
148     
149     public ClassElement[] getAllClasses() {
150         ClassElement[] tops = getClasses();
151         Collection list = new LinkedList();
152         
153         for (int i = 0; i < tops.length; i++) {
154             addAllClasses(tops[i], list);
155         }
156         return (ClassElement[])list.toArray(new ClassElement [0]);
157     }
158     
159     private void addAllClasses(ClassElement c, Collection col) {
160         col.add(c);
161         ClassElement[] inner = c.getClasses();
162         for (int i = 0; i < inner.length; i++)
163             addAllClasses(inner[i], col);
164     }
165
166     // Setters/changers
167
///////////////////////////////////////////////////////////////////////////////////
168
public void setPackage(Identifier packageID) throws SourceException {
169         checkWritable(false);
170         checkDocument();
171         boolean failed = true;
172         repository.beginTrans (true);
173         try {
174             if (resource.isValid()) {
175                 setClassPath();
176                 Identifier old = getPackage ();
177                 if (old == packageID ||
178                     (old != null && packageID != null &&
179                         old.getSourceName().equals(packageID.getSourceName()))) {
180                     failed = false;
181                     return;
182                 }
183                 PropertyChangeEvent JavaDoc evt;
184                 JavaPackageClass pkgProxy = ((JavaModelPackage)resource.refImmediatePackage()).getJavaPackage();
185                 String JavaDoc packId=packageID!=null?packageID.getFullName():"";
186                 JavaClass defClasses[];
187                 int i;
188                 evt = new PropertyChangeEvent JavaDoc(getEventSource(), PROP_PACKAGE,
189                     old, packageID);
190                 checkVetoablePropertyChange(evt);
191
192                 resource.setPackageName(packId);
193
194                 /*
195                 List defClassesList = new LinkedList();
196                 Iterator iter = resource.getClassifiers().iterator ();
197                 while(iter.hasNext()) {
198                     Object obj = iter.next();
199                     if (obj instanceof JavaClass) {
200                         defClassesList.add(obj);
201                     }
202                 }
203                 JavaClass[] defClasses=new JavaClass[defClassesList.size()];
204                 defClassesList.toArray(defClasses);
205                 for (i=0;i<defClasses.length;i++) {
206                     defClasses[i].setJavaPackage(jpck);
207                 }
208                  */

209                 failed = false;
210             } else {
211                 failed = false;
212                 throwIsInvalid ();
213             }
214         } finally {
215             repository.endTrans (failed);
216         }
217     }
218
219     public void firePackageChange (Identifier oldId, Identifier newId) {
220         if (oldId == newId ||
221             (oldId != null && newId != null &&
222                 oldId.getSourceName().equals(newId.getSourceName()))) {
223             return;
224         }
225         PropertyChangeEvent JavaDoc evt = new PropertyChangeEvent JavaDoc (
226             getEventSource(),
227             PROP_PACKAGE,
228             oldId, newId
229         );
230         fireOwnPropertyChange(evt);
231         
232         // [PENDING]
233
// System.out.println("SourceElementImpl: notifyConnectionChange");
234
}
235     
236     public void changeImports(Import[] defs, int operation) throws SourceException {
237         checkWritable(false);
238         checkDocument();
239         imports.changeImports (defs, operation);
240     }
241     
242     public void changeClasses(ClassElement[] classes, int operation) throws SourceException {
243         checkWritable(false);
244         checkDocument();
245         topClasses.changeMembers(classes, operation);
246     }
247     
248     private void notifyCreate(Element[] els) {
249         for (int i = 0; i < els.length; i++) {
250             ElementImpl impl = (ElementImpl)els[i].getCookie(ElementImpl.class);
251             impl.notifyCreate();
252         }
253     }
254     
255     protected void notifyCreate() {
256         /*
257         Element[] els;
258         
259         if (this.imports != null) {
260             notifyCreate(imports.getElements());
261         }
262         if (this.topClasses != null) {
263             notifyCreate(topClasses.getElements());
264         }
265         super.notifyCreate();
266          */

267     }
268     
269     private Binding.Source getSourceBinding() {
270         return (Binding.Source)getBinding();
271     }
272
273     /** Dummy method to satisfy typing requirements. This method does not work with the
274      * model, but rather serves as interface to the parser. The method may be implemented
275      * in subclasses or delegating implementations.
276      */

277     public int getStatus() {
278         return SourceElement.STATUS_NOT;
279     }
280     
281     public org.openide.util.Task prepare() {
282         // [TODO] replace FilesystemChangeMonitor with something else
283
// return FilesystemChangeMonitor.getDefault().parseSourceFile(javaObject);
284
return org.openide.util.Task.EMPTY; // [PENDING]
285
}
286     
287     public void runAtomic(Runnable JavaDoc run) {
288         boolean failed = true;
289         repository.beginTrans (true);
290         try {
291             setClassPath();
292             run.run ();
293             failed = false;
294         } finally {
295             repository.endTrans (failed);
296         }
297     }
298     
299     public void runAtomicAsUser(Runnable JavaDoc run) throws SourceException {
300         runAtomic (run); // [PENDING]
301
}
302     
303     public void updateMembers(String JavaDoc name, Element[] elements, int[] optMap) {
304         /*
305         if (name == ElementProperties.PROP_IMPORTS) {
306             if (imports == null) {
307                 if (elements.length == 0)
308                     return;
309                 initializeImports();
310             }
311             imports.updateMembers(elements, optMap);
312         } else if (name == ElementProperties.PROP_CLASSES) {
313             if (topClasses == null) {
314                 if (elements.length == 0)
315                     return;
316                 initializeClasses();
317             }
318             topClasses.updateMembers(elements, optMap);
319         } else {
320             throw new IllegalArgumentException("Unsupported property: " + name); // NOI18N
321         }
322          */

323     }
324     
325     protected void checkWritable(boolean unsafeOp) throws SourceException {
326         SourceException e = null;
327         int status = resource.getStatus();
328         if ((status & ResourceImpl.HAS_GENERICS) != 0) {
329             e = new SourceException.IO("Source containing generics cannot be modified: " + resource.getName()); //NOI18N
330
} else if ((status & ResourceImpl.HAS_ANNOTATION) != 0) {
331             e = new SourceException.IO("Source containing annotations cannot be modified: " + resource.getName()); //NOI18N
332
}
333         if (e != null) {
334             if (JDK15_CHECKS_DISABLED) {
335                 if (unsafeOp) {
336                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
337                 }
338             } else {
339                 throw e;
340             }
341         }
342     }
343     
344     // Implementation details
345
///////////////////////////////////////////////////////////////////////////////////
346
public void setParent(ElementImpl impl) {
347         // no-op, sources cannot have parents.
348
}
349     
350     protected SourceElementImpl findSource() {
351         return this;
352     }
353     
354     public Object JavaDoc readResolve() {
355         return null;
356     }
357     
358     protected Element cloneSelf() {
359         return null;
360     }
361     
362     protected Identifier createLocalIdentifier(Identifier id, int status) {
363         return identSupp.create(id, status);
364     }
365     
366     protected boolean checkIdentifierContext(Identifier id) {
367         return identSupp.checkContext(id);
368     }
369     
370     protected boolean parentValid() {
371         return isValid();
372     }
373     
374     protected void notifyRemove() {
375         /*
376         Element[] allElems;
377         
378         if (imports != null) {
379             allElems = imports.getElements();
380             for (int i = 0; i < allElems.length; i++) {
381                 ElementImpl impl = imports.getElementImpl(allElems[i]);
382                 impl.notifyRemove();
383             }
384         }
385         if (topClasses != null) {
386             allElems = topClasses.getElements();
387             for (int i = 0; i < allElems.length; i++) {
388                 ElementImpl impl = topClasses.getElementImpl(allElems[i]);
389                 impl.notifyRemove();
390             }
391         }
392         super.notifyRemove();
393          */

394     }
395     
396     /** Implementation of getCookie(). The implementation fakes cookies of all interfaces
397      * on this object; if the cookie is not available, it asks the environment to
398      * provide the cookie and IF the cookie descends from the implementation hierarchy
399      * passes the request on to the real SourceElement's implementation, optionally
400      * parsing it from the source.
401      *
402      */

403     public Node.Cookie getCookie(Class JavaDoc type) {
404         if (type.isAssignableFrom(getClass()))
405             return this;
406         // if (type.isAssignableFrom(JMIElementCookie.class))
407
// return super.getCookie(type);
408
return javaObject.getCookie(type);
409     }
410     
411     // ..........................................................................
412

413     static class SourceListener extends ElementImpl.ElementListener {
414         
415         Identifier packageId = null;
416         
417         SourceListener (SourceElementImpl impl) {
418             super (impl);
419             String JavaDoc pkgName = ((Resource) javaElement).getPackageName();
420             packageId = pkgName != null ? Identifier.create (pkgName) : null;
421         }
422         
423         /*
424         public void connect() {
425             ((MDRChangeSource) javaElement.refOutermostPackage()).addListener(this);
426             super.connect();
427         }
428         
429         public void remove() {
430             ((MDRChangeSource) javaElement.refOutermostPackage()).removeListener(this);
431             super.remove();
432         }
433          */

434         
435         public void doChange(MDRChangeEvent event) {
436             super.doChange (event);
437             if ((event instanceof AttributeEvent) &&
438                 ((AttributeEvent) event).getAttributeName ().equals ("packageName")) { // NOI18N
439
Identifier oldId = packageId;
440                 String JavaDoc name = (String JavaDoc) ((AttributeEvent) event).getNewElement ();
441                 packageId = name != null ? Identifier.create (name) : null;
442                 ((SourceElementImpl) impl).firePackageChange (oldId, packageId);
443             }
444         }
445     }
446     
447 }
Popular Tags