KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > 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.explorer;
21
22 import org.openide.util.lookup.AbstractLookup;
23 import org.openide.util.lookup.InstanceContent;
24 import org.openide.windows.TopComponent;
25 import org.openide.nodes.Node;
26 import java.util.Set JavaDoc;
27 import org.openide.util.actions.SystemAction;
28 import java.awt.event.ActionEvent JavaDoc;
29 import javax.swing.Action JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import org.openide.windows.WindowManager;
33 import javax.swing.JFrame JavaDoc;
34 import java.awt.Frame JavaDoc;
35 import org.openide.windows.Workspace;
36 import org.openide.util.NotImplementedException;
37 import java.awt.Image JavaDoc;
38 import java.beans.PropertyChangeSupport JavaDoc;
39 import org.openide.util.Lookup;
40 import java.util.ArrayList JavaDoc;
41
42 /** Utilities for actions tests.
43  * @author Jesse Glick
44  */

45 public abstract class ActionsInfraHid {
46     
47     private ActionsInfraHid() {}
48     
49     public static final UsefulThings UT;
50     static {
51         String JavaDoc tm = System.getProperty("org.openide.TopManager");
52         if (tm != null) throw new IllegalStateException JavaDoc("TopManager was initialized already: " + tm);
53         String JavaDoc lookup = System.getProperty("org.openide.util.Lookup");
54         if (lookup != null && !lookup.equals(UsefulLookup.class.getName())) throw new IllegalStateException JavaDoc("Already had a Lookup installed: " + lookup);
55         System.setProperty("org.openide.util.Lookup", UsefulLookup.class.getName());
56         UT = new UsefulThings();
57         Lookup l = Lookup.getDefault();
58         if (!(l instanceof UsefulLookup)) throw new IllegalStateException JavaDoc(Lookup.getDefault().toString());
59         if (l.lookup(TopComponent.Registry.class) == null) throw new IllegalStateException JavaDoc("no TC.R");
60         //if (l.lookup(WindowManager.class) == null) throw new IllegalStateException("no WindowManager");
61
//if (CallbackSystemAction.getRegistry() == null) throw new IllegalStateException("no TC.R again!");
62
}
63     public static void main(String JavaDoc[] args) {
64         System.err.println("ActionsInfraHid OK.");
65     }
66     
67     /** Lookup which provides a TC.Registry and ActionManager.
68      */

69     public static final class UsefulLookup extends AbstractLookup {
70         public UsefulLookup() {
71             super(getContent());
72         }
73         private static AbstractLookup.Content getContent() {
74             InstanceContent c = new InstanceContent();
75             c.add(UT);
76             c.add(ActionsInfraHid.class.getClassLoader());
77             
78             return c;
79         }
80     }
81     
82     /** An action manager and top component registry.
83      */

84     public static final class UsefulThings implements TopComponent.Registry, org.openide.util.ContextGlobalProvider {
85         // Registry:
86
private TopComponent activated;
87         /** instances to keep */
88         private InstanceContent ic = new InstanceContent ();
89         /** lookup */
90         private Lookup lookup = new AbstractLookup (ic);
91         /** changes */
92         private final PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
93         
94         public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
95             pcs.addPropertyChangeListener(l);
96         }
97         
98         public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
99             pcs.removePropertyChangeListener(l);
100         }
101         
102         private void firePropertyChange(String JavaDoc p, Object JavaDoc o, Object JavaDoc n) {
103             pcs.firePropertyChange(p, o, n);
104         }
105         
106         
107         public TopComponent getActivated() {
108             return activated;
109         }
110         
111         public void setActivated(TopComponent nue) {
112             TopComponent old = activated;
113             activated = nue;
114             firePropertyChange(PROP_ACTIVATED, old, nue);
115             updateLookup ();
116         }
117         
118         private Node[] activatedNodes = new Node[0];
119         private Node[] currentNodes = null;
120         
121         public Node[] getActivatedNodes() {
122             return activatedNodes;
123         }
124         
125         public Node[] getCurrentNodes() {
126             return currentNodes;
127         }
128         
129         public void setCurrentNodes(Node[] nue) {
130             if (nue != null) {
131                 Node[] old = activatedNodes;
132                 activatedNodes = nue;
133                 firePropertyChange(PROP_ACTIVATED_NODES, old, nue);
134             }
135             Node[] old = currentNodes;
136             currentNodes = nue;
137             firePropertyChange(PROP_CURRENT_NODES, old, nue);
138             updateLookup ();
139         }
140         
141         private Set JavaDoc opened = null;
142         
143         public Set JavaDoc getOpened() {
144             return opened;
145         }
146         
147         public void setOpened(Set JavaDoc nue) {
148             Set JavaDoc old = opened;
149             opened = nue;
150             firePropertyChange(PROP_OPENED, old, nue);
151         }
152         
153         private void updateLookup () {
154             ArrayList JavaDoc items = new ArrayList JavaDoc ();
155             if (currentNodes != null) {
156                 for (int i = 0; i < currentNodes.length; i++) {
157                     items.add (new IPair (currentNodes[i]));
158                 }
159             } else {
160                 items.add (IPair.NULL_NODES);
161             }
162             if (activated != null) {
163                 items.add (new IPair (activated.getActionMap ()));
164             }
165             ic.setPairs (items);
166         }
167                 
168         //
169
// ContextGlobalProvider
170
//
171
public Lookup createGlobalContext() {
172             return lookup;
173         }
174     }
175     
176     /** Prop listener that will tell you if it gets a change.
177      */

