KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > multiview > MultiViewActionMapTest


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
21 package org.netbeans.core.multiview;
22 import java.awt.event.ActionEvent JavaDoc;
23
24 import javax.swing.AbstractAction JavaDoc;
25
26 import javax.swing.Action JavaDoc;
27 import javax.swing.ActionMap JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29
30
31
32
33 import junit.framework.Test;
34 import junit.framework.TestSuite;
35 import junit.textui.TestRunner;
36 import org.netbeans.core.api.multiview.MultiViewHandler;
37 import org.netbeans.core.api.multiview.MultiViews;
38 import org.netbeans.core.spi.multiview.MultiViewDescription;
39 import org.netbeans.core.spi.multiview.MultiViewElement;
40
41 import org.netbeans.core.spi.multiview.MultiViewFactory;
42 import org.netbeans.junit.NbTestCase;
43 import org.netbeans.junit.NbTestSuite;
44
45 import org.openide.util.Lookup;
46 import org.openide.util.LookupEvent;
47
48
49 import org.openide.util.LookupListener;
50 import org.openide.util.Utilities;
51 import org.openide.util.lookup.Lookups;
52
53
54 import org.openide.windows.TopComponent;
55
56
57
58 /**
59  *
60  * @author Milos Kleint
61  */

62 public class MultiViewActionMapTest extends NbTestCase {
63     
64     /** Creates a new instance of SFSTest */
65     public MultiViewActionMapTest(String JavaDoc name) {
66         super (name);
67     }
68     
69     /**
70      * @param args the command line arguments
71      */

72     public static void main(java.lang.String JavaDoc[] args) {
73         TestRunner.run(suite());
74     }
75     
76     public static Test suite() {
77         TestSuite suite = new NbTestSuite(MultiViewActionMapTest.class);
78         
79         return suite;
80     }
81
82     protected boolean runInEQ () {
83         return true;
84     }
85     
86     
87     public void testElementIsTopComponent() throws Exception JavaDoc {
88         MVElemTopComponent elem1 = new MVElemTopComponent();
89         MVElemTopComponent elem2 = new MVElemTopComponent();
90         MVElemTopComponent elem3 = new MVElemTopComponent();
91         doTestActionMap(elem1, elem2, elem3);
92     }
93     
94     public void testElementIsNotTC() throws Exception JavaDoc {
95         MVElem elem1 = new MVElem();
96         MVElem elem2 = new MVElem();
97         MVElem elem3 = new MVElem();
98         doTestActionMap(elem1, elem2, elem3);
99     }
100     
101     private void doTestActionMap(MultiViewElement elem1, MultiViewElement elem2, MultiViewElement elem3) {
102         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
103         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
104         MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
105         
106         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 };
107         TopComponent tc = MultiViewFactory.createMultiView(descs, desc1);
108         // WARNING: as anything else the first element's action map is set only after the tc is opened..
109
tc.open();
110         
111         Action JavaDoc act = new TestAction("MultiViewAction");
112         // add action to the MVTC map
113
tc.getActionMap().put("testkey", act);
114         ActionMap JavaDoc obj = (ActionMap JavaDoc)tc.getLookup().lookup(ActionMap JavaDoc.class);
115         assertNotNull(obj);
116         assertEquals(obj.getClass(), MultiViewTopComponentLookup.LookupProxyActionMap.class);
117         Action JavaDoc res = (Action JavaDoc)obj.get("testkey");
118         assertNotNull(res);
119         assertEquals("MultiViewAction", res.getValue(Action.NAME));
120         // remove action from the MVTC map
121
tc.getActionMap().remove("testkey");
122         obj = (ActionMap JavaDoc)tc.getLookup().lookup(ActionMap JavaDoc.class);
123         res = (Action JavaDoc)obj.get("testkey");
124         assertNull(res);
125         
126         // make sure the action in MVTC has higher priority..
127
JComponent JavaDoc elemtc = elem1.getVisualRepresentation();
128         Action JavaDoc innerAct = new TestAction("InnerAction");
129         elemtc.getActionMap().put("testkey", innerAct);
130         assertNotNull(elemtc.getActionMap().get("testkey"));
131         obj = (ActionMap JavaDoc)tc.getLookup().lookup(ActionMap JavaDoc.class);
132         // check if anything there in elemen'ts actionmap
133
assertNotNull(obj);
134         res = (Action JavaDoc)obj.get("testkey");
135         assertNotNull(res);
136         // put actin to the mvtc actionmap as well..
137
tc.getActionMap().put("testkey", act);
138         assertNotNull(obj);
139         res = (Action JavaDoc)obj.get("testkey");
140         assertNotNull(res);
141         assertEquals("MultiViewAction", res.getValue(Action.NAME));
142         //remove from mvtc's map..
143
tc.getActionMap().remove("testkey");
144         res = (Action JavaDoc)obj.get("testkey");
145         assertNotNull(res);
146         assertEquals("InnerAction", res.getValue(Action.NAME));
147         // now switch to the other element...
148
MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
149         // test related hack, easy establishing a connection from Desc->perspective
150
Accessor.DEFAULT.createPerspective(desc2);
151         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc2));
152         obj = (ActionMap JavaDoc)tc.getLookup().lookup(ActionMap JavaDoc.class);
153         res = (Action JavaDoc)obj.get("testkey");
154         assertNull(res); // is not defined in element2
155
}
156     
157     public void testActionMapChanges() throws Exception JavaDoc {
158         MVElemTopComponent elem1 = new MVElemTopComponent();
159         MVElemTopComponent elem2 = new MVElemTopComponent();
160         MVElem elem3 = new MVElem();
161         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
162         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
163         MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
164         
165         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 };
166         TopComponent tc = MultiViewFactory.createMultiView(descs, desc1);
167         // WARNING: as anything else the first element's action map is set only after the tc is opened..
168
Lookup.Result result = tc.getLookup().lookup(new Lookup.Template(ActionMap JavaDoc.class));
169         LookListener list = new LookListener();
170         list.resetCount();
171         result.addLookupListener(list);
172         result.allItems();
173         
174         tc.open();
175         assertEquals(1, list.getCount());
176         
177         MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
178         // test related hack, easy establishing a connection from Desc->perspective
179
Accessor.DEFAULT.createPerspective(desc2);
180         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc2));
181         assertEquals(2, list.getCount());
182         
183         Accessor.DEFAULT.createPerspective(desc3);
184         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc3));
185         assertEquals(3, list.getCount());
186     }
187     
188     public void testSimplifiedActionMapChanges81117() {
189         MultiViewTopComponentLookup.InitialProxyLookup lookup = new MultiViewTopComponentLookup.InitialProxyLookup(new ActionMap JavaDoc());
190         Lookup.Result res = lookup.lookup(new Lookup.Template(ActionMap JavaDoc.class));
191         LookListener list = new LookListener();
192         list.resetCount();
193         res.addLookupListener(list);
194 // assertEquals(1, res.allInstances().size());
195
assertEquals(0, list.getCount());
196         lookup.refreshLookup();
197         assertEquals(1, list.getCount());
198 // assertEquals(1, res.allInstances().size());
199

200         MultiViewTopComponentLookup lookup2 = new MultiViewTopComponentLookup(new ActionMap JavaDoc());
201         res = lookup2.lookup(new Lookup.Template(ActionMap JavaDoc.class));
202         list = new LookListener();
203         list.resetCount();
204         res.addLookupListener(list);
205 // assertEquals(1, res.allInstances().size());
206
assertEquals(0, list.getCount());
207         lookup2.setElementLookup(Lookups.fixed(new Object JavaDoc[] {new Object JavaDoc()} ));
208         assertEquals(1, list.getCount());
209 // assertEquals(1, res.allInstances().size());
210

211     }
212     
213     
214     public void testActionMapChangesForElementsWithComponentShowingInit() throws Exception JavaDoc {
215         Action JavaDoc act1 = new TestAction("MultiViewAction1");
216         Action JavaDoc act2 = new TestAction("MultiViewAction2");
217         MVElemTopComponent elem1 = new ComponentShowingElement("testAction", act1);
218         MVElemTopComponent elem2 = new ComponentShowingElement("testAction", act2);
219         MVElem elem3 = new MVElem();
220         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
221         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
222         MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
223         
224         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 };
225         TopComponent tc = MultiViewFactory.createMultiView(descs, desc1);
226         Lookup.Result result = tc.getLookup().lookup(new Lookup.Template(ActionMap JavaDoc.class));
227         LookListener2 list = new LookListener2();
228         result.addLookupListener(list);
229         list.setCorrectValues("testAction", act1);
230         // WARNING: as anything else the first element's action map is set only after the tc is opened..
231
tc.open();
232         assertEquals(1, list.getCount());
233         MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
234         // test related hack, easy establishing a connection from Desc->perspective
235
Accessor.DEFAULT.createPerspective(desc2);
236         list.setCorrectValues("testAction", act2);
237         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc2));
238         assertEquals(2, list.getCount());
239         Accessor.DEFAULT.createPerspective(desc3);
240         list.setCorrectValues("testAction", null);
241         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc3));
242         assertEquals(3, list.getCount());
243     }
244     
245     
246     public class ComponentShowingElement extends MVElemTopComponent {
247         private String JavaDoc key;
248         private Action JavaDoc action;
249         
250         public ComponentShowingElement(String JavaDoc actionkey, Action JavaDoc value) {
251             action = value;
252             key = actionkey;
253         }
254         
255         public void componentShowing() {
256             super.componentShowing();
257             getActionMap().put(key, action);
258         }
259         
260     }
261     
262     private class LookListener2 implements LookupListener {
263         private String JavaDoc key;
264         private Action JavaDoc action;
265         int count = 0;
266         private ActionMap JavaDoc lastMap;
267         
268         public void setCorrectValues(String JavaDoc keyValue, Action JavaDoc actionValue) {
269             action = actionValue;
270             key = keyValue;
271         }
272         
273         public int getCount() {
274             return count;
275         }
276         
277         public void resultChanged (LookupEvent ev) {
278             Lookup.Result res = (Lookup.Result)ev.getSource();
279             assertEquals(1, res.allInstances().size());
280             ActionMap JavaDoc map = (ActionMap JavaDoc)res.allInstances().iterator().next();
281             if (lastMap != null) {
282                 // because of CallbackSystemAction.GlobalManager
283
assertNotSame(map, lastMap);
284             }
285             lastMap = map;
286             Action JavaDoc act = map.get(key);
287             assertEquals(action, act);
288             count++;
289         }
290     }
291     
292 // //
293
// // Set of tests for ActionMap and context.. copied from CallbackSystemActionTest
294
// //
295
//
296
// public void testLookupOfStateInActionMap () throws Exception {
297
//
298
// class MyAction extends javax.swing.AbstractAction
299
// implements org.openide.util.actions.ActionPerformer {
300
// int actionPerformed;
301
// int performAction;
302
//
303
// public void actionPerformed (java.awt.event.ActionEvent ev) {
304
// actionPerformed++;
305
// }
306
//
307
// public void performAction (SystemAction a) {
308
// performAction++;
309
// }
310
// }
311
// MyAction action = new MyAction ();
312
//
313
// ActionMap map = new ActionMap ();
314
// CallbackSystemAction system = (CallbackSystemAction)SystemAction.get(SurviveFocusChgCallbackAction.class);
315
// system.setActionPerformer (null);
316
// map.put (system.getActionMapKey(), action);
317
//
318
// javax.swing.Action clone;
319
// clone = system.createContextAwareInstance(org.openide.util.Lookup.EMPTY);
320
//
321
// assertTrue ("Action should not be enabled if no callback provided", !clone.isEnabled());
322
//
323
// system.setActionPerformer (action);
324
// assertTrue ("Is enabled, because it has a performer", clone.isEnabled());
325
// system.setActionPerformer (null);
326
// assertTrue ("Is disabled, because the performer has been unregistered", !clone.isEnabled ());
327
//
328
// //
329
// // test with actionmap
330
// //
331
// action.setEnabled (false);
332
//
333
// org.openide.util.Lookup context = org.openide.util.lookup.Lookups.singleton(map);
334
// clone = system.createContextAwareInstance(context);
335
//
336
// CntListener listener = new CntListener ();
337
// clone.addPropertyChangeListener (listener);
338
//
339
// assertTrue ("Not enabled now", !clone.isEnabled ());
340
// action.setEnabled (true);
341
// assertTrue ("Clone is enabled because the action in ActionMap is", clone.isEnabled ());
342
// listener.assertCnt ("One change expected", 1);
343
//
344
// system.setActionPerformer (action);
345
// clone.actionPerformed(new java.awt.event.ActionEvent (this, 0, ""));
346
// assertEquals ("MyAction.actionPerformed invoked", 1, action.actionPerformed);
347
// assertEquals ("MyAction.performAction is not invoked", 0, action.performAction);
348
//
349
//
350
// action.setEnabled (false);
351
// assertTrue ("Clone is disabled because the action in ActionMap is", !clone.isEnabled ());
352
// listener.assertCnt ("Another change expected", 1);
353
//
354
// clone.actionPerformed(new java.awt.event.ActionEvent (this, 0, ""));
355
// assertEquals ("MyAction.actionPerformed invoked again", 2, action.actionPerformed);
356
// assertEquals ("MyAction.performAction is not invoked, remains 0", 0, action.performAction);
357
//
358
// }
359
//
360
// private static final class CntListener extends Object
361
// implements java.beans.PropertyChangeListener {
362
// private int cnt;
363
//
364
// public void propertyChange(java.beans.PropertyChangeEvent evt) {
365
// cnt++;
366
// }
367
//
368
// public void assertCnt (String msg, int count) {
369
// assertEquals (msg, count, this.cnt);
370
// this.cnt = 0;
371
// }
372
// } // end of CntListener
373

