KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > 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.actions;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Set JavaDoc;
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 /** Utilities for actions tests.
38  * @author Jesse Glick
39  */

40 public abstract class ActionsInfraHid {
41     
42     private ActionsInfraHid() {}
43     
44     public static final UsefulThings UT;
45     static {
46         MockServices.setServices(new Class JavaDoc[] {UsefulThings.class});
47         UT = (UsefulThings) Lookup.getDefault().lookup(UsefulThings.class);
48     }
49     
50     /** An action manager and top component registry.
51      */

52     public static final class UsefulThings implements TopComponent.Registry, ContextGlobalProvider {
53         // Registry:
54
private TopComponent activated;
55         /** instances to keep */
56         private InstanceContent ic = new InstanceContent ();
57         /** lookup */
58         private Lookup lookup = new AbstractLookup (ic);
59         /** changes */
60         private final PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
61         
62         public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
63             pcs.addPropertyChangeListener(l);
64         }
65         
66         public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
67             pcs.removePropertyChangeListener(l);
68         }
69         
70         private void firePropertyChange(String JavaDoc p, Object JavaDoc o, Object JavaDoc 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 JavaDoc opened = null;
110         
111         public Set JavaDoc getOpened() {
112             return opened;
113         }
114         
115         public void setOpened(Set JavaDoc nue) {
116             Set JavaDoc old = opened;
117             opened = nue;
118             firePropertyChange(PROP_OPENED, old, nue);
119         }
120         
121         private void updateLookup () {
122             ArrayList JavaDoc items = new ArrayList JavaDoc ();
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         //
137
// ContextGlobalProvider
138
//
139
public Lookup createGlobalContext() {
140             return lookup;
141         }
142     }
143     
144     /** Prop listener that will tell you if it gets a change.
145      */

146     public static final class WaitPCL implements PropertyChangeListener JavaDoc {
147         /** whether a change has been received, and if so count */
148         public int gotit = 0;
149         /** optional property name to filter by (if null, accept any) */
150         private final String JavaDoc prop;
151         public WaitPCL(String JavaDoc p) {
152             prop = p;
153         }
154         public synchronized void propertyChange(PropertyChangeEvent JavaDoc 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 JavaDoc ie) {
170                 ie.printStackTrace();
171             }
172             return gotit > 0;
173         }
174     }
175
176     // Stolen from RequestProcessorTest.
177
public static void doGC() {
178         doGC(10);
179     }
180     public static void doGC(int count) {
181         ArrayList JavaDoc l = new ArrayList JavaDoc(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 JavaDoc obj;
191         
192         public static final IPair NULL_NODES = new IPair(new AbstractNode(Children.LEAF));
193         
194         public IPair (Object JavaDoc obj) {
195             this.obj = obj;
196         }
197         
198         protected boolean creatorOf(Object JavaDoc obj) {
199             return this.obj == obj;
200         }
201         
202         public String JavaDoc getDisplayName() {
203             return obj.toString ();
204         }
205         
206         public String JavaDoc getId() {
207             if (this == NULL_NODES) {
208                 return "none"; // NOI18N
209
}
210             return obj.toString ();
211         }
212         
213         public Object JavaDoc getInstance() {
214             if (this == NULL_NODES) {
215                 return null;
216             }
217             return obj;
218         }
219         
220         public Class JavaDoc getType() {
221             return obj.getClass();
222         }
223         
224         protected boolean instanceOf(Class JavaDoc c) {
225             return c.isInstance(obj);
226         }
227         
228     } // end of IPair
229
}
230
Popular Tags