178     public static final class WaitPCL implements PropertyChangeListener JavaDoc {
179         /** whether a change has been received, and if so count */
180         public int gotit = 0;
181         /** optional property name to filter by (if null, accept any) */
182         private final String JavaDoc prop;
183         public WaitPCL(String JavaDoc p) {
184             prop = p;
185         }
186         public synchronized void propertyChange(PropertyChangeEvent JavaDoc evt) {
187             if (prop == null || prop.equals(evt.getPropertyName())) {
188                 gotit++;
189                 notifyAll();
190             }
191         }
192         public boolean changed() {
193             return changed(1500);
194         }
195         public synchronized boolean changed(int timeout) {
196             if (gotit > 0) {
197                 return true;
198             }
199             try {
200                 wait(timeout);
201             } catch (InterruptedException JavaDoc ie) {
202                 ie.printStackTrace();
203             }
204             return gotit > 0;
205         }
206     }
207
208     // Stolen from RequestProcessorTest.
209
public static void doGC() {
210         doGC(10);
211     }
212     public static void doGC(int count) {
213         ArrayList JavaDoc l = new ArrayList JavaDoc(count);
214         while (count-- > 0) {
215             System.gc();
216             System.runFinalization();
217             l.add(new byte[1000]);
218         }
219     }
220
221     private static final class IPair extends AbstractLookup.Pair {
222         private Object JavaDoc obj;
223         
224         public static final IPair NULL_NODES = new IPair (
225             new org.openide.nodes.AbstractNode (org.openide.nodes.Children.LEAF)
226         );
227         
228         public IPair (Object JavaDoc obj) {
229             this.obj = obj;
230         }
231         
232         protected boolean creatorOf(Object JavaDoc obj) {
233             return this.obj == obj;
234         }
235         
236         public String JavaDoc getDisplayName() {
237             return obj.toString ();
238         }
239         
240         public String JavaDoc getId() {
241             if (this == NULL_NODES) {
242                 return "none"; // NOI18N
243
}
244             return obj.toString ();
245         }
246         
247         public Object JavaDoc getInstance() {
248             if (this == NULL_NODES) {
249                 return null;
250             }
251             return obj;
252         }
253         
254         public Class JavaDoc getType() {
255             return obj.getClass();
256         }
257         
258         protected boolean instanceOf(Class JavaDoc c) {
259             return c.isInstance(obj);
260         }
261         
262     } // end of IPair
263
}
264
Popular Tags