KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
23 import javax.jmi.reflect.RefObject;
24 import org.netbeans.api.mdr.events.*;
25 import org.netbeans.jmi.javamodel.Resource;
26 import org.netbeans.jmi.javamodel.JavaClass;
27 import org.openide.src.*;
28
29
30 class TopClassesCollection extends ClassesCollection {
31
32     private Resource resource;
33     private SourceElementImpl sourceImpl;
34
35     public TopClassesCollection(FeaturesCollection members, Resource resource, SourceElementImpl sourceImpl) {
36         super (members);
37         this.resource = resource;
38         this.sourceImpl = sourceImpl;
39     }
40
41     public RefObject createFeature(RefObject parent, Element elem) {
42         JavaClass res = members.createClass ((ClassElement) elem);
43         return res;
44     }
45     
46     public boolean isClassMember () {
47         return false;
48     }
49     
50     public List getFeatures () {
51         members.repository.beginTrans(false);
52         try {
53             return resource.getClassifiers();
54         } finally {
55             members.repository.endTrans();
56         }
57     }
58     
59     public boolean isValid() {
60         return resource.isValid();
61     }
62     
63     public Element createElement(RefObject topClass) {
64         return (ClassElement) members.model.createTopClass ((SourceElement) sourceImpl.getElement (), (JavaClass)topClass).getElement ();
65     }
66
67     protected void addMembers (Element[] items) throws SourceException {
68         if (items.length == 0)
69             return;
70         
71         members.repository.beginTrans (true);
72         boolean failed = true;
73         try {
74             if (isValid()) {
75                 members.parentImpl.setClassPath();
76                 List features = getFeatures ();
77                 for (int x = 0; x < items.length; x++) {
78                     features.add (createFeature (members.javaClass, items[x]));
79                 }
80                 failed = false;
81             } else {
82                 failed = false;
83                 members.parentImpl.throwIsInvalid ();
84             }
85         } finally {
86             members.repository.endTrans (failed);
87         }
88     }
89     
90     protected void setMembers (Element[] items) throws SourceException {
91         members.repository.beginTrans (true);
92         boolean failed = true;
93         try {
94             if (isValid()) {
95                 members.parentImpl.setClassPath();
96                 List features = getFeatures ();
97                 features.clear ();
98                 for (int x = 0; x < items.length; x++) {
99                     features.add (createFeature (members.javaClass, items[x]));
100                 }
101                 failed = false;
102             } else {
103                 failed = false;
104                 members.parentImpl.throwIsInvalid ();
105             }
106         } finally {
107             members.repository.endTrans (failed);
108         }
109     }
110     
111     public ClassElement getClass(Identifier name) {
112         String JavaDoc clsName = name.getName ();
113         if (clsName == null)
114             return null;
115         members.repository.beginTrans (false);
116         try {
117             if (isValid()) {
118                 Iterator iter = resource.getClassifiers ().iterator ();
119                 while (iter.hasNext ()) {
120                     Object JavaDoc obj = iter.next ();
121                     if (!(obj instanceof JavaClass)) {
122                         continue;
123                     }
124                     JavaClass cls = (JavaClass) obj;
125                     String JavaDoc clsFullName = cls.getName();
126                     String JavaDoc clsSimpleName = cls.getSimpleName();
127                     if (clsName.equals (clsSimpleName)) {
128                         String JavaDoc fqn = name.getFullName ();
129                         if (!fqn.equals (clsSimpleName) && !fqn.equals (clsFullName))
130                             return null;
131                         else
132                             return (ClassElement) cachedElement (cls);
133                     } // if
134
} // while
135
return null;
136             } else {
137                 return null;
138             }
139         } finally {
140             members.repository.endTrans (false);
141         }
142     }
143     
144     // ..........................................................................
145

146     static class TopClassesListener extends ObjectsCollection.FeaturesListener {
147     
148         TopClassesListener (ElementImpl impl) {
149             super (impl);
150             fireMembers = false;
151         }
152         
153         public void connect () {
154             if (REGISTER_LISTENER) {
155                 ((MDRChangeSource) javaElement).addListener (this);
156                 features = new ArrayList ();
157                 Iterator iter = ((Resource) javaElement).getClassifiers ().iterator();
158                 while (iter.hasNext()) {
159                     Object JavaDoc obj = iter.next();
160                     if (obj instanceof JavaClass) {
161                         features.add (obj);
162                     }
163                 } // while
164
}
165         }
166         
167         public ObjectsCollection getFeatureCollection (RefObject feature) {
168             return ((SourceElementImpl) impl).topClasses;
169         }
170                 
171         protected boolean isWatchedAttribute (AttributeEvent ev) {
172             return ev.getAttributeName ().equals ("classifiers"); // NOI18N
173
}
174         
175         public Element cachedElement (RefObject f) {
176             if (f instanceof JavaClass) {
177                 return ((SourceElementImpl) impl).topClasses.cachedElement (f);
178             } else {
179                 return null;
180             }
181         }
182
183     }
184     
185 }
186
Popular Tags