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 net.refractions.udig.core.internal.GeometryBuilder;
011: import net.refractions.udig.project.ui.internal.Messages;
012:
013: import org.eclipse.jface.viewers.ILabelProvider;
014: import org.eclipse.jface.viewers.LabelProvider;
015: import org.eclipse.ui.views.properties.IPropertyDescriptor;
016: import org.eclipse.ui.views.properties.IPropertySource2;
017: import org.eclipse.ui.views.properties.PropertyDescriptor;
018:
019: import com.vividsolutions.jts.geom.Coordinate;
020: import com.vividsolutions.jts.geom.Geometry;
021: import com.vividsolutions.jts.geom.GeometryCollection;
022: import com.vividsolutions.jts.geom.LinearRing;
023: import com.vividsolutions.jts.geom.Polygon;
024:
025: /**
026: * TODO provide type description
027: *
028: * @author jeichar
029: * @since TODO provide version
030: */
031: public class GeomPropertySource implements IPropertySource2 {
032: private static final String TYPE = "TYPE"; //$NON-NLS-1$
033: private static final String AREA = "AREA"; //$NON-NLS-1$
034: private static final String COORDS = "COORDS"; //$NON-NLS-1$
035:
036: private Geometry geom;
037: GeometryCollection collection;
038: IPropertyDescriptor[] propertyDescriptors;
039: GeometryBuilder builder = GeometryBuilder.create();
040: private static final String SHELL = "SHELL"; //$NON-NLS-1$
041: private static final String HOLES = "HOLES"; //$NON-NLS-1$
042: private static final String COLLECTION = "COLLECTION"; //$NON-NLS-1$
043:
044: /**
045: * Creates a new instance of GeomPropertySource
046: *
047: * @param geometry the geometry that is the source for this PropertySource
048: */
049: public GeomPropertySource(Geometry geometry) {
050: this .geom = geometry;
051: if (geometry instanceof GeometryCollection) {
052: collection = (GeometryCollection) geometry;
053: createGeometryCollectionDescriptors();
054: } else if (geometry instanceof Polygon) {
055: createPolygonDescriptors();
056: } else {
057: createGeometryDescriptors();
058: }
059: }
060:
061: /**
062: * TODO summary sentence for createPolygonDescriptors ...
063: */
064: private void createPolygonDescriptors() {
065: Polygon poly = (Polygon) geom;
066: propertyDescriptors = new IPropertyDescriptor[poly
067: .getNumInteriorRing() + 2];
068: propertyDescriptors[0] = new PropertyDescriptor(new ID(AREA),
069: Messages.GeomPropertySource_area);
070: propertyDescriptors[1] = new GeometryPropertyDescriptor(new ID(
071: SHELL), Messages.GeomPropertySource_exteriorShell);
072: for (int i = 0; i < propertyDescriptors.length - 2; i++) {
073: propertyDescriptors[i + 2] = new GeometryPropertyDescriptor(
074: new ID(HOLES, i),
075: Messages.GeomPropertySource_polygonHole);
076: }
077: }
078:
079: private void createGeometryDescriptors() {
080: IPropertyDescriptor coords = new PropertyDescriptor(new ID(
081: COORDS), Messages.GeomPropertySource_coordinates) {
082: /**
083: * @see org.eclipse.ui.views.properties.PropertyDescriptor#getLabelProvider()
084: */
085: public ILabelProvider getLabelProvider() {
086: return new LabelProvider() {
087: /**
088: * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
089: */
090: public String getText(Object element) {
091: return ""; //$NON-NLS-1$
092: }
093: };
094: }
095: };
096: propertyDescriptors = new IPropertyDescriptor[] {
097: new PropertyDescriptor(new ID(AREA),
098: Messages.GeomPropertySource_area), coords };
099: }
100:
101: void createGeometryCollectionDescriptors() {
102: propertyDescriptors = new IPropertyDescriptor[collection
103: .getNumGeometries() + 1];
104: propertyDescriptors[0] = new PropertyDescriptor(new ID(AREA),
105: Messages.GeomPropertySource_area);
106: for (int i = 0; i < propertyDescriptors.length - 1; i++) {
107: Geometry geometry = collection.getGeometryN(i);
108: propertyDescriptors[i + 1] = new GeometryPropertyDescriptor(
109: new ID(COLLECTION, i, collection.getGeometryN(i)),
110: geometry.getGeometryType());
111: }
112: }
113:
114: /**
115: * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
116: */
117: public Object getEditableValue() {
118: return geom;
119: }
120:
121: /**
122: * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
123: */
124: public IPropertyDescriptor[] getPropertyDescriptors() {
125:
126: IPropertyDescriptor[] c = new IPropertyDescriptor[propertyDescriptors.length];
127: System.arraycopy(propertyDescriptors, 0, c, 0, c.length);
128: return c;
129: }
130:
131: /**
132: * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
133: */
134: public Object getPropertyValue(Object idObject) {
135: ID id = (ID) idObject;
136: if (id.id == COLLECTION) {
137: return new GeomPropertySource(collection
138: .getGeometryN(id.index));
139: }
140: if (id.id == TYPE)
141: return geom.getGeometryType();
142: if (id.id == AREA)
143: return String.valueOf(geom.getArea());
144: if (id.id == COORDS) {
145: return new CoordinateSetPropertySource(geom
146: .getCoordinates(), geom);
147: }
148: if (id.id == HOLES) {
149: Polygon poly = (Polygon) geom;
150: return new GeomPropertySource(poly
151: .getInteriorRingN(id.index));
152: }
153: if (id.id == SHELL) {
154: Polygon poly = (Polygon) geom;
155: return new GeomPropertySource(poly.getExteriorRing());
156: }
157: return null;
158: }
159:
160: /**
161: * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
162: */
163: public boolean isPropertySet(Object id) {
164: // TODO Auto-generated method stub
165: return false;
166: }
167:
168: /**
169: * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
170: */
171: public void resetPropertyValue(Object id) {
172: // TODO Auto-generated method stub
173: }
174:
175: /**
176: * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object,
177: * java.lang.Object)
178: */
179: public void setPropertyValue(Object idObject, Object value) {
180: ID id = (ID) idObject;
181: if (id.id == COORDS) {
182: geom = builder.safeCreateGeometry(geom.getClass(),
183: (Coordinate[]) value);
184: }
185: if (id.id == SHELL) {
186: Polygon polygon = (Polygon) geom;
187: LinearRing[] rings = new LinearRing[polygon
188: .getNumInteriorRing()];
189: for (int i = 0; i < rings.length; i++) {
190: rings[i] = (LinearRing) polygon.getInteriorRingN(i);
191: }
192: geom = builder.factory.createPolygon((LinearRing) value,
193: rings);
194: }
195: if (id.id == HOLES) {
196: Polygon polygon = (Polygon) geom;
197: LinearRing[] rings = new LinearRing[polygon
198: .getNumInteriorRing()];
199: for (int i = 0; i < rings.length; i++) {
200: rings[i] = (LinearRing) polygon.getInteriorRingN(i);
201: }
202: rings[id.index] = (LinearRing) value;
203: geom = builder.factory.createPolygon((LinearRing) polygon
204: .getExteriorRing(), rings);
205: }
206: if (id.id == COLLECTION) {
207: Geometry[] geoms = new Geometry[collection
208: .getNumGeometries()];
209: for (int i = 0; i < geoms.length; i++) {
210: geoms[i] = collection.getGeometryN(i);
211: }
212: geoms[id.index] = (Geometry) value;
213: geom = collection = builder.factory
214: .createGeometryCollection(geoms);
215: }
216: }
217:
218: static class ID {
219: int index;
220: String id;
221:
222: ID(String id, int index, Object data) {
223: this .index = index;
224: this .id = id;
225: }
226:
227: ID(String id, int index) {
228: this (id, index, null);
229: }
230:
231: ID(String id) {
232: this (id, 0, null);
233: }
234: }
235:
236: /**
237: * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertyResettable(java.lang.Object)
238: */
239: public boolean isPropertyResettable(Object id) {
240: // TODO implement method body
241: return true;
242: }
243: }
|