1 19 20 package org.netbeans.core.multiview; 21 22 import javax.swing.Action ; 23 import org.openide.util.Lookup; 24 import org.openide.util.WeakListeners; 25 import java.beans.*; 26 import java.util.*; 27 import javax.swing.ActionMap ; 28 29 import org.openide.nodes.*; 30 import org.openide.util.LookupEvent; 31 import org.openide.util.LookupListener; 32 import org.openide.util.lookup.AbstractLookup; 33 import org.openide.util.lookup.Lookups; 34 import org.openide.util.lookup.ProxyLookup; 35 36 class MultiViewTopComponentLookup extends Lookup { 37 38 private MyProxyLookup proxy; 39 private InitialProxyLookup initial; 40 41 public MultiViewTopComponentLookup(ActionMap initialObject) { 42 super(); 43 initial = new InitialProxyLookup(initialObject); 45 proxy = new MyProxyLookup(initial); 46 } 47 48 49 public void setElementLookup(Lookup look) { 50 proxy.setElementLookup(look); 51 initial.refreshLookup(); 52 } 53 54 public Lookup.Item lookupItem(Lookup.Template template) { 55 Lookup.Item retValue; 56 if (template.getType() == ActionMap .class || (template.getId() != null && template.getId().equals("javax.swing.ActionMap"))) { 57 return initial.lookupItem(template); 58 } 59 retValue = super.lookupItem(template); 61 return retValue; 62 } 63 64 65 public Object lookup(Class clazz) { 66 if (clazz == ActionMap .class) { 67 return initial.lookup(clazz); 68 } 69 Object retValue; 70 71 retValue = proxy.lookup(clazz); 72 return retValue; 73 } 74 75 public Lookup.Result lookup(Lookup.Template template) { 76 77 if (template.getType() == ActionMap .class || (template.getId() != null && template.getId().equals("javax.swing.ActionMap"))) { 78 return initial.lookup(template); 79 } 80 Lookup.Result retValue; 81 retValue = proxy.lookup(template); 82 retValue = new ExclusionResult(retValue); 83 return retValue; 84 } 85 86 89 private static final class ExclusionResult extends Lookup.Result implements LookupListener { 90 91 private final Lookup.Result delegate; 92 private final List listeners = new ArrayList(); private Collection lastResults; 94 95 public ExclusionResult(Lookup.Result delegate) { 96 this.delegate = delegate; 97 } 98 99 public Collection allInstances() { 100 Set s = new HashSet(delegate.allInstances()); 102 return s; 103 } 104 105 public Set allClasses() { 106 return delegate.allClasses(); } 108 109 public Collection allItems() { 110 Set s = new HashSet(delegate.allItems()); 112 Iterator it = s.iterator(); 113 Set instances = new HashSet(); 114 while (it.hasNext()) { 115 Lookup.Item i = (Lookup.Item)it.next(); 116 if (instances.contains(i.getInstance())) { 117 it.remove(); 118 } else { 119 instances.add(i.getInstance()); 120 } 121 } 122 return s; 123 } 124 125 public void addLookupListener(LookupListener l) { 126 synchronized (listeners) { 127 if (listeners.isEmpty()) { 128 if (lastResults == null) { 129 lastResults = allInstances(); 130 } 131 delegate.addLookupListener(this); 132 } 133 listeners.add(l); 134 } 135 } 136 137 public void removeLookupListener(LookupListener l) { 138 synchronized (listeners) { 139 listeners.remove(l); 140 if (listeners.isEmpty()) { 141 delegate.removeLookupListener(this); 142 lastResults = null; 143 } 144 } 145 } 146 147 public void resultChanged(LookupEvent ev) { 148 synchronized (listeners) { 149 Collection current = allInstances(); 150 boolean equal = lastResults != null && current != null && current.containsAll(lastResults) && lastResults.containsAll(current); 151 if (equal) { 152 return ; 154 } 155 lastResults = current; 156 } 157 158 LookupEvent ev2 = new LookupEvent(this); 159 LookupListener[] ls; 160 synchronized (listeners) { 161 ls = (LookupListener[])listeners.toArray(new LookupListener[listeners.size()]); 162 } 163 for (int i = 0; i < ls.length; i++) { 164 ls[i].resultChanged(ev2); 165 } 166 } 167 168 } 169 170 private static class MyProxyLookup extends ProxyLookup { 171 private Lookup initialLookup; 172 public MyProxyLookup(Lookup initial) { 173 super(new Lookup[] {initial}); 174 initialLookup = initial; 175 } 176 177 public void setElementLookup(Lookup look) { 178 setLookups(new Lookup[] {initialLookup, look}); 179 } 180 } 181 182 static class InitialProxyLookup extends ProxyLookup { 183 private ActionMap initObject; 184 public InitialProxyLookup(ActionMap obj) { 185 super(new Lookup[] {Lookups.fixed(new Object [] {new LookupProxyActionMap(obj)})}); 186 initObject = obj; 187 } 188 189 public void refreshLookup() { 190 setLookups(new Lookup[] {Lookups.fixed(new Object [] {new LookupProxyActionMap(initObject)})}); 191 } 192 193 } 194 195 199 static class LookupProxyActionMap extends ActionMap { 200 private ActionMap map; 201 public LookupProxyActionMap(ActionMap original) { 202 map = original; 203 } 204 205 public void setParent(ActionMap map) { 206 this.map.setParent(map); 207 } 208 209 210 public ActionMap getParent() { 211 return map.getParent(); 212 } 213 214 public void put(Object key, Action action) { 215 map.put(key, action); 216 } 217 218 public Action get(Object key) { 219 return map.get(key); 220 } 221 222 public void remove(Object key) { 223 map.remove(key); 224 } 225 226 public void clear() { 227 map.clear(); 228 } 229 230 public Object [] keys() { 231 return map.keys(); 232 } 233 234 public int size() { 235 return map.size(); 236 } 237 238 public Object [] allKeys() { 239 return map.allKeys(); 240 } 241 242 } 243 } 244 | Popular Tags |