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.EditManagerEvent;
019: import net.refractions.udig.project.IBlackboard;
020: import net.refractions.udig.project.IBlackboardListener;
021: import net.refractions.udig.project.IEditManagerListener;
022: import net.refractions.udig.project.ILayer;
023: import net.refractions.udig.project.IMap;
024: import net.refractions.udig.tools.edit.Activator;
025: import net.refractions.udig.tools.edit.EditPlugin;
026: import net.refractions.udig.tools.edit.EditToolHandler;
027: import net.refractions.udig.tools.edit.commands.DrawPointCommand;
028: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
029: import net.refractions.udig.tools.edit.support.EditBlackboard;
030: import net.refractions.udig.tools.edit.support.EditBlackboardAdapter;
031: import net.refractions.udig.tools.edit.support.EditBlackboardEvent;
032: import net.refractions.udig.tools.edit.support.EditBlackboardListener;
033: import net.refractions.udig.tools.edit.support.EditBlackboardEvent.EventType;
034:
035: /**
036: * Activates and deactivates the draw command that draws the vertices for the EditGeom.
037: * @author jones
038: * @since 1.1.0
039: */
040: public class DrawCurrentGeomVerticesActivator implements Activator {
041:
042: DrawPointCommand command;
043: IBlackboardListener mapBBlistener = new IBlackboardListener() {
044:
045: public void blackBoardChanged(BlackboardEvent event) {
046: if (event.getSource() != handler.getContext().getMap()
047: .getBlackboard()) {
048: event.getSource().removeListener(this );
049: return;
050: }
051: if (event.getKey() == EditToolHandler.CURRENT_SHAPE
052: || event.getKey().equals(
053: EditToolHandler.CURRENT_SHAPE)) {
054: handler.repaint();
055: }
056: }
057:
058: public void blackBoardCleared(IBlackboard source) {
059: handler.repaint();
060: }
061:
062: };
063: private EditToolHandler handler;
064: private EditBlackboardListener editBBListener = new EditBlackboardAdapter() {
065: @Override
066: public void changed(EditBlackboardEvent e) {
067: if (e.getEditBlackboard() != handler
068: .getEditBlackboard(handler.getEditLayer())) {
069: if (e.getSource() instanceof EditBlackboard) {
070: EditBlackboard bb = (EditBlackboard) e.getSource();
071: bb.getListeners().remove(this );
072: return;
073: }
074: }
075:
076: if (e.getType() == EventType.SELECTION) {
077: handler.repaint();
078: }
079: }
080:
081: @Override
082: public void batchChange(java.util.List<EditBlackboardEvent> e) {
083:
084: for (EditBlackboardEvent event : e) {
085: if (event.getEditBlackboard() != handler
086: .getEditBlackboard(handler.getEditLayer())) {
087: EditBlackboard bb = (EditBlackboard) event
088: .getSource();
089: bb.getListeners().remove(this );
090: return;
091: }
092:
093: if (event.getType() == EventType.SELECTION
094: && handler.getCurrentShape() != null) {
095: handler.repaint();
096: break;
097: }
098: }
099: };
100:
101: };
102: private IEditManagerListener editManagerListener = new IEditManagerListener() {
103:
104: public void changed(EditManagerEvent event) {
105: if (event.getSource() != handler.getContext()
106: .getEditManager()) {
107: event.getSource().removeListener(this );
108: return;
109: }
110:
111: if (event.getType() == EditManagerEvent.SELECTED_LAYER
112: && event.getOldValue() != event.getNewValue()) {
113: if (event.getOldValue() != null) {
114: ILayer layer = (ILayer) event.getOldValue();
115: handler.getEditBlackboard(layer).getListeners()
116: .remove(editBBListener);
117: }
118: if (event.getNewValue() != null) {
119: ILayer layer = (ILayer) event.getNewValue();
120: handler.getEditBlackboard(layer).getListeners()
121: .add(editBBListener);
122: handler.repaint();
123: }
124: }
125: }
126:
127: };
128:
129: public void activate(final EditToolHandler handler) {
130: this .handler = handler;
131: command = new DrawPointCommand(handler, handler
132: .getCurrentShape(), handler.getContext()
133: .getViewportPane());
134: command.setRadius(PreferenceUtil.instance().getVertexRadius());
135: command.setFill(PreferenceUtil.instance()
136: .getDrawVertexFillColor());
137: command.setOutline(PreferenceUtil.instance()
138: .getDrawVertexLineColor());
139: command.setSelectionFill(PreferenceUtil.instance()
140: .getDrawSelectionFillColor());
141: command.setDrawCurrentShape(true);
142: handler.getContext().getViewportPane().addDrawCommand(command);
143: handler.getContext().getMap().getEditManager().addListener(
144: editManagerListener);
145: handler.getContext().getMap().getBlackboard().addListener(
146: mapBBlistener);
147: handler.getEditBlackboard(handler.getEditLayer())
148: .getListeners().add(editBBListener);
149: }
150:
151: public void deactivate(EditToolHandler handler) {
152: command.setValid(false);
153: IMap map = handler.getContext().getMap();
154: map.getBlackboard().removeListener(mapBBlistener);
155: map.getEditManager().removeListener(editManagerListener);
156: handler.getEditBlackboard(handler.getEditLayer())
157: .getListeners().remove(editBBListener);
158: }
159:
160: public void handleActivateError(EditToolHandler handler,
161: Throwable error) {
162: EditPlugin.log("Unexpected Error while activating", error); //$NON-NLS-1$
163: if (command != null)
164: command.setValid(false);
165: handler.getContext().getMap().getBlackboard().removeListener(
166: mapBBlistener);
167: }
168:
169: public void handleDeactivateError(EditToolHandler handler,
170: Throwable error) {
171: EditPlugin.log("Unexpected Error while deactivating", error); //$NON-NLS-1$
172: if (command != null)
173: command.setValid(false);
174: handler.getContext().getMap().getBlackboard().removeListener(
175: mapBBlistener);
176: }
177:
178: }
|