@Override public void setUp() { _manager = new DefaultCommandManager(); }
/** * Calls the <code>updateState</code> method of all registered commands. */ @Override public void updateState() { final int n = getNumCommands(); for (int i = 0; i < n; i++) { getCommandAt(i).updateState(); } }
/** * Gets the tool command associated with the given command-ID or <code>null</code> if a tool command with the given * command-ID has not been registered. */ @Override public ToolCommand getToolCommand(String commandID) { final Command command = getCommand(commandID); if (command instanceof ToolCommand) { return (ToolCommand) command; } return null; }
public void testCreateOneCommand() { assertEquals(0, _manager.getNumCommands()); addCommand("test"); assertEquals(1, _manager.getNumCommands()); final Command command = _manager.getExecCommand("test"); assertNotNull(command); final Command commandAt = _manager.getCommandAt(0); assertSame(command, commandAt); }
@Test public void testAddContextDependingMenuItems() { DefaultCommandManager manager = new DefaultCommandManager(); createCommand("com1", manager); createCommand("com2", manager); createCommand("com3", manager); manager.getCommandAt(0).setProperty(Command.ACTION_KEY_CONTEXT, "image"); manager.getCommandAt(1).setProperty(Command.ACTION_KEY_CONTEXT, "notImage"); manager.getCommandAt(2).setProperty(Command.ACTION_KEY_CONTEXT, "image"); manager.getCommandAt(2).setEnabled(false); DefaultCommandUIFactory uiFactory = new DefaultCommandUIFactory(); uiFactory.setCommandManager(manager); JPopupMenu menu = uiFactory.addContextDependentMenuItems("image", new JPopupMenu()); assertEquals(2, menu.getComponentCount()); menu = uiFactory.addContextDependentMenuItems("notImage", new JPopupMenu()); assertEquals(1, menu.getComponentCount()); }
public void testCreateCommandWithConstraintsAndListener() { final CommandListener listener = new CommandListener() { public void actionPerformed(CommandEvent event) { } public void updateState(CommandEvent event) { } }; ExecCommand command = createCommand("test"); command.addCommandListener(listener); _manager.addCommand(command); final Command commandAt = _manager.getCommandAt(0); final CommandListener[] listeners = (CommandListener[]) commandAt.getEventListenerList().getListeners( CommandListener.class); assertEquals(1, listeners.length); assertSame(listener, listeners[0]); }
/** * Creates a new executable command for the given unique command ID and the given command listener. * Finally the created command is added to internal list of registered commands. * * @param commandID a unique command ID * @param listener the command listener which executes the command and updates its state * * @return a new executable command * * @see #createToolCommand * @see #createCommandGroup * @see #addCommand(Command) */ @Override public ExecCommand createExecCommand(String commandID, CommandListener listener) { Guardian.assertNotNullOrEmpty("commandID", commandID); ExecCommand command = new ExecCommand(commandID, listener); addCommand(command); return command; }
/** * Index out of bounds exception wenn der index out of range ist. */ public void testGetCommandAt_IndexOutOfBoundsException() { addCommand("test"); assertNotNull(_manager.getCommandAt(0)); try { _manager.getCommandAt(1); fail("IndexOutOfBoundsException not expected"); } catch (IndexOutOfBoundsException e) { // Exception expected } }
public void testCreateMoreCommands() { assertEquals(0, _manager.getNumCommands()); addCommand("test1"); addCommand("test2"); assertEquals(4, _manager.getNumCommands()); command = _manager.getExecCommand("test2"); assertNotNull(command); Command commandAt = null; commandAt = _manager.getCommandAt(1); assertSame(command, commandAt); command = _manager.getExecCommand("test1"); assertNotNull(command); commandAt = null; commandAt = _manager.getCommandAt(0); assertSame(command, commandAt); command = _manager.getExecCommand("test3"); assertNotNull(command); commandAt = null; commandAt = _manager.getCommandAt(2); assertSame(command, commandAt); command = _manager.getExecCommand("test4"); assertNotNull(command); commandAt = null; commandAt = _manager.getCommandAt(3); assertSame(command, commandAt);
@Test public void testAddContextDependingMenuItemsOnlyOneTime() { DefaultCommandManager manager = new DefaultCommandManager(); createCommand("com1", manager); createCommand("com2", manager); createCommand("com3", manager); manager.getCommandAt(0).setProperty(Command.ACTION_KEY_CONTEXT, "band"); manager.getCommandAt(1).setProperty(Command.ACTION_KEY_CONTEXT, new String[]{"band", "virtualBand"}); manager.getCommandAt(2).setProperty(Command.ACTION_KEY_CONTEXT, "virtualBand"); DefaultCommandUIFactory uiFactory = new DefaultCommandUIFactory(); uiFactory.setCommandManager(manager); JPopupMenu bandMenu = new JPopupMenu(); uiFactory.addContextDependentMenuItems("virtualBand", bandMenu); assertEquals(2, bandMenu.getComponentCount()); JPopupMenu virtualBandMenu = new JPopupMenu(); uiFactory.addContextDependentMenuItems("band", virtualBandMenu); assertEquals(2, virtualBandMenu.getComponentCount()); JPopupMenu bandAndVbMenu = new JPopupMenu(); uiFactory.addContextDependentMenuItems("band", bandAndVbMenu); uiFactory.addContextDependentMenuItems("virtualBand", bandAndVbMenu); assertEquals(3, bandAndVbMenu.getComponentCount()); }
/** * Creates a new command group command for the given unique command ID an the given command state listener. * Finally the created command group is added to internal list of registered commands. * * @param commandGroupID a unique command group ID * @param listener the command state listener used to update the command group state * * @return a new command group * * @see #createExecCommand * @see #createToolCommand * @see #addCommand(Command) */ @Override public CommandGroup createCommandGroup(String commandGroupID, CommandStateListener listener) { Guardian.assertNotNullOrEmpty("commandGroupID", commandGroupID); CommandGroup commandGroup = new CommandGroup(commandGroupID, listener); addCommand(commandGroup); return commandGroup; }
/** * Updates the component tree of all commands since the Java look-and-feel has changed. */ @Override public void updateComponentTreeUI() { final int n = getNumCommands(); for (int i = 0; i < n; i++) { getCommandAt(i).updateComponentTreeUI(); } }
/** * Creates a new tool command for the given unique command ID an the given tool. * Finally the created command is added to internal list of registered commands. * * @param commandID a unique command ID * @param listener the command state listener used to update the tool command's state * @param tool the tool which executes the command and updates its state * * @return a new tool command * * @see #createExecCommand * @see #createCommandGroup * @see #addCommand(Command) */ @Override public ToolCommand createToolCommand(String commandID, CommandStateListener listener, Interactor tool) { Guardian.assertNotNullOrEmpty("commandID", commandID); Guardian.assertNotNull("tool", tool); ToolCommand command = new ToolCommand(commandID, listener, tool); addCommand(command); return command; }
private void initCommandManager() { commandManager = new DefaultCommandManager(); commandUIFactory = new DefaultCommandUIFactory(); commandUIFactory.setCommandManager(commandManager); commandMenuInserter = new DefaultCommandMenuInserter(commandManager); }
/** * Gets the command group associated with the given command-ID or <code>null</code> if an command group with the * given command-ID has not been registered. */ @Override public CommandGroup getCommandGroup(String commandID) { Command command = getCommand(commandID); if (command instanceof CommandGroup) { return (CommandGroup) command; } return null; }
private static ExecCommand createCommand(String commandId, DefaultCommandManager manager) { ExecCommand command = new ExecCommand(); command.setCommandID(commandId); manager.addCommand(command); return command; } }
@Test public void testPlaceAtContextTop() { DefaultCommandManager manager = new DefaultCommandManager(); ExecCommand standardCommand1 = createCommand("com1", manager); ExecCommand standardCommand2 = createCommand("com2", manager); ExecCommand topCommand = createCommand("com3", manager); standardCommand1.setProperty(Command.ACTION_KEY_CONTEXT, "band"); standardCommand2.setProperty(Command.ACTION_KEY_CONTEXT, "band"); topCommand.setProperty(Command.ACTION_KEY_CONTEXT, "band"); topCommand.setPlaceAtContextTop(true); DefaultCommandUIFactory uiFactory = new DefaultCommandUIFactory(); uiFactory.setCommandManager(manager); JPopupMenu popup = new JPopupMenu(); uiFactory.addContextDependentMenuItems("band", popup); assertEquals(3, popup.getComponentCount()); String[] expectedOrder = new String[]{"com3", "com1", "com2",}; assertEquals(expectedOrder[0], popup.getComponent(0).getName()); assertEquals(expectedOrder[1], popup.getComponent(1).getName()); assertEquals(expectedOrder[2], popup.getComponent(2).getName()); }
/** * Gets the command associated with the given command-ID or <code>null</code> if an command with the given * command-ID has not been registered. */ @Override public ExecCommand getExecCommand(String commandID) { final Command command = getCommand(commandID); if (command instanceof ExecCommand) { return (ExecCommand) command; } return null; }
private void addCommand(String commandId) { ExecCommand command1 = new ExecCommand(); command1.setCommandID(commandId); _manager.addCommand(command1); }