1 19 20 package org.openide.util.actions; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.Arrays ; 25 import java.util.Collections ; 26 import java.util.HashSet ; 27 import junit.framework.Assert; 28 import org.openide.nodes.Node; 29 import org.openide.util.ContextGlobalProvider; 30 import org.openide.util.Lookup; 31 import org.openide.util.Utilities; 32 import org.openide.util.lookup.AbstractLookup; 33 import org.openide.util.lookup.InstanceContent; 34 35 38 public class ActionsInfraHid implements ContextGlobalProvider { 39 40 private static Node[] currentNodes = null; 41 private static final NodeLookup nodeLookup = new NodeLookup(); 42 43 public Lookup createGlobalContext() { 44 return nodeLookup; 45 } 46 47 private static Lookup.Result nodeResult; 48 static { 49 try { 50 nodeResult = Utilities.actionsGlobalContext().lookupResult(Node.class); 51 Assert.assertEquals(Collections.emptySet(), new HashSet (nodeResult.allInstances())); 52 } catch (Throwable t) { 53 t.printStackTrace(); 54 } 55 } 56 57 public static void setCurrentNodes(Node[] nue) { 58 currentNodes = nue; 59 nodeLookup.refresh(); 60 Assert.assertEquals(nue != null ? new HashSet (Arrays.asList(nue)) : Collections.EMPTY_SET, 61 new HashSet (nodeResult.allInstances())); 62 } 63 64 private static final class NodeLookup extends AbstractLookup implements InstanceContent.Convertor { 65 private final InstanceContent content; 66 public NodeLookup() { 67 this(new InstanceContent()); 68 } 69 private NodeLookup(InstanceContent content) { 70 super(content); 71 this.content = content; 72 refresh(); 73 } 74 public void refresh() { 75 if (currentNodes != null) { 77 content.set(Arrays.asList(currentNodes), null); 78 } else { 79 content.set(Collections.singleton(new Object ()), this); 80 } 81 } 82 public Object convert(Object obj) { 83 return null; 84 } 85 public Class type(Object obj) { 86 return Node.class; 87 } 88 public String id(Object obj) { 89 return "none"; } 91 public String displayName(Object obj) { 92 return null; 93 } 94 } 95 96 123 124 126 public static final class WaitPCL implements PropertyChangeListener { 127 128 public int gotit = 0; 129 130 private final String prop; 131 public WaitPCL(String p) { 132 prop = p; 133 } 134 public synchronized void propertyChange(PropertyChangeEvent evt) { 135 if (prop == null || prop.equals(evt.getPropertyName())) { 136 gotit++; 137 notifyAll(); 138 } 139 } 140 public boolean changed() { 141 return changed(1500); 142 } 143 public synchronized boolean changed(int timeout) { 144 if (gotit > 0) { 145 return true; 146 } 147 try { 148 wait(timeout); 149 } catch (InterruptedException ie) { 150 ie.printStackTrace(); 151 } 152 return gotit > 0; 153 } 154 } 155 156 } 157 | Popular Tags |