001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.tools.edit.activator;
016:
017: import net.refractions.udig.project.BlackboardEvent;
018: import net.refractions.udig.project.IBlackboard;
019: import net.refractions.udig.project.IBlackboardListener;
020: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
021: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseMotionListener;
022: import net.refractions.udig.project.ui.render.displayAdapter.ViewportPane;
023: import net.refractions.udig.tools.edit.Activator;
024: import net.refractions.udig.tools.edit.EditPlugin;
025: import net.refractions.udig.tools.edit.EditState;
026: import net.refractions.udig.tools.edit.EditToolHandler;
027: import net.refractions.udig.tools.edit.commands.DrawEditGeomsCommand;
028: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
029: import net.refractions.udig.tools.edit.support.Point;
030: import net.refractions.udig.tools.edit.support.PrimitiveShape;
031:
032: /**
033: * Adds a DrawGeomsCommand to the draw commands and invalidates it at the end.
034: *
035: * @author jones
036: * @since 1.1.0
037: */
038: public class DrawGeomsActivator implements Activator {
039:
040: private DrawEditGeomsCommand command;
041: private DrawType type;
042: private boolean showMouseLocation = true;
043: private MapMouseMotionListener listener;
044: private ViewportPane pane;
045: private EditToolHandler handler;
046: private IBlackboardListener mapBBListener;
047:
048: /**
049: * Returns true if the mouse position will be shown. Default is true.
050: *
051: * @return Returns true if the mouse position will be shown. Default is true.
052: */
053: public boolean isShowMouseLocation() {
054: return this .showMouseLocation;
055: }
056:
057: /**
058: * @param showMouseLocation The showMouseLocation to set.
059: */
060: public void setShowMouseLocation(boolean showMouseLocation) {
061: this .showMouseLocation = showMouseLocation;
062: }
063:
064: public DrawGeomsActivator(DrawType type) {
065: this .type = type;
066: }
067:
068: public void activate(final EditToolHandler handler) {
069:
070: this .handler = handler;
071: command = new DrawEditGeomsCommand(handler);
072: command.setFill(PreferenceUtil.instance().getDrawGeomsFill());
073: command.setLine(PreferenceUtil.instance().getDrawGeomsLine());
074: pane = handler.getContext().getViewportPane();
075:
076: addMouseListener();
077:
078: handler.getContext().getViewportPane().addDrawCommand(command);
079: }
080:
081: private void removeMouseListener() {
082: if (pane != null && listener != null)
083: pane.removeMouseMotionListener(listener);
084: listener = null;
085: handler.getContext().getMap().getBlackboard().removeListener(
086: mapBBListener);
087: }
088:
089: private void addMouseListener() {
090: listener = new MapMouseMotionListener() {
091:
092: public void mouseMoved(MapMouseEvent event) {
093: if (type == DrawType.POINT || !showMouseLocation)
094: return;
095: if (listener != this ) {
096: ((ViewportPane) event.source)
097: .removeMouseMotionListener(this );
098: }
099: boolean change = false;
100: if (handler.getCurrentState() == EditState.CREATING)
101: change = command.setCurrentLocation(Point.valueOf(
102: event.x, event.y), handler
103: .getCurrentShape());
104: else
105: change = command.setCurrentLocation(null, null);
106: if (change)
107: handler.repaint();
108: }
109:
110: public void mouseDragged(MapMouseEvent event) {
111: mouseMoved(event);
112: }
113:
114: public void mouseHovered(MapMouseEvent event) {
115: }
116:
117: };
118: pane.addMouseMotionListener(listener);
119:
120: mapBBListener = new IBlackboardListener() {
121:
122: public void blackBoardCleared(IBlackboard source) {
123: if (mapBBListener != this ) {
124: source.removeListener(this );
125: }
126: command.setCurrentLocation(null, null);
127: }
128:
129: public void blackBoardChanged(BlackboardEvent event) {
130: if (mapBBListener != this ) {
131: event.getSource().removeListener(this );
132: }
133: if (EditToolHandler.CURRENT_SHAPE
134: .equals(event.getKey())) {
135: command.setCurrentLocation(null,
136: (PrimitiveShape) event.getNewValue());
137: }
138: }
139:
140: };
141:
142: handler.getContext().getMap().getBlackboard().addListener(
143: mapBBListener);
144: }
145:
146: public void deactivate(EditToolHandler handler) {
147: if (command != null)
148: command.setValid(false);
149: mapBBListener = null;
150: listener = null;
151: removeMouseListener();
152: }
153:
154: public void handleActivateError(EditToolHandler handler,
155: Throwable error) {
156: EditPlugin.log("Error creating and sending command", error); //$NON-NLS-1$
157: }
158:
159: public void handleDeactivateError(EditToolHandler handler,
160: Throwable error) {
161: EditPlugin.log("Error invalidating command", error); //$NON-NLS-1$
162: }
163:
164: public enum DrawType {
165: POLYGON, LINE, POINT
166: }
167:
168: }
|