001: package net.refractions.udig.mapgraphic.internal;
002:
003: import java.io.IOException;
004: import java.net.MalformedURLException;
005: import java.net.URL;
006: import java.text.MessageFormat;
007:
008: import net.refractions.udig.catalog.IGeoResource;
009: import net.refractions.udig.catalog.IGeoResourceInfo;
010: import net.refractions.udig.catalog.IService;
011: import net.refractions.udig.mapgraphic.MapGraphic;
012: import net.refractions.udig.mapgraphic.MapGraphicFactory;
013:
014: import org.eclipse.core.runtime.IConfigurationElement;
015: import org.eclipse.core.runtime.IProgressMonitor;
016: import org.eclipse.ui.plugin.AbstractUIPlugin;
017: import org.geotools.geometry.jts.ReferencedEnvelope;
018: import org.geotools.referencing.crs.DefaultEngineeringCRS;
019: import org.geotools.referencing.crs.DefaultGeographicCRS;
020:
021: import com.vividsolutions.jts.geom.Envelope;
022:
023: public class MapGraphicResource extends IGeoResource {
024:
025: /** the service which contains all decorator resources **/
026: private final MapGraphicService parent;
027:
028: /** the info for the map graphic resource **/
029: private volatile MapGraphicResourceInfo info;
030:
031: /** the map graphic itself **/
032: private volatile MapGraphic mapGraphic;
033:
034: /** the map graphic name **/
035: private String name;
036:
037: /** the map graphic id **/
038: private final String id;
039:
040: private IConfigurationElement element;
041:
042: MapGraphicResource(MapGraphicService service,
043: IConfigurationElement element) {
044: parent = service;
045: this .name = element.getAttribute("name"); //$NON-NLS-1$
046: this .id = element.getAttribute("id"); //$NON-NLS-1$
047: this .element = element;
048: mapGraphic = MapGraphicFactory.getInstance().createMapGraphic(
049: id);
050: }
051:
052: /*
053: * @see net.refractions.udig.catalog.IGeoResource#resolve(java.lang.Class, org.eclipse.core.runtime.IProgressMonitor)
054: */
055: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
056: throws IOException {
057: if (adaptee == null)
058: return null;
059:
060: // if (adaptee.isAssignableFrom(IService.class)) {
061: // return adaptee.cast(parent);
062: // }
063: if (adaptee.isAssignableFrom(IGeoResourceInfo.class)) {
064: return adaptee.cast(getInfo(monitor));
065: }
066: //TODO: this is not safe, the caller of this method might not have
067: // the MapGraphicClasses on their classpath
068: if (adaptee.isAssignableFrom(MapGraphic.class)) {
069: return adaptee.cast(mapGraphic);
070: }
071: if (adaptee.isAssignableFrom(MapGraphicFactory.class)) {
072: return adaptee.cast(MapGraphicFactory.getInstance());
073: }
074:
075: return super .resolve(adaptee, monitor);
076: }
077:
078: public IService service(IProgressMonitor monitor)
079: throws IOException {
080: return parent;
081: }
082:
083: public IGeoResourceInfo getInfo(IProgressMonitor monitor)
084: throws IOException {
085: if (info == null) {
086: info = new MapGraphicResourceInfo(element);
087: }
088: return info;
089: }
090:
091: /*
092: * @see net.refractions.udig.catalog.IResolve#canResolve(java.lang.Class)
093: */
094: public <T> boolean canResolve(Class<T> adaptee) {
095:
096: return adaptee != null
097: && (adaptee.isAssignableFrom(element.getClass())
098: || adaptee.isAssignableFrom(IService.class)
099: || adaptee.isAssignableFrom(IGeoResource.class)
100: ||
101: //TODO: this is not safe, the caller of this method might not have
102: // the MapGraphicClasses on their classpath
103: adaptee.isAssignableFrom(MapGraphic.class)
104: || adaptee
105: .isAssignableFrom(MapGraphicFactory.class) || (mapGraphic != null && adaptee
106: .isAssignableFrom(mapGraphic.getClass())))
107: || super .canResolve(adaptee);
108: }
109:
110: /*
111: * @see net.refractions.udig.catalog.IResolve#getStatus()
112: */
113: public Status getStatus() {
114: return parent.getStatus();
115: }
116:
117: /*
118: * @see net.refractions.udig.catalog.IResolve#getMessage()
119: */
120: public Throwable getMessage() {
121: return parent.getMessage();
122: }
123:
124: /*
125: * @see net.refractions.udig.catalog.IResolve#getIdentifier()
126: */
127: public URL getIdentifier() {
128: try {
129: return new URL(parent.getIdentifier().toString() + "#" + id); //$NON-NLS-1$
130: } catch (MalformedURLException e) {
131: e.printStackTrace();
132: }
133:
134: return null;
135: }
136:
137: class MapGraphicResourceInfo extends IGeoResourceInfo {
138:
139: public MapGraphicResourceInfo(IConfigurationElement element) {
140: String iconPath = element.getAttribute("icon"); //$NON-NLS-1$
141: if (iconPath != null && iconPath.length() > 0)
142: this .icon = AbstractUIPlugin.imageDescriptorFromPlugin(
143: element.getNamespaceIdentifier(), iconPath);
144: }
145:
146: /*
147: * @see net.refractions.udig.catalog.IGeoResourceInfo#getName()
148: */
149: @Override
150: public String getName() {
151: return MapGraphicResource.this .name;
152: }
153:
154: @Override
155: public String getTitle() {
156: return getName();
157: }
158:
159: @Override
160: public String getDescription() {
161: MessageFormat formatter = new MessageFormat(
162: Messages.MapGraphicResource_description);
163: return formatter.format(name);
164: }
165:
166: @Override
167: public ReferencedEnvelope getBounds() {
168: if (bounds == null) {
169: Envelope e = new Envelope();
170: e.setToNull();
171:
172: bounds = new ReferencedEnvelope(e,
173: DefaultGeographicCRS.WGS84);
174: }
175: return bounds;
176: }
177: }
178:
179: }
|