001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.project.internal.impl;
016:
017: import java.util.Arrays;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.concurrent.CopyOnWriteArraySet;
022:
023: import net.refractions.udig.core.internal.ExtensionPointList;
024: import net.refractions.udig.project.IMap;
025: import net.refractions.udig.project.interceptor.LayerInterceptor;
026: import net.refractions.udig.project.interceptor.MapInterceptor;
027: import net.refractions.udig.project.internal.Layer;
028: import net.refractions.udig.project.internal.ProjectPlugin;
029: import net.refractions.udig.project.internal.Trace;
030:
031: import org.eclipse.core.runtime.CoreException;
032: import org.eclipse.core.runtime.IConfigurationElement;
033: import org.eclipse.emf.common.notify.Adapter;
034: import org.eclipse.emf.common.notify.NotificationChain;
035: import org.eclipse.emf.ecore.InternalEObject;
036:
037: /**
038: * Wraps a EList and makes sure that when a layer is added the layer interceptor is fired and a deep
039: * adapter is added.
040: *
041: * @author Jesse
042: * @since 1.1.0
043: */
044: class LayersList2 extends SynchronizedEObjectWithInverseResolvingEList {
045:
046: /** long serialVersionUID field */
047: private static final long serialVersionUID = 4584718175140573610L;
048:
049: private Collection<Adapter> deepAdapters = new CopyOnWriteArraySet<Adapter>();
050:
051: public LayersList2(Class dataClass, InternalEObject owner,
052: int featureID, int inverseFeatureID) {
053: super (dataClass, owner, featureID, inverseFeatureID);
054: }
055:
056: /**
057: * adds the adapter to all layers and the context model
058: *
059: * @param adapter adapter to add to all layers.
060: */
061: @SuppressWarnings("unchecked")
062: public void addDeepAdapter(Adapter adapter) {
063: deepAdapters.add(adapter);
064: if (!owner.eAdapters().contains(adapter))
065: owner.eAdapters().add(adapter);
066: for (Object object : this ) {
067: Layer layer = (Layer) object;
068: if (!layer.eAdapters().contains(adapter))
069: layer.eAdapters().add(adapter);
070: }
071: }
072:
073: /**
074: * removes the adapter from all layers.
075: *
076: * @param toRemove
077: */
078: public void removeDeepAdapter(Adapter toRemove) {
079: deepAdapters.remove(toRemove);
080: owner.eAdapters().remove(toRemove);
081: for (Object object : this ) {
082: ((Layer) object).eAdapters().remove(toRemove);
083: }
084: }
085:
086: @Override
087: protected void didAdd(int index, Object newObject) {
088: super .didAdd(index, newObject);
089:
090: }
091:
092: @Override
093: public NotificationChain inverseAdd(Object object,
094: NotificationChain notifications) {
095: NotificationChain notificationChain = super .inverseAdd(object,
096: notifications);
097: if (ProjectPlugin.isDebugging(Trace.MODEL))
098: ProjectPlugin
099: .trace(
100: getClass(),
101: "inverseAdd() = " + ((Layer) object).getID() + " to map " + getMap().getName(), null); //$NON-NLS-1$ //$NON-NLS-2$
102:
103: runAddInterceptors(object);
104: return notificationChain;
105: }
106:
107: @Override
108: protected Object assign(int index, Object object) {
109: if (!(object instanceof Layer))
110: throw new AssertionError(
111: "Can only add " + Layer.class.getName() + " to a map. Was: " //$NON-NLS-1$ //$NON-NLS-2$
112: + object.getClass().getName());
113:
114: if (ProjectPlugin.isDebugging(Trace.MODEL))
115: ProjectPlugin
116: .trace(
117: getClass(),
118: "Adding " + ((Layer) object).getID() + " to map " + getMap().getName() + " at location: " + index, null); //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
119: Object object2 = super .assign(index, object);
120: if (ProjectPlugin.isDebugging(Trace.MODEL))
121: ProjectPlugin.trace(getClass(),
122: "Resulting list=" + this , null); //$NON-NLS-1$
123:
124: return object2;
125: }
126:
127: private IMap getMap() {
128: if (owner instanceof IMap)
129: return (IMap) owner;
130: if (owner.eContainer() instanceof IMap)
131: return (IMap) owner.eContainer();
132: throw new IllegalStateException(
133: "Owner should be IMap or ContextModel"); //$NON-NLS-1$
134: }
135:
136: @Override
137: protected Object doRemove(int index) {
138: Object toRemove = get(index);
139: runRemoveInterceptor(toRemove);
140:
141: if (ProjectPlugin.isDebugging(Trace.MODEL))
142: ProjectPlugin
143: .trace(
144: getClass(),
145: "Removing " + ((Layer) toRemove).getID() + " from map " + getMap().getName(), null); //$NON-NLS-1$ //$NON-NLS-2$
146:
147: return super .doRemove(index);
148: }
149:
150: @Override
151: protected void doClear() {
152: Object[] toRemove = toArray();
153: removeAllInterceptors(Arrays.asList(toRemove));
154:
155: if (ProjectPlugin.isDebugging(Trace.MODEL))
156: ProjectPlugin
157: .trace(
158: getClass(),
159: "Removing all layers from map:" + getMap().getName(), null); //$NON-NLS-1$
160:
161: super .doClear();
162: }
163:
164: @SuppressWarnings("unchecked")
165: private void removeAllInterceptors(Collection c) {
166: for (Iterator iter = c.iterator(); iter.hasNext();) {
167: Layer element = (Layer) iter.next();
168: runLayerInterceptor(element, "layerRemoved"); //$NON-NLS-1$
169: element.eAdapters().removeAll(deepAdapters);
170: }
171: }
172:
173: @SuppressWarnings("unchecked")
174: private void runAddInterceptors(Object element) {
175: Layer layer = (Layer) element;
176: for (Adapter deepAdapter : deepAdapters) {
177: if (!layer.eAdapters().contains(deepAdapter))
178: layer.eAdapters().add(deepAdapter);
179: }
180: runLayerInterceptor(layer, LayerInterceptor.ADDED_ID);
181: }
182:
183: @SuppressWarnings("unchecked")
184: private void runRemoveInterceptor(Object remove) {
185: if (remove == null || !contains(remove))
186: return;
187: Layer layer = (Layer) remove;
188: runLayerInterceptor(layer, LayerInterceptor.REMOVED_ID);
189: (layer).eAdapters().removeAll(deepAdapters);
190: }
191:
192: private void runLayerInterceptor(Layer layer,
193: String configurationName) {
194: runNonDeprecatedInterceptors(layer, configurationName);
195: runDeprecatedInterceptors(layer, configurationName);
196: }
197:
198: private void runNonDeprecatedInterceptors(Layer layer,
199: String configurationName) {
200: List<IConfigurationElement> list = ExtensionPointList
201: .getExtensionPointList(LayerInterceptor.EXTENSION_ID);
202: for (IConfigurationElement element : list) {
203: if (element.getName().equals(configurationName)) {
204: String attribute = element.getAttribute("name"); //$NON-NLS-1$
205: try {
206: LayerInterceptor interceptor = (LayerInterceptor) element
207: .createExecutableExtension("class"); //$NON-NLS-1$
208: interceptor.run(layer);
209: } catch (CoreException e) {
210: ProjectPlugin
211: .log(
212: "Error creating class: " + element.getAttribute("class") + " part of layer interceptor: " + attribute, e); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
213: } catch (Throwable t) {
214: ProjectPlugin
215: .log(
216: "error running interceptor: " + attribute, t); //$NON-NLS-1$
217: }
218: }
219: }
220: }
221:
222: private void runDeprecatedInterceptors(Layer layer,
223: String configurationName) {
224: List<IConfigurationElement> interceptors = ExtensionPointList
225: .getExtensionPointList(MapInterceptor.MAP_INTERCEPTOR_EXTENSIONPOINT);
226: for (IConfigurationElement element : interceptors) {
227: if (!configurationName.equals(element.getName()))
228: continue;
229: try {
230: LayerInterceptor interceptor = (LayerInterceptor) element
231: .createExecutableExtension("class"); //$NON-NLS-1$
232: interceptor.run(layer);
233: } catch (Throwable e) {
234: ProjectPlugin.log("", e); //$NON-NLS-1$
235: }
236: }
237: }
238:
239: }
|