KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import org.openide.src.*;
25
26 import javax.jmi.reflect.RefObject;
27 import javax.jmi.reflect.RefBaseObject;
28 import org.netbeans.api.mdr.events.*;
29
30 import org.netbeans.jmi.javamodel.Resource;
31
32 class ImportsCollection extends ObjectsCollection {
33
34     static final ImportElement[] EMPTY = new ImportElement[0];
35
36     private Resource resource;
37
38     public ImportsCollection(FeaturesCollection members, Resource resource) {
39         super (members);
40         this.resource = resource;
41     }
42
43     public RefObject createFeature(RefObject parent, Element elem) {
44         org.netbeans.jmi.javamodel.Import res = members.createImport ((ImportElement) elem);
45         return res;
46     }
47     
48     public Element [] getEmptyArray () {
49         return EMPTY;
50     }
51     
52     public String JavaDoc getPropertyName () {
53         return ElementProperties.PROP_IMPORTS;
54     }
55     
56     public boolean isOfType (RefObject feature) {
57         boolean isStatic = false;
58         members.repository.beginTrans(false);
59         try {
60             if (((org.netbeans.jmi.javamodel.Import) feature).isValid())
61                 isStatic = ((org.netbeans.jmi.javamodel.Import) feature).isStatic();
62         } finally {
63             members.repository.endTrans(false);
64         }
65         return (feature instanceof org.netbeans.jmi.javamodel.Import) && !isStatic;
66     }
67     
68     public boolean isClassMember () {
69         return false;
70     }
71     
72     public List getFeatures () {
73         return resource.getImports ();
74     }
75     
76     public boolean isValid() {
77         return resource.isValid();
78     }
79     
80     public Element createElement (RefObject imp) {
81         return (ImportElement) members.model.createImport ((SourceElement) members.parentImpl.getElement (), (org.netbeans.jmi.javamodel.Import)imp).getElement ();
82     }
83  
84     public Import[] getImports() {
85         ImportElement[] elems = (ImportElement[]) getElements ();
86         Import[] res = new Import [elems.length];
87         for (int x = 0; x < elems.length; x++) {
88             res [x] = elems [x].getImport ();
89         }
90         return res;
91     }
92     
93     protected void addMembers (Element[] items) throws SourceException {
94         if (items.length == 0)
95             return;
96         
97         members.repository.beginTrans (true);
98         boolean failed = true;
99         try {
100             if (resource.isValid()) {
101                 members.parentImpl.setClassPath();
102                 List features = getFeatures ();
103                 ImportElement[] imps = new ImportElement[features.size()];
104                 Map posToImps = new HashMap ();
105                 Iterator iter = features.iterator();
106                 for (int x = 0; iter.hasNext(); x++) {
107                     imps[x] = (ImportElement) cachedElement ((RefObject) iter.next());
108                 }
109
110                 for (int x = 0; x < items.length; x++) {
111                     int pos = 0;
112                     Import newImp = ((ImportElement) items[x]).getImport();
113                     for (int y = 0; y < imps.length; y++) {
114                         Import imp = ((ImportElement)imps[y]).getImport();
115                         if(imp.getIdentifier().getFullName().compareTo(newImp.getIdentifier().getFullName() ) < 0 ) {
116                             pos = y + 1;
117                         }
118                     }
119                     Integer JavaDoc integer = new Integer JavaDoc(pos);
120                     List list = (List) posToImps.get (integer);
121                     if (list == null) {
122                         list = new LinkedList();
123                         posToImps.put (integer, list);
124                     }
125                     list.add (items[x]);
126                 }
127
128                 ListIterator listIter = features.listIterator();
129                 for (int x = 0; x <= imps.length; x++) {
130                     List list = (List) posToImps.get (new Integer JavaDoc(x));
131                     if (list != null) {
132                         iter = list.iterator();
133                         while (iter.hasNext()) {
134                             listIter.add (createFeature (members.javaClass, (Element)iter.next()));
135                         }
136                     }
137                     if (listIter.hasNext())
138                         listIter.next();
139                 }
140                 failed = false;
141             } else {
142                 failed = false;
143                 members.parentImpl.throwIsInvalid ();
144             }
145         } finally {
146             members.repository.endTrans (failed);
147         }
148     }
149     
150     protected void setMembers (Element[] items) throws SourceException {
151         members.repository.beginTrans (true);
152         boolean failed = true;
153         try {
154             if (resource.isValid()) {
155                 members.parentImpl.setClassPath();
156                 List features = getFeatures ();
157                 features.clear ();
158                 for (int x = 0; x < items.length; x++) {
159                     features.add (createFeature (members.javaClass, items[x]));
160                 }
161                 failed = false;
162             } else {
163                 failed = false;
164                 members.parentImpl.throwIsInvalid ();
165             }
166         } finally {
167             members.repository.endTrans (failed);
168         }
169     }
170     
171     public void changeImports(Import[] defs, int operation) throws SourceException {
172         ImportElement[] elems = new ImportElement [defs.length];
173         for (int x = 0; x < defs.length; x++) {
174             elems [x] = new ImportElement (
175                 new ImportElement.MemoryImpl (),
176                 (SourceElement) members.parentImpl.getElement ()
177             );
178             elems [x].setImport (defs [x]);
179         }
180         changeMembers (elems, operation);
181     }
182     
183     public boolean matches (Element elem, RefObject f) {
184         org.netbeans.jmi.javamodel.Import imp = (org.netbeans.jmi.javamodel.Import) f;
185         if (imp.isStatic())
186             return false;
187         Import srcImp = ((ImportElement) elem).getImport ();
188         String JavaDoc name1 = imp.getName ();
189         boolean isNamed1 = true;
190         if (name1.endsWith("*")) { // NOI18N
191
isNamed1 = false;
192             name1 = name1.substring (0, name1.length() - 2);
193         }
194         String JavaDoc name2 = srcImp.getIdentifier ().getFullName ();
195         if ((name1 == null) || (name2 == null))
196             return false;
197         return (isNamed1 == srcImp.isClass ()) && name1.equals (name2);
198     }
199     
200     // ..........................................................................
201

202     static class ImportsListener extends ObjectsCollection.FeaturesListener {
203
204         ImportsListener (ElementImpl impl) {
205             super (impl);
206             fireMembers = false;
207         }
208
209         public void connect () {
210             if (REGISTER_LISTENER) {
211                 ((MDRChangeSource) javaElement).addListener (this);
212                 features = new ArrayList ();
213                 features.addAll (((Resource) javaElement).getImports ());
214             }
215         }
216
217         public ObjectsCollection getFeatureCollection (RefObject feature) {
218             return ((SourceElementImpl) impl).imports;
219         }
220         
221         protected boolean isWatchedAttribute (AttributeEvent ev) {
222             return ev.getAttributeName ().equals ("imports"); // NOI18N
223
}
224         
225         public Element cachedElement (RefObject f) {
226             return ((SourceElementImpl) impl).imports.cachedElement (f);
227         }
228         
229     }
230
231 }
232
Popular Tags