15a16 > import java.util.*; 18a20,21 > import java.awt.event.ActionEvent; > import javax.swing.AbstractAction; 29c32 < public abstract class AbstractTool implements Tool { --- > public abstract class AbstractTool implements Tool, ViewChangeListener { 31c34 < protected DrawingView fView; --- > private DrawingEditor myDrawingEditor; 38a42 > private AbstractTool.EventDispatcher myEventDispatcher; 43,44c47,50 < public AbstractTool(DrawingView itsView) { < fView = itsView; --- > public AbstractTool(DrawingEditor newDrawingEditor) { > myDrawingEditor = newDrawingEditor; > setEventDispatcher(createEventDispatcher()); > editor().addViewChangeListener(this); 48c54 < * Activates the tool for the given view. This method is called --- > * Activates the tool for use on the given view. This method is called 50a57,58 > * Since tools will be disabled unless it is useable, there will always > * be an active view when this is called. based on isUsable() 52a61 > if (view() != null) { 53a63,64 > getEventDispatcher().fireToolActivatedEvent(); > } 62a74 > if ((isActive() == true) && (view() != null)) { 63a76,102 > getEventDispatcher().fireToolDeactivatedEvent(); > } > } > > /** > * Fired when the selected view changes. > * Subclasses should always call super. ViewSelectionChanged() this allows > * the tools state to be updated and referenced to the new view. > */ > public void viewSelectionChanged(DrawingView oldView, DrawingView newView) { > if (isActive() == true) { > deactivate(); > activate(); > } > checkUsable(); > } > > /** > * 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) { 106c145 < * Gets the tool's editor (convenience method). --- > * Gets the tool's editor. 109c148 < return view().editor(); --- > return myDrawingEditor; 113c152 < * Gets the tool's view. --- > * Gets the tool's view (convienence method). 116c155,162 < return fView; --- > return editor().view(); > } > > /** > * Tests if the tool can be used or "executed." > */ > public boolean isUsable() { > return (view() != null) && (view().isInteractive()); 125a172,254 > > public boolean isActive() { > return editor().tool() == this; > } > > public void addToolListener(ToolListener newToolListener) { > getEventDispatcher().addToolListener(newToolListener); > } > > public void removeToolListener(ToolListener oldToolListener) { > getEventDispatcher().removeToolListener(oldToolListener); > } > > private void setEventDispatcher(AbstractTool.EventDispatcher newEventDispatcher) { > myEventDispatcher = newEventDispatcher; > } > > protected AbstractTool.EventDispatcher getEventDispatcher() { > return myEventDispatcher; > } > > public AbstractTool.EventDispatcher createEventDispatcher() { > return new AbstractTool.EventDispatcher(this); > } > > protected void checkUsable() { > if (isUsable()) { > getEventDispatcher().fireToolUsableEvent(); > } > else { > getEventDispatcher().fireToolUnusableEvent(); > } > } > > public static class EventDispatcher { > private Vector myRegisteredListeners; > private Tool myObservedTool; > > public EventDispatcher(Tool newObservedTool) { > myRegisteredListeners = new Vector(); > myObservedTool = newObservedTool; > } > > public void fireToolUsableEvent() { > Enumeration le = myRegisteredListeners.elements(); > while (le.hasMoreElements()) { > ((ToolListener)le.nextElement()).toolUsable(new EventObject(myObservedTool)); > } > } > > public void fireToolUnusableEvent() { > Enumeration le = myRegisteredListeners.elements(); > while (le.hasMoreElements()) { > ((ToolListener)le.nextElement()).toolUnusable(new EventObject(myObservedTool)); > } > } > > public void fireToolActivatedEvent() { > Enumeration le = myRegisteredListeners.elements(); > while (le.hasMoreElements()) { > ((ToolListener)le.nextElement()).toolActivated(new EventObject(myObservedTool)); > } > } > > public void fireToolDeactivatedEvent() { > Enumeration le = myRegisteredListeners.elements(); > while (le.hasMoreElements()) { > ((ToolListener)le.nextElement()).toolDeactivated(new EventObject(myObservedTool)); > } > } > > public void addToolListener(ToolListener newToolListener) { > if (!myRegisteredListeners.contains(newToolListener)) { > myRegisteredListeners.add(newToolListener); > } > } > > public void removeToolListener(ToolListener oldToolListener) { > if (myRegisteredListeners.contains(oldToolListener)) { > myRegisteredListeners.remove(oldToolListener); > } > } > }