1 19 20 package org.netbeans.api.nodes2looks; 21 22 import java.io.Serializable ; 23 import java.util.ArrayList ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import org.openide.nodes.Node; 30 import org.openide.util.Lookup; 31 import org.netbeans.spi.looks.Look; 32 import org.netbeans.spi.looks.LookSelector; 33 34 39 final class Cache extends Object implements Serializable , Node.Handle { 40 static final long serialVersionUID = 5338245712342L; 41 42 private Node.Handle handle; 43 private Map cache = new HashMap (); 44 45 public Cache() {} 46 47 public Cache (Node.Handle del) { 48 this.handle = del instanceof Cache ? ((Cache)del).handle : del; 49 } 50 51 public Node getNode() throws java.io.IOException { 52 Node n = handle.getNode (); 53 if (n instanceof LookNode) { 54 LookNode ln = (LookNode)n; 55 this.handle = ln.getCache().handle; 56 ln.setCache( this ); 57 } 58 return n; 59 } 60 61 62 public void store (LookNode node) { 63 64 org.netbeans.spi.looks.Look ld = node.getLook(); 65 Object ro = node.getRepresentedObject(); 66 String roID = ld.getName( ro, node.getLookup() ); 67 68 69 if ( roID == null ) { 70 return; 71 } 72 73 String nodePath = getLookNodePath( node.getParentNode() ); 74 75 List items = (List )cache.get( nodePath ); 76 if ( items == null ) { 77 items = new ArrayList (); 78 cache.put( nodePath, items ); 79 } 80 81 String item[] = { ld.getName(), ro.getClass().getName(), roID}; 82 items.add( item ); 83 84 85 } 86 87 public Look find (LookNode parentNode, Object representedObject ) { 88 89 String path = getLookNodePath( parentNode ); 90 91 List items = (List )cache.get( path ); 92 93 if ( items == null ) { 94 return null; 95 } 96 97 99 HashMap ldName2ld = new HashMap (); 100 101 for( Iterator it = items.iterator(); it.hasNext(); ) { 102 String [] item = (String [])it.next(); 103 104 if (!item[1].equals (representedObject.getClass().getName())) { 105 continue; 106 } 107 108 Look look = LookSelector_getLook ( parentNode.getLookSelectorForChildren(), item[0], representedObject ); 109 if (look == null) { 110 continue; 111 } 112 113 if (look != null && item[2].equals (look.getName ( representedObject, Lookup.EMPTY ))) { return look; 115 } 116 } 117 118 return null; 119 } 120 121 122 124 private static Look LookSelector_getLook ( LookSelector ls, String name, Object representedObject ) { 126 127 Enumeration e = ls.getLooks( representedObject ); 128 129 while( e.hasMoreElements() ) { 130 Look l = (Look)e.nextElement(); 131 if ( l.getName().equals( name ) ) { 132 return l; 133 } 134 } 135 136 return null; 137 } 138 139 140 private static String getLookNodePath( Node node ) { 141 StringBuffer buf = new StringBuffer (512); 142 143 for (;;) { 144 if (!(node instanceof LookNode) ) { 145 break; 146 } 147 148 buf.insert (0, node.getName() ); 149 node = node.getParentNode(); 150 } 151 152 return buf.toString(); 153 } 154 155 } 156 | Popular Tags |