TargetManager { public boolean doCommand(Object command, Object value) { if (target != null) { if (target.hasCommand(command) && target.doCommand(command, value)) { return true; } } if (targetList != null) { Iterator<Targetable> iter = targetList.iterator(); while (iter.hasNext()) { Targetable target = iter.next(); if (target.hasCommand(command) && target.doCommand(command, value)) { return true; } } } ActionEvent evt = null; if (value instanceof ActionEvent) { evt = (ActionEvent)value; } Component comp = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner(); while (comp != null) { if (comp instanceof JComponent) { ActionMap map = ((JComponent)comp).getActionMap(); Action action = map.get(command); if (action != null) { if (evt == null) { evt = new ActionEvent(comp, 0, command.toString()); } action.actionPerformed(evt); return true; } } comp = comp.getParent(); } return false; } TargetManager(); static TargetManager getInstance(); void addTarget(Targetable target, boolean prepend); void addTarget(Targetable target); void removeTarget(Targetable target); Targetable[] getTargets(); void setTarget(Targetable newTarget); Targetable getTarget(); void addPropertyChangeListener(PropertyChangeListener listener); void removePropertyChangeListener(PropertyChangeListener listener); boolean doCommand(Object command, Object value);  }
@Test public void testDoCommand() { FooTarget foo = new FooTarget(); BarTarget bar = new BarTarget(); TargetManager manager = TargetManager.getInstance(); manager.setTarget(foo); assertTrue(manager.doCommand("foo", null)); assertFalse(manager.doCommand("bar", null)); manager.setTarget(bar); assertTrue(manager.doCommand("bar", null)); assertFalse(manager.doCommand("foo", null)); manager.setTarget(null); manager.addTarget(foo); manager.addTarget(bar); assertTrue(manager.doCommand("bar", null)); assertTrue(manager.doCommand("foo", null)); }