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.util.HashMap;
018: import java.util.List;
019: import java.util.Map;
020:
021: import net.refractions.udig.tools.edit.support.EditBlackboardEvent.EventType;
022:
023: /**
024: * Wraps a {@link net.refractions.udig.tools.edit.support.EditGeom} so that it can be drawn to a
025: * Graphics2d object.
026: *
027: * @author jones
028: * @since 1.1.0
029: */
030: public class EditGeomPathIterator extends AbstractPathIterator {
031:
032: protected boolean isPolygon;
033:
034: protected EditGeomPathIterator(EditGeom shape) {
035: super (shape);
036: }
037:
038: public void dispose() {
039: }
040:
041: private static final Map<EditBlackboard, EditBlackboardListener> listeners = new HashMap<EditBlackboard, EditBlackboardListener>();
042:
043: private static final Map<EditGeom, EditGeomPathIterator> map = new HashMap<EditGeom, EditGeomPathIterator>();
044:
045: public static EditGeomPathIterator getPathIterator(EditGeom geom) {
046: EditGeomPathIterator iter = map.get(geom);
047: if (iter == null) {
048: iter = new EditGeomPathIterator(geom);
049: EditBlackboardListener listener = listeners.get(geom
050: .getEditBlackboard());
051: if (listener == null) {
052: listener = new EditBlackboardAdapter() {
053: @SuppressWarnings("unchecked")
054: @Override
055: public void changed(EditBlackboardEvent event) {
056: if (event.getType() == EventType.REMOVE_GEOMS) {
057: List<EditGeom> geoms = (List<EditGeom>) event
058: .getOldValue();
059: for (EditGeom geom : geoms) {
060: map.remove(geom);
061: }
062: }
063: }
064:
065: @Override
066: public void batchChange(List<EditBlackboardEvent> e) {
067: for (EditBlackboardEvent event : e) {
068: changed(event);
069: }
070: }
071: };
072: listeners.put(geom.getEditBlackboard(), listener);
073: }
074: geom.getEditBlackboard().getListeners().add(listener);
075: map.put(geom, iter);
076: }
077: return iter;
078: }
079:
080: public int currentSegment(float[] coords) {
081:
082: int result = super .currentSegment(coords);
083:
084: if ((isPolygon || geom.getShapeType() == ShapeType.POLYGON)
085: && result != SEG_MOVETO) {
086:
087: if (!points.hasNext() && isClosed(currentShape)) {
088: result = SEG_CLOSE;
089: }
090: }
091:
092: return result;
093:
094: }
095:
096: // returns true if shape is closed
097: protected boolean isClosed(PrimitiveShape shape) {
098: return shape.getPoint(0).equals(
099: shape.getPoint(shape.getNumPoints() - 1));
100: }
101:
102: public void setPolygon(boolean isPolygon) {
103: this .isPolygon = isPolygon;
104: }
105:
106: /**
107: * @return Returns the isPolygon.
108: */
109: public boolean isPolygon() {
110: return isPolygon;
111: }
112:
113: }
|