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.support;
016:
017: import java.awt.Shape;
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021:
022: import net.refractions.udig.tools.edit.support.EditBlackboardEvent.EventType;
023:
024: import org.eclipse.swt.graphics.Device;
025: import org.eclipse.swt.graphics.Path;
026:
027: /**
028: * Extends {@link EditGeomPathIterator} so that
029: * @author jones
030: * @since 1.1.0
031: */
032: public class CurrentEditGeomPathIterator extends EditGeomPathIterator {
033: enum State {
034: waiting, onCurrent, done, onlyPointInShape;
035: };
036:
037: private Point location;
038: State currentState;
039: private PrimitiveShape shape;
040:
041: protected CurrentEditGeomPathIterator(EditGeom shape) {
042: super (shape);
043: }
044:
045: private static final Map<EditBlackboard, EditBlackboardListener> listeners = new HashMap<EditBlackboard, EditBlackboardListener>();
046:
047: private static final Map<EditGeom, CurrentEditGeomPathIterator> map = new HashMap<EditGeom, CurrentEditGeomPathIterator>();
048:
049: public static CurrentEditGeomPathIterator getPathIterator(
050: EditGeom geom) {
051: CurrentEditGeomPathIterator iter = map.get(geom);
052: if (iter == null) {
053: iter = new CurrentEditGeomPathIterator(geom);
054: EditBlackboardListener listener = listeners.get(geom
055: .getEditBlackboard());
056: if (listener == null) {
057: listener = new EditBlackboardAdapter() {
058: @SuppressWarnings("unchecked")
059: @Override
060: public void changed(EditBlackboardEvent event) {
061: if (event.getType() == EventType.REMOVE_GEOMS) {
062: List<EditGeom> geoms = (List<EditGeom>) event
063: .getOldValue();
064: for (EditGeom geom : geoms) {
065: map.remove(geom);
066: }
067: }
068: }
069:
070: @Override
071: public void batchChange(List<EditBlackboardEvent> e) {
072: for (EditBlackboardEvent event : e) {
073: changed(event);
074: }
075: }
076: };
077: listeners.put(geom.getEditBlackboard(), listener);
078: }
079: geom.getEditBlackboard().getListeners().add(listener);
080: map.put(geom, iter);
081: }
082: return iter;
083: }
084:
085: @Override
086: public boolean isDone() {
087: boolean super Done = super .isDone();
088:
089: return super Done && (currentState == State.done);
090: }
091:
092: private void prepareToPath() {
093: if (location == null)
094: currentState = State.done;
095: else if (shape.getNumPoints() == 0) {
096: currentState = State.onlyPointInShape;
097: } else {
098: currentState = State.waiting;
099: }
100: }
101:
102: @Override
103: public Shape toShape() {
104: prepareToPath();
105: return super .toShape();
106: }
107:
108: public Path toPath(Device device) {
109: prepareToPath();
110: return super .toPath(device);
111: }
112:
113: @Override
114: protected Shape createPoint() {
115: return null;
116: }
117:
118: @Override
119: protected Path createPointPath(Device device) {
120: return null;
121: }
122:
123: @Override
124: public void next() {
125: if (!super .isDone()) {
126: super .next();
127: return;
128: }
129: if (currentState == State.waiting)
130: currentState = State.onCurrent;
131: else
132: currentState = State.done;
133: }
134:
135: @Override
136: public int currentSegment(float[] coords) {
137:
138: if (!super .isDone())
139: return super .currentSegment(coords);
140:
141: if (currentState == State.onCurrent
142: || currentState == State.onlyPointInShape) {
143: coords[0] = location.getX();
144: coords[1] = location.getY();
145: if (currentState == State.onlyPointInShape) {
146: return SEG_MOVETO;
147: }
148: }
149: if (currentState == State.done && isPolygon())
150: return SEG_CLOSE;
151:
152: return SEG_LINETO;
153:
154: }
155:
156: /**
157: * @return Returns the location.
158: */
159: public Point getLocation() {
160: return location;
161: }
162:
163: /**
164: * @param location The location to set.
165: * @param shape if null then
166: */
167: public void setLocation(Point location, PrimitiveShape shape) {
168: if (shape != null) {
169: this.location = location;
170: this.shape = shape;
171: } else {
172: this.location = null;
173: this.shape = null;
174: }
175: }
176:
177: }
|