1 19 package org.openide.nodes; 20 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.concurrent.ConcurrentHashMap ; 26 import org.openide.util.lookup.AbstractLookup; 27 import org.openide.util.lookup.AbstractLookup.Pair; 28 29 import java.lang.ref.WeakReference ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import org.openide.util.Lookup; 35 import org.openide.util.lookup.InstanceContent; 36 37 38 40 final class CookieSetLkp extends AbstractLookup { 41 private final CookieSet.Before before; 42 43 public CookieSetLkp(CookieSet.Before b) { 44 this.before = b; 45 } 46 47 public void add(Object obj) { 48 addPair(new SimpleItem<Object >(obj)); 49 } 50 public final <T,R> void add(T inst, InstanceContent.Convertor<T,R> conv) { 51 addPair(new ConvertingItem<T,R>(inst, conv)); 52 } 53 54 public void remove(Object obj) { 55 removePair(new SimpleItem<Object >(obj)); 56 } 57 public final <T,R> void remove(T inst, InstanceContent.Convertor<T,R> conv) { 58 removePair(new ConvertingItem<T,R>(inst, conv)); 59 } 60 61 void superRemovePair(Pair pair) { 62 removePair(pair); 63 } 64 65 private ThreadLocal <Object > isInReplaceInst = new ThreadLocal <Object >(); 66 <T> void replaceInstances(Class <? extends T> clazz, T[] instances, CookieSet set) { 67 Iterator <? extends Lookup.Item> it; 68 Set <Lookup.Item> toRemove; 69 List <AbstractLookup.Pair> pairs; 70 71 Object prev = isInReplaceInst.get(); 72 try { 73 isInReplaceInst.set(this); 74 75 it = lookupResult(Object .class).allItems().iterator(); 76 toRemove = new HashSet <Lookup.Item>(lookupResult(clazz).allItems()); 77 pairs = new ArrayList <AbstractLookup.Pair>(); 78 79 boolean change = false; 80 int index = 0; 81 while (it.hasNext()) { 82 Lookup.Item item = it.next(); 83 assert item instanceof AbstractLookup.Pair; 84 85 if (toRemove.remove(item)) { 86 if (index < instances.length) { 87 if (item instanceof SimpleItem) { 88 SimpleItem<?> simple = (SimpleItem<?>)item; 89 if (simple.obj == instances[index]) { 90 index++; 91 pairs.add(simple); 92 continue; 93 } 94 } 95 96 change = true; 97 pairs.add(new SimpleItem<T>(instances[index++])); 98 } else { 99 change = true; 100 } 101 } else { 102 pairs.add((AbstractLookup.Pair)item); 103 } 104 } 105 assert toRemove.isEmpty(); 106 107 while (index < instances.length) { 108 change = true; 109 pairs.add(new SimpleItem<T>(instances[index++])); 110 } 111 112 if (change) { 113 setPairs(pairs); 114 set.fireChangeEvent(); 115 } 116 } finally { 117 isInReplaceInst.set(prev); 118 } 119 } 120 121 protected void beforeLookup(Lookup.Template<?> template) { 122 beforeLookupImpl(template.getType()); 123 } 124 125 final void beforeLookupImpl(Class <?> clazz) { 126 if (before != null && isInReplaceInst.get() == null) { 127 before.beforeLookup(clazz); 128 } 129 } 130 131 133 final static class SimpleItem<T> extends Pair<T> { 134 private T obj; 135 136 139 public SimpleItem(T obj) { 140 if (obj == null) { 141 throw new NullPointerException (); 142 } 143 this.obj = obj; 144 } 145 146 149 public boolean instanceOf(Class <?> c) { 150 return c.isInstance(obj); 151 } 152 153 158 public T getInstance() { 159 return obj; 160 } 161 162 public boolean equals(Object o) { 163 if (o instanceof SimpleItem) { 164 return obj.equals(((SimpleItem) o).obj); 165 } else { 166 return false; 167 } 168 } 169 170 public int hashCode() { 171 return obj.hashCode(); 172 } 173 174 178 public String getId() { 179 return "IL[" + obj.toString(); } 181 182 184 public String getDisplayName() { 185 return obj.toString(); 186 } 187 188 195 protected boolean creatorOf(Object obj) { 196 return obj == this.obj; 197 } 198 199 202 @SuppressWarnings ("unchecked") 203 public Class <? extends T> getType() { 204 return (Class <? extends T>)obj.getClass(); 205 } 206 } 208 210 final static class ConvertingItem<T,R> extends Pair<R> { 211 212 private T obj; 213 214 215 private WeakReference <R> ref; 216 217 218 private InstanceContent.Convertor<? super T,R> conv; 219 220 224 public ConvertingItem(T obj, InstanceContent.Convertor<? super T,R> conv) { 225 this.obj = obj; 226 this.conv = conv; 227 } 228 229 232 public boolean instanceOf(Class <?> c) { 233 return c.isAssignableFrom(getType()); 234 } 235 236 239 private R getConverted() { 240 if (ref == null) { 241 return null; 242 } 243 244 return ref.get(); 245 } 246 247 252 public synchronized R getInstance() { 253 R converted = getConverted(); 254 255 if (converted == null) { 256 converted = conv.convert(obj); 257 ref = new WeakReference <R>(converted); 258 } 259 260 return converted; 261 } 262 263 public boolean equals(Object o) { 264 if (o instanceof ConvertingItem) { 265 return obj.equals(((ConvertingItem) o).obj); 266 } else { 267 return false; 268 } 269 } 270 271 public int hashCode() { 272 return obj.hashCode(); 273 } 274 275 279 public String getId() { 280 return conv.id(obj); 281 } 282 283 285 public String getDisplayName() { 286 return conv.displayName(obj); 287 } 288 289 296 protected boolean creatorOf(Object obj) { 297 if (conv == null) { 298 return obj == this.obj; 299 } else { 300 return obj == getConverted(); 301 } 302 } 303 304 307 @SuppressWarnings ("unchecked") 308 public Class <? extends R> getType() { 309 R converted = getConverted(); 310 311 if (converted == null) { 312 return conv.type(obj); 313 } 314 315 return (Class <? extends R>)converted.getClass(); 316 } 317 } } 319 | Popular Tags |