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: /**
022: * PathIterator that wraps a primitive shape.
023: *
024: * @author jones
025: * @since 1.1.0
026: */
027: public class PrimitiveShapeIterator extends AbstractShapeIterator {
028:
029: private static final Map<PrimitiveShape, PrimitiveShapeIterator> map = new HashMap<PrimitiveShape, PrimitiveShapeIterator>();
030: private static EditBlackboardListener bbListener = new EditBlackboardAdapter() {
031: @SuppressWarnings("unchecked")
032: @Override
033: public void changed(EditBlackboardEvent event) {
034: switch (event.getType()) {
035: case CLEARED:
036: ((EditBlackboard) event.getSource()).getListeners()
037: .remove(this );
038: map.clear();
039: break;
040: case REMOVE_GEOMS:
041: List<EditGeom> geoms = (List<EditGeom>) event
042: .getOldValue();
043: for (EditGeom geom : geoms) {
044: map.remove(geom);
045: }
046:
047: if (map.isEmpty()) {
048: ((EditBlackboard) event.getSource()).getListeners()
049: .remove(this );
050: }
051: break;
052: default:
053: break;
054: }
055: }
056:
057: @Override
058: public void batchChange(List<EditBlackboardEvent> e) {
059: for (EditBlackboardEvent event : e) {
060: changed(event);
061: }
062: }
063: };
064: private boolean isPolygon;
065:
066: protected PrimitiveShapeIterator(PrimitiveShape shape) {
067: super (shape);
068: }
069:
070: public static PrimitiveShapeIterator getPathIterator(
071: PrimitiveShape shape) {
072: PrimitiveShapeIterator iter = map.get(shape);
073: if (iter == null) {
074: iter = new PrimitiveShapeIterator(shape);
075: shape.getEditBlackboard().getListeners().add(bbListener);
076: map.put(shape, iter);
077: iter.reset();
078: } else {
079: iter.reset();
080: }
081: return iter;
082: }
083:
084: public int currentSegment(float[] coords) {
085:
086: int result = super .currentSegment(coords);
087:
088: if ((isPolygon || shape.getEditGeom().getShapeType() == ShapeType.POLYGON)
089: && result != SEG_MOVETO) {
090:
091: if (!points.hasNext() && isClosed(shape)) {
092: result = SEG_CLOSE;
093: }
094: }
095:
096: return result;
097:
098: }
099:
100: // returns true if shape is closed
101: protected boolean isClosed(PrimitiveShape shape) {
102: return shape.getPoint(0).equals(
103: shape.getPoint(shape.getNumPoints() - 1));
104: }
105:
106: public void setPolygon(boolean isPolygon) {
107: this .isPolygon = isPolygon;
108: }
109:
110: /**
111: * @return Returns the isPolygon.
112: */
113: public boolean isPolygon() {
114: return isPolygon;
115: }
116: }
|