Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a6ac20b2e7e2cc7beb1c826522737c16d811c570 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/*******************************************************************************
 * Copyright (c) 2012, 2017 Mia-Software, CEA, Christian W. Damus, and others.
 * 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:
 *    Nicolas Bros (Mia-Software) - Bug 379683 - customizable Tree content provider
 *    Gregoire Dupe (Mia-Software) - Bug 385292 - [CustomizedTreeContentProvider] StackOverFlow when refreshing a TreeViewer with ICustomizedTreeContentProvider
 *    Gregoire Dupe (Mia-Software) - Bug 386387 - [CustomizedTreeContentProvider] The TreeElements are not preserved between two calls to getElements()
 *    Christian W. Damus (CEA) - bug 430700
 *    Christian W. Damus (CEA) - bug 440795
 *    Christian W. Damus (CEA) - bug 441857
 *    Sebastien Gabel (Esterel Technologies) - Bug 438931 - Non deterministic order of the facet references defined in custom file
 *    Christian W. Damus - bugs 451683, 509742
 *
 *******************************************************************************/
package org.eclipse.papyrus.emf.facet.custom.ui.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.emf.facet.custom.core.ICustomizationManager;
import org.eclipse.papyrus.emf.facet.custom.core.exception.CustomizationException;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.custom.Customization;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.internal.treeproxy.EAttributeTreeElement;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.internal.treeproxy.EObjectTreeElement;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.internal.treeproxy.EReferenceTreeElement;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.internal.treeproxy.TreeElement;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.internal.treeproxy.TreeproxyFactory;
import org.eclipse.papyrus.emf.facet.custom.ui.IContentPropertiesHandler;
import org.eclipse.papyrus.emf.facet.custom.ui.IContentPropertiesHandlerFactory;
import org.eclipse.papyrus.emf.facet.custom.ui.ICustomizedContentProvider;
import org.eclipse.papyrus.emf.facet.custom.ui.ICustomizedTreeContentProvider;
import org.eclipse.papyrus.emf.facet.efacet.core.FacetUtils;
import org.eclipse.papyrus.emf.facet.efacet.core.IFacetManager;
import org.eclipse.papyrus.emf.facet.efacet.core.exception.FacetManagerException;
import org.eclipse.papyrus.emf.facet.util.core.Logger;

@SuppressWarnings("deprecation")
// @SuppressWarnings("deprecation") : Bug 380229 - [deprecated] ICustomizedContentProvider
public class CustomizedTreeContentProvider implements ICustomizedTreeContentProvider, ICustomizedContentProvider {

	private static final Comparator<EStructuralFeature> BY_RANK = CustomizedTreeContentProvider::compareRanks;

	private final ICustomizationManager customManager;
	private final IContentPropertiesHandler contentHandler;
	private Object[] rootElements;

	private final Map<EObjectCacheElement, TreeElement> cache;

	private class EObjectCacheElement {

		// Not null
		private final EObject element;

		// May be null
		private final TreeElement parent;

		public EObjectCacheElement(EObject element, TreeElement parent) {
			this.element = element;
			this.parent = parent;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + getOuterType().hashCode();
			result = prime * result + ((element == null) ? 0 : element.hashCode());
			result = prime * result + ((parent == null) ? 0 : parent.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj) {
				return true;
			}
			if (obj == null) {
				return false;
			}
			if (!(obj instanceof EObjectCacheElement)) {
				return false;
			}
			EObjectCacheElement other = (EObjectCacheElement) obj;
			if (!getOuterType().equals(other.getOuterType())) {
				return false;
			}
			if (element == null) {
				if (other.element != null) {
					return false;
				}
			} else if (!element.equals(other.element)) {
				return false;
			}
			if (parent == null) {
				if (other.parent != null) {
					return false;
				}
			} else if (!parent.equals(other.parent)) {
				return false;
			}
			return true;
		}

		private CustomizedTreeContentProvider getOuterType() {
			return CustomizedTreeContentProvider.this;
		}
	}