374     public void testActionsGlobalContext() throws Exception JavaDoc {
375         Lookup look = Utilities.actionsGlobalContext();
376         MVElemTopComponent elem1 = new MVElemTopComponent();
377         MVElemTopComponent elem2 = new MVElemTopComponent();
378         MVElemTopComponent elem3 = new MVElemTopComponent();
379         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
380         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
381         MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
382         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 };
383         TopComponent tc = MultiViewFactory.createMultiView(descs, desc1);
384         // WARNING: as anything else the first element's action map is set only after the tc is opened..
385
tc.open();
386         tc.requestActive();
387         
388         ActionMap JavaDoc map = (ActionMap JavaDoc)look.lookup(ActionMap JavaDoc.class);
389         assertNotNull("is null", map);
390         assertEquals("is wrong class=" + map.getClass(), map.getClass(), MultiViewTopComponentLookup.LookupProxyActionMap.class);
391         Action JavaDoc res = map.get("testkey");
392         assertNull(res);
393         Action JavaDoc act = new TestAction("MultiViewAction");
394         // add action to the MVTC map
395
elem1.getVisualRepresentation().getActionMap().put("testkey", act);
396         res = map.get("testkey");
397         assertNotNull(res);
398         
399         // test switching to a different component..
400
TopComponent tc2 = new TopComponent();
401         tc2.open();
402         tc2.requestActive();
403         map = (ActionMap JavaDoc)look.lookup(ActionMap JavaDoc.class);
404         res = map.get("testkey");
405         assertNull(res);
406         
407         // switch back and test a different element..
408
tc.requestActive();
409         map = (ActionMap JavaDoc)look.lookup(ActionMap JavaDoc.class);
410         MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
411         // test related hack, easy establishing a connection from Desc->perspective
412
Accessor.DEFAULT.createPerspective(desc2);
413         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc2));
414         res = map.get("testkey");
415         assertNull(res);
416         // now switch back to the original element and see if the action is stil there..
417
Accessor.DEFAULT.createPerspective(desc1);
418         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc1));
419         res = map.get("testkey");
420         assertNotNull(res);
421         
422     }
423     
424     
425     private class TestAction extends AbstractAction JavaDoc {
426         public TestAction(String JavaDoc name) {
427             super(name);
428         }
429         
430         public void actionPerformed(ActionEvent JavaDoc event) {
431             
432         }
433         
434     }
435     
436     private class LookListener implements LookupListener {
437         int count = 0;
438         
439         public void resetCount() {
440             count = 0;
441         }
442         
443         
444         public int getCount() {
445             return count;
446         }
447         
448         public void resultChanged (LookupEvent ev) {
449             count++;
450         }
451     }
452     
453  }
454
455
Popular Tags