001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004, Refractions Research Inc. This
003: * library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
004: * License as published by the Free Software Foundation; version 2.1 of the License. This library is distributed in the
005: * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
006: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
007: */
008: package net.refractions.udig.project.ui.internal.properties;
009:
010: import java.util.ArrayList;
011: import java.util.List;
012:
013: import net.refractions.udig.core.internal.GeometryBuilder;
014: import net.refractions.udig.project.ui.internal.Messages;
015:
016: import org.eclipse.jface.viewers.ICellEditorValidator;
017: import org.eclipse.ui.views.properties.IPropertyDescriptor;
018: import org.eclipse.ui.views.properties.IPropertySource2;
019: import org.eclipse.ui.views.properties.PropertyDescriptor;
020: import org.eclipse.ui.views.properties.TextPropertyDescriptor;
021:
022: import com.vividsolutions.jts.geom.Coordinate;
023: import com.vividsolutions.jts.geom.Geometry;
024:
025: /**
026: * A PropertySource for an array of coordinates.
027: *
028: * @author jeichar
029: * @since 0.3
030: */
031: public class CoordinateSetPropertySource implements IPropertySource2 {
032: /**
033: * A validator that validates a coordinate entry
034: *
035: * @author jones
036: * @since 0.3
037: */
038: public class CoordinateValidator implements ICellEditorValidator {
039: GeometryBuilder builder = GeometryBuilder.create();
040:
041: /**
042: * @see org.eclipse.jface.viewers.ICellEditorValidator#isValid(java.lang.Object)
043: */
044: public String isValid(Object newValue) {
045: try {
046: Coordinate[] coords = parseString((String) newValue);
047: builder.createGeometry(geom.getClass(), coords);
048: } catch (Exception e) {
049: return e.getLocalizedMessage();
050: }
051: return null;
052: }
053:
054: }
055:
056: private Coordinate[] coords;
057: private Coordinate[] oldcoords;
058: IPropertyDescriptor[] descriptors;
059: protected Geometry geom;
060:
061: /**
062: * Construct <code>CoordinateSetPropertySource</code>.
063: *
064: * @param coords
065: * @param geom
066: */
067: public CoordinateSetPropertySource(Coordinate[] coords,
068: Geometry geom) {
069: this (coords);
070: this .geom = geom;
071: }
072:
073: /**
074: * Creates a new instance of CoordinatePropertySource
075: *
076: * @param coords
077: */
078: public CoordinateSetPropertySource(Coordinate[] coords) {
079: oldcoords = this .coords = coords;
080: }
081:
082: /**
083: * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
084: */
085: public Object getEditableValue() {
086:
087: Coordinate[] c = new Coordinate[coords.length];
088: System.arraycopy(coords, 0, c, 0, c.length);
089: return c;
090: }
091:
092: /**
093: * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
094: */
095: public IPropertyDescriptor[] getPropertyDescriptors() {
096: if (descriptors == null) {
097: if (geom != null) {
098: descriptors = new PropertyDescriptor[] { new TextPropertyDescriptor(
099: coords,
100: Messages.CoordinateSetPropertySource_coordinates) };
101: ((TextPropertyDescriptor) descriptors[0])
102: .setValidator(new CoordinateValidator());
103: } else
104: descriptors = new PropertyDescriptor[] { new PropertyDescriptor(
105: coords,
106: Messages.CoordinateSetPropertySource_coordinates) };
107: }
108:
109: IPropertyDescriptor[] c = new IPropertyDescriptor[descriptors.length];
110: System.arraycopy(descriptors, 0, c, 0, c.length);
111: return c;
112: }
113:
114: /**
115: * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
116: */
117: public Object getPropertyValue(Object id) {
118: return coordsToString(coords);
119: }
120:
121: private String coordsToString(Coordinate[] coords) {
122:
123: StringBuilder cBuffer = new StringBuilder();
124: for (int j = 0; j < coords.length; j++) {
125: if (j != 0)
126: cBuffer.append(", "); //$NON-NLS-1$
127: cBuffer.append("("); //$NON-NLS-1$
128: cBuffer.append(coords[j].x);
129: cBuffer.append(", "); //$NON-NLS-1$
130: cBuffer.append(coords[j].y);
131: if (!(Double.isNaN(coords[j].z) || Double
132: .isInfinite(coords[j].z))) {
133: cBuffer.append(", "); //$NON-NLS-1$
134: cBuffer.append(coords[j].z);
135: }
136: cBuffer.append(")"); //$NON-NLS-1$
137:
138: }
139: return cBuffer.toString();
140: }
141:
142: /**
143: * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
144: */
145: public boolean isPropertySet(Object id) {
146: return coords == oldcoords;
147: }
148:
149: /**
150: * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
151: */
152: public void resetPropertyValue(Object id) {
153: coords = oldcoords;
154: }
155:
156: /**
157: * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object,
158: * java.lang.Object)
159: */
160: public void setPropertyValue(Object id, Object value) {
161: coords = parseString((String) value);
162: }
163:
164: /**
165: * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertyResettable(java.lang.Object)
166: */
167: public boolean isPropertyResettable(Object id) {
168: return true;
169: }
170:
171: Coordinate[] parseString(String stringValue) {
172: String[] parts = stringValue.split(","); //$NON-NLS-1$
173: List<Coordinate> newCoords = new ArrayList<Coordinate>();
174: double[] points = new double[2];
175: int q = 0;
176: for (int i = 0; i < parts.length; i++) {
177: String part = parts[i].trim();
178: String[] values = part.split("\\s"); //$NON-NLS-1$
179: for (int j = 0; j < values.length; j++) {
180: String value = values[j].trim();
181: if (value.startsWith("(")) { //$NON-NLS-1$
182: points[q] = Double.parseDouble(value.substring(1));
183: } else if (value.endsWith(")")) { //$NON-NLS-1$
184: points[q] = Double.parseDouble(value.substring(0,
185: value.length() - 1));
186: } else {
187: points[q] = Double.parseDouble(value.substring(1));
188: }
189: q++;
190: if (q == 2) {
191: q = 0;
192: newCoords.add(new Coordinate(points[0], points[1]));
193: }
194: }
195: }
196: return (Coordinate[]) newCoords
197: .toArray(new Coordinate[newCoords.size()]);
198: }
199:
200: }
|