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:
019: import org.eclipse.swt.graphics.Path;
020: import org.eclipse.swt.graphics.PathData;
021:
022: /**
023: * Wraps a Path and allows it to be traversed like a PathIterator.
024: * <p><em>WARNING: This class takes a snap shot of the path upon creation so any changes after creation will be missed</em></p>
025: * @author Jesse
026: * @since 1.1.0
027: */
028: public class PathToPathIteratorAdapter implements PathIterator {
029:
030: private PathData data;
031: private int pointIndex = 0, typeIndex = 0;;
032:
033: /**
034: * Creates a new instance.
035: *
036: * <p><em>WARNING: This class takes a snap shot of the path upon creation so any changes after creation will be missed</em></p>
037: */
038: public PathToPathIteratorAdapter(Path path) {
039: this .data = path.getPathData();
040: }
041:
042: public int currentSegment(float[] coords) {
043: int num = getNumPoints();
044: System.arraycopy(data.points, pointIndex, coords, 0, num);
045: return getSegType();
046: }
047:
048: private int getSegType() {
049: switch (data.types[typeIndex]) {
050: case 1: // moveTo
051: return SEG_MOVETO;
052: case 2: // lineTo
053: return SEG_LINETO;
054: case 3: // quadTo
055: return SEG_QUADTO;
056: case 4: // cubeTo
057: return SEG_CUBICTO;
058: case 5: // close
059: return SEG_CLOSE;
060: }
061: throw new IllegalArgumentException(data.types[typeIndex]
062: + " is an unknown value"); //$NON-NLS-1$
063: }
064:
065: public int currentSegment(double[] coords) {
066: int num = getNumPoints();
067: for (int i = 0; i < num; i++) {
068: coords[i] = data.points[i + pointIndex];
069: }
070: return getSegType();
071: }
072:
073: public int getWindingRule() {
074: return WIND_EVEN_ODD;
075: }
076:
077: public boolean isDone() {
078: return typeIndex == data.types.length;
079: }
080:
081: public void next() {
082: pointIndex += getNumPoints();
083: typeIndex++;
084: }
085:
086: private int getNumPoints() {
087: switch (data.types[typeIndex]) {
088: case 1: // moveTo
089: return 2;
090: case 2: // lineTo
091: return 2;
092: case 3: // quadTo
093: return 4;
094: case 4: // cubeTo
095: return 6;
096: case 5: // close
097: return 0;
098: }
099: throw new IllegalArgumentException(data.types[typeIndex]
100: + " is an unknown value"); //$NON-NLS-1$
101: }
102:
103: }
|