1 19 package org.openide.nodes; 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Enumeration ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 import javax.swing.Action ; 33 import javax.swing.ActionMap ; 34 import javax.swing.JPopupMenu ; 35 import org.openide.util.Enumerations; 36 import org.openide.util.Lookup; 37 import org.openide.util.Utilities; 38 import org.openide.util.WeakListeners; 39 import org.openide.util.actions.SystemAction; 40 import org.openide.util.lookup.Lookups; 41 import org.openide.util.lookup.ProxyLookup; 42 43 47 public final class NodeOp extends Object { 48 49 private static SystemAction[] defaultActions; 50 51 private NodeOp() { 52 } 53 54 58 @Deprecated 59 public static SystemAction[] getDefaultActions() { 60 if (defaultActions == null) { 61 defaultActions = createFromNames(new String [] {"Tools", "Properties"}); } 63 64 return defaultActions; 65 } 66 67 68 @Deprecated 69 public static void setDefaultActions(SystemAction[] def) { 70 throw new SecurityException (); 71 } 72 73 78 public static JPopupMenu findContextMenu(Node[] nodes) { 79 return findContextMenuImpl(nodes, null); 80 } 81 82 88 static JPopupMenu findContextMenuImpl(Node[] nodes, ActionMap actionMap) { 89 Action [] arr = findActions(nodes); 90 91 List <Lookup> allLookups = new ArrayList <Lookup>(); 93 94 for (Node n : nodes) { 95 allLookups.add(n.getLookup()); 96 } 97 98 if (actionMap != null) { 99 allLookups.add(Lookups.singleton(actionMap)); 100 } 101 102 Lookup lookup = new ProxyLookup(allLookups.toArray(new Lookup[allLookups.size()])); 103 104 return Utilities.actionsToPopup(arr, lookup); 105 } 106 107 115 public static Action [] findActions(Node[] nodes) { 116 Map <Action ,Integer > actions = new HashMap <Action ,Integer >(); 117 118 Action [][] actionsByNode = new Action [nodes.length][]; 119 120 for (int n = 0; n < nodes.length; n++) { 122 actionsByNode[n] = nodes[n].getActions(false); 123 124 if (actionsByNode[n] == null) { 125 actionsByNode[n] = defaultActions; 128 } 129 130 Set <Action > counted = new HashSet <Action >(); 132 133 for (Action a : actionsByNode[n]) { 134 if (a != null) { 135 if (counted.contains(a)) { 137 continue; 138 } 139 140 counted.add(a); 141 142 Integer cntInt = actions.get(a); 143 actions.put(a, cntInt == null ? 1 : cntInt + 1); 144 } 145 } 146 } 147 148 if (!actions.isEmpty()) { 150 List <Action > result = new ArrayList <Action >(); 152 Set <Action > counted = new HashSet <Action >(); 153 154 for (Action action : actionsByNode[0]) { 155 156 if (action != null) { 157 if (counted.contains(action)) { 159 continue; 160 } 161 162 counted.add(action); 163 164 Integer cntInt = actions.get(action); 165 166 int cnt = (cntInt == null) ? 0 : cntInt; 167 168 if (cnt == nodes.length) { 169 result.add(action); 170 } 171 } else { 172 result.add(null); 174 } 175 } 176 177 return result.toArray(new Action [result.size()]); 178 } else { 179 return new Action [0]; 181 } 182 } 183 184 189 public static boolean isSon(Node parent, Node son) { 190 return son.getParentNode() == parent; 191 } 192 193 200 public static String [] createPath(Node node, Node parent) { 201 LinkedList <String > ar = new LinkedList <String >(); 202 203 while ((node != null) && (node != parent)) { 204 if (node.getName() == null) { 205 boolean isFilter = false; 206 207 if (node instanceof FilterNode) { 208 isFilter = true; 209 } 210 211 throw new IllegalArgumentException ( 212 "Node:" + node.getClass() +"[" + node.getDisplayName() + "]" +(isFilter ? (" of original:" + ((FilterNode) node).getOriginal().getClass()) : "") +" gets null name!" 216 ); } 218 219 ar.addFirst(node.getName()); 220 node = node.getParentNode(); 221 } 222 223 String [] res = new String [ar.size()]; 224 ar.toArray(res); 225 226 return res; 227 } 228 229 234 public static Node findChild(Node node, String name) { 235 return node.getChildren().findChild(name); 236 } 237 238 247 public static Node findPath(Node start, Enumeration <String > names) 248 throws NodeNotFoundException { 249 int depth = 0; 250 251 while (names.hasMoreElements()) { 252 String name = names.nextElement(); 253 Node next = findChild(start, name); 254 255 if (next == null) { 256 throw new NodeNotFoundException(start, name, depth); 260 } else { 261 start = next; 263 } 264 265 depth++; 267 } 268 269 return start; 270 } 271 272 281 public static Node findPath(Node start, String [] names) 282 throws NodeNotFoundException { 283 return findPath(start, Enumerations.array(names)); 284 } 285 286 290 public static Node findRoot(Node node) { 291 for (;;) { 292 Node parent = node.getParentNode(); 293 294 if (parent == null) { 295 return node; 296 } 297 298 node = parent; 299 } 300 } 301 302 313 public static int[] computePermutation(Node[] arr1, Node[] arr2) 314 throws IllegalArgumentException { 315 if (arr1.length != arr2.length) { 316 int max = Math.max(arr1.length, arr2.length); 317 StringBuffer sb = new StringBuffer (); 318 319 for (int i = 0; i < max; i++) { 320 sb.append(i + " "); 322 if (i < arr1.length) { 323 sb.append(arr1[i].getName()); 324 } else { 325 sb.append("---"); } 327 328 sb.append(" = "); 330 if (i < arr2.length) { 331 sb.append(arr2[i].getName()); 332 } else { 333 sb.append("---"); } 335 336 sb.append('\n'); 337 } 338 339 throw new IllegalArgumentException (sb.toString()); 340 } 341 342 Map <Node,Integer > map = new HashMap <Node,Integer >(); 345 346 for (int i = 0; i < arr2.length; i++) { 347 map.put(arr2[i], i); 348 } 349 350 int[] perm = new int[arr1.length]; 353 int diff = 0; 354 355 for (int i = 0; i < arr1.length; i++) { 356 Integer newPos = map.get(arr1[i]); 358 359 if (newPos == null) { 360 throw new IllegalArgumentException ("Missing permutation index " + i); } 363 364 perm[i] = newPos; 366 367 if (perm[i] != i) { 368 diff++; 369 } 370 } 371 372 return (diff == 0) ? null : perm; 373 } 374 375 381 public static Node.Handle[] toHandles(Node[] nodes) { 382 List <Node.Handle> ll = new LinkedList <Node.Handle>(); 383 384 for (Node n : nodes) { 385 Node.Handle h = n.getHandle(); 386 387 if (h != null) { 388 ll.add(h); 389 } 390 } 391 392 return ll.toArray(new Node.Handle[ll.size()]); 393 } 394 395 400 public static Node[] fromHandles(Node.Handle[] handles) 401 throws IOException { 402 Node[] arr = new Node[handles.length]; 403 404 for (int i = 0; i < handles.length; i++) { 405 arr[i] = handles[i].getNode(); 406 } 407 408 return arr; 409 } 410 411 419 public static NodeListener weakNodeListener(NodeListener l, Object source) { 420 return WeakListeners.create(NodeListener.class, l, source); 421 } 422 423 430 static SystemAction[] createFromNames(String [] arr) { 431 List <SystemAction> ll = new LinkedList <SystemAction>(); 432 433 for (String n : arr) { 434 if (n == null) { 435 ll.add(null); 436 437 continue; 438 } 439 440 String name = "org.openide.actions." + n + "Action"; 442 try { 443 ClassLoader l = Lookup.getDefault().lookup(ClassLoader .class); 444 if (l == null) { 445 l = Thread.currentThread().getContextClassLoader(); 446 } 447 if (l == null) { 448 l = NodeOp.class.getClassLoader(); 449 } 450 Class <? extends SystemAction> c = Class.forName(name, true, l).asSubclass(SystemAction.class); 451 ll.add(SystemAction.get(c)); 452 } catch (ClassNotFoundException ex) { 453 Logger.getAnonymousLogger().log(Level.WARNING, "NodeOp.java: Missing class " + name, ex); 455 } 457 } 458 459 return ll.toArray(new SystemAction[ll.size()]); 460 } 461 462 465 static void exception(Throwable ex) { 466 Logger.getLogger(NodeOp.class.getName()).log(Level.WARNING, null, ex); 467 } 468 469 472 static void warning(Throwable ex) { 473 Logger.getLogger(NodeOp.class.getName()).log(Level.WARNING, null, ex); 474 } 475 } 476 | Popular Tags |