001: package net.refractions.udig.mapgraphic.internal;
002:
003: import java.io.IOException;
004: import java.io.Serializable;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007: import java.util.ArrayList;
008: import java.util.HashMap;
009: import java.util.List;
010: import java.util.Map;
011:
012: import net.refractions.udig.catalog.IGeoResource;
013: import net.refractions.udig.catalog.IService;
014: import net.refractions.udig.catalog.IServiceInfo;
015: import net.refractions.udig.catalog.ITransientResolve;
016: import net.refractions.udig.mapgraphic.MapGraphicFactory;
017:
018: import org.eclipse.core.runtime.IConfigurationElement;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.core.runtime.NullProgressMonitor;
021:
022: /**
023: * Service implementation for map graphics
024: */
025: public class MapGraphicService extends IService {
026:
027: /** Dummy url for a MapGraphic */
028: public final static URL SERVICE_URL;
029: static {
030: URL tmp;
031: try {
032: tmp = new URL("file:///localhost/mapgraphic"); //$NON-NLS-1$
033: } catch (MalformedURLException e) {
034: tmp = null;
035: e.printStackTrace();
036: }
037: SERVICE_URL = tmp;
038: }
039:
040: /** MapGraphic resource children * */
041: private volatile List<MapGraphicResource> members;
042:
043: /** info object * */
044: private MapGraphicServiceInfo info;
045:
046: /**
047: * Construct <code>MapGraphicService</code>.
048: */
049: public MapGraphicService() {
050: // NoOp
051: }
052:
053: /*
054: * @see net.refractions.udig.catalog.IService#resolve(java.lang.Class,
055: * org.eclipse.core.runtime.IProgressMonitor)
056: */
057: @Override
058: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
059: throws IOException {
060: if (monitor == null)
061: monitor = new NullProgressMonitor();
062:
063: if (adaptee == null) {
064: throw new NullPointerException("No adaptor specified"); //$NON-NLS-1$
065: }
066: if (adaptee.isAssignableFrom(MapGraphicFactory.class)) {
067: return adaptee.cast(MapGraphicFactory.getInstance());
068: }
069: if (adaptee.isAssignableFrom(ITransientResolve.class)) {
070: return adaptee.cast(this );
071: }
072: return super .resolve(adaptee, monitor);
073: }
074:
075: @Override
076: public synchronized IServiceInfo getInfo(IProgressMonitor monitor)
077: throws IOException {
078: if (info == null) {
079: info = new MapGraphicServiceInfo();
080: }
081: return info;
082: }
083:
084: /*
085: * @see net.refractions.udig.catalog.IService#members(org.eclipse.core.runtime.IProgressMonitor)
086: */
087: @Override
088: public List<? extends IGeoResource> members(IProgressMonitor monitor)
089: throws IOException {
090:
091: if (members == null) {
092: synchronized (MapGraphicFactory.getInstance()) {
093: if (members == null) {
094: members = new ArrayList<MapGraphicResource>();
095: List<IConfigurationElement> graphics = MapGraphicFactory
096: .getInstance().getMapGraphics();
097:
098: if (graphics != null) {
099: for (IConfigurationElement graphic : graphics) {
100: members.add(new MapGraphicResource(this ,
101: graphic));
102: }
103: }
104: }
105: }
106: }
107:
108: return members;
109: }
110:
111: /*
112: * @see net.refractions.udig.catalog.IService#getConnectionParams()
113: */
114: @Override
115: public Map<String, Serializable> getConnectionParams() {
116: HashMap<String, Serializable> params = new HashMap<String, Serializable>();
117: params.put(MapGraphicServiceExtension.KEY, SERVICE_URL);
118: return params;
119: }
120:
121: /*
122: * @see net.refractions.udig.catalog.IResolve#canResolve(java.lang.Class)
123: */
124: public <T> boolean canResolve(Class<T> adaptee) {
125: return adaptee != null
126: && (adaptee.isAssignableFrom(MapGraphicFactory.class)
127: || adaptee
128: .isAssignableFrom(ITransientResolve.class) || super
129: .canResolve(adaptee));
130: }
131:
132: /*
133: * @see net.refractions.udig.catalog.IResolve#getStatus()
134: */
135: public Status getStatus() {
136: return Status.CONNECTED;
137: }
138:
139: /*
140: * @see net.refractions.udig.catalog.IResolve#getMessage()
141: */
142: public Throwable getMessage() {
143: return null;
144: }
145:
146: /*
147: * @see net.refractions.udig.catalog.IResolve#getIdentifier()
148: */
149: public URL getIdentifier() {
150: return SERVICE_URL;
151: }
152:
153: static class MapGraphicServiceInfo extends IServiceInfo {
154: /*
155: * @see net.refractions.udig.catalog.IServiceInfo#getTitle()
156: */
157: @Override
158: public String getTitle() {
159: return Messages.MapGraphicService_title;
160: }
161:
162: @Override
163: public String getDescription() {
164: return Messages.MapGraphicService_description;
165: }
166: }
167: }
|