Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.openide.windows; |
15 |
|
16 |
import java.awt.KeyboardFocusManager; |
17 |
import java.util.ArrayList; |
18 |
import javax.swing.ActionMap; |
19 |
import javax.swing.text.DefaultEditorKit; |
20 |
|
21 |
import junit.framework.*; |
22 |
|
23 |
import org.netbeans.junit.*; |
24 |
import org.openide.explorer.*; |
25 |
import org.openide.nodes.AbstractNode; |
26 |
import org.openide.nodes.Children; |
27 |
import org.openide.nodes.FilterNode; |
28 |
import org.openide.nodes.Node; |
29 |
import org.openide.util.Lookup; |
30 |
import org.openide.util.lookup.AbstractLookup; |
31 |
import org.openide.util.lookup.InstanceContent; |
32 |
|
33 |
|
34 |
|
35 |
/** Tests behaviour of GlobalContextProviderImpl |
36 |
* and its cooperation with activated and current nodes when TopComponent is |
37 |
* using its own lookup as in examples of ExplorerUtils... |
38 |
* |
39 |
* @author Jaroslav Tulach |
40 |
*/ |
41 |
public class TopComponentGetLookupOverridenTest extends TopComponentGetLookupTest { |
42 |
public TopComponentGetLookupOverridenTest (java.lang.String testName) { |
43 |
super(testName); |
44 |
} |
45 |
|
46 |
public static void main(java.lang.String[] args) { |
47 |
junit.textui.TestRunner.run(suite()); |
48 |
} |
49 |
|
50 |
public static Test suite() { |
51 |
TestSuite suite = new NbTestSuite(TopComponentGetLookupOverridenTest.class); |
52 |
|
53 |
return suite; |
54 |
} |
55 |
|
56 |
/** Setup component with lookup. |
57 |
*/ |
58 |
protected void setUp () { |
59 |
top = new ListingYourComponent (); |
60 |
lookup = top.getLookup (); |
61 |
} |
62 |
|
63 |
private static class ListingYourComponent extends YourComponent |
64 |
implements java.beans.PropertyChangeListener { |
65 |
public ListingYourComponent () { |
66 |
addPropertyChangeListener (this); |
67 |
getExplorerManager ().setRootContext (new AbstractNode (new Children.Array ())); |
68 |
} |
69 |
|
70 |
public void propertyChange (java.beans.PropertyChangeEvent ev) { |
71 |
ExplorerManager manager = getExplorerManager (); |
72 |
|
73 |
if ("activatedNodes".equals (ev.getPropertyName())) { |
74 |
try { |
75 |
Node[] arr = getActivatedNodes (); |
76 |
Children.Array ch = (Children.Array)manager.getRootContext ().getChildren (); |
77 |
for (int i = 0; i < arr.length; i++) { |
78 |
if (arr[i].getParentNode() != manager.getRootContext()) { |
79 |
assertTrue ("If this fails we are in troubles", ch.add (new Node[] { arr[i] })); |
80 |
} |
81 |
} |
82 |
manager.setSelectedNodes (getActivatedNodes ()); |
83 |
} catch (java.beans.PropertyVetoException ex) { |
84 |
ex.printStackTrace(); |
85 |
fail (ex.getMessage()); |
86 |
} |
87 |
} |
88 |
} |
89 |
} // end of ListingYourComponent |
90 |
|
91 |
// The following class is copied from example in ExplorerUtils: |
92 |
// |
93 |
public static class YourComponent extends TopComponent |
94 |
implements ExplorerManager.Provider, Lookup.Provider { |
95 |
private ExplorerManager manager; |
96 |
public YourComponent() { |
97 |
this.manager = new ExplorerManager (); |
98 |
ActionMap map = getActionMap (); |
99 |
map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager)); |
100 |
map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager)); |
101 |
map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager)); |
102 |
map.put("delete", ExplorerUtils.actionDelete(manager, true)); // or false |
103 |
|
104 |
associateLookup (ExplorerUtils.createLookup (manager, map)); |
105 |
} |
106 |
public ExplorerManager getExplorerManager() { |
107 |
return manager; |
108 |
} |
109 |
// It is good idea to switch all listeners on and off when the |
110 |
// component is shown or hidden. In the case of TopComponent use: |
111 |
protected void componentActivated() { |
112 |
ExplorerUtils.activateActions(manager, true); |
113 |
} |
114 |
protected void componentDeactivated() { |
115 |
ExplorerUtils.activateActions(manager, false); |
116 |
} |
117 |
} // end of YourComponent |
118 |
} |
119 |
|