	public CustomizedTreeContentProvider(final ICustomizationManager customManager) {
		this.customManager = customManager;
		this.contentHandler = IContentPropertiesHandlerFactory.DEFAULT.createIContentPropertiesHandler(customManager);
		this.cache = new HashMap<EObjectCacheElement, TreeElement>();
	}

	protected Object[] getRootElements(final Object inputElement) {
		Object[] result;
		if (inputElement == null) {
			result = new Object[0];
		} else if (inputElement instanceof EObject) {
			result = new EObject[] { (EObject) inputElement };
		} else if (inputElement instanceof Collection<?>) {
			result = ((Collection<?>) inputElement).toArray();
		} else if (inputElement instanceof EObject[]) {
			result = (EObject[]) inputElement;
		} else {
			throw new IllegalArgumentException("Unhandled input element type: " + inputElement.getClass().getSimpleName()); //$NON-NLS-1$
		}
		return result;
	}

	@Override
	public Object[] getElements(final Object inputElement) {
		// Reconcile the (possibly changed) list of root elements with our tree element proxies

		final Object[] elements = getRootElements(inputElement);
		final List<Object> elementList = new ArrayList<Object>();

		// Index the existing elements
		final Map<Object, Object> index = new IdentityHashMap<Object, Object>(elements.length + 1);
		if (this.rootElements != null) {
			for (Object next : this.rootElements) {
				if (next instanceof EObjectTreeElement) {
					// Maps to its tree-element proxy
					index.put(((EObjectTreeElement) next).getEObject(), next);
				} else {
					// Maps to itself
					index.put(next, next);
				}
			}
		}

		for (final Object element : elements) {
			if (!isVisible(element)) {
				continue;
			}

			Object existing = index.get(element);
			if (existing != null) {
				// Just add it
				elementList.add(existing);
			} else if (element instanceof EObject) {
				final EObjectTreeElement eObjectProxy = getEObjectProxy(element, null);
				elementList.add(eObjectProxy);
			} else {
				elementList.add(element);
			}
		}

		this.rootElements = elementList.toArray();
		return this.rootElements;
	}

	protected static EObjectTreeElement createEObjectProxy(final Object element, final TreeElement parent) {
		final EObject eObject = (EObject) element;
		final EObjectTreeElement eObjectProxy = TreeproxyFactory.eINSTANCE.createEObjectTreeElement();
		eObjectProxy.setEObject(eObject);
		eObjectProxy.setParent(parent);
		return eObjectProxy;
	}

	@Override
	public Object[] getChildren(final Object parentElement) {
		Object[] result;
		if (parentElement == null) {
			result = new Object[0];
		} else if (parentElement instanceof EObjectTreeElement) {
			final EObjectTreeElement eObjectProxy = (EObjectTreeElement) parentElement;
			result = getChildren(eObjectProxy);
		} else if (parentElement instanceof EAttributeTreeElement) {
			final EAttributeTreeElement attributeProxy = (EAttributeTreeElement) parentElement;
			result = getChildren(attributeProxy);
		} else if (parentElement instanceof EReferenceTreeElement) {
			final EReferenceTreeElement referenceProxy = (EReferenceTreeElement) parentElement;
			result = getChildren(referenceProxy);
		} else {
			Logger.logError("Unknown element in tree: " + parentElement.getClass(), Activator.getDefault()); //$NON-NLS-1$
			result = new Object[0];
		}
		return result;
	}

