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.project.ui.internal.tool.display;
016:
017: import java.net.URL;
018: import java.util.Set;
019: import java.util.concurrent.CopyOnWriteArraySet;
020: import java.util.concurrent.atomic.AtomicBoolean;
021:
022: import net.refractions.udig.catalog.CatalogPlugin;
023: import net.refractions.udig.catalog.IGeoResource;
024: import net.refractions.udig.catalog.objectproperty.ObjectPropertyCatalogListener;
025: import net.refractions.udig.project.ILayer;
026: import net.refractions.udig.ui.operations.AbstractPropertyValue;
027: import net.refractions.udig.ui.operations.PropertyValue;
028:
029: import org.geotools.data.FeatureStore;
030: import org.geotools.feature.FeatureType;
031:
032: import com.vividsolutions.jts.geom.Geometry;
033: import com.vividsolutions.jts.geom.GeometryCollection;
034: import com.vividsolutions.jts.geom.LineString;
035: import com.vividsolutions.jts.geom.LinearRing;
036: import com.vividsolutions.jts.geom.MultiLineString;
037: import com.vividsolutions.jts.geom.MultiPoint;
038: import com.vividsolutions.jts.geom.MultiPolygon;
039: import com.vividsolutions.jts.geom.Point;
040: import com.vividsolutions.jts.geom.Polygon;
041:
042: /**
043: * Returns true if the value is a subclass of the layer's schema's default geometry. If the layer
044: * does not have a schema false is returned.
045: * <p>
046: * The value must be a Geometry class fully qualified (com.vividsolutions.jts.geom.Geometry) unless
047: * it is one of the following abbreviations (case is unimportant):
048: * <ul>
049: * <li>Geometry = com.vividsolutions.jts.geom.Geometry</li>
050: * <li>Polygon = com.vividsolutions.jts.geom.Polygon</li>
051: * <li>Point = com.vividsolutions.jts.geom.Point</li>
052: * <li>MultiPolygon = com.vividsolutions.jts.geom.MultiPolygon</li>
053: * <li>MultiPoint = com.vividsolutions.jts.geom.MultiPoint</li>
054: * <li>MultiLineString, MultiLine = com.vividsolutions.jts.geom.MultiLineString</li>
055: * <li>LinearRing = com.vividsolutions.jts.geom.LinearRing</li>
056: * <li>Line, LineString = com.vividsolutions.jts.geom.LineString</li>
057: * <li>GeometryCollection = com.vividsolutions.jts.geom.GeometryCollection</li>
058: * </ul>
059: * </p>
060: *
061: * Note: If the object passed into isTrue is not the editLayer, and the editLayer
062: * is locked, the editLayer will be used in place of the passed in layer.
063: *
064: * TODO change this class name to EditGeometryProperty or something similar
065: *
066: * @author jones
067: * @since 1.1.0
068: */
069: public class GeometryProperty extends AbstractPropertyValue<ILayer>
070: implements PropertyValue<ILayer> {
071:
072: Set<URL> ids = new CopyOnWriteArraySet<URL>();
073: private volatile AtomicBoolean isEvaluating = new AtomicBoolean(
074: false);
075:
076: @SuppressWarnings("unchecked")
077: public synchronized boolean isTrue(ILayer object, String value) {
078: isEvaluating.set(true);
079:
080: if (object.getMap() != null
081: && object.getMap().getEditManager().isEditLayerLocked()) {
082: object = object.getMap().getEditManager().getEditLayer();
083: }
084:
085: try {
086: if (ids.add(object.getID())) {
087: IGeoResource resource = object
088: .findGeoResource(FeatureStore.class);
089: if (resource != null)
090: CatalogPlugin.getDefault().getLocalCatalog()
091: .addCatalogListener(
092: new ObjectPropertyCatalogListener(
093: object, resource,
094: isEvaluating, this ));
095: }
096: FeatureType schema = object.getSchema();
097: if (schema == null || schema.getDefaultGeometry() == null)
098: return false;
099:
100: try {
101:
102: Class<? extends Object> declared = parseValue(value);
103: Class<? extends Object> type = schema
104: .getDefaultGeometry().getType();
105: return type.isAssignableFrom(declared);
106: } catch (ClassNotFoundException e) {
107: return false;
108: }
109: } finally {
110: isEvaluating.set(false);
111: }
112: }
113:
114: private Class<? extends Object> parseValue(String value)
115: throws ClassNotFoundException {
116: Class<? extends Object> result = null;
117:
118: if (value.equalsIgnoreCase("geometry")) //$NON-NLS-1$
119: result = Geometry.class;
120: else if (value.equalsIgnoreCase("polygon")) //$NON-NLS-1$
121: result = Polygon.class;
122: else if (value.equalsIgnoreCase("line") || value.equalsIgnoreCase("linestring")) //$NON-NLS-1$ //$NON-NLS-2$
123: result = LineString.class;
124: else if (value.equalsIgnoreCase("point")) //$NON-NLS-1$
125: result = Point.class;
126: else if (value.equalsIgnoreCase("multipoint")) //$NON-NLS-1$
127: result = MultiPoint.class;
128: else if (value.equalsIgnoreCase("multipolygon")) //$NON-NLS-1$
129: result = MultiPolygon.class;
130: else if (value.equalsIgnoreCase("linearring")) //$NON-NLS-1$
131: result = LinearRing.class;
132: else if (value.equalsIgnoreCase("multilinestring") || value.equalsIgnoreCase("multiline")) //$NON-NLS-1$ //$NON-NLS-2$
133: result = MultiLineString.class;
134: else if (value.equalsIgnoreCase("geometrycollection")) //$NON-NLS-1$
135: result = GeometryCollection.class;
136:
137: if (result == null)
138: result = Class.forName(value);
139: return result;
140: }
141:
142: public boolean canCacheResult() {
143: return true;
144: }
145:
146: public boolean isBlocking() {
147: return true;
148: }
149: }
|