KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > windows > TopComponentGetLookupOverridenTest


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.windows;
21
22 import java.awt.KeyboardFocusManager JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import javax.swing.ActionMap JavaDoc;
27 import javax.swing.text.DefaultEditorKit JavaDoc;
28
29 import junit.framework.*;
30
31 import org.netbeans.junit.*;
32 import org.openide.explorer.*;
33 import org.openide.nodes.AbstractNode;
34 import org.openide.nodes.Children;
35 import org.openide.nodes.FilterNode;
36 import org.openide.nodes.Node;
37 import org.openide.util.Lookup;
38 import org.openide.util.lookup.AbstractLookup;
39 import org.openide.util.lookup.InstanceContent;
40
41
42
43 /** Tests behaviour of GlobalContextProviderImpl
44  * and its cooperation with activated and current nodes when TopComponent is
45  * using its own lookup as in examples of ExplorerUtils...
46  *
47  * @author Jaroslav Tulach
48  */

49 public class TopComponentGetLookupOverridenTest extends TopComponentGetLookupTest {
50     private Logger JavaDoc LOG = Logger.getLogger(TopComponentGetLookupOverridenTest.class + ".TEST-" + getName());
51
52     public TopComponentGetLookupOverridenTest (java.lang.String JavaDoc testName) {
53         super(testName);
54     }
55     
56     /** Setup component with lookup.
57      */

58     protected void setUp () {
59         ListingYourComponent tc = new ListingYourComponent (LOG);
60         top = tc;
61         get = tc.delegate;
62         lookup = tc.delegate.getLookup ();
63     }
64
65
66     private static class ListingYourComponent extends TopComponent
67     implements java.beans.PropertyChangeListener JavaDoc {
68         YourComponent delegate;
69         private Logger JavaDoc LOG;
70
71         public ListingYourComponent (Logger JavaDoc l) {
72             delegate = new YourComponent();
73             LOG = l;
74             
75             addPropertyChangeListener (this);
76             delegate.getExplorerManager ().setRootContext (new AbstractNode (new Children.Array ()));
77             java.lang.ref.SoftReference JavaDoc ref = new java.lang.ref.SoftReference JavaDoc (new Object JavaDoc ());
78             assertGC ("Trying to simulate issue 40842, to GC TopComponent$SynchronizeNodes", ref);
79             
80             delegate.getExplorerManager().addPropertyChangeListener(this);
81         }
82         
83         private ThreadLocal JavaDoc callbacks = new ThreadLocal JavaDoc ();
84         public void propertyChange (java.beans.PropertyChangeEvent JavaDoc ev) {
85             ExplorerManager manager = delegate.getExplorerManager ();
86
87             LOG.info("propertyChange: " + ev.getPropertyName());
88
89             if ("activatedNodes".equals (ev.getPropertyName())) {
90                 if (Boolean.TRUE.equals (callbacks.get ())) {
91                     LOG.info(" it was callback");
92                     return;
93                 }
94                 try {
95                     callbacks.set (Boolean.TRUE);
96                     Node[] arr = getActivatedNodes ();
97
98                     LOG.info("settings ndoes to zero");
99                     // first of all clear the previous values otherwise
100
// we will not test SynchronizeNodes (associateLookup (..., true))
101
setActivatedNodes (ownNode());
102
103
104                     Children.Array ch = (Children.Array)manager.getRootContext ().getChildren ();
105                     for (int i = 0; i < arr.length; i++) {
106                         if (arr[i].getParentNode() != manager.getRootContext()) {
107                             assertTrue ("If this fails we are in troubles", ch.add (new Node[] { arr[i] }));
108                         }
109                     }
110                     LOG.info("em setSelectedNodes: " + Arrays.asList(arr));
111                     manager.setSelectedNodes (arr);
112                     LOG.info("em setSelectedNodes done: " + Arrays.asList(arr));
113                 } catch (java.beans.PropertyVetoException JavaDoc ex) {
114                     ex.printStackTrace();
115                     fail (ex.getMessage());
116                 } finally {
117                     callbacks.set (null);
118                 }
119             }
120
121         }
122
123         public String JavaDoc toString() {
124             return "ListingYourComponent";
125         }
126
127
128         private static Node[] ownNode() {
129             AbstractNode a = new AbstractNode(Children.LEAF);
130             a.setName("ownNode");
131             return new Node[] { a };
132         }
133     } // end of ListingYourComponent
134

135     // The following class is copied from example in ExplorerUtils:
136
//
137
public static class YourComponent extends TopComponent
138     implements ExplorerManager.Provider, Lookup.Provider {
139         private ExplorerManager manager;
140         public YourComponent() {
141             this.manager = new ExplorerManager ();
142             ActionMap JavaDoc map = getActionMap ();
143             map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
144             map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
145             map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
146             map.put("delete", ExplorerUtils.actionDelete(manager, true)); // or false
147

148             associateLookup (ExplorerUtils.createLookup (manager, map));
149         }
150         public ExplorerManager getExplorerManager() {
151             return manager;
152         }
153         // It is good idea to switch all listeners on and off when the
154
// component is shown or hidden. In the case of TopComponent use:
155
protected void componentActivated() {
156             ExplorerUtils.activateActions(manager, true);
157         }
158         protected void componentDeactivated() {
159             ExplorerUtils.activateActions(manager, false);
160         }
161         public String JavaDoc toString() {
162             return "YourComponent";
163         }
164     } // end of YourComponent
165
}
166     
167
Popular Tags