KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > navigation > ElementNode


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 package org.netbeans.modules.retouche.navigation;
20
21
22 import java.awt.Image JavaDoc;
23 import java.util.Comparator JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.Icon JavaDoc;
30 import org.netbeans.api.gsf.Element;
31 import org.netbeans.api.gsf.ElementHandle;
32 import org.netbeans.api.gsf.ElementKind;
33 import org.netbeans.api.gsf.Modifier;
34 import org.netbeans.api.gsf.StructureItem;
35 import org.netbeans.api.gsf.StructureItem;
36 import org.netbeans.modules.retouche.navigation.actions.OpenAction;
37 import org.netbeans.modules.retouche.navigation.Icons;
38 import org.openide.filesystems.FileObject;
39 import org.openide.nodes.AbstractNode;
40 import org.openide.nodes.Children;
41 import org.openide.nodes.Children;
42 import org.openide.nodes.Node;
43 import org.openide.util.Utilities;
44
45 /**
46  * This file is originally from Retouche, the Java Support
47  * infrastructure in NetBeans. I have modified the file as little
48  * as possible to make merging Retouche fixes back as simple as
49  * possible.
50  * <p>
51  * GSF changes made: Instead of accessing fields on Description object,
52  * replace references to Description with StructureItem interface (descriptions
53  * supplied by language plugins), make method calls on this interface rather
54  * than accessing fields directly. Some data such as the "ui" field was moved
55  * into ElementNode itself rather than sitting on the description object which
56  * is no longer under our control.
57  * <p>
58  * Node representing an element
59  *
60  *
61  * @author Petr Hrebejk
62  */

