15a16 > import CH.ifa.draw.util.CommandListener; 23c24 < public abstract class AbstractCommand implements Command, FigureSelectionListener { --- > public abstract class AbstractCommand implements Command, FigureSelectionListener, ViewChangeListener { 26a28,29 > private boolean myIsViewRequired; > private AbstractCommand.EventDispatcher myEventDispatcher; 29c32 < * the DrawingView this command applies to --- > * the DrawingEditor this command applies to 31c34 < private DrawingView fView; --- > private DrawingEditor myDrawingEditor; 35a39 > * @param newDrawingEditor the DrawingEditor which manages the views 37c41,45 < public AbstractCommand(String newName, DrawingView newView) { --- > public AbstractCommand(String newName, DrawingEditor newDrawingEditor) { > this(newName, newDrawingEditor, true); > } > > public AbstractCommand(String newName, DrawingEditor newDrawingEditor, boolean newIsViewRequired) { 39,40c47,81 < setView(newView); < view().addFigureSelectionListener(this); --- > 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(); > } 50c91 < * @return view associated with this command --- > * @return DrawingEditor associated with this command 52,53c93,94 < public DrawingView view() { < return fView; --- > public DrawingEditor getDrawingEditor() { > return myDrawingEditor; 56,57c97,107 < private void setView(DrawingView newView) { < fView = newView; --- > private void setDrawingEditor(DrawingEditor newDrawingEditor) { > myDrawingEditor = newDrawingEditor; > } > > /** > * Convenience method > * > * @return DrawingView currently active in the editor > */ > public DrawingView view() { > return getDrawingEditor().view(); 81c131,135 < public abstract void execute(); --- > public void execute() { > if (view() == null) { > throw new JHotDrawRuntimeException("execute should NOT be getting called when view() == null"); > }; > } 84c138,141 < * Tests if the command can be executed. --- > * Tests if the command can be executed. The view must be valid when this is > * 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); > } > } > }