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.awt.geom.GeneralPath;
019: import java.awt.geom.PathIterator;
020: import java.util.Iterator;
021: import java.util.NoSuchElementException;
022:
023: /**
024: * PathIterator for a simple primitive shape.
025: * @author jones
026: * @since 1.1.0
027: */
028: public abstract class AbstractShapeIterator implements PathIterator {
029:
030: private PrimitiveShape pointShape;
031:
032: protected PrimitiveShape shape;
033:
034: protected Iterator<Point> points;
035: protected Point currentPoint, nextPoint;
036:
037: private PrimitiveShape trueShape;
038:
039: protected AbstractShapeIterator(PrimitiveShape shape) {
040: this .trueShape = shape;
041: }
042:
043: public synchronized Shape toShape() {
044: reset();
045: GeneralPath path = new GeneralPath();
046: path.append(this , false);
047: return path;
048: }
049:
050: public synchronized void reset() {
051: currentPoint = null;
052: if (trueShape.getNumPoints() == 1)
053: shape = getPointShape();
054: else
055: shape = trueShape;
056: points = shape.iterator();
057: nextPoint = null;
058: }
059:
060: private synchronized PrimitiveShape getPointShape() {
061: if (pointShape == null) {
062: pointShape = new PrimitiveShape(trueShape.getEditGeom());
063: Point point = trueShape.getPoint(0);
064: pointShape.getEditGeom().initializing = true;
065: Point valueOf = Point.valueOf(point.getX() - 2, point
066: .getY() - 2);
067: pointShape.getMutator().addPoint(valueOf, null);
068: valueOf = Point.valueOf(point.getX() + 2, point.getY() - 2);
069: pointShape.getMutator().addPoint(valueOf, null);
070: valueOf = Point.valueOf(point.getX() + 2, point.getY() + 2);
071: pointShape.getMutator().addPoint(valueOf, null);
072: valueOf = Point.valueOf(point.getX() - 2, point.getY() + 2);
073: pointShape.getMutator().addPoint(valueOf, null);
074: valueOf = Point.valueOf(point.getX() - 2, point.getY() - 2);
075: pointShape.getMutator().addPoint(valueOf, null);
076: pointShape.getEditGeom().initializing = false;
077: }
078:
079: return pointShape;
080: }
081:
082: public int getWindingRule() {
083: return WIND_EVEN_ODD;
084: }
085:
086: public boolean isDone() {
087: return isDoneInternal();
088: }
089:
090: private boolean isDoneInternal() {
091: if (nextPoint != null)
092: return false;
093:
094: if (points.hasNext()) {
095: nextPoint = points.next();
096: return false;
097: }
098:
099: return true;
100: }
101:
102: public void next() {
103: try {
104: nextPoint = null;
105: if (points.hasNext()) {
106: if (isDone()) {
107: return;
108: }
109: currentPoint = nextPoint;
110: } else {
111: isDone();
112: currentPoint = null;
113: }
114: } catch (NoSuchElementException e) {
115: throw new NoSuchElementException(
116: "Shape is done, make sure to check isDone before calling next()"); //$NON-NLS-1$
117: }
118: }
119:
120: public int currentSegment(float[] coords) {
121: int result = SEG_LINETO;
122:
123: if (currentPoint == null) {
124: coords[0] = nextPoint.getX();
125: coords[1] = nextPoint.getY();
126: return SEG_MOVETO;
127: }
128:
129: coords[0] = currentPoint.getX();
130: coords[1] = currentPoint.getY();
131:
132: return result;
133: }
134:
135: public int currentSegment(double[] coords) {
136: int result = SEG_LINETO;
137:
138: if (currentPoint == null) {
139: coords[0] = nextPoint.getX();
140: coords[1] = nextPoint.getY();
141: return SEG_MOVETO;
142: }
143:
144: coords[0] = currentPoint.getX();
145: coords[1] = currentPoint.getY();
146:
147: return result;
148: }
149:
150: }
|