15a16,16 > import CH.ifa.draw.util.CommandListener; 23,23c24,24 < public abstract class AbstractCommand implements Command, FigureSelectionListener { --- > public abstract class AbstractCommand implements Command, FigureSelectionListener, ViewChangeListener { 26a28,29 > private boolean myIsViewRequired; > private AbstractCommand.EventDispatcher myEventDispatcher; 29,29c32,32 < * the DrawingView this command applies to --- > * the DrawingEditor this command applies to 30a34,34 > private DrawingEditor myDrawingEditor; 31,31d33 < private DrawingView fView; 35a39,39 > * @param newDrawingEditor the DrawingEditor which manages the views 37a42,45 > this(newName, newDrawingEditor, true); > } > > public AbstractCommand(String newName, DrawingEditor newDrawingEditor, boolean newIsViewRequired) { 37,37c41,41 < public AbstractCommand(String newName, DrawingView newView) { --- > public AbstractCommand(String newName, DrawingEditor newDrawingEditor) { 38a47,81 > setDrawingEditor(newDrawingEditor); > getDrawingEditor().addViewChangeListener(this); > myIsViewRequired = newIsViewRequired; > setEventDispatcher(createEventDispatcher()); > } > > public void viewSelectionChanged(DrawingView oldView, DrawingView newView) { > if (oldView != null) { > oldView.removeFigureSelectionListener(this); > } > if (newView != null) { > newView.addFigureSelectionListener(this); > } > checkExecutable(); > } > > /** > * Sent when a new view is created > */ > public void viewCreated(DrawingView view) { > } > > /** > * Send when an existing view is about to be destroyed. > */ > public void viewDestroying(DrawingView view) { > } > > protected void checkExecutable() { > if (isExecutable()) { > getEventDispatcher().fireCommandExecutableEvent(); > } > else { > getEventDispatcher().fireCommandNotExecutableEvent(); > } 39,40d46 < setView(newView); < view().addFigureSelectionListener(this); 50,50c91,91 < * @return view associated with this command --- > * @return DrawingEditor associated with this command 51a97,105 > private void setDrawingEditor(DrawingEditor newDrawingEditor) { > myDrawingEditor = newDrawingEditor; > } > > /** > * Convenience method > * > * @return DrawingView currently active in the editor > */ 51a93,94 > public DrawingEditor getDrawingEditor() { > return myDrawingEditor; 52a107,107 > return getDrawingEditor().view(); 52,52c106,106 < public DrawingView view() { --- > public DrawingView view() { 53,53d106 < return fView; 56,57d96 < private void setView(DrawingView newView) { < fView = newView; 80a131,135 > public void execute() { > if (view() == null) { > throw new JHotDrawRuntimeException("execute should NOT be getting called when view() == null"); > }; > } 81,81d130 < public abstract void execute(); 84,84c138,138 < * Tests if the command can be executed. --- > * Tests if the command can be executed. The view must be valid when this is 84a139,141 > * called. Per default, a command is executable if at > * least one figure is selected in the current activated > * view. 86a144,150 > if (view() == null) { > return false; > } > else if (isExecutableWithView()) { > return view().isInteractive(); > } > else { 88a153,157 > } > > protected boolean isExecutableWithView() { > return myIsViewRequired; > } 96a166,228 > > public void addCommandListener(CommandListener newCommandListener) { > getEventDispatcher().addCommandListener(newCommandListener); > } > > public void removeCommandListener(CommandListener oldCommandListener) { > getEventDispatcher().removeCommandListener(oldCommandListener); > } > > private void setEventDispatcher(AbstractCommand.EventDispatcher newEventDispatcher) { > myEventDispatcher = newEventDispatcher; > } > > protected AbstractCommand.EventDispatcher getEventDispatcher() { > return myEventDispatcher; > } > > public AbstractCommand.EventDispatcher createEventDispatcher() { > return new AbstractCommand.EventDispatcher(this); > } > > public static class EventDispatcher { > private Vector myRegisteredListeners; > private Command myObservedCommand; > > public EventDispatcher(Command newObservedCommand) { > myRegisteredListeners = new Vector(); > myObservedCommand = newObservedCommand; > } > > public void fireCommandExecutedEvent() { > Enumeration le = myRegisteredListeners.elements(); > while (le.hasMoreElements()) { > ((CommandListener)le.nextElement()).commandExecuted(new EventObject(myObservedCommand)); > } > } > > public void fireCommandExecutableEvent() { > Enumeration le = myRegisteredListeners.elements(); > while (le.hasMoreElements()) { > ((CommandListener)le.nextElement()).commandExecutable(new EventObject(myObservedCommand)); > } > } > > public void fireCommandNotExecutableEvent() { > Enumeration le = myRegisteredListeners.elements(); > while (le.hasMoreElements()) { > ((CommandListener)le.nextElement()).commandNotExecutable(new EventObject(myObservedCommand)); > } > } > > public void addCommandListener(CommandListener newCommandListener) { > if (!myRegisteredListeners.contains(newCommandListener)) { > myRegisteredListeners.add(newCommandListener); > } > } > > public void removeCommandListener(CommandListener oldCommandListener) { > if (myRegisteredListeners.contains(oldCommandListener)) { > myRegisteredListeners.remove(oldCommandListener); > } > } > }