KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > actions > ActionContainerFactoryTest


1 /*
2  * $Id: ActionContainerFactoryTest.java,v 1.2 2005/01/26 20:01:00 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.actions;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.List JavaDoc;
12
13 import javax.swing.AbstractButton JavaDoc;
14 import javax.swing.Action JavaDoc;
15 import javax.swing.Icon JavaDoc;
16 import javax.swing.JButton JavaDoc;
17 import javax.swing.JCheckBoxMenuItem JavaDoc;
18 import javax.swing.JMenu JavaDoc;
19 import javax.swing.JMenuItem JavaDoc;
20 import javax.swing.JPopupMenu JavaDoc;
21 import javax.swing.JRadioButtonMenuItem JavaDoc;
22 import javax.swing.JSeparator JavaDoc;
23 import javax.swing.JToggleButton JavaDoc;
24 import javax.swing.JToolBar JavaDoc;
25 import javax.swing.KeyStroke JavaDoc;
26
27 import junit.framework.TestCase;
28
29 import org.jdesktop.swing.Application;
30
31 /**
32  * Test the action container factory - which is a factory class which creates
33  * UI components from actions.
34  */

35 public class ActionContainerFactoryTest extends TestCase {
36
37     private static final String JavaDoc FONT_ITALIC = "font-italic";
38     private static final String JavaDoc FONT_BOLD = "font-bold";
39     
40     private static final String JavaDoc PASTE_COMMAND = "paste-command";
41     private static final String JavaDoc COPY_COMMAND = "copy-command";
42     private static final String JavaDoc CUT_COMMAND = "cut-command";
43     
44     private static final String JavaDoc NEW_COMMAND = "new-command";
45     private static final String JavaDoc OPEN_COMMAND = "open-command";
46     
47     private static final String JavaDoc LEFT_COMMAND = "left-command";
48     private static final String JavaDoc CENTER_COMMAND = "center-command";
49     private static final String JavaDoc RIGHT_COMMAND = "right-command";
50     
51     private static final String JavaDoc EDIT_MENU_COMMAND = "edit-menu-command";
52     
53     private ActionContainerFactory factory;
54     private List JavaDoc commandList;
55     private List JavaDoc menuList;
56     private List JavaDoc hierList;
57     
58     protected void setUp() {
59         factory = new ActionContainerFactory(new ActionManager());
60
61         // regular command actions
62
action(NEW_COMMAND, "New", "N", "Create a new document",
63                "/toolbarButtonGraphics/general/New16.gif",
64                "/toolbarButtonGraphics/general/New24.gif", "control N", false);
65
66         action(OPEN_COMMAND, "Open", "O", "Opens a document",
67                 "/toolbarButtonGraphics/general/Open16.gif",
68                 "/toolbarButtonGraphics/general/Open24.gif", "control O", false);
69
70         action(CUT_COMMAND, "Cut", "C", "Cut selection",
71                 "/toolbarButtonGraphics/general/Cut16.gif",
72                 "/toolbarButtonGraphics/general/Cut24.gif", "control X", false);
73
74         action(COPY_COMMAND, "Copy", "V", "Copy selection",
75                 "/toolbarButtonGraphics/general/Copy16.gif",
76                 "/toolbarButtonGraphics/general/Copy24.gif", "control C", false);
77
78         action(PASTE_COMMAND, "Paste", "V", "Paste from clipboard",
79                 "/toolbarButtonGraphics/general/Paste16.gif",
80                 "/toolbarButtonGraphics/general/Paste24.gif", "control V", false);
81         
82         action(EDIT_MENU_COMMAND, "Edit", "E", "Edit operations",
83                     null, null, null, false);
84
85         // toggle actions
86
action(FONT_BOLD, "Bold", "B", "Bold current selection",
87                "/toolbarButtonGraphics/text/Bold16.gif",
88                "/toolbarButtonGraphics/text/Bold24.gif", "control B", true);
89
90         action(FONT_ITALIC, "Italic", "I", "Italic current selection",
91                 "/toolbarButtonGraphics/text/Italic16.gif",
92                 "/toolbarButtonGraphics/text/Italic24.gif", "control I", true);
93
94         // group actions - must be toggle and have a button group
95
String JavaDoc alignGroup = "group-align";
96         
97         action(LEFT_COMMAND, "Left", "L", "", null, null, null, true, alignGroup);
98         action(CENTER_COMMAND, "Center", "C", "", null, null, null, true, alignGroup);
99         action(RIGHT_COMMAND, "Right", "R", "", null, null, null, true, alignGroup);
100         
101         commandList = new ArrayList JavaDoc();
102         commandList.add(NEW_COMMAND);
103         commandList.add(OPEN_COMMAND);
104         commandList.add(null);
105         commandList.add(CUT_COMMAND);
106         commandList.add(COPY_COMMAND);
107         commandList.add(PASTE_COMMAND);
108         commandList.add(null);
109         commandList.add(FONT_BOLD);
110         commandList.add(FONT_ITALIC);
111         
112         menuList = new ArrayList JavaDoc();
113         menuList.add(EDIT_MENU_COMMAND);
114         menuList.add(CUT_COMMAND);
115         menuList.add(COPY_COMMAND);
116         menuList.add(PASTE_COMMAND);
117         
118         hierList = new ArrayList JavaDoc();
119         hierList.add(NEW_COMMAND);
120         hierList.add(OPEN_COMMAND);
121         hierList.add(null);
122         hierList.add(menuList);
123         hierList.add(null);
124         hierList.add(FONT_BOLD);
125         hierList.add(FONT_ITALIC);
126     }
127
128     protected void tearDown() {
129         factory.setActionManager(null);
130         factory = null;
131     }
132
133     /**
134      * Sanity check for the integrity of the data.
135      * NOTE: This test really belongs in ActionManagerTest
136      */

137     public void testAbstractActionExt() {
138         AbstractActionExt action = (AbstractActionExt)factory.getActionManager().getAction(NEW_COMMAND);
139         assertNotNull(action);
140
141         Icon JavaDoc smicon = action.getSmallIcon();
142         Icon JavaDoc lgicon = action.getLargeIcon();
143         assertNotNull(smicon);
144         assertNotNull(lgicon);
145
146         assertNotSame("Error: both icons are the same", smicon, lgicon);
147
148         assertTrue(smicon.toString().endsWith("16.gif"));
149         assertTrue(lgicon.toString().endsWith("24.gif"));
150         
151         assertEquals(KeyStroke.getKeyStroke("control N"), action.getAccelerator());
152     }
153     
154     /**
155      * Test to ensure that the ACF in the AM is the same instance.
156      * NOTE: This two way relationship should go away. See issue 164.
157      */

158     public void testActionManager() {
159         ActionContainerFactory factory = new ActionContainerFactory(new ActionManager());
160         ActionManager manager = factory.getActionManager();
161         
162         assertEquals(factory, manager.getFactory());
163     }
164
165     /**
166      * Test the contruction of JButton, JToggleButton, JMenuItem and
167      * JCheckBoxMenuItem
168      * TODO: Should test the construction of JRadioButtonMenuItem
169      */

170     public void testCreateButton() {
171         AbstractButton JavaDoc button;
172         JMenuItem JavaDoc menuItem;
173         Action JavaDoc action;
174
175         action = factory.getActionManager().getAction(NEW_COMMAND);
176         button = factory.createButton(action);
177         assertNotNull(button);
178         assertTrue(button instanceof JButton JavaDoc);
179         assertEquals(button.getMnemonic(), 'N');
180         assertEquals(button.getToolTipText(), action.getValue(Action.SHORT_DESCRIPTION));
181         // Buttons with large icons will not display text
182
assertEquals(button.getText(), "");
183         // large icons for buttons
184
assertEquals(action.getValue(AbstractActionExt.LARGE_ICON), button.getIcon());
185         
186         menuItem = factory.createMenuItem(action);
187         assertNotNull(menuItem);
188         assertEquals(menuItem.getMnemonic(), 'N');
189         assertEquals(menuItem.getToolTipText(), action.getValue(Action.SHORT_DESCRIPTION));
190         assertEquals(menuItem.getAccelerator(), KeyStroke.getKeyStroke("control N"));
191         assertEquals(menuItem.getText(), action.getValue(Action.NAME));
192         // small icons for menu items
193
assertEquals(action.getValue(Action.SMALL_ICON), menuItem.getIcon());
194
195         action = factory.getActionManager().getAction(FONT_BOLD);
196         button = factory.createButton(action);
197         assertNotNull(button);
198         assertTrue(button instanceof JToggleButton JavaDoc);
199
200         menuItem = factory.createMenuItem(action);
201         assertNotNull(menuItem);
202         assertTrue(menuItem instanceof JCheckBoxMenuItem JavaDoc);
203         assertEquals(menuItem.getAccelerator(), KeyStroke.getKeyStroke("control B"));
204     }
205     
206     /**
207      * Test to ensure that the same list produces the same action layout.
208      * This also does a basic test to ensure that the popup and toolbar contruction
209      * methods create the expected components.
210      */

211     public void testSharedList() {
212         // These two items should have the same layout.
213
JPopupMenu JavaDoc popup = factory.createPopup(commandList);
214         JToolBar JavaDoc toolbar = factory.createToolBar(commandList);
215         
216         int listSize = commandList.size();
217
218         assertTrue(popup.getComponentCount() == listSize);
219         assertTrue(toolbar.getComponentCount() == listSize);
220
221         // The first item should be a command
222
assertTrue(toolbar.getComponent(0) instanceof JButton JavaDoc);
223         assertTrue(popup.getComponent(0) instanceof JMenuItem JavaDoc);
224         assertFalse(popup.getComponent(0) instanceof JCheckBoxMenuItem JavaDoc);
225         assertFalse(popup.getComponent(0) instanceof JRadioButtonMenuItem JavaDoc);
226         
227         // The 3rd item should be a separator
228
assertTrue(toolbar.getComponent(2) instanceof JSeparator JavaDoc);
229         assertTrue(popup.getComponent(2) instanceof JSeparator JavaDoc);
230         
231         // The last items should be toggle
232
assertTrue(toolbar.getComponent(listSize - 1) instanceof JToggleButton JavaDoc);
233         assertTrue(popup.getComponent(listSize - 1) instanceof JCheckBoxMenuItem JavaDoc);
234
235         // disable the first action. Both actions should be disabled.
236
ActionManager manager = factory.getActionManager();
237         manager.setEnabled(NEW_COMMAND, false);
238
239         assertTrue(toolbar.getComponent(0).isEnabled() == false);
240         assertTrue(popup.getComponent(0).isEnabled() == false);
241     }
242     
243     public void testCreateMenu() {
244         JMenu JavaDoc menu = factory.createMenu(menuList);
245         
246         assertNotNull(menu);
247         assertTrue(menu.getMenuComponentCount() == (menuList.size() - 1));
248         
249         // the menu will be the nonsensical "new-command"
250
menu = factory.createMenu(commandList);
251
252         assertNotNull(menu);
253         assertTrue(menu.getMenuComponentCount() == (commandList.size() - 1));
254         // first item is a command
255
assertTrue(menu.getMenuComponent(0) instanceof JMenuItem JavaDoc);
256         assertFalse(menu.getMenuComponent(0) instanceof JCheckBoxMenuItem JavaDoc);
257         assertFalse(menu.getMenuComponent(0) instanceof JRadioButtonMenuItem JavaDoc);
258         // second item is a separator
259
assertTrue(menu.getMenuComponent(1) instanceof JSeparator JavaDoc);
260         // last item is a toggle
261
assertTrue(menu.getMenuComponent(commandList.size() - 2) instanceof JCheckBoxMenuItem JavaDoc);
262         
263     }
264
265     public void testGroupActions() {
266         List JavaDoc list = new ArrayList JavaDoc();
267         list.add(LEFT_COMMAND);
268         list.add(CENTER_COMMAND);
269         list.add(RIGHT_COMMAND);
270
271         // These two items should have the same layout.
272
JPopupMenu JavaDoc popup = factory.createPopup(list);
273         JToolBar JavaDoc toolbar = factory.createToolBar(list);
274
275         assertTrue(popup.getComponentCount() == list.size());
276         assertTrue(toolbar.getComponentCount() == list.size());
277
278         // all items should be a toggle
279
for (int i = 0; i < list.size(); i++) {
280             assertTrue(toolbar.getComponent(i) instanceof JToggleButton JavaDoc);
281             assertTrue(popup.getComponent(i) instanceof JRadioButtonMenuItem JavaDoc);
282         }
283
284         // one action should be selected and the others will not
285
JToggleButton JavaDoc toggle;
286         JRadioButtonMenuItem JavaDoc radio;
287
288         factory.getActionManager().setSelected(LEFT_COMMAND, true);
289         for (int i = 0; i < 3; i++) {
290             toggle = (JToggleButton JavaDoc)toolbar.getComponent(i);
291             radio = (JRadioButtonMenuItem JavaDoc)popup.getComponent(i);
292             if (i == 0) {
293                 assertTrue(toggle.isSelected());
294                 assertTrue(radio.isSelected());
295             } else {
296                 assertFalse(toggle.isSelected());
297                 assertFalse(radio.isSelected());
298             }
299         }
300         factory.getActionManager().setSelected(CENTER_COMMAND, true);
301         for (int i = 0; i < 3; i++) {
302             toggle = (JToggleButton JavaDoc)toolbar.getComponent(i);
303             radio = (JRadioButtonMenuItem JavaDoc)popup.getComponent(i);
304             if (i == 1) {
305                 assertTrue(toggle.isSelected());
306                 assertTrue(radio.isSelected());
307             } else {
308                 assertFalse(toggle.isSelected());
309                 assertFalse(radio.isSelected());
310             }
311         }
312         factory.getActionManager().setSelected(RIGHT_COMMAND, true);
313         for (int i = 0; i < 3; i++) {
314             toggle = (JToggleButton JavaDoc)toolbar.getComponent(i);
315             radio = (JRadioButtonMenuItem JavaDoc)popup.getComponent(i);
316             if (i == 2) {
317                 assertTrue(toggle.isSelected());
318                 assertTrue(radio.isSelected());
319             } else {
320                 assertFalse(toggle.isSelected());
321                 assertFalse(radio.isSelected());
322             }
323         }
324     }
325
326     /**
327      * Tests the contruction of hierarchical menus (including popups) from a
328      * List which contains action ids, null and List elements.
329      * Test case for Issue 161
330      */

331     public void testHierarchyList() {
332         JMenu JavaDoc menu = factory.createMenu(hierList);
333         assertNotNull(menu);
334         
335         assertTrue(menu.getMenuComponentCount() == (hierList.size() - 1));
336         JMenu JavaDoc subMenu = (JMenu JavaDoc)menu.getMenuComponent(2);
337         assertTrue(subMenu.getMenuComponentCount() == (menuList.size() - 1));
338         
339         JPopupMenu JavaDoc popup = factory.createPopup(hierList);
340         assertNotNull(popup);
341         assertTrue(popup.getComponentCount() == hierList.size());
342         
343         subMenu = (JMenu JavaDoc)popup.getComponent(3);
344         assertTrue(subMenu.getMenuComponentCount() == (menuList.size() - 1));
345     }
346
347     /**
348      * Convenience method to create actions with all the attributes. A factory
349      * method for other factory methods.
350      */

351     private AbstractActionExt action(String JavaDoc id, String JavaDoc name, String JavaDoc mnemonic, String JavaDoc desc,
352                           String JavaDoc smicon, String JavaDoc lgicon, String JavaDoc accel, boolean toggle) {
353         return action(id, name, mnemonic, desc, smicon, lgicon, accel, toggle, null);
354     }
355
356         /**
357      * Convenience method to create actions with all the attributes. A factory
358      * method for other factory methods.
359      */

360     private AbstractActionExt action(String JavaDoc id, String JavaDoc name, String JavaDoc mnemonic, String JavaDoc desc,
361                           String JavaDoc smicon, String JavaDoc lgicon, String JavaDoc accel, boolean toggle, String JavaDoc group) {
362         AbstractActionExt action;
363         action = ActionFactory.createTargetableAction(id, name, mnemonic, toggle, group);
364
365         ActionFactory.decorateAction(action, desc, desc,
366                                      Application.getIcon(smicon, this),
367                                      Application.getIcon(lgicon, this),
368                                      KeyStroke.getKeyStroke(accel));
369
370         return (AbstractActionExt)factory.getActionManager().addAction(action);
371     }
372
373 }
374
Popular Tags