1 19 package org.netbeans.modules.retouche.navigation; 20 21 22 import java.awt.Image ; 23 import java.util.Comparator ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Set ; 28 import javax.swing.Action ; 29 import javax.swing.Icon ; 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 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; 73 74 public ElementNode( StructureItem description, ClassMemberPanelUI ui, FileObject fileObject) { 75 super(description.isLeaf() ? Children.LEAF: new ElementChilren((List <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 84 public Image getIcon(int type) { 85 Icon 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 96 public Image getOpenedIcon(int type) { 97 return getIcon(type); 98 } 99 100 @Override 101 public java.lang.String getDisplayName() { 102 return description.getName(); 103 } 104 105 @Override 106 public String getHtmlDisplayName() { 107 return description.getHtml(); 108 } 109 110 @Override 111 public Action [] getActions( boolean context ) { 112 113 if ( context || description.getName() == null ) { 114 return ui.getActions(); 115 } 116 else { 117 Action panelActions[] = ui.getActions(); 118 119 Action actions[] = new Action [ 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 130 public Action getPreferredAction() { 131 return getOpenAction(); 132 } 133 134 135 private synchronized Action 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 <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 <StructureItem> oldSubs = new HashSet <StructureItem>( description.getNestedItems() ); 165 166 167 Node[] nodes = ch.getNodes( true ); 171 HashMap <StructureItem,ElementNode> oldD2node = new HashMap <StructureItem,ElementNode>(); 172 for (Node node : nodes) { 173 oldD2node.put(((ElementNode)node).description, (ElementNode)node); 174 } 175 176 ((ElementChilren)ch).resetKeys((List <StructureItem>)newDescription.getNestedItems(), ui.getFilters()); 178 179 180 nodes = ch.getNodes( true ); 182 183 for( StructureItem newSub : newDescription.getNestedItems() ) { 184 ElementNode node = oldD2node.get(newSub); 185 if ( node != null ) { if ( !oldSubs.contains(newSub) && node.getChildren() != Children.LEAF) { 187 ui.expandNode(node); } 189 node.updateRecursively( newSub ); } 191 } 192 } 193 194 StructureItem oldDescription = description; description = newDescription; if ( oldDescription.getHtml() != null && !oldDescription.getHtml().equals(description.getHtml())) { 197 fireDisplayNameChange(oldDescription.getHtml(), description.getHtml()); 199 } 200 if( oldDescription.getModifiers() != null && !oldDescription.getModifiers().equals(newDescription.getModifiers())) { 201 fireIconChange(); 202 fireOpenedIconChange(); 203 } 204 } 205 206 public StructureItem getDescritption() { 208 return description; 209 } 210 211 public FileObject getFileObject() { 212 return fileObject; 213 } 214 215 private static final class ElementChilren extends Children.Keys<StructureItem> { 217 private ClassMemberPanelUI ui; 218 private FileObject fileObject; 219 220 public ElementChilren(List <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 <StructureItem> descriptions, ClassMemberFilters filters ) { 231 setKeys( filters.filter(descriptions) ); 232 } 233 } 234 235 237 static class Description { 238 239 public static final Comparator <StructureItem> ALPHA_COMPARATOR = 240 new DescriptionComparator(true); 241 public static final Comparator <StructureItem> POSITION_COMPARATOR = 242 new DescriptionComparator(false); 243 244 ClassMemberPanelUI ui; 245 246 248 String name; 249 ElementHandle<? extends Element> elementHandle; 250 ElementKind kind; 251 Set <Modifier> modifiers; 252 List <Description> subs; 253 String htmlHeader; 254 long pos; 255 256 Description( ClassMemberPanelUI ui ) { 257 this.ui = ui; 258 } 259 260 @Override 261 public boolean equals(Object o) { 262 263 if ( o == null ) { 264 return false; 266 } 267 268 if ( !(o instanceof Description)) { 269 return false; 271 } 272 273 Description d = (Description)o; 274 275 if ( kind != d.kind ) { 276 return false; 278 } 279 280 if ( !name.equals(d.name) ) { 281 return false; 283 } 284 285 289 295 296 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 return hash; 308 } 309 310 private static class DescriptionComparator implements Comparator <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 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 } 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 default: 347 return 100; 348 } 349 } 350 } 351 352 } 353 354 private static class WaitNode extends AbstractNode { 355 356 private Image waitIcon = Utilities.loadImage("org/netbeans/modules/retouche/navigation/resources/wait.gif"); 358 WaitNode( ) { 359 super( Children.LEAF ); 360 } 361 362 @Override 363 public Image getIcon(int type) { 364 return waitIcon; 365 } 366 367 @Override 368 public Image getOpenedIcon(int type) { 369 return getIcon(type); 370 } 371 372 @java.lang.Override 373 public java.lang.String getDisplayName() { 374 return "Please Wait..."; 375 } 376 377 } 378 379 380 } 381 | Popular Tags |