Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ed5983ee2b18337d7d747e3c76f0116878c4e0cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*******************************************************************************
 * Copyright (c) 2008 Conselleria de Infraestructuras y Transporte, Generalitat 
 * de la Comunitat Valenciana . All rights reserved. This program
 * and the accompanying materials are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Francisco Javier Cano Muñoz (Prodevelop) - initial api implementation
 *
 ******************************************************************************/
package org.eclipse.papyrus.navigator.providers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.provider.EObjectItemProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.uml2.uml.PackageImport;

/**
 * A provider that takes envelopes children in groups by type. EObjecs are
 * grouped by their {@link EClass}. {@link Object}s are grouped by their first
 * Interface.
 * 
 * @author <a href="mailto:fjcano@prodevelop.es">Francisco Javier Cano Muñoz</a>
 * @see <a href=https://bugs.eclipse.org/bugs/show_bug.cgi?id=290422>Bug
 *      #290422</a>
 * 
 */
public class GroupableTreeArrayContentProvider extends EObjectItemProvider {

	/**
	 * A <Map> from <Object> to containing <PackagingNode>.
	 */
	private static Map<Object, PackagingNode> parentNodes = null;

	/**
	 * A <Map> from <Object> to contained <PackagingNodes>.
	 */
	private static Map<Object, Collection<?>> virtualSuperNodes = null;

	synchronized protected static Map<Object, PackagingNode> getParentNodes() {
		if(parentNodes == null) {
			parentNodes = new HashMap<Object, PackagingNode>();
		}
		return parentNodes;
	}

	synchronized protected static Map<Object, Collection<?>> getVirtualSuperNodes() {
		if(virtualSuperNodes == null) {
			virtualSuperNodes = new HashMap<Object, Collection<?>>();
		}
		return virtualSuperNodes;
	}

	AdapterFactory helperAdapterFactory;

	public GroupableTreeArrayContentProvider(AdapterFactory adapterFactory, AdapterFactory helper) {
		super(adapterFactory);
		helperAdapterFactory = helper;
	}

	@Override
	public Collection<?> getChildren(Object parentElement) {
		if(parentElement instanceof PackagingNode) {
			return ((PackagingNode)parentElement).getContainedNodes();
		} else if(getVirtualSuperNodes().containsKey(parentElement)) {
			updateElementChildren(parentElement);
			return getVirtualSuperNodes().get(parentElement);
		} else {
			Collection<?> children = getVirtualSupernodes(parentElement);
			getVirtualSuperNodes().put(parentElement, children);
			return children;
		}
	}

	public Object getParent(Object element) {
		if(element instanceof PackagingNode) {
			return ((PackagingNode)element).getParent();
		} else if(getParentNodes().containsKey(element)) {
			return getParentNodes().get(element);
		} else if(element instanceof EObject && element instanceof View == false) {
			createVirtualSuperNodesTilParent((EObject)element);
			if(getParentNodes().containsKey(element)) {
				return getParentNodes().get(element);
			}
		}
		Object adapter = helperAdapterFactory.adapt(element, ITreeItemContentProvider.class);
		if(adapter instanceof ITreeItemContentProvider) {
			return ((ITreeItemContentProvider)adapter).getParent(element);
		} else
			return null;
	}

	public boolean hasChildren(Object element) {
		if(element instanceof PackagingNode) {
			return true;
		} else {
			if(element instanceof PackageImport) {
				return ((PackageImport)element).getImportedPackage().getPackagedElements().size() > 0;
			}
			Object adapter = helperAdapterFactory.adapt(element, ITreeItemContentProvider.class);
			if(adapter instanceof ITreeItemContentProvider) {
				return ((ITreeItemContentProvider)adapter).hasChildren(element);
			} else
				return false;
		}
	}

	@Override
	public Collection<?> getElements(Object inputElement) {

		return getVirtualSupernodes(inputElement);
	}

	private String getKey(Object o) {
		if(o instanceof EObject) {
			return ((EObject)o).eClass().getName();
		} else {
			return o.getClass().getInterfaces()[0].getSimpleName();
		}
	}

	private Collection<?> getVirtualSupernodes(Collection<?> node) {
		Object[] nodes = node != null ? node.toArray(new Object[node.size()]) : new Object[0];
		return getVirtualSupernodes(nodes);
	}

