KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > awt > UtilitiesActionsTest


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 package org.openide.awt;
20
21 import java.awt.Component JavaDoc;
22 import javax.swing.AbstractAction JavaDoc;
23 import javax.swing.Action JavaDoc;
24 import javax.swing.JComponent JavaDoc;
25 import javax.swing.JMenuItem JavaDoc;
26 import javax.swing.JPopupMenu JavaDoc;
27
28 import junit.framework.*;
29
30 import org.netbeans.junit.*;
31 import org.openide.util.Lookup;
32 import org.openide.util.Utilities;
33 import org.openide.util.actions.Presenter;
34
35 /** Tests of actions related methods in Utilities class.
36  */

37 public class UtilitiesActionsTest extends NbTestCase {
38
39     public UtilitiesActionsTest(java.lang.String JavaDoc testName) {
40         super(testName);
41     }
42     
43     public static void main(java.lang.String JavaDoc[] args) {
44         junit.textui.TestRunner.run(new NbTestSuite(UtilitiesActionsTest.class));
45     }
46     
47     public void testDynamicMenuContent() {
48         Action JavaDoc[] acts = new Action JavaDoc[] {
49             new Act1(),
50             new Act2()
51             
52         };
53         JPopupMenu JavaDoc popup = Utilities.actionsToPopup(acts, Lookup.EMPTY);
54         Component JavaDoc[] comp = popup.getComponents();
55         boolean onepresent = false;
56         boolean twopresent = false;
57         boolean threepresent = false;
58         boolean fourpresent = false;
59         boolean zeropresent = false;
60         for (int i = 0; i < comp.length; i++) {
61             if ("0".equals(((JMenuItem JavaDoc)comp[i]).getText())) {
62                 zeropresent = true;
63             }
64             if ("2".equals(((JMenuItem JavaDoc)comp[i]).getText())) {
65                 twopresent = true;
66             }
67             if ("4".equals(((JMenuItem JavaDoc)comp[i]).getText())) {
68                 fourpresent = true;
69             }
70             if ("3".equals(((JMenuItem JavaDoc)comp[i]).getText())) {
71                 threepresent = true;
72             }
73             if ("1".equals(((JMenuItem JavaDoc)comp[i]).getText())) {
74                 onepresent = true;
75             }
76         }
77         assertTrue(threepresent);
78         assertTrue(fourpresent);
79         assertTrue(onepresent);
80         assertTrue(twopresent);
81         assertTrue(zeropresent);
82     }
83     
84     private class Act1 extends AbstractAction JavaDoc implements Presenter.Popup, Presenter.Menu {
85         public void actionPerformed(java.awt.event.ActionEvent JavaDoc actionEvent) {
86         }
87         
88         public JMenuItem JavaDoc getPopupPresenter() {
89             return new JMenuItem JavaDoc("0");
90         }
91         
92         public JMenuItem JavaDoc getMenuPresenter() {
93             return new JMenuItem JavaDoc("0");
94         }
95     }
96     
97     private class Act2 extends AbstractAction JavaDoc implements Presenter.Menu, Presenter.Popup {
98         public void actionPerformed(java.awt.event.ActionEvent JavaDoc actionEvent) {
99         }
100         public JMenuItem JavaDoc getPopupPresenter() {
101             return new Dyna();
102         }
103         
104         public JMenuItem JavaDoc getMenuPresenter() {
105             return new Dyna();
106         }
107     }
108     
109     private class Dyna extends JMenuItem JavaDoc implements DynamicMenuContent {
110         private JMenuItem JavaDoc itm1;
111         private JMenuItem JavaDoc itm2;
112         public JComponent JavaDoc[] getMenuPresenters() {
113             itm1 = new JMenuItem JavaDoc();
114             itm1.setText("1");
115             itm2 = new Dyna2();
116             itm2.setText("2");
117             return new JComponent JavaDoc[] {
118                 itm1,
119                 itm2
120             };
121         }
122     
123         public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
124             ((JMenuItem JavaDoc)items[0]).setText("1x");
125             ((JMenuItem JavaDoc)items[1]).setText("2x");
126             return items;
127         }
128     }
129     
130     private class Dyna2 extends JMenuItem JavaDoc implements DynamicMenuContent {
131         private JMenuItem JavaDoc itm1;
132         private JMenuItem JavaDoc itm2;
133         public JComponent JavaDoc[] getMenuPresenters() {
134             itm1 = new JMenuItem JavaDoc();
135             itm1.setText("3");
136             itm2 = new JMenuItem JavaDoc();
137             itm2.setText("4");
138             return new JComponent JavaDoc[] {
139                 itm1,
140                 itm2,
141                 this
142             };
143         }
144     
145         public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
146             ((JMenuItem JavaDoc)items[0]).setText("3x");
147             ((JMenuItem JavaDoc)items[1]).setText("4x");
148             return items;
149         }
150     }
151     
152 }
153
Popular Tags