001: /**
002: * <copyright>
003: * </copyright>
004: *
005: * $Id: LayerFactoryImpl.java 25248 2007-04-22 22:00:21Z jeichar $
006: */package net.refractions.udig.project.internal.impl;
007:
008: import java.awt.Color;
009: import java.io.IOException;
010: import java.util.Iterator;
011: import java.util.LinkedList;
012: import java.util.List;
013:
014: import net.refractions.udig.catalog.CatalogPlugin;
015: import net.refractions.udig.catalog.IGeoResource;
016: import net.refractions.udig.catalog.IResolve;
017: import net.refractions.udig.catalog.IService;
018: import net.refractions.udig.core.internal.ExtensionPointList;
019: import net.refractions.udig.project.interceptor.LayerInterceptor;
020: import net.refractions.udig.project.internal.Layer;
021: import net.refractions.udig.project.internal.LayerFactory;
022: import net.refractions.udig.project.internal.Map;
023: import net.refractions.udig.project.internal.ProjectFactory;
024: import net.refractions.udig.project.internal.ProjectPackage;
025: import net.refractions.udig.project.internal.ProjectPlugin;
026: import net.refractions.udig.ui.ProgressManager;
027: import net.refractions.udig.ui.palette.ColourScheme;
028:
029: import org.eclipse.core.runtime.CoreException;
030: import org.eclipse.core.runtime.IConfigurationElement;
031: import org.eclipse.core.runtime.NullProgressMonitor;
032: import org.eclipse.emf.common.notify.Notification;
033: import org.eclipse.emf.common.notify.NotificationChain;
034: import org.eclipse.emf.common.util.EList;
035: import org.eclipse.emf.ecore.EClass;
036: import org.eclipse.emf.ecore.EObject;
037: import org.eclipse.emf.ecore.EStructuralFeature;
038: import org.eclipse.emf.ecore.InternalEObject;
039: import org.eclipse.emf.ecore.impl.ENotificationImpl;
040: import org.eclipse.emf.ecore.impl.EObjectImpl;
041: import org.eclipse.emf.ecore.util.EDataTypeUniqueEList;
042: import org.eclipse.emf.ecore.util.EcoreUtil;
043:
044: /**
045: * <!-- begin-user-doc --> An implementation of the model object '<em><b>Layer Factory</b></em>'.
046: * <!-- end-user-doc -->
047: * <p>
048: * The following features are implemented:
049: * <ul>
050: * <li>{@link net.refractions.udig.project.internal.impl.LayerFactoryImpl#getMap <em>Map</em>}</li>
051: * </ul>
052: * </p>
053: *
054: * @generated
055: */
056: public class LayerFactoryImpl extends EObjectImpl implements
057: LayerFactory {
058: /**
059: * <!-- begin-user-doc --> <!-- end-user-doc -->
060: *
061: * @generated NOT
062: */
063: public static final String copyright = "uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004, Refractions Research Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; version 2.1 of the License. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details."; //$NON-NLS-1$
064:
065: /**
066: * <!-- begin-user-doc --> <!-- end-user-doc -->
067: * @generated
068: */
069: protected LayerFactoryImpl() {
070: super ();
071: }
072:
073: /**
074: * <!-- begin-user-doc --> <!-- end-user-doc -->
075: * @generated
076: */
077: protected EClass eStaticClass() {
078: return ProjectPackage.eINSTANCE.getLayerFactory();
079: }
080:
081: /**
082: * <!-- begin-user-doc --> <!-- end-user-doc -->
083: * @generated
084: */
085: public Map getMap() {
086: if (eContainerFeatureID != ProjectPackage.LAYER_FACTORY__MAP)
087: return null;
088: return (Map) eContainer;
089: }
090:
091: /**
092: * <!-- begin-user-doc --> <!-- end-user-doc -->
093: * @generated
094: */
095: public void setMap(Map newMap) {
096: if (newMap != eContainer
097: || (eContainerFeatureID != ProjectPackage.LAYER_FACTORY__MAP && newMap != null)) {
098: if (EcoreUtil.isAncestor(this , (EObject) newMap))
099: throw new IllegalArgumentException(
100: "Recursive containment not allowed for " + toString()); //$NON-NLS-1$
101: NotificationChain msgs = null;
102: if (eContainer != null)
103: msgs = eBasicRemoveFromContainer(msgs);
104: if (newMap != null)
105: msgs = ((InternalEObject) newMap).eInverseAdd(this ,
106: ProjectPackage.MAP__LAYER_FACTORY, Map.class,
107: msgs);
108: msgs = eBasicSetContainer((InternalEObject) newMap,
109: ProjectPackage.LAYER_FACTORY__MAP, msgs);
110: if (msgs != null)
111: msgs.dispatch();
112: } else if (eNotificationRequired())
113: eNotify(new ENotificationImpl(this , Notification.SET,
114: ProjectPackage.LAYER_FACTORY__MAP, newMap, newMap));
115: }
116:
117: /**
118: * <!-- begin-user-doc --> <!-- end-user-doc -->
119: * @generated
120: */
121: public NotificationChain eInverseAdd(InternalEObject otherEnd,
122: int featureID, Class baseClass, NotificationChain msgs) {
123: if (featureID >= 0) {
124: switch (eDerivedStructuralFeatureID(featureID, baseClass)) {
125: case ProjectPackage.LAYER_FACTORY__MAP:
126: if (eContainer != null)
127: msgs = eBasicRemoveFromContainer(msgs);
128: return eBasicSetContainer(otherEnd,
129: ProjectPackage.LAYER_FACTORY__MAP, msgs);
130: default:
131: return eDynamicInverseAdd(otherEnd, featureID,
132: baseClass, msgs);
133: }
134: }
135: if (eContainer != null)
136: msgs = eBasicRemoveFromContainer(msgs);
137: return eBasicSetContainer(otherEnd, featureID, msgs);
138: }
139:
140: /**
141: * <!-- begin-user-doc --> <!-- end-user-doc -->
142: * @generated
143: */
144: public NotificationChain eInverseRemove(InternalEObject otherEnd,
145: int featureID, Class baseClass, NotificationChain msgs) {
146: if (featureID >= 0) {
147: switch (eDerivedStructuralFeatureID(featureID, baseClass)) {
148: case ProjectPackage.LAYER_FACTORY__MAP:
149: return eBasicSetContainer(null,
150: ProjectPackage.LAYER_FACTORY__MAP, msgs);
151: default:
152: return eDynamicInverseRemove(otherEnd, featureID,
153: baseClass, msgs);
154: }
155: }
156: return eBasicSetContainer(null, featureID, msgs);
157: }
158:
159: /**
160: * <!-- begin-user-doc --> <!-- end-user-doc -->
161: * @generated
162: */
163: public NotificationChain eBasicRemoveFromContainer(
164: NotificationChain msgs) {
165: if (eContainerFeatureID >= 0) {
166: switch (eContainerFeatureID) {
167: case ProjectPackage.LAYER_FACTORY__MAP:
168: return eContainer.eInverseRemove(this ,
169: ProjectPackage.MAP__LAYER_FACTORY, Map.class,
170: msgs);
171: default:
172: return eDynamicBasicRemoveFromContainer(msgs);
173: }
174: }
175: return eContainer.eInverseRemove(this , EOPPOSITE_FEATURE_BASE
176: - eContainerFeatureID, null, msgs);
177: }
178:
179: /**
180: * <!-- begin-user-doc --> <!-- end-user-doc -->
181: * @generated
182: */
183: public Object eGet(EStructuralFeature eFeature, boolean resolve) {
184: switch (eDerivedStructuralFeatureID(eFeature)) {
185: case ProjectPackage.LAYER_FACTORY__MAP:
186: return getMap();
187: }
188: return eDynamicGet(eFeature, resolve);
189: }
190:
191: /**
192: * <!-- begin-user-doc --> <!-- end-user-doc -->
193: * @generated
194: */
195: public void eSet(EStructuralFeature eFeature, Object newValue) {
196: switch (eDerivedStructuralFeatureID(eFeature)) {
197: case ProjectPackage.LAYER_FACTORY__MAP:
198: setMap((Map) newValue);
199: return;
200: }
201: eDynamicSet(eFeature, newValue);
202: }
203:
204: /**
205: * <!-- begin-user-doc --> <!-- end-user-doc -->
206: * @generated
207: */
208: public void eUnset(EStructuralFeature eFeature) {
209: switch (eDerivedStructuralFeatureID(eFeature)) {
210: case ProjectPackage.LAYER_FACTORY__MAP:
211: setMap((Map) null);
212: return;
213: }
214: eDynamicUnset(eFeature);
215: }
216:
217: /**
218: * <!-- begin-user-doc --> <!-- end-user-doc -->
219: * @generated
220: */
221: public boolean eIsSet(EStructuralFeature eFeature) {
222: switch (eDerivedStructuralFeatureID(eFeature)) {
223: case ProjectPackage.LAYER_FACTORY__MAP:
224: return getMap() != null;
225: }
226: return eDynamicIsSet(eFeature);
227: }
228:
229: /**
230: * Creates a list of {@linkplain Layer}objects from the provided selection.
231: *
232: * @param selection A selection of CatalogEntries obtained from a {@linkplain CatalogTreeViewer}
233: * object.
234: * @return a list of {@linkplain Layer}objects from the provided selection.
235: * @throws IOException
236: */
237: @SuppressWarnings("unchecked")
238: public List<Layer> getLayers(List selection) throws IOException {
239: List<Layer> layers = new LinkedList<Layer>();
240: for (Iterator<Object> iter = selection.iterator(); iter
241: .hasNext();) {
242: Object obj = iter.next();
243: if (obj instanceof IService) {
244: layers.addAll(getLayers((IService) obj));
245: } else if (obj instanceof IGeoResource) {
246: IGeoResource entry = (IGeoResource) obj;
247: Layer ref = createLayer(entry);
248:
249: if (ref != null)
250: layers.add(ref);
251: }
252: }
253: return layers;
254: }
255:
256: public List<Layer> getLayers(IService service) throws IOException {
257: Layer ref = null;
258: List<Layer> layers = new LinkedList<Layer>();
259:
260: Iterator<? extends IGeoResource> rentryIter = service.members(
261: null).iterator();
262: while (rentryIter.hasNext()) {
263: IGeoResource entry = rentryIter.next();
264:
265: ref = createLayer(entry);
266:
267: if (ref != null)
268: layers.add(ref);
269: }
270: return layers;
271: }
272:
273: // /**
274: // * Creates a LayerRef for WMSs.
275: // *
276: // * @param service the registry entry parent of the CatalogEntry that the Layer ref references.
277: // * @param geoResource the CatalogEntry that the Ref refers to.
278: // * @return a LayerRef for DataStores.
279: // * @throws IOException
280: // */
281: // protected Layer createWMSLayer( IService service, IGeoResource geoResource ) throws IOException {
282: //
283: // org.geotools.data.ows.Layer wmslayer = null;
284: //
285: // IGeoResourceInfo info = geoResource.resolve(IGeoResourceInfo.class, null);
286: // String layerName = info.getName();
287: // if (layerName == null || layerName.length() == 0) {
288: // throw new IllegalArgumentException("Cannot determine name of resource."); //$NON-NLS-1$
289: // }
290: //
291: // WebMapServer wms = service.resolve(WebMapServer.class, null);
292: // if (wms == null) {
293: // throw new IOException("Cannot communicate with Web Map Server."); //$NON-NLS-1$
294: // }
295: // org.geotools.data.ows.Layer[] layers = WMSUtils.getNamedLayers(wms.getCapabilities());
296: // for( org.geotools.data.ows.Layer layer : layers ) {
297: // String targetName = layer.getName();
298: // if (targetName.equals(layerName)) {
299: // wmslayer = layer;
300: // break;
301: // }
302: // }
303: //
304: // if (wmslayer == null) {
305: // throw new IOException(
306: // "This resource (" + layerName + ") is not a part of the service. This is likely caused by inconsistent data returned by a search."); //$NON-NLS-1$//$NON-NLS-2$
307: // }
308: //
309: // Layer layer = ProjectFactory.eINSTANCE.createLayer();
310: //
311: // layer.setID(geoResource.getIdentifier());
312: //
313: // return layer;
314: // }
315:
316: /**
317: * Returns a LayerFactory object
318: *
319: * @return a LayerFactory object.
320: */
321: public static LayerFactoryImpl create() {
322: return new LayerFactoryImpl();
323: }
324:
325: /**
326: * Creates a layer from a service and a resource. The layer is represented the data in resource.
327: * May return null if it cannot resolve the service.
328: *
329: * @param service
330: * @param resource
331: * @return
332: * @throws IOException
333: */
334: @SuppressWarnings("unchecked")
335: public Layer createLayer(IGeoResource resource) throws IOException {
336: IService service = resource.service(ProgressManager.instance()
337: .get());
338:
339: if (service == null) {
340: return null;
341: }
342: //check that the service is part of catalog... If not add
343: if (CatalogPlugin.getDefault().getLocalCatalog().getById(
344: IService.class, service.getIdentifier(),
345: new NullProgressMonitor()) == null) {
346: CatalogPlugin.getDefault().getLocalCatalog().add(
347: resource.service(null));
348: }
349:
350: Layer layer = ProjectFactory.eINSTANCE.createLayer();
351: layer.setID(resource.getIdentifier());
352:
353: if (layer == null) {
354: throw new IOException(
355: "Unable to create layer from resource '" + resource.getIdentifier() + "'"); //$NON-NLS-1$ //$NON-NLS-2$
356: }
357:
358: // process the style content extension point to initially populate
359: // the style blackboard with style info
360: // TODO: the style objects need access to preference system
361: final Layer theLayer = layer;
362:
363: List<IResolve> resolves = CatalogPlugin.getDefault()
364: .getLocalCatalog().find(layer.getID(),
365: ProgressManager.instance().get());
366: EList resources = new EDataTypeUniqueEList(IGeoResource.class,
367: this , ProjectPackage.LAYER__GEO_RESOURCES);
368: LayerResource preferredResource = null;
369: for (IResolve resolve : resolves) {
370: if (resolve instanceof IGeoResource) {
371: LayerResource layerResource = new LayerResource(
372: (LayerImpl) layer, (IGeoResource) resolve);
373: resources.add(layerResource);
374: if (resolve == resource)
375: preferredResource = layerResource;
376: }
377: }
378:
379: ((LayerImpl) layer).geoResources = resources;
380: layer.setGeoResource(preferredResource);
381:
382: //determine the default colour
383: ColourScheme colourScheme = getMap().getColourScheme();
384: Color colour = colourScheme
385: .addItem(theLayer.getID().toString());
386: theLayer.setDefaultColor(colour);
387:
388: runLayerCreatedInterceptor(layer);
389:
390: return layer;
391: }
392:
393: private void runLayerCreatedInterceptor(Layer layer) {
394: List<IConfigurationElement> list = ExtensionPointList
395: .getExtensionPointList(LayerInterceptor.EXTENSION_ID);
396: for (IConfigurationElement element : list) {
397: if (element.getName().equals(LayerInterceptor.CREATED_ID)) {
398: String attribute = element.getAttribute("name"); //$NON-NLS-1$
399: try {
400: LayerInterceptor interceptor = (LayerInterceptor) element
401: .createExecutableExtension("class"); //$NON-NLS-1$
402: interceptor.run(layer);
403: } catch (CoreException e) {
404: ProjectPlugin
405: .log(
406: "Error creating class: " + element.getAttribute("class") + " part of layer interceptor: " + attribute, e); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
407: } catch (Throwable t) {
408: ProjectPlugin
409: .log(
410: "error running interceptor: " + attribute, t); //$NON-NLS-1$
411: }
412: }
413: }
414: }
415:
416: } // LayerFactoryImpl
|