15a16,16 > import java.util.*; 18a20,21 > import java.awt.event.ActionEvent; > import javax.swing.AbstractAction; 29,29c32,32 < public abstract class AbstractTool implements Tool { --- > public abstract class AbstractTool implements Tool, ViewChangeListener { 30a34,34 > private DrawingEditor myDrawingEditor; 31,31d33 < protected DrawingView fView; 38a42,42 > private AbstractTool.EventDispatcher myEventDispatcher; 42a47,50 > public AbstractTool(DrawingEditor newDrawingEditor) { > myDrawingEditor = newDrawingEditor; > setEventDispatcher(createEventDispatcher()); > editor().addViewChangeListener(this); 43,44d46 < public AbstractTool(DrawingView itsView) { < fView = itsView; 48,48c54,54 < * 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,61 > if (view() != null) { 53a63,64 > getEventDispatcher().fireToolActivatedEvent(); > } 62a74,74 > 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) { 106,106c145,145 < * Gets the tool's editor (convenience method). --- > * Gets the tool's editor. 109,109c155,155 < return view().editor(); --- > return editor().view(); 109a156,162 > } > > /** > * Tests if the tool can be used or "executed." > */ > public boolean isUsable() { > return (view() != null) && (view().isInteractive()); 113,113c152,152 < * Gets the tool's view. --- > * Gets the tool's view (convienence method). 115a148,148 > return myDrawingEditor; 116,116d147 < return fView; 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); > } > } > }