KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > actions > ActionsInfraHid


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util.actions;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
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 /** Utilities for actions tests.
36  * @author Jesse Glick
37  */

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 JavaDoc(nodeResult.allInstances()));
52         } catch (Throwable JavaDoc 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 JavaDoc(Arrays.asList(nue)) : Collections.EMPTY_SET,
61                 new HashSet JavaDoc(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             //System.err.println("NL.refresh; currentNodes = " + currentNodes);
76
if (currentNodes != null) {
77                 content.set(Arrays.asList(currentNodes), null);
78             } else {
79                 content.set(Collections.singleton(new Object JavaDoc()), this);
80             }
81         }
82         public Object JavaDoc convert(Object JavaDoc obj) {
83             return null;
84         }
85         public Class JavaDoc type(Object JavaDoc obj) {
86             return Node.class;
87         }
88         public String JavaDoc id(Object JavaDoc obj) {
89             return "none"; // magic, see NodeAction.NodesL.resultChanged
90
}
91         public String JavaDoc displayName(Object JavaDoc obj) {
92             return null;
93         }
94     }
95
96     /*
97     private static final class NodeLookup extends ProxyLookup implements InstanceContent.Convertor {
98         public NodeLookup() {
99             refresh();
100         }
101         public void refresh() {
102             //System.err.println("NL.refresh; currentNodes = " + currentNodes);
103             setLookups(new Lookup[] {
104                 currentNodes != null ?
105                     Lookups.fixed(currentNodes) :
106                     Lookups.fixed(new Object[] {null}, this),
107             });
108         }
109         public Object convert(Object obj) {
110             return null;
111         }
112         public Class type(Object obj) {
113             return Object.class;
114         }
115         public String id(Object obj) {
116             return "none";
117         }
118         public String displayName(Object obj) {
119             return null;
120         }
121     }
122      */

123
124     /** Prop listener that will tell you if it gets a change.
125      */

126     public static final class WaitPCL implements PropertyChangeListener JavaDoc {
127         /** whether a change has been received, and if so count */
128         public int gotit = 0;
129         /** optional property name to filter by (if null, accept any) */
130         private final String JavaDoc prop;
131         public WaitPCL(String JavaDoc p) {
132             prop = p;
133         }
134         public synchronized void propertyChange(PropertyChangeEvent JavaDoc 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 JavaDoc ie) {
150                 ie.printStackTrace();
151             }
152             return gotit > 0;
153         }
154     }
155     
156 }
157
Popular Tags