001: package net.refractions.udig.catalog.util;
002:
003: import java.io.IOException;
004: import java.io.Serializable;
005: import java.net.MalformedURLException;
006: import java.net.URI;
007: import java.net.URISyntaxException;
008: import java.net.URL;
009: import java.util.ArrayList;
010: import java.util.List;
011: import java.util.Map;
012:
013: import net.refractions.udig.catalog.CatalogPlugin;
014: import net.refractions.udig.catalog.ICatalog;
015: import net.refractions.udig.catalog.IGeoResource;
016: import net.refractions.udig.catalog.IGeoResourceInfo;
017: import net.refractions.udig.catalog.IService;
018: import net.refractions.udig.catalog.IServiceInfo;
019: import net.refractions.udig.catalog.IResolve.Status;
020:
021: import org.eclipse.core.runtime.IProgressMonitor;
022: import org.eclipse.core.runtime.NullProgressMonitor;
023: import org.eclipse.jface.resource.ImageDescriptor;
024: import org.geotools.catalog.Catalog;
025: import org.geotools.catalog.CatalogInfo;
026: import org.geotools.catalog.GeoResource;
027: import org.geotools.catalog.GeoResourceInfo;
028: import org.geotools.catalog.Resolve;
029: import org.geotools.catalog.ResolveChangeEvent;
030: import org.geotools.catalog.ResolveChangeListener;
031: import org.geotools.catalog.Service;
032: import org.geotools.catalog.ServiceInfo;
033: import org.geotools.geometry.jts.ReferencedEnvelope;
034: import org.geotools.util.ProgressListener;
035: import org.opengis.referencing.crs.CoordinateReferenceSystem;
036:
037: import com.vividsolutions.jts.geom.Envelope;
038:
039: /**
040: * This class provides support for GeoTools Java 1.4 catalog interfaces.
041: * <p>
042: * This factory produces Java 5 wrappers around base GeoTools constructs.
043: *
044: * @author Jody Garnett
045: */
046: public class GeoToolsAdapters {
047: static Catalog localWrapper;
048:
049: /**
050: * Serves up the local catalog as a GeoTools Catalog.
051: * <p>
052: * This is helpful when creating many of the geotools service
053: * instances (who often want to provide event notification).
054: *
055: * @return Catalog
056: */
057: static synchronized public Catalog getLocalCatalog() {
058: if (localWrapper != null) {
059: return localWrapper;
060: }
061: localWrapper = new Catalog() {
062:
063: private ICatalog local = CatalogPlugin.getDefault()
064: .getLocalCatalog();
065:
066: public void add(Service service)
067: throws UnsupportedOperationException {
068: local.add(GeoToolsAdapters.service(service));
069: }
070:
071: public List find(URI arg0, ProgressListener arg1) {
072: return null;
073: }
074:
075: public List findService(URI arg0, ProgressListener arg1) {
076: return null;
077: }
078:
079: public CatalogInfo getInfo(ProgressListener arg0)
080: throws IOException {
081: return null;
082: }
083:
084: public void remove(Service service)
085: throws UnsupportedOperationException {
086:
087: }
088:
089: public void replace(URI arg0, Service arg1)
090: throws UnsupportedOperationException {
091:
092: }
093:
094: public Object resolve(Class adaptee,
095: ProgressListener monitor) throws IOException {
096: return local.resolve(adaptee, progress(monitor));
097: }
098:
099: public List search(String arg0, Envelope arg1,
100: ProgressListener arg2) throws IOException {
101: return null;
102: }
103:
104: public void addListener(ResolveChangeListener listener)
105: throws UnsupportedOperationException {
106: }
107:
108: public boolean canResolve(Class adaptee) {
109: return local.canResolve(adaptee);
110: }
111:
112: public void fire(ResolveChangeEvent event) {
113: }
114:
115: public URI getIdentifier() {
116: try {
117: return local.getIdentifier().toURI();
118: } catch (URISyntaxException e) {
119: throw new RuntimeException(e);
120: }
121: }
122:
123: public Throwable getMessage() {
124: return local.getMessage();
125: }
126:
127: public Status getStatus() {
128: return status(local.getStatus());
129: }
130:
131: public List members(ProgressListener arg0)
132: throws IOException {
133: return null;
134: }
135:
136: public Resolve parent(ProgressListener monitor)
137: throws IOException {
138: return null;
139: }
140:
141: public void removeListener(ResolveChangeListener arg0) {
142: }
143:
144: };
145: return localWrapper;
146: }
147:
148: static public List<IGeoResource> resourceList(
149: List<GeoResource> resourceList) {
150: List<IGeoResource> list = new ArrayList<IGeoResource>();
151: for (GeoResource handle : resourceList) {
152: list.add(resource(handle));
153: }
154: return list;
155: }
156:
157: static public IGeoResource resource(final GeoResource resource) {
158: final URL url;
159: try {
160: url = resource.getIdentifier().toURL();
161: } catch (MalformedURLException e) {
162: return null;
163: }
164: return new IGeoResource() {
165: @Override
166: public URL getIdentifier() {
167: return url;
168: }
169:
170: @Override
171: public <T> T resolve(Class<T> adaptee,
172: IProgressMonitor monitor) throws IOException {
173: if (monitor == null)
174: monitor = new NullProgressMonitor();
175:
176: if (adaptee == null) {
177: throw new NullPointerException(
178: "No adaptor specified"); //$NON-NLS-1$
179: }
180: if (resource.canResolve(adaptee)) {
181: return adaptee.cast(resource.resolve(adaptee,
182: progress(monitor)));
183: }
184: return super .resolve(adaptee, monitor);
185:
186: }
187:
188: public <T> boolean canResolve(Class<T> adaptee) {
189: return resource.canResolve(adaptee)
190: || super .canResolve(adaptee);
191: }
192:
193: public IGeoResourceInfo getInfo(IProgressMonitor monitor)
194: throws IOException {
195: return info(resource.getInfo(progress(monitor)));
196: }
197:
198: public IService service(IProgressMonitor monitor)
199: throws IOException {
200: return GeoToolsAdapters.service((Service) resource
201: .parent(progress(monitor)));
202: }
203:
204: public Throwable getMessage() {
205: return resource.getMessage();
206: }
207:
208: public Status getStatus() {
209: return status(resource.getStatus());
210: }
211: };
212: }
213:
214: static public IGeoResourceInfo info(final GeoResourceInfo info) {
215: return new IGeoResourceInfo() {
216: @Override
217: public ReferencedEnvelope getBounds() {
218: return (ReferencedEnvelope) info.getBounds();
219: }
220:
221: @Override
222: public CoordinateReferenceSystem getCRS() {
223: return info.getCRS();
224: }
225:
226: @Override
227: public String getDescription() {
228: return info.getDescription();
229: }
230:
231: @Override
232: public ImageDescriptor getIcon() {
233: // need to pain super.getIcon() into a graphic
234: return null;
235: }
236:
237: @Override
238: public String[] getKeywords() {
239: return info.getKeywords();
240: }
241:
242: @Override
243: public String getName() {
244: return info.getName();
245: }
246:
247: @Override
248: public URI getSchema() {
249: return info.getSchema();
250: }
251:
252: @Override
253: public String getTitle() {
254: return info.getTitle();
255: }
256: };
257: }
258:
259: static public IServiceInfo info(final ServiceInfo info) {
260: return new IServiceInfo() {
261: public String getAbstract() {
262: return info.getAbstract();
263: }
264:
265: public String getDescription() {
266: return info.getDescription();
267: }
268:
269: public ImageDescriptor getIcon() {
270: // we need to paint info.getIcon();
271: return null;
272: }
273:
274: public String[] getKeywords() {
275: return info.getKeywords();
276: }
277:
278: public URL getPublisher() {
279: return super .getPublisher();
280: }
281:
282: public URI getSchema() {
283: return info.getSchema();
284: }
285:
286: public URL getSource() {
287: try {
288: return info.getSource().toURL();
289: } catch (MalformedURLException e) {
290: return null;
291: }
292: }
293:
294: public String getTitle() {
295: return info.getTitle();
296: }
297: };
298: }
299:
300: static public IService service(final Service service) {
301: final URL url;
302: try {
303: url = service.getIdentifier().toURL();
304: } catch (MalformedURLException e) {
305: return null;
306: }
307: return new IService() {
308: @SuppressWarnings("unchecked")
309: public Map<String, Serializable> getConnectionParams() {
310: return service.getConnectionParams();
311: }
312:
313: @SuppressWarnings("unchecked")
314: @Override
315: public List<? extends IGeoResource> members(
316: IProgressMonitor monitor) throws IOException {
317: return resourceList(service.members(progress(monitor)));
318: }
319:
320: @Override
321: public <T> T resolve(Class<T> adaptee,
322: IProgressMonitor monitor) throws IOException {
323: if (monitor == null)
324: monitor = new NullProgressMonitor();
325:
326: if (adaptee == null) {
327: throw new NullPointerException(
328: "No adaptor specified"); //$NON-NLS-1$
329: }
330: if (service.canResolve(adaptee)) {
331: return adaptee.cast(service.resolve(adaptee,
332: progress(monitor)));
333: }
334: return super .resolve(adaptee, monitor);
335: }
336:
337: public <T> boolean canResolve(Class<T> adaptee) {
338: return service.canResolve(adaptee)
339: || super .canResolve(adaptee);
340: }
341:
342: @Override
343: public IServiceInfo getInfo(IProgressMonitor monitor)
344: throws IOException {
345: return info(service.getInfo(progress(monitor)));
346: }
347:
348: public URL getIdentifier() {
349: return url;
350: }
351:
352: public Throwable getMessage() {
353: return service.getMessage();
354: }
355:
356: public Status getStatus() {
357: return status(service.getStatus());
358: }
359:
360: public void dispose(IProgressMonitor monitor) {
361: }
362: };
363: }
364:
365: static public Status status(
366: org.geotools.catalog.Resolve.Status status) {
367: if (status == null)
368: return null;
369: if (status == org.geotools.catalog.Resolve.Status.BROKEN)
370: return Status.BROKEN;
371: if (status == org.geotools.catalog.Resolve.Status.CONNECTED)
372: return Status.CONNECTED;
373: if (status == org.geotools.catalog.Resolve.Status.NOTCONNECTED)
374: return Status.NOTCONNECTED;
375: return Status.NOTCONNECTED;
376: }
377:
378: static public org.geotools.catalog.Resolve.Status status(
379: Status status) {
380:
381: if (status == Status.BROKEN)
382: return org.geotools.catalog.Resolve.Status.BROKEN;
383: if (status == Status.CONNECTED)
384: return org.geotools.catalog.Resolve.Status.CONNECTED;
385: if (status == Status.NOTCONNECTED)
386: return org.geotools.catalog.Resolve.Status.NOTCONNECTED;
387: return null;
388: }
389:
390: static public ProgressListener progress(
391: final IProgressMonitor monitor) {
392: if (monitor == null)
393: return null;
394: return new ProgressListener() {
395: private String description;
396: private int progress;
397:
398: public void complete() {
399: monitor.done();
400: }
401:
402: public void dispose() {
403: description = null;
404: }
405:
406: public void exceptionOccurred(Throwable arg0) {
407: }
408:
409: public String getDescription() {
410: return description;
411: }
412:
413: public boolean isCanceled() {
414: return monitor.isCanceled();
415: }
416:
417: public void progress(float amount) {
418: int current = (int) (100.0 * amount);
419: monitor.worked(current - progress);
420: progress = current;
421: }
422:
423: public void setCanceled(boolean arg0) {
424: monitor.setCanceled(true);
425: }
426:
427: public void setDescription(String text) {
428: description = text;
429: }
430:
431: public void started() {
432: monitor.beginTask(description, 100);
433: }
434:
435: public void warningOccurred(String arg0, String arg1,
436: String arg2) {
437: }
438: };
439: }
440:
441: static public IProgressMonitor progress(
442: final ProgressListener monitor) {
443: if (monitor == null)
444: return null;
445: return new IProgressMonitor() {
446: int total;
447: int amount;
448:
449: public void beginTask(String name, int totalWork) {
450: amount = 0;
451: total = totalWork;
452: monitor.setDescription(name);
453: monitor.progress(work());
454: }
455:
456: float work() {
457: return (float) amount / (float) total;
458: }
459:
460: public void done() {
461: amount = total;
462: monitor.complete();
463: monitor.dispose();
464: }
465:
466: public void internalWorked(double work) {
467: }
468:
469: public boolean isCanceled() {
470: return monitor.isCanceled();
471: }
472:
473: public void setCanceled(boolean cancel) {
474: monitor.setCanceled(cancel);
475: }
476:
477: public void setTaskName(String name) {
478: monitor.setDescription(name);
479: }
480:
481: public void subTask(String name) {
482: monitor.setDescription(name);
483: }
484:
485: public void worked(int work) {
486: amount += total;
487: }
488: };
489: }
490: }
|