KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.AbstractAction JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.JMenu JavaDoc;
29 import javax.swing.JMenuItem JavaDoc;
30 import javax.swing.JPopupMenu JavaDoc;
31 import javax.swing.JSeparator JavaDoc;
32 import junit.framework.TestCase;
33 import org.openide.util.actions.Presenter;
34
35 /**
36  *
37  * @author mkleint
38  */

39 public class DynaMenuModelTest extends TestCase {
40
41     public DynaMenuModelTest(String JavaDoc testName) {
42         super(testName);
43     }
44
45     /**
46      * Test of loadSubmenu method, of class org.openide.awt.DynaMenuModel.
47      */

48     public void testLoadSubmenu() {
49         System.out.println("loadSubmenu");
50         
51         List JavaDoc cInstances = new ArrayList JavaDoc();
52         cInstances.add(new Act1());
53         cInstances.add(new Act2());
54         JMenu JavaDoc m = new JMenu JavaDoc();
55         DynaMenuModel instance = new DynaMenuModel();
56         
57         instance.loadSubmenu(cInstances, m);
58         Component JavaDoc[] comps = m.getPopupMenu().getComponents();
59         assertEquals("0", ((JMenuItem JavaDoc)comps[0]).getText());
60         assertEquals("1", ((JMenuItem JavaDoc)comps[1]).getText());
61         assertEquals("2", ((JMenuItem JavaDoc)comps[2]).getText());
62         
63     }
64
65     /**
66      * Test of checkSubmenu method, of class org.openide.awt.DynaMenuModel.
67      */

68     public void testCheckSubmenu() {
69         List JavaDoc cInstances = new ArrayList JavaDoc();
70         cInstances.add(new Act1());
71         cInstances.add(new Act2());
72         JMenu JavaDoc m = new JMenu JavaDoc();
73         DynaMenuModel instance = new DynaMenuModel();
74         
75         instance.loadSubmenu(cInstances, m);
76         instance.checkSubmenu(m);
77         
78         Component JavaDoc[] comps = m.getPopupMenu().getComponents();
79         assertEquals("0", ((JMenuItem JavaDoc)comps[0]).getText());
80         assertEquals("1x", ((JMenuItem JavaDoc)comps[1]).getText());
81         assertEquals("2x", ((JMenuItem JavaDoc)comps[2]).getText());
82         
83     }
84     
85     
86     
87     public void testSeparators() {
88         JPopupMenu JavaDoc menu = new JPopupMenu JavaDoc();
89         menu.add(new JMenu JavaDoc());
90         menu.add(new JSeparator JavaDoc());
91         menu.add(new DynaMenuModel.InvisibleMenuItem());
92         menu.add(new JSeparator JavaDoc());
93         menu.add(new JMenuItem JavaDoc());
94         DynaMenuModel.checkSeparators(menu.getComponents(), menu);
95         Component JavaDoc[] menus = menu.getComponents();
96         assertTrue(menus[1].isVisible());
97         assertFalse(menus[3].isVisible());
98         
99         menu = new JPopupMenu JavaDoc();
100         menu.add(new JMenu JavaDoc());
101         menu.add(new JSeparator JavaDoc());
102         menu.add(new DynaMenuModel.InvisibleMenuItem());
103         menu.add(new JMenuItem JavaDoc());
104         menu.add(new DynaMenuModel.InvisibleMenuItem());
105         menu.add(new JSeparator JavaDoc());
106         menu.add(new JSeparator JavaDoc());
107         menu.add(new JMenuItem JavaDoc());
108         DynaMenuModel.checkSeparators(menu.getComponents(), menu);
109         Component JavaDoc[] menus2 = menu.getComponents();
110         assertTrue(menus2[1].isVisible());
111         assertTrue(menus2[5].isVisible());
112         assertFalse(menus2[6].isVisible());
113         
114         
115         
116     }
117     
118     private class Act1 extends AbstractAction JavaDoc implements Presenter.Menu {
119         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
120         }
121         
122         public JMenuItem JavaDoc getMenuPresenter() {
123             return new JMenuItem JavaDoc("0");
124         }
125     }
126     
127     private class Act2 extends AbstractAction JavaDoc implements Presenter.Menu {
128         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
129         }
130         
131         public JMenuItem JavaDoc getMenuPresenter() {
132             return new Dyna();
133         }
134     }
135     
136     private class Dyna extends JMenuItem JavaDoc implements DynamicMenuContent {
137         private JMenuItem JavaDoc itm1;
138         private JMenuItem JavaDoc itm2;
139         public JComponent JavaDoc[] getMenuPresenters() {
140             itm1 = new JMenuItem JavaDoc();
141             itm1.setText("1");
142             itm2 = new JMenuItem JavaDoc();
143             itm2.setText("2");
144             return new JComponent JavaDoc[] {
145                 itm1,
146                 itm2
147             };
148         }
149     
150         public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
151             ((JMenuItem JavaDoc)items[0]).setText("1x");
152             ((JMenuItem JavaDoc)items[1]).setText("2x");
153             return items;
154         }
155     }
156     
157 }
158
Popular Tags