KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.ActionEvent JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.util.Collections JavaDoc;
26 import javax.swing.AbstractAction JavaDoc;
27 import javax.swing.ActionMap JavaDoc;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.nodes.Node;
30 import org.openide.util.Lookup;
31 import org.openide.util.lookup.AbstractLookup;
32 import org.openide.util.lookup.InstanceContent;
33
34 /**
35  * Checks behaviour of a bridge that listens on a provided lookup
36  * and initializes activated nodes according to the nodes in the
37  * lookup.
38  *
39  * @author Jaroslav Tulach, Jesse Glick
40  */

41 public final class TopComponentLookupToNodesBridge extends NbTestCase {
42     /** own action map */
43     protected ActionMap JavaDoc map;
44     /** top component we work on */
45     protected TopComponent top;
46     /** instance in the lookup */
47     protected InstanceContent ic;
48     /** its lookup */
49     protected Lookup lookup;
50     
51     public TopComponentLookupToNodesBridge(String JavaDoc testName) {
52         super(testName);
53     }
54     
55     protected boolean runInEQ() {
56         return true;
57     }
58     
59     /** Setup component with lookup.
60      */

61     protected void setUp() {
62         System.setProperty("org.openide.util.Lookup", "-");
63         
64         map = new ActionMap JavaDoc();
65         ic = new InstanceContent();
66         ic.add(map);
67         
68         lookup = new AbstractLookup(ic);
69         top = new TopComponent(lookup);
70     }
71     
72     
73     public void testTheLookupIsReturned() {
74         assertEquals("Lookup provided to TC in constructor is returned", lookup, top.getLookup());
75     }
76     
77     public void testActionMapIsTakenFromTheLookupIfProvided() {
78         Action JavaDoc a1 = new Action JavaDoc();
79         map.put("key", a1);
80         
81         assertEquals("Action map is set", a1, top.getActionMap().get("key"));
82         
83         ActionMap JavaDoc another = new ActionMap JavaDoc();
84         
85         ic.set(Collections.singleton(another), null);
86         assertEquals("And is not changed (right now) if modified in list", a1, top.getActionMap().get("key"));
87     }
88     
89     public void testEmptyLookupGeneratesZeroLengthArray() {
90         assertNotNull("Array is there", top.getActivatedNodes());
91         assertEquals("No nodes", 0, top.getActivatedNodes().length);
92     }
93     
94     public void testNodeIsThereIfInLookup() {
95         class Listener implements PropertyChangeListener JavaDoc {
96             public int cnt;
97             public String JavaDoc name;
98             
99             public void propertyChange(PropertyChangeEvent JavaDoc ev) {
100                 cnt++;
101                 name = ev.getPropertyName();
102             }
103         }
104         
105         Listener JavaDoc l = new Listener JavaDoc();
106         top.addPropertyChangeListener(l);
107         
108         ic.add(Node.EMPTY);
109         
110         assertNotNull("Array exists", top.getActivatedNodes());
111         assertEquals("One node", 1, top.getActivatedNodes().length);
112         assertEquals("The node", Node.EMPTY, top.getActivatedNodes()[0]);
113         assertEquals("One PCE", 1, l.cnt);
114         assertEquals("Name of property", "activatedNodes", l.name);
115         
116         
117         ic.set(Collections.nCopies(2, Node.EMPTY), null);
118         
119         assertEquals("Two nodes", 2, top.getActivatedNodes().length);
120         assertEquals("The same", Node.EMPTY, top.getActivatedNodes()[0]);
121         assertEquals("The same", Node.EMPTY, top.getActivatedNodes()[1]);
122         assertEquals("second PCE change", 2, l.cnt);
123         assertEquals("Name of property", "activatedNodes", l.name);
124         
125     }
126     
127     private static final class Action extends AbstractAction JavaDoc {
128         public void actionPerformed(ActionEvent JavaDoc e) {
129         }
130     }
131 }
132
Popular Tags