	private Collection<?> getVirtualSupernodes(Object[] node) {
		Map<String, Object> superNodes = new HashMap<String, Object>();
		Object[] nodeList = node != null ? node : new Object[0];

		for(int i = 0; i < nodeList.length; i++) {
			if(nodeList[i] instanceof View) {
				superNodes.put(nodeList[i].toString(), nodeList[i]);
			} else {
				String key = getKey(nodeList[i]);
				PackagingNode ghostNode = null;
				if(!superNodes.containsKey(key)) {
					ghostNode = new PackagingNode(key, super.getParent(nodeList[i]));
					superNodes.put(key, ghostNode);
				} else {
					ghostNode = superNodes.get(key) instanceof PackagingNode ? (PackagingNode)superNodes.get(key) : null;
				}
				if(ghostNode != null) {
					ghostNode.getContainedNodes().add(nodeList[i]);
					getParentNodes().put(nodeList[i], ghostNode);
				}
			}
		}
		return superNodes.values();
	}

	private Collection<?> getVirtualSupernodes(Object parent) {
		Collection<?> children = null;
		if(parent instanceof PackageImport) {
			// fjcano #297372 : show PackageImport's imported Package's children
			children = ((PackageImport)parent).getImportedPackage().getPackagedElements();
		} else {
			Object adapter = helperAdapterFactory.adapt(parent, ITreeItemContentProvider.class);
			if(adapter instanceof ITreeItemContentProvider) {
				children = ((ITreeItemContentProvider)adapter).getChildren(parent);
			}
		}
		if(parent instanceof EObject && children != null) {
			// Map<Object, Collection<?>> superNodes = getVirtualSuperNodes();
			// collection of EClass nodes to create
			Collection<EClass> eClasses = new ArrayList<EClass>();
			// collection of other elements to add as children
			Collection<Object> othersToAdd = new ArrayList<Object>();
			for(Object child : children) {
				if(child instanceof EObject && !(child instanceof Diagram || child instanceof EAnnotation)) {
					// for each child EObject we'll find its EClass and add it
					// as a node to create
					EClass eClass = ((EObject)child).eClass();
					if(!eClasses.contains(eClass)) {
						eClasses.add(eClass);
					}
				} else {
					// if not an EObject it will be added as a child as is
					othersToAdd.add(child);
				}
			}
			// final collection of actual children to show in the tree for this
			// parent element
			Collection<Object> superNodes = new ArrayList<Object>();
			// EObjectPackagingNodes for EObjects
			for(EClass eClass : eClasses) {
				EObjectPackagingNode node = new EObjectPackagingNode(eClass, (EObject)parent);
				superNodes.add(node);
				// add to the parentNodes collection these newly created
				// elements
				for(Object o : node.getContainedNodes()) {
					getParentNodes().put(o, node);
				}
			}
			// other Objects that are children
			for(Object object : othersToAdd) {
				superNodes.add(object);
			}
			return superNodes;
		} else {
			return getVirtualSupernodes(children);
		}
	}

	protected void updateElementChildren(Object parent) {
		if(getVirtualSuperNodes().containsKey(parent)) {
			Collection<Object> oldCollection = (Collection<Object>)getVirtualSuperNodes().get(parent);
			Collection<Object> newCollection = (Collection<Object>)getVirtualSupernodes(parent);
			mergeCollections(oldCollection, newCollection);
		} else {
			return;
		}
	}

	/**
	 * Adds to oldC the new elements in newC. Removes from oldC the elements
	 * that don't appear in newC.
	 * 
	 * @param oldC
	 * @param newC
	 */
	protected void mergeCollections(Collection<Object> oldC, Collection<Object> newC) {
		if(oldC == null || newC == null || (oldC.size() == 0 && newC.size() == 0)) {
			return;
		}
		// elements to remove from oldC
		Collection<Object> toRemove = new ArrayList<Object>();
		for(Object o : oldC) {
			if(!newC.contains(o)) {
				toRemove.add(o);
			}
		}
		// elements to add to oldC
		Collection<Object> toAdd = new ArrayList<Object>();
		for(Object o : newC) {
			if(!oldC.contains(o)) {
				toAdd.add(o);
			}
		}
		// remove elements
		for(Object o : toRemove) {
			oldC.remove(o);
		}
		// add elements
		for(Object o : toAdd) {
			oldC.add(o);
		}
	}

	protected void createVirtualSuperNodesTilParent(EObject element) {
		if(getVirtualSuperNodes().containsKey(element)) {
			return;
		}
		if(element.eContainer() != null) {
			createVirtualSuperNodesTilParent(element.eContainer());
		}
		getChildren(element);
	}

	public void dispose() {
	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// TODO Auto-generated method stub
	}

}

Back to the top