1 7 8 package org.jdesktop.swing.actions; 9 10 import java.util.ArrayList ; 11 import java.util.List ; 12 13 import javax.swing.AbstractButton ; 14 import javax.swing.Action ; 15 import javax.swing.Icon ; 16 import javax.swing.JButton ; 17 import javax.swing.JCheckBoxMenuItem ; 18 import javax.swing.JMenu ; 19 import javax.swing.JMenuItem ; 20 import javax.swing.JPopupMenu ; 21 import javax.swing.JRadioButtonMenuItem ; 22 import javax.swing.JSeparator ; 23 import javax.swing.JToggleButton ; 24 import javax.swing.JToolBar ; 25 import javax.swing.KeyStroke ; 26 27 import junit.framework.TestCase; 28 29 import org.jdesktop.swing.Application; 30 31 35 public class ActionContainerFactoryTest extends TestCase { 36 37 private static final String FONT_ITALIC = "font-italic"; 38 private static final String FONT_BOLD = "font-bold"; 39 40 private static final String PASTE_COMMAND = "paste-command"; 41 private static final String COPY_COMMAND = "copy-command"; 42 private static final String CUT_COMMAND = "cut-command"; 43 44 private static final String NEW_COMMAND = "new-command"; 45 private static final String OPEN_COMMAND = "open-command"; 46 47 private static final String LEFT_COMMAND = "left-command"; 48 private static final String CENTER_COMMAND = "center-command"; 49 private static final String RIGHT_COMMAND = "right-command"; 50 51 private static final String EDIT_MENU_COMMAND = "edit-menu-command"; 52 53 private ActionContainerFactory factory; 54 private List commandList; 55 private List menuList; 56 private List hierList; 57 58 protected void setUp() { 59 factory = new ActionContainerFactory(new ActionManager()); 60 61 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 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 String 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 (); 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 (); 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 (); 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 137 public void testAbstractActionExt() { 138 AbstractActionExt action = (AbstractActionExt)factory.getActionManager().getAction(NEW_COMMAND); 139 assertNotNull(action); 140 141 Icon smicon = action.getSmallIcon(); 142 Icon 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 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 170 public void testCreateButton() { 171 AbstractButton button; 172 JMenuItem menuItem; 173 Action action; 174 175 action = factory.getActionManager().getAction(NEW_COMMAND); 176 button = factory.createButton(action); 177 assertNotNull(button); 178 assertTrue(button instanceof JButton ); 179 assertEquals(button.getMnemonic(), 'N'); 180 assertEquals(button.getToolTipText(), action.getValue(Action.SHORT_DESCRIPTION)); 181 assertEquals(button.getText(), ""); 183 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 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 ); 199 200 menuItem = factory.createMenuItem(action); 201 assertNotNull(menuItem); 202 assertTrue(menuItem instanceof JCheckBoxMenuItem ); 203 assertEquals(menuItem.getAccelerator(), KeyStroke.getKeyStroke("control B")); 204 } 205 206 211 public void testSharedList() { 212 JPopupMenu popup = factory.createPopup(commandList); 214 JToolBar toolbar = factory.createToolBar(commandList); 215 216 int listSize = commandList.size(); 217 218 assertTrue(popup.getComponentCount() == listSize); 219 assertTrue(toolbar.getComponentCount() == listSize); 220 221 assertTrue(toolbar.getComponent(0) instanceof JButton ); 223 assertTrue(popup.getComponent(0) instanceof JMenuItem ); 224 assertFalse(popup.getComponent(0) instanceof JCheckBoxMenuItem ); 225 assertFalse(popup.getComponent(0) instanceof JRadioButtonMenuItem ); 226 227 assertTrue(toolbar.getComponent(2) instanceof JSeparator ); 229 assertTrue(popup.getComponent(2) instanceof JSeparator ); 230 231 assertTrue(toolbar.getComponent(listSize - 1) instanceof JToggleButton ); 233 assertTrue(popup.getComponent(listSize - 1) instanceof JCheckBoxMenuItem ); 234 235 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 menu = factory.createMenu(menuList); 245 246 assertNotNull(menu); 247 assertTrue(menu.getMenuComponentCount() == (menuList.size() - 1)); 248 249 menu = factory.createMenu(commandList); 251 252 assertNotNull(menu); 253 assertTrue(menu.getMenuComponentCount() == (commandList.size() - 1)); 254 assertTrue(menu.getMenuComponent(0) instanceof JMenuItem ); 256 assertFalse(menu.getMenuComponent(0) instanceof JCheckBoxMenuItem ); 257 assertFalse(menu.getMenuComponent(0) instanceof JRadioButtonMenuItem ); 258 assertTrue(menu.getMenuComponent(1) instanceof JSeparator ); 260 assertTrue(menu.getMenuComponent(commandList.size() - 2) instanceof JCheckBoxMenuItem ); 262 263 } 264 265 public void testGroupActions() { 266 List list = new ArrayList (); 267 list.add(LEFT_COMMAND); 268 list.add(CENTER_COMMAND); 269 list.add(RIGHT_COMMAND); 270 271 JPopupMenu popup = factory.createPopup(list); 273 JToolBar toolbar = factory.createToolBar(list); 274 275 assertTrue(popup.getComponentCount() == list.size()); 276 assertTrue(toolbar.getComponentCount() == list.size()); 277 278 for (int i = 0; i < list.size(); i++) { 280 assertTrue(toolbar.getComponent(i) instanceof JToggleButton ); 281 assertTrue(popup.getComponent(i) instanceof JRadioButtonMenuItem ); 282 } 283 284 JToggleButton toggle; 286 JRadioButtonMenuItem radio; 287 288 factory.getActionManager().setSelected(LEFT_COMMAND, true); 289 for (int i = 0; i < 3; i++) { 290 toggle = (JToggleButton )toolbar.getComponent(i); 291 radio = (JRadioButtonMenuItem )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 )toolbar.getComponent(i); 303 radio = (JRadioButtonMenuItem )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 )toolbar.getComponent(i); 315 radio = (JRadioButtonMenuItem )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 331 public void testHierarchyList() { 332 JMenu menu = factory.createMenu(hierList); 333 assertNotNull(menu); 334 335 assertTrue(menu.getMenuComponentCount() == (hierList.size() - 1)); 336 JMenu subMenu = (JMenu )menu.getMenuComponent(2); 337 assertTrue(subMenu.getMenuComponentCount() == (menuList.size() - 1)); 338 339 JPopupMenu popup = factory.createPopup(hierList); 340 assertNotNull(popup); 341 assertTrue(popup.getComponentCount() == hierList.size()); 342 343 subMenu = (JMenu )popup.getComponent(3); 344 assertTrue(subMenu.getMenuComponentCount() == (menuList.size() - 1)); 345 } 346 347 351 private AbstractActionExt action(String id, String name, String mnemonic, String desc, 352 String smicon, String lgicon, String accel, boolean toggle) { 353 return action(id, name, mnemonic, desc, smicon, lgicon, accel, toggle, null); 354 } 355 356 360 private AbstractActionExt action(String id, String name, String mnemonic, String desc, 361 String smicon, String lgicon, String accel, boolean toggle, String 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 |