1 19 20 package org.openide.actions; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.util.ArrayList ; 26 import java.util.Set ; 27 import org.netbeans.junit.MockServices; 28 import org.openide.nodes.AbstractNode; 29 import org.openide.nodes.Children; 30 import org.openide.nodes.Node; 31 import org.openide.util.ContextGlobalProvider; 32 import org.openide.util.Lookup; 33 import org.openide.util.lookup.AbstractLookup; 34 import org.openide.util.lookup.InstanceContent; 35 import org.openide.windows.TopComponent; 36 37 40 public abstract class ActionsInfraHid { 41 42 private ActionsInfraHid() {} 43 44 public static final UsefulThings UT; 45 static { 46 MockServices.setServices(new Class [] {UsefulThings.class}); 47 UT = (UsefulThings) Lookup.getDefault().lookup(UsefulThings.class); 48 } 49 50 52 public static final class UsefulThings implements TopComponent.Registry, ContextGlobalProvider { 53 private TopComponent activated; 55 56 private InstanceContent ic = new InstanceContent (); 57 58 private Lookup lookup = new AbstractLookup (ic); 59 60 private final PropertyChangeSupport pcs = new PropertyChangeSupport (this); 61 62 public void addPropertyChangeListener(PropertyChangeListener l) { 63 pcs.addPropertyChangeListener(l); 64 } 65 66 public void removePropertyChangeListener(PropertyChangeListener l) { 67 pcs.removePropertyChangeListener(l); 68 } 69 70 private void firePropertyChange(String p, Object o, Object n) { 71 pcs.firePropertyChange(p, o, n); 72 } 73 74 75 public TopComponent getActivated() { 76 return activated; 77 } 78 79 public void setActivated(TopComponent nue) { 80 TopComponent old = activated; 81 activated = nue; 82 firePropertyChange(PROP_ACTIVATED, old, nue); 83 updateLookup (); 84 } 85 86 private Node[] activatedNodes = new Node[0]; 87 private Node[] currentNodes = null; 88 89 public Node[] getActivatedNodes() { 90 return activatedNodes; 91 } 92 93 public Node[] getCurrentNodes() { 94 return currentNodes; 95 } 96 97 public void setCurrentNodes(Node[] nue) { 98 if (nue != null) { 99 Node[] old = activatedNodes; 100 activatedNodes = nue; 101 firePropertyChange(PROP_ACTIVATED_NODES, old, nue); 102 } 103 Node[] old = currentNodes; 104 currentNodes = nue; 105 firePropertyChange(PROP_CURRENT_NODES, old, nue); 106 updateLookup (); 107 } 108 109 private Set opened = null; 110 111 public Set getOpened() { 112 return opened; 113 } 114 115 public void setOpened(Set nue) { 116 Set old = opened; 117 opened = nue; 118 firePropertyChange(PROP_OPENED, old, nue); 119 } 120 121 private void updateLookup () { 122 ArrayList items = new ArrayList (); 123 if (currentNodes != null) { 124 for (int i = 0; i < currentNodes.length; i++) { 125 items.add (new IPair (currentNodes[i])); 126 } 127 } else { 128 items.add (IPair.NULL_NODES); 129 } 130 if (activated != null) { 131 items.add (new IPair (activated.getActionMap ())); 132 } 133 ic.setPairs (items); 134 } 135 136 public Lookup createGlobalContext() { 140 return lookup; 141 } 142 } 143 144 146 public static final class WaitPCL implements PropertyChangeListener { 147 148 public int gotit = 0; 149 150 private final String prop; 151 public WaitPCL(String p) { 152 prop = p; 153 } 154 public synchronized void propertyChange(PropertyChangeEvent evt) { 155 if (prop == null || prop.equals(evt.getPropertyName())) { 156 gotit++; 157 notifyAll(); 158 } 159 } 160 public boolean changed() { 161 return changed(1500); 162 } 163 public synchronized boolean changed(int timeout) { 164 if (gotit > 0) { 165 return true; 166 } 167 try { 168 wait(timeout); 169 } catch (InterruptedException ie) { 170 ie.printStackTrace(); 171 } 172 return gotit > 0; 173 } 174 } 175 176 public static void doGC() { 178 doGC(10); 179 } 180 public static void doGC(int count) { 181 ArrayList l = new ArrayList (count); 182 while (count-- > 0) { 183 System.gc(); 184 System.runFinalization(); 185 l.add(new byte[1000]); 186 } 187 } 188 189 private static final class IPair extends AbstractLookup.Pair { 190 private Object obj; 191 192 public static final IPair NULL_NODES = new IPair(new AbstractNode(Children.LEAF)); 193 194 public IPair (Object obj) { 195 this.obj = obj; 196 } 197 198 protected boolean creatorOf(Object obj) { 199 return this.obj == obj; 200 } 201 202 public String getDisplayName() { 203 return obj.toString (); 204 } 205 206 public String getId() { 207 if (this == NULL_NODES) { 208 return "none"; } 210 return obj.toString (); 211 } 212 213 public Object getInstance() { 214 if (this == NULL_NODES) { 215 return null; 216 } 217 return obj; 218 } 219 220 public Class getType() { 221 return obj.getClass(); 222 } 223 224 protected boolean instanceOf(Class c) { 225 return c.isInstance(obj); 226 } 227 228 } } 230 | Popular Tags |