	public Object[] getChildren(final EObjectTreeElement treeElement) {
		List<EStructuralFeature> facetFeatures;
		try {
			facetFeatures = FacetUtils.getETypedElements(treeElement.getEObject(), EStructuralFeature.class, customManager.getFacetManager());
		} catch (FacetManagerException e) {
			facetFeatures = Collections.emptyList();
			Logger.logError(e, Activator.getDefault());
		}

		// Where is the zero rank at which we insert native model features?
		int zeroPoint = getZeroRank(facetFeatures);
		List<EStructuralFeature> priorityFeatures = facetFeatures.subList(0, zeroPoint);
		List<EStructuralFeature> otherFeatures = facetFeatures.subList(zeroPoint, facetFeatures.size());

		final Collection<TreeElement> children = new ArrayList<TreeElement>();
		createAttributes(treeElement, priorityFeatures, otherFeatures, children);
		createReferences(treeElement, priorityFeatures, otherFeatures, children);
		return children.toArray();
	}

	/**
	 * Finds the index in a rank-sorted list of facet-features of the first feature defined
	 * by a facet-set of rank zero or greater (facet-sets that are not customizations are
	 * deemed to have rank zero).
	 * 
	 * @param facetFeatures
	 *            a list of facet features
	 * @return the index of the first feature of non-negative rank, or the size of the
	 *         list if all have negative rank (or {@code 0} if the list is empty)
	 */
	private int getZeroRank(List<EStructuralFeature> facetFeatures) {
		// The actual ordering is suspectible to user intervention, so it is possible
		// that the binary search will not find the best insertion point because the
		// list may not actually be sorted. But, it will find something and hopefully
		// that will be near the middle-ish
		int search = Collections.binarySearch(facetFeatures, null, BY_RANK);

		// We know that 'null' is not in the list, so the search result is the insertion point
		return -(1 + search);
	}

	static int compareRanks(EStructuralFeature o1, EStructuralFeature o2) {
		// the 'null' key (by definition the insertion point) has a magic
		// rank between -1 and 0
		if (o1 == null) {
			return (rank(o2) >= 0) ? +1 : -1;
		} else if (o2 == null) {
			return (rank(o1) < 0) ? -1 : +1;
		} else {
			return rank(o1) - rank(o2);
		}
	}

	static int rank(EStructuralFeature eStructuralFeature) {
		EObject eClass = eStructuralFeature.eContainer();
		EObject ePackage = eClass.eContainer();

		return (ePackage instanceof Customization)
				? ((Customization) ePackage).getRank()
				: 0;
	}

	public Object[] getChildren(final EAttributeTreeElement attributeProxy) {
		final List<Object> children = new ArrayList<Object>();
		final EAttribute eAttribute = attributeProxy.getEAttribute();
		if (eAttribute.isMany()) {
			final TreeElement parent = attributeProxy.getParent();
			if (!(parent instanceof EObjectTreeElement)) {
				throw new IllegalStateException("An attribute should appear only under a model element"); //$NON-NLS-1$
			}
			final EObjectTreeElement parentProxy = (EObjectTreeElement) parent;
			final EObject eObject = parentProxy.getEObject();

			try {
				final IFacetManager facetManager = this.customManager.getFacetManager();
				final List<Object> result = facetManager.getOrInvokeMultiValued(eObject, eAttribute, null);
				for (final Object object : result) {
					if (object instanceof EObject) {
						final EObject childEObject = (EObject) object;
						children.add(getEObjectProxy(childEObject, attributeProxy));
					}
					children.add(object);
				}
			} catch (final FacetManagerException e) {
				Logger.logError(e, Activator.getDefault());
			}
		}
		return children.toArray();
	}

	public Object[] getChildren(final EReferenceTreeElement referenceProxy) {
		List<? extends Object> result;
		final EReference eReference = referenceProxy.getEReference();
		final TreeElement parent = referenceProxy.getParent();
		if (!(parent instanceof EObjectTreeElement)) {
			throw new IllegalStateException("A reference should appear only under a model element"); //$NON-NLS-1$
		}
		final EObjectTreeElement parentProxy = (EObjectTreeElement) parent;
		final EObject eObject = parentProxy.getEObject();
		if (eReference.isMany()) {
			result = getMultiValuedReferenceChildren(eReference, eObject, referenceProxy);
		} else {
			final Object child = getSingleValuedReferenceChild(eReference, eObject, referenceProxy);
			if (child == null) {
				result = Collections.emptyList();
			} else {
				result = Collections.singletonList(child);
			}
		}
		return result.toArray();
	}

