1 19 package org.openide.util.lookup; 20 21 import org.openide.util.Lookup; 22 23 import java.io.*; 24 25 import java.lang.ref.WeakReference ; 26 27 import java.util.*; 28 import org.openide.util.lookup.AbstractLookup.Pair; 29 30 31 34 final class DelegatingStorage<Transaction> extends Object 35 implements Serializable, AbstractLookup.Storage<Transaction> { 36 37 private AbstractLookup.Storage<Transaction> delegate; 38 39 40 private Thread owner; 41 42 public DelegatingStorage(AbstractLookup.Storage<Transaction> d) { 43 this.delegate = d; 44 this.owner = Thread.currentThread(); 45 } 46 47 48 public Object writeReplace() { 49 return this.delegate; 50 } 51 52 54 public void checkForTreeModification() { 55 if (Thread.currentThread() == owner) { 56 throw new AbstractLookup.ISE("You are trying to modify lookup from lookup query!"); } 58 } 59 60 62 public static boolean isSimple(AbstractLookup.Storage s) { 63 if (s instanceof DelegatingStorage) { 64 return ((DelegatingStorage) s).delegate instanceof ArrayStorage; 65 } else { 66 return s instanceof ArrayStorage; 67 } 68 } 69 70 72 public AbstractLookup.Storage<Transaction> exitDelegate() { 73 if (Thread.currentThread() != owner) { 74 throw new IllegalStateException ("Onwer: " + owner + " caller: " + Thread.currentThread()); } 76 77 AbstractLookup.Storage<Transaction> d = delegate; 78 delegate = null; 79 80 return d; 81 } 82 83 public boolean add(AbstractLookup.Pair<?> item, Transaction transaction) { 84 return delegate.add(item, transaction); 85 } 86 87 public void remove(org.openide.util.lookup.AbstractLookup.Pair item, Transaction transaction) { 88 delegate.remove(item, transaction); 89 } 90 91 public void retainAll(Map retain, Transaction transaction) { 92 delegate.retainAll(retain, transaction); 93 } 94 95 100 @SuppressWarnings ("unchecked") 101 private void changeDelegate(InheritanceTree st) { 102 delegate = (AbstractLookup.Storage<Transaction>)st; 103 } 104 105 public Transaction beginTransaction(int ensure) { 106 try { 107 return delegate.beginTransaction(ensure); 108 } catch (UnsupportedOperationException ex) { 109 ArrayStorage arr = (ArrayStorage) delegate; 111 InheritanceTree inh = new InheritanceTree(); 112 changeDelegate(inh); 113 114 Enumeration<Pair<Object >> en = arr.lookup(Object .class); 118 119 while (en.hasMoreElements()) { 120 if (!inh.add(en.nextElement(), new ArrayList<Class >())) { 121 throw new IllegalStateException ("All objects have to be accepted"); } 123 } 124 125 AbstractLookup.ReferenceToResult<?> ref = arr.cleanUpResult(null); 129 130 if (ref != null) { 131 ref.cloneList(inh); 132 } 133 134 return delegate.beginTransaction(ensure); 136 } 137 } 138 139 public org.openide.util.lookup.AbstractLookup.ReferenceToResult cleanUpResult( 140 org.openide.util.Lookup.Template templ 141 ) { 142 return delegate.cleanUpResult(templ); 143 } 144 145 public void endTransaction(Transaction transaction, Set<AbstractLookup.R> modified) { 146 delegate.endTransaction(transaction, modified); 147 } 148 149 public <T> Enumeration<Pair<T>> lookup(Class <T> clazz) { 150 return delegate.lookup(clazz); 151 } 152 153 public org.openide.util.lookup.AbstractLookup.ReferenceToResult registerReferenceToResult( 154 org.openide.util.lookup.AbstractLookup.ReferenceToResult newRef 155 ) { 156 return delegate.registerReferenceToResult(newRef); 157 } 158 } 159 | Popular Tags |