001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.catalog.internal.arcsde;
018:
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URI;
022: import java.net.URL;
023:
024: import net.refractions.udig.catalog.CatalogPlugin;
025: import net.refractions.udig.catalog.IGeoResource;
026: import net.refractions.udig.catalog.IGeoResourceInfo;
027: import net.refractions.udig.catalog.IResolveChangeEvent;
028: import net.refractions.udig.catalog.IResolveDelta;
029: import net.refractions.udig.catalog.IService;
030: import net.refractions.udig.catalog.arcsde.internal.Messages;
031: import net.refractions.udig.catalog.internal.CatalogImpl;
032: import net.refractions.udig.catalog.internal.ResolveChangeEvent;
033: import net.refractions.udig.catalog.internal.ResolveDelta;
034: import net.refractions.udig.ui.graphics.Glyph;
035:
036: import org.eclipse.core.runtime.IProgressMonitor;
037: import org.eclipse.core.runtime.IStatus;
038: import org.geotools.data.FeatureSource;
039: import org.geotools.data.FeatureStore;
040: import org.geotools.feature.Feature;
041: import org.geotools.feature.FeatureIterator;
042: import org.geotools.feature.FeatureType;
043: import org.geotools.geometry.jts.ReferencedEnvelope;
044: import org.opengis.referencing.crs.CoordinateReferenceSystem;
045:
046: import com.vividsolutions.jts.geom.Envelope;
047:
048: /**
049: * Provides ...TODO summary sentence
050: * <p>
051: * TODO Description
052: * </p>
053: *
054: * @author David Zwiers, Refractions Research
055: * @since 0.6
056: */
057: public class ArcGeoResource extends IGeoResource {
058: ArcServiceImpl parent;
059: String typename = null;
060:
061: private ArcGeoResource() {/* not for use */
062: }
063:
064: /**
065: * Construct <code>PostGISGeoResource</code>.
066: *
067: * @param parent
068: * @param typename
069: */
070: public ArcGeoResource(ArcServiceImpl parent, String typename) {
071: this .parent = parent;
072: this .typename = typename;
073: }
074:
075: public URL getIdentifier() {
076: try {
077: return new URL(parent.getIdentifier().toString()
078: + "#" + typename); //$NON-NLS-1$
079: } catch (MalformedURLException e) {
080: return parent.getIdentifier();
081: }
082: }
083:
084: /*
085: * @see net.refractions.udig.catalog.IGeoResource#getStatus()
086: */
087: public Status getStatus() {
088: return parent.getStatus();
089: }
090:
091: /*
092: * @see net.refractions.udig.catalog.IGeoResource#getStatusMessage()
093: */
094: public Throwable getMessage() {
095: return parent.getMessage();
096: }
097:
098: /*
099: * Required adaptions: <ul> <li>IGeoResourceInfo.class <li>IService.class </ul>
100: *
101: * @see net.refractions.udig.catalog.IResolve#resolve(java.lang.Class,
102: * org.eclipse.core.runtime.IProgressMonitor)
103: */
104: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
105: throws IOException {
106: if (adaptee == null)
107: return null;
108: if (adaptee.isAssignableFrom(IService.class))
109: return adaptee.cast(parent);
110: if (adaptee.isAssignableFrom(IGeoResource.class))
111: return adaptee.cast(this );
112: if (adaptee.isAssignableFrom(IGeoResourceInfo.class))
113: return adaptee.cast(getInfo(monitor));
114: if (adaptee.isAssignableFrom(FeatureStore.class)) {
115: FeatureSource fs = parent.getDS(monitor).getFeatureSource(
116: typename);
117: if (fs instanceof FeatureStore)
118: return adaptee.cast(fs);
119: if (adaptee.isAssignableFrom(FeatureSource.class))
120: return adaptee.cast(parent.getDS(null)
121: .getFeatureSource(typename));
122: }
123: return null;
124: }
125:
126: @Override
127: public IService service(IProgressMonitor monitor)
128: throws IOException {
129: return parent;
130: }
131:
132: /*
133: * @see net.refractions.udig.catalog.IResolve#canResolve(java.lang.Class)
134: */
135: public <T> boolean canResolve(Class<T> adaptee) {
136: if (adaptee == null)
137: return false;
138: return (adaptee.isAssignableFrom(IGeoResourceInfo.class)
139: || adaptee.isAssignableFrom(FeatureStore.class)
140: || adaptee.isAssignableFrom(FeatureSource.class) || adaptee
141: .isAssignableFrom(IService.class));
142: }
143:
144: private volatile IGeoResourceInfo info;
145:
146: public IGeoResourceInfo getInfo(IProgressMonitor monitor)
147: throws IOException {
148: if (info == null && getStatus() != Status.BROKEN) {
149: synchronized (parent.getDS(monitor)) {
150: if (info == null) {
151: info = new IGeoResourceShpInfo();
152: }
153: }
154: IResolveDelta delta = new ResolveDelta(this ,
155: IResolveDelta.Kind.CHANGED);
156: ((CatalogImpl) CatalogPlugin.getDefault().getLocalCatalog())
157: .fire(new ResolveChangeEvent(this ,
158: IResolveChangeEvent.Type.POST_CHANGE, delta));
159: }
160: return info;
161: }
162:
163: class IGeoResourceShpInfo extends IGeoResourceInfo {
164:
165: private FeatureType ft = null;
166:
167: IGeoResourceShpInfo() throws IOException {
168: ft = parent.getDS(null).getSchema(typename);
169:
170: try {
171: FeatureSource source = parent.getDS(null)
172: .getFeatureSource(typename);
173: bounds = new ReferencedEnvelope(source.getBounds(),
174: getCRS());
175: if (bounds == null) {
176: bounds = new ReferencedEnvelope(new Envelope(),
177: source.getSchema().getDefaultGeometry()
178: .getCoordinateSystem());
179: FeatureIterator iter = source.getFeatures()
180: .features();
181: try {
182: while (iter.hasNext()) {
183: Feature element = iter.next();
184: if (bounds.isNull())
185: bounds.init(element.getBounds());
186: else
187: bounds.expandToInclude(element
188: .getBounds());
189: }
190: } finally {
191: iter.close();
192: }
193: }
194: } catch (Exception e) {
195: CatalogPlugin
196: .getDefault()
197: .getLog()
198: .log(
199: new org.eclipse.core.runtime.Status(
200: IStatus.WARNING,
201: "net.refractions.udig.catalog", 0, Messages.ArcGeoResource_error_layer_bounds, e)); //$NON-NLS-1$
202: bounds = new ReferencedEnvelope(new Envelope(), null);
203: }
204:
205: icon = Glyph.icon(ft);
206: keywords = new String[] { "postgis", //$NON-NLS-1$
207: ft.getTypeName(), ft.getNamespace().toString() };
208: }
209:
210: public CoordinateReferenceSystem getCRS() {
211: return ft.getDefaultGeometry().getCoordinateSystem();
212: }
213:
214: public String getName() {
215: return ft.getTypeName();
216: }
217:
218: public URI getSchema() {
219: return ft.getNamespace();
220: }
221:
222: public String getTitle() {
223: return ft.getTypeName();
224: }
225: }
226: }
|