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.geom.PathIterator;
018: import java.util.Iterator;
019:
020: /**
021: * Draws all the Control handles for a {@link EditGeom}. This may not be possible to implement because if two vertices are
022: * overlapping there will be a hole where the overlap but try it out it may be acceptable.
023: *
024: * <p><b>Warning. this class is not fully tested yet so use with care</b></p>
025: * @author Jesse
026: * @since 1.1.0
027: */
028: public class ControlPointPathIterator implements PathIterator {
029:
030: int count = 0, x = 1, y = 1, rectPos = 0;
031: private final Iterator<Point> points;
032: private final int xRadius;
033: private final int yRadius;
034:
035: public ControlPointPathIterator(final EditGeom geom,
036: boolean selection, int width, int height) {
037: final Selection selectionCollection = geom.createSelection();
038:
039: int xRadius = width / 2;
040: int yRadius = height / 2;
041: if (xRadius < 1)
042: xRadius = 1;
043: if (yRadius < 1)
044: yRadius = 1;
045:
046: this .xRadius = xRadius;
047: this .yRadius = yRadius;
048:
049: if (selection) {
050: points = selectionCollection.iterator();
051: } else {
052: points = new EditGeomPointIterator(geom,
053: selectionCollection);
054: }
055:
056: if (points.hasNext()) {
057: Point next = points.next();
058: x = next.getX();
059: y = next.getY();
060: } else {
061: rectPos = 5;
062: }
063: }
064:
065: public int currentSegment(float[] coords) {
066: double[] c = new double[2];
067: int result = currentSegment(c);
068: coords[0] = (float) c[0];
069: coords[1] = (float) c[1];
070: return result;
071: }
072:
073: public int currentSegment(double[] coords) {
074: switch (rectPos) {
075: case 0:
076: coords[0] = x - xRadius;
077: coords[1] = y - yRadius;
078: break;
079: case 1:
080: coords[0] = x + xRadius;
081: coords[1] = y - yRadius;
082: break;
083: case 2:
084: coords[0] = x + xRadius;
085: coords[1] = y + yRadius;
086: break;
087: case 3:
088: coords[0] = x - xRadius;
089: coords[1] = y + yRadius;
090: break;
091: case 4:
092: coords[0] = x - xRadius;
093: coords[1] = y - yRadius;
094: break;
095: case 5:
096: return PathIterator.SEG_CLOSE;
097: default:
098: break;
099: }
100:
101: if (rectPos == 0) {
102: return PathIterator.SEG_MOVETO;
103: }
104: return PathIterator.SEG_LINETO;
105: }
106:
107: public int getWindingRule() {
108: return WIND_EVEN_ODD;
109: }
110:
111: public boolean isDone() {
112: return !points.hasNext() && rectPos > 5;
113: }
114:
115: public void next() {
116: rectPos++;
117: if (rectPos == 5 && points.hasNext()) {
118: rectPos = 0;
119: count++;
120:
121: Point next = points.next();
122: x = next.getX();
123: y = next.getY();
124: }
125: }
126:
127: }
|