63 public class ElementNode extends AbstractNode {
64
65     
66     private static Node WAIT_NODE;
67     
68     private OpenAction openAction;
69     private StructureItem description;
70     private ClassMemberPanelUI ui;
71     private FileObject fileObject; // For the root description
72

73     /** Creates a new instance of TreeNode */
74     public ElementNode( StructureItem description, ClassMemberPanelUI ui, FileObject fileObject) {
75         super(description.isLeaf() ? Children.LEAF: new ElementChilren((List JavaDoc<StructureItem>)description.getNestedItems(), ui.getFilters(), ui, fileObject));
76         this.description = description;
77         setDisplayName( description.getName() );
78         this.ui = ui;
79         this.fileObject = fileObject;
80     }
81     
82     
83     @Override JavaDoc
84     public Image JavaDoc getIcon(int type) {
85 // return description.kind == null ? super.getIcon(type) : Utilities.icon2Image(UiUtils.getElementIcon(description.kind, description.modifiers));
86
// return description.kind == null ? super.getIcon(type) : Utilities.icon2Image(Icons.getElementIcon(description.kind, description.modifiers));
87
Icon JavaDoc icon = Icons.getElementIcon(description.getKind(), description.getModifiers());
88         if (icon != null) {
89             return Utilities.icon2Image(icon);
90         } else {
91             return super.getIcon(type);
92         }
93     }
94
95     @Override JavaDoc
96     public Image JavaDoc getOpenedIcon(int type) {
97         return getIcon(type);
98     }
99                    
100     @Override JavaDoc
101     public java.lang.String JavaDoc getDisplayName() {
102         return description.getName();
103     }
104             
105     @Override JavaDoc
106     public String JavaDoc getHtmlDisplayName() {
107         return description.getHtml();
108     }
109     
110     @Override JavaDoc
111     public Action JavaDoc[] getActions( boolean context ) {
112         
113         if ( context || description.getName() == null ) {
114             return ui.getActions();
115         }
116         else {
117             Action JavaDoc panelActions[] = ui.getActions();
118             
119             Action JavaDoc actions[] = new Action JavaDoc[ 2 + panelActions.length ];
120             actions[0] = getOpenAction();
121             actions[1] = null;
122             for( int i = 0; i < panelActions.length; i++ ){
123                 actions[2 + i] = panelActions[i];
124             }
125             return actions;
126         }
127     }
128     
129     @Override JavaDoc
130     public Action JavaDoc getPreferredAction() {
131         return getOpenAction();
132     }
133     
134     
135     private synchronized Action JavaDoc getOpenAction() {
136         if ( openAction == null ) {
137             FileObject fo = ui.getFileObject();
138             openAction = new OpenAction(description.getElementHandle(), fo);
139         }
140         return openAction;
141     }
142     
143     static synchronized Node getWaitNode() {
144         if ( WAIT_NODE == null ) {
145             WAIT_NODE = new WaitNode();
146         }
147         return WAIT_NODE;
148     }
149     
150     public void refreshRecursively() {
151         Children ch = getChildren();
152         if ( ch instanceof ElementChilren ) {
153            ((ElementChilren)ch).resetKeys((List JavaDoc<StructureItem>)description.getNestedItems(), ui.getFilters());
154            for( Node sub : ch.getNodes() ) {
155                ui.expandNode(sub);
156                ((ElementNode)sub).refreshRecursively();
157            }
158         }
159     }
160     
161     public void updateRecursively( StructureItem newDescription ) {
162         Children ch = getChildren();
163         if ( ch instanceof ElementChilren ) {
164            HashSet JavaDoc<StructureItem> oldSubs = new HashSet JavaDoc<StructureItem>( description.getNestedItems() );
165
166            
167            // Create a hashtable which maps StructureItem to node.
168
// We will then identify the nodes by the description. The trick is
169
// that the new and old description are equal and have the same hashcode
170
Node[] nodes = ch.getNodes( true );
171            HashMap JavaDoc<StructureItem,ElementNode> oldD2node = new HashMap JavaDoc<StructureItem,ElementNode>();
172            for (Node node : nodes) {
173                oldD2node.put(((ElementNode)node).description, (ElementNode)node);
174            }
175            
176            // Now refresh keys
177
((ElementChilren)ch).resetKeys((List JavaDoc<StructureItem>)newDescription.getNestedItems(), ui.getFilters());
178
179            
180            // Reread nodes
181
nodes = ch.getNodes( true );
182            
183            for( StructureItem newSub : newDescription.getNestedItems() ) {
184                 ElementNode node = oldD2node.get(newSub);
185                 if ( node != null ) { // filtered out
186
if ( !oldSubs.contains(newSub) && node.getChildren() != Children.LEAF) {
187                         ui.expandNode(node); // Make sure new nodes get expanded
188
}
189                     node.updateRecursively( newSub ); // update the node recursively
190
}
191            }
192         }
193                         
194         StructureItem oldDescription = description; // Remember old description
195
description = newDescription; // set new descrioption to the new node
196
if ( oldDescription.getHtml() != null && !oldDescription.getHtml().equals(description.getHtml())) {
197             // Different headers => we need to fire displayname change
198
fireDisplayNameChange(oldDescription.getHtml(), description.getHtml());
199         }
200         if( oldDescription.getModifiers() != null && !oldDescription.getModifiers().equals(newDescription.getModifiers())) {
201             fireIconChange();
202             fireOpenedIconChange();
203         }
204     }
205     
206     // XXX There's a typo in this name!!!
207
public StructureItem getDescritption() {
208         return description;
209     }
210     
211     public FileObject getFileObject() {
212         return fileObject;
213     }
214     
215     // XXX There's a typo in this name
216
private static final class ElementChilren extends Children.Keys<StructureItem> {
217         private ClassMemberPanelUI ui;
218         private FileObject fileObject;
219         
220         public ElementChilren(List JavaDoc<StructureItem> descriptions, ClassMemberFilters filters, ClassMemberPanelUI ui, FileObject fileObject) {
221             resetKeys( descriptions, filters );
222             this.ui = ui;
223             this.fileObject = fileObject;
224         }
225         
226         protected Node[] createNodes(StructureItem key) {
227             return new Node[] {new ElementNode(key, ui, fileObject)};
228         }
229         
230         void resetKeys( List JavaDoc<StructureItem> descriptions, ClassMemberFilters filters ) {
231             setKeys( filters.filter(descriptions) );
232         }
233     }
234                        
235     /** Stores all interesting data about given element.
236      */

237     static class Description {
238         
239         public static final Comparator JavaDoc<StructureItem> ALPHA_COMPARATOR =
240             new DescriptionComparator(true);
241         public static final Comparator JavaDoc<StructureItem> POSITION_COMPARATOR =
242             new DescriptionComparator(false);
243         
244         ClassMemberPanelUI ui;
245                 
246         //FileObject fileObject; // For the root description
247

248         String JavaDoc name;
249         ElementHandle<? extends Element> elementHandle;
250         ElementKind kind;
251         Set JavaDoc<Modifier> modifiers;
252         List JavaDoc<Description> subs;
253         String JavaDoc htmlHeader;
254         long pos;
255         
256         Description( ClassMemberPanelUI ui ) {
257             this.ui = ui;
258         }
259                                 
260         @Override JavaDoc
261         public boolean equals(Object JavaDoc o) {
262                         
263             if ( o == null ) {
264                 //System.out.println("- f nul");
265
return false;
266             }
267             
268             if ( !(o instanceof Description)) {
269                 // System.out.println("- not a desc");
270
return false;
271             }
272             
273             Description d = (Description)o;
274             
275             if ( kind != d.kind ) {
276                 // System.out.println("- kind");
277
return false;
278             }
279             
280             if ( !name.equals(d.name) ) {
281                 // System.out.println("- name");
282
return false;
283             }
284
285 // if ( !this.elementHandle.signatureEquals(d.elementHandle) ) {
286
// return false;
287
// }
288

289             /*
290             if ( !modifiers.equals(d.modifiers)) {
291                 // E.println("- modifiers");
292                 return false;
293             }
294             */

295             
296             // System.out.println("Equals called");
297
return true;
298         }
299         
300         
301         public int hashCode() {
302             int hash = 7;
303
304             hash = 29 * hash + (this.name != null ? this.name.hashCode() : 0);
305             hash = 29 * hash + (this.kind != null ? this.kind.hashCode() : 0);
306             // hash = 29 * hash + (this.modifiers != null ? this.modifiers.hashCode() : 0);
307
return hash;
308         }
309
310         private static class DescriptionComparator implements Comparator JavaDoc<StructureItem> {
311             
312             boolean alpha;
313             
314             DescriptionComparator( boolean alpha ) {
315                 this.alpha = alpha;
316             }
317             
318             public int compare(StructureItem d1, StructureItem d2) {
319 // TODO: Handle position-based sorting
320
// if ( alpha ) {
321
if ( k2i(d1.getKind()) != k2i(d2.getKind()) ) {
322                         return k2i(d1.getKind()) - k2i(d2.getKind());
323                     }
324                     
325                     return d1.getName().compareTo(d2.getName());
326 // }
327
// else {
328
// return 0;
329
// return d1.pos == d2.pos ? 0 : d1.pos < d2.pos ? -1 : 1;
330
// }
331
}
332             
333             int k2i( ElementKind kind ) {
334                 switch( kind ) {
335                     case CONSTRUCTOR:
336                         return 1;
337                     case METHOD:
338                         return 2;
339                     case FIELD:
340                         return 3;
341                     case CLASS:
342 // case INTERFACE:
343
// case ENUM:
344
// case ANNOTATION_TYPE:
345
// return 4;
346
default:
347                         return 100;
348                 }
349             }
350         }
351         
352     }
353         
354     private static class WaitNode extends AbstractNode {
355         
356         private Image JavaDoc waitIcon = Utilities.loadImage("org/netbeans/modules/retouche/navigation/resources/wait.gif"); // NOI18N
357

358         WaitNode( ) {
359             super( Children.LEAF );
360         }
361         
362         @Override JavaDoc
363         public Image JavaDoc getIcon(int type) {
364              return waitIcon;
365         }
366
367         @Override JavaDoc
368         public Image JavaDoc getOpenedIcon(int type) {
369             return getIcon(type);
370         }
371
372         @java.lang.Override JavaDoc
373         public java.lang.String JavaDoc getDisplayName() {
374             return "Please Wait...";
375         }
376         
377     }
378     
379     
380 }
381
Popular Tags