1 19 20 package org.netbeans.api.enode; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.ArrayList ; 25 import java.awt.Image ; 26 import javax.swing.Action ; 27 import javax.swing.ImageIcon ; 28 import javax.swing.Icon ; 29 30 import org.openide.nodes.AbstractNode; 31 import org.openide.nodes.Children; 32 import org.openide.util.Lookup; 33 34 35 41 public class ExtensibleNode extends AbstractNode { 42 43 46 public static final String E_NODE_ACTIONS = "/ExtensibleNode/Actions/"; 48 51 public static final String E_NODE_LOOKUP = "/ExtensibleNode/Lookup/"; 53 56 public static final String E_NODE_ICONS = "/ExtensibleNode/Icons/"; 58 61 public static final String E_NODE_SUBMENUS = "/ExtensibleNode/SubMenu/"; 65 private String [] paths; 66 67 70 private ExtensibleActions actionManager; 71 72 75 private ExtensibleIcons iconManager; 76 77 80 private IconChangeListener iconChangeListener; 81 82 85 private String iconName; 86 87 96 public ExtensibleNode(String path, boolean useHierarchicalPath) { 97 this(useHierarchicalPath ? 98 computeHierarchicalPaths(path) : 99 new String [] { path } 100 ); 101 } 102 103 109 public ExtensibleNode(String [] paths) { 110 this(Children.LEAF, paths); 111 } 112 113 123 public ExtensibleNode(Children ch, String path, boolean useHierarchicalPath) { 124 this(ch, useHierarchicalPath ? 125 computeHierarchicalPaths(path) : 126 new String [] { path } 127 ); 128 } 129 130 137 public ExtensibleNode(Children ch, String [] paths) { 138 this(ch, paths, new ExtensibleLookup()); 139 } 140 141 157 public ExtensibleNode(Children ch, Lookup l, String path, boolean useHierarchicalPath) { 158 this(ch, l, useHierarchicalPath ? 159 computeHierarchicalPaths(path) : 160 new String [] { path } 161 ); 162 } 163 164 177 public ExtensibleNode(Children ch, Lookup l, String [] paths) { 178 super(ch, l); 179 this.paths = paths; 180 } 181 182 186 private ExtensibleNode(Children ch, String [] paths, ExtensibleLookup l) { 187 super(ch, l); 188 this.paths = paths; 189 l.setNode(this); 190 } 191 192 199 public Action [] getActions (boolean context) { 200 if (context) { 201 return super.getActions(context); 202 } 203 return getActionManager().getActions(); 204 } 205 206 209 public Image getIcon(int type) { 210 int size = (type == 1 || type == 3) ? 16 : 32; 211 ImageIcon ii = null; 212 if (getIconName() == null) { 213 ii = getIconManager().getDefaultIcon(size); 214 } else { 215 ii = getIconManager().getIcon(getIconName(), size); 216 } 217 return ii.getImage(); 218 } 219 220 223 public Image getOpenedIcon(int type) { 224 return getIcon(type); 225 } 226 227 230 private ExtensibleActions getActionManager() { 231 if (actionManager == null) { 232 actionManager = ExtensibleActions.getInstance(getPaths()); 233 } 234 return actionManager; 235 } 236 237 240 private ExtensibleIcons getIconManager() { 241 if (iconManager == null) { 242 iconManager = ExtensibleIcons.getInstance(getPaths()); 243 iconManager.addPropertyChangeListener(getIconChangeListener()); 244 } 245 return iconManager; 246 } 247 248 255 public final String [] getPaths() { 256 return paths; 257 } 258 259 267 public final void setPaths(String path, boolean useHierarchicalPath) { 268 setPaths(useHierarchicalPath ? 269 computeHierarchicalPaths(path) : 270 new String [] { path }); 271 } 272 273 278 public final void setPaths(String [] paths) { 279 if (iconChangeListener != null) { 280 iconManager.removePropertyChangeListener(iconChangeListener); 281 } 282 283 iconManager = null; 285 actionManager = null; 286 287 Lookup myLookup = getLookup(); 289 if (myLookup instanceof ExtensibleLookup) { 290 ExtensibleLookup el = (ExtensibleLookup)myLookup; 291 el.setNode(this); 292 } 293 294 Object oldValue = this.paths; 296 this.paths = paths; 297 firePropertyChange("paths", oldValue, paths); 298 299 fireIconChange(); 300 fireOpenedIconChange(); 301 } 302 303 308 public final String getIconName() { 309 return iconName; 310 } 311 312 316 public final void setIconName(String name) { 317 Object oldVal = iconName; 318 iconName = name; 319 firePropertyChange("iconName", oldVal, iconName); fireIconChange(); 321 fireOpenedIconChange(); 322 } 323 324 327 private IconChangeListener getIconChangeListener() { 328 if (iconChangeListener == null) { 329 iconChangeListener = new IconChangeListener(); 330 } 331 return iconChangeListener; 332 } 333 334 339 static String [] computeHierarchicalPaths(String path) { 340 if (path == null) { 341 return new String [0]; 342 } 343 String tmp = path; 344 ArrayList list = new ArrayList (); 345 while (tmp.length() > 0) { 346 list.add(tmp); 347 if (tmp.lastIndexOf('/') >= 0) { 348 tmp = tmp.substring(0, tmp.lastIndexOf('/')); 349 } else { 350 tmp = ""; 351 } 352 } 353 list.add(tmp); return (String [])list.toArray(new String [list.size()]); 355 } 356 357 360 private final class IconChangeListener implements PropertyChangeListener { 361 public void propertyChange(PropertyChangeEvent ev) { 362 fireIconChange(); 363 fireOpenedIconChange(); 364 } 365 } 366 } 367 | Popular Tags |