	private TreeElement getSingleValuedReferenceChild(final EReference eReference,
			final EObject eObject, final EReferenceTreeElement parent) {
		TreeElement child = null;
		if (parent.getReferedEObjectTE() == null || parent.getReferedEObjectTE().size() == 0) {
			try {
				final IFacetManager facetManager = this.customManager
						.getFacetManager();
				final EObject referedEObject = facetManager.getOrInvoke(
						eObject, eReference, EObject.class);
				if (referedEObject != null) {
					child = getEObjectProxy(referedEObject, parent);
				}
			} catch (final FacetManagerException e) {
				Logger.logError(e, Activator.getDefault());
			}
		} else {
			child = parent.getReferedEObjectTE().get(0);
		}
		return child;
	}

	private List<EObjectTreeElement> getMultiValuedReferenceChildren(
			final EReference eReference, final EObject eObject,
			final EReferenceTreeElement parent) {
		List<EObjectTreeElement> children = new ArrayList<EObjectTreeElement>();
		if (parent.getReferedEObjectTE() == null || parent.getReferedEObjectTE().size() == 0) {
			try {
				final IFacetManager facetManager = this.customManager
						.getFacetManager();
				final List<Object> result = facetManager
						.getOrInvokeMultiValued(eObject, eReference, null);
				for (final Object object : result) {
					if (object instanceof EObject) {
						final EObject childEObject = (EObject) object;
						children.add(getEObjectProxy(childEObject, parent));
					}
				}
			} catch (final FacetManagerException e) {
				Logger.logError(e, Activator.getDefault());
			}
		} else {
			children = parent.getReferedEObjectTE();
		}
		return children;
	}

	@Override
	public Object getParent(final Object element) {
		Object result = null;
		if (element instanceof TreeElement) {
			// It's a TreeElement, which means we've already displayed it.
			// Finding its parent will be easy
			final TreeElement treeElement = (TreeElement) element;
			result = treeElement.getParent();
		} else if (element instanceof EObject) {
			// It's an EObject, meaning we don't know whether it's already
			// been displayed. Delegate to the #parent custom query
			EObject modelElement = (EObject) element;
			result = customGetParent(modelElement);
		}
		return result;
	}

	/**
	 * Computes the parent TreeElement for a raw model element
	 * 
	 * @param modelElement
	 * @return
	 */
	private TreeElement customGetParent(final EObject modelElement) {
		// The custom EObject/Facet parent path, without references
		List<EObject> customParentPath = getModelElementPath(modelElement);

		// The custom EObject/Facet parent path, including references
		List<TreeElement> fullElementPath = getFullElementPath(customParentPath);

		// Build the full TreeElement path and return the leaf element
		return getParentTreeElementForPath(fullElementPath);
	}

	/**
	 * Returns the (customized) hierarchy of model elements until
	 * the given element (Included).
	 * 
	 * This may be different from the semantic container path
	 * if the tree structure has been customized (e.g. via custom
	 * facet references)
	 * 
	 * This path is the "natural path", i.e. it is unique (Even when the
	 * content provider actually represents a cyclic graph). It is however
	 * not necessarily the shortest path.
	 */
	private List<EObject> getModelElementPath(final EObject modelElement) {

		// Ordered from child to parent
		Collection<EObject> customParentPath = new LinkedHashSet<>();
		customParentPath.add(modelElement);

		EObject currentModelElement = modelElement;
		try {
			while (currentModelElement != null) {
				EObject parent = customManager.getCustomValueOf(currentModelElement, contentHandler.getParent(), EObject.class);
				if (parent != null) {
					if (customParentPath.contains(parent)) {
						String message = "Cyclic path detected when computing the hierarchy for " + modelElement;
						message += "\nPath: "+customParentPath;
						message += "This may indicate an inconsistency in the facet/custom implementation(s) of the #parent query";
						Logger.logError(message, Activator.getDefault());
						break;
					}
					customParentPath.add(parent);
				}
				currentModelElement = parent;
			}
		} catch (CustomizationException ex) {
			Logger.logError(ex, Activator.getDefault());
		}

		List<EObject> parentPath = new ArrayList<>(customParentPath);

		// Order from parent to child
		Collections.reverse(parentPath);
		return parentPath;
	}

	/**
	 * Builds and returns the complete list of TreeElements corresponding
	 * to the given model element path. The resulting path will include the
	 * necessary EReferences (Which may include FacetReferences).
	 */
	private List<TreeElement> getFullElementPath(Collection<EObject> customParentPath) {
		List<TreeElement> treePath = new ArrayList<>(); // Contains EObjects and EReferences
		TreeElement currentPathElement = null;
		EObject currentSemanticParent = null;
		
		for (EObject semanticElement : customParentPath) {

			if (currentPathElement == null) {
				// Root element
				EObjectCacheElement cacheElement = new EObjectCacheElement(semanticElement, null);
				currentPathElement = cache.get(cacheElement);
				if (currentPathElement == null) {
					currentPathElement = Arrays.stream(getElements(null)) //
							.filter(EObjectTreeElement.class::isInstance) //
							.map(EObjectTreeElement.class::cast) //
							.filter(tree -> tree.getEObject() == semanticElement) //
							.findAny().orElse(null);
				}
			} else {

				TreeElement nextElement = Arrays.stream(getChildren(currentPathElement)) //
						.filter(EObjectTreeElement.class::isInstance)//
						.map(EObjectTreeElement.class::cast) //
						.filter(tree -> tree.getEObject() == semanticElement) //
						.findAny() //
						.orElse(null);
				if (nextElement == null) {
					// Try references that may contain this element
					List<EReference> references = getVisibleReferences(currentSemanticParent);
					for (EReference reference : references) {
						if (reference.getEType().isInstance(semanticElement)) {
							// Potential match

							EObjectCacheElement cacheElement = new EObjectCacheElement(reference, currentPathElement);
							TreeElement cachedElement = cache.get(cacheElement);
							if (cachedElement == null || false == cachedElement instanceof EReferenceTreeElement) {
								// Since we called getChildren() earlier, this shouldn't happen
								System.err.println("Reference is visible but wasn't found");
								continue;
							}

							EReferenceTreeElement cachedRef = (EReferenceTreeElement) cachedElement;

							List<EObjectTreeElement> children;
							if (reference.getUpperBound() == 1) {
								children = Collections.singletonList(getSingleValuedReferenceChild(reference, currentSemanticParent, cachedElement));
							} else {
								children = getMultiValuedReferenceChildren(reference, currentSemanticParent, cachedRef);
							}

							nextElement = children.stream() //
									.filter(tree -> tree.getEObject() == semanticElement) //
									.findAny() //
									.orElse(null);

							if (nextElement != null) {
								treePath.add(cachedRef);
								break;
							}
						}
					}
				}

				currentPathElement = nextElement;
			}

			currentSemanticParent = semanticElement;
			if (currentPathElement != null) {
				treePath.add(currentPathElement);
			} else {
				System.err.println("Invalid root element: " + currentPathElement);
			}
		}

		return treePath;
	}

	private TreeElement getParentTreeElementForPath(List<TreeElement> fullElementPath) {
		if (fullElementPath.size() < 2) {
			return null;
		}
		return fullElementPath.get(fullElementPath.size() - 2);
	}

	@Override
	public boolean hasChildren(final Object element) {
		return getChildren(element).length > 0;
	}

	@Override
	public void inputChanged(final Viewer viewer, final Object oldInput, final Object newInput) {
		if (oldInput != newInput) {
			cache.clear();
		}
	}

	@Override
	public ICustomizationManager getCustomizationManager() {
		return this.customManager;
	}

	public boolean isVisible(final Object object) {
		Boolean result = Boolean.TRUE;
		if (object instanceof EObject) {
			final EObject eObject = (EObject) object;
			try {
				result = this.customManager.getCustomValueOf(eObject, this.contentHandler.getIsVisible(), Boolean.class);
			} catch (final CustomizationException e) {
				Logger.logError(e, Activator.getDefault());
			}
		}
		return result.booleanValue();
	}

	private boolean collapseLink(final Object object, final EReference eTypedElement) {
		Boolean result = Boolean.TRUE;
		if (object instanceof EObject) {
			final EObject eObject = (EObject) object;
			try {
				result = this.customManager.getCustomValueOf(eObject, eTypedElement, this.contentHandler.getCollapseLink(), Boolean.class);
			} catch (final CustomizationException e) {
				Logger.logError(e, Activator.getDefault());
			}
		}
		return result.booleanValue();
	}



	private void createReferences(final EObjectTreeElement treeElement,
			Collection<EStructuralFeature> priorityFacetFeatures, Collection<EStructuralFeature> otherFacetFeatures,
			Collection<TreeElement> children) {
		final EObject eObject = treeElement.getEObject();

		// High-priority (negative rank) features, first
		if (!priorityFacetFeatures.isEmpty()) {
			for (EStructuralFeature next : priorityFacetFeatures) {
				if (next instanceof EReference) {
					createReference(treeElement, eObject, (EReference) next, children);
				}
			}
		}

		// Filtered native model references have implicit zero rank
		for (EReference next : getVisibleReferences(eObject)) {
			createReference(treeElement, eObject, next, children);
		}

		// Lower priority (positive rank) features follow
		if (!otherFacetFeatures.isEmpty()) {
			for (EStructuralFeature next : otherFacetFeatures) {
				if (next instanceof EReference) {
					createReference(treeElement, eObject, (EReference) next, children);
				}
			}
		}
	}

	private void createReference(EObjectTreeElement treeElement, EObject eObject, EReference eReference, Collection<TreeElement> children) {
		if (collapseLink(eObject, eReference)) {
			if (eReference.getUpperBound() != 1) {
				collectMultiValuedReferenceChildren(eReference, eObject, treeElement, children);
			} else {
				EObjectTreeElement child = getSingleValuedReferenceChild(eReference, eObject, treeElement);
				if (child != null) {
					children.add(child);
				}
			}
		} else {
			children.add(getEReferenceProxy(eReference, treeElement));
		}
	}

	private void createAttributes(final EObjectTreeElement treeElement,
			Collection<EStructuralFeature> priorityFacetFeatures, Collection<EStructuralFeature> otherFacetFeatures,
			Collection<TreeElement> children) {

		final EObject eObject = treeElement.getEObject();

		// High-priority (negative rank) features, first
		if (!priorityFacetFeatures.isEmpty()) {
			for (EStructuralFeature next : priorityFacetFeatures) {
				if (next instanceof EAttribute) {
					createAttribute(treeElement, eObject, (EAttribute) next, children);
				}
			}
		}

		// Filtered native model attributes have implicit zero rank
		for (EAttribute next : getVisibleAttributes(eObject)) {
			createAttribute(treeElement, eObject, next, children);
		}

		// Lower priority (positive rank) features follow
		if (!otherFacetFeatures.isEmpty()) {
			for (EStructuralFeature next : otherFacetFeatures) {
				if (next instanceof EAttribute) {
					createAttribute(treeElement, eObject, (EAttribute) next, children);
				}
			}
		}
	}

	private List<EAttribute> getVisibleAttributes(EObject eObject) {
		List<EAttribute> result = null;
		try {
			result = this.customManager.getCustomValuesOf(eObject, this.contentHandler.getVisibleAttributes(), EAttribute.class);
		} catch (final CustomizationException e) {
			Logger.logError(e, Activator.getDefault());
		}

		return result == null ? Collections.emptyList() : result;
	}

	private List<EReference> getVisibleReferences(EObject eObject) {
		List<EReference> result = null;
		try {
			result = this.customManager.getCustomValuesOf(eObject, this.contentHandler.getVisibleReferences(), EReference.class);
		} catch (final CustomizationException e) {
			Logger.logError(e, Activator.getDefault());
		}

		return result == null ? Collections.emptyList() : result;
	}

	private void createAttribute(EObjectTreeElement treeElement, EObject eObject, EAttribute eAttribute, Collection<? super TreeElement> children) {
		TreeElement eAttributeTreeElement = getEAttributeProxy(eAttribute, treeElement);
		if (eAttributeTreeElement != null) {
			children.add(eAttributeTreeElement);
		}
	}


	private EReferenceTreeElement createReferenceProxy(final EReference reference, final EObjectTreeElement parent) {
		final EReferenceTreeElement referenceProxy = TreeproxyFactory.eINSTANCE.createEReferenceTreeElement();
		referenceProxy.setEReference(reference);
		referenceProxy.setParent(parent);
		return referenceProxy;
	}

	private EAttributeTreeElement createAttributeProxy(final EAttribute attribute, final EObjectTreeElement parent) {
		final EAttributeTreeElement attributeProxy = TreeproxyFactory.eINSTANCE.createEAttributeTreeElement();
		attributeProxy.setEAttribute(attribute);
		attributeProxy.setParent(parent);
		return attributeProxy;
	}

	private EObjectTreeElement getSingleValuedReferenceChild(final EReference eReference, final EObject eObject, final TreeElement parent) {
		EObjectTreeElement child = null;
		try {
			final IFacetManager facetManager = this.customManager.getFacetManager();
			final Object result = facetManager.getOrInvoke(eObject, eReference, null);
			if (result instanceof EObject) {
				final EObject childEObject = (EObject) result;
				child = getEObjectProxy(childEObject, parent);
			}
		} catch (final FacetManagerException e) {
			Logger.logError(e, Activator.getDefault());
		}
		return child;
	}

	private void collectMultiValuedReferenceChildren(final EReference eReference, final EObject eObject, final TreeElement parent, Collection<TreeElement> children) {
		try {
			final IFacetManager facetManager = this.customManager.getFacetManager();
			final List<Object> result = facetManager.getOrInvokeMultiValued(eObject, eReference, null);
			for (final Object object : result) {
				if (object instanceof EObject) {
					final EObject childEObject = (EObject) object;
					children.add(getEObjectProxy(childEObject, parent));
				}
			}
		} catch (final FacetManagerException e) {
			Logger.logError(e, Activator.getDefault());
		}
	}

	protected EObjectTreeElement getEObjectProxy(final Object element, final TreeElement parent) {
		EObjectCacheElement cacheElement = new EObjectCacheElement((EObject) element, parent);
		if (!(cache.containsKey(cacheElement))) {
			cache.put(cacheElement, createEObjectProxy(element, parent));
		}
		return (EObjectTreeElement) cache.get(cacheElement);
	}

	protected EAttributeTreeElement getEAttributeProxy(final Object element, final TreeElement parent) {
		EObjectCacheElement cacheElement = new EObjectCacheElement((EObject) element, parent);
		if (!(cache.containsKey(cacheElement))) {
			cache.put(cacheElement, createAttributeProxy((EAttribute) element, (EObjectTreeElement) parent));
		}
		return (EAttributeTreeElement) cache.get(cacheElement);
	}

	protected EReferenceTreeElement getEReferenceProxy(final Object element, final EObjectTreeElement parent) {
		EObjectCacheElement cacheElement = new EObjectCacheElement((EObject) element, parent);
		if (!(cache.containsKey(cacheElement))) {
			cache.put(cacheElement, createReferenceProxy((EReference) element, parent));
		}
		return (EReferenceTreeElement) cache.get(cacheElement);
	}

	@Override
	public void dispose() {
		cache.clear();
		rootElements = null;
	}
}

Back to the top