Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2f35923e17e1b6ee889620e74a495ed901b18da2 (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
/*****************************************************************************
 * Copyright (c) 2016, 2021 CEA LIST, 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 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus - bug 573987
 *
 *****************************************************************************/

package org.eclipse.papyrus.views.properties.toolsmiths.query;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.papyrus.emf.facet.efacet.core.IFacetManager;
import org.eclipse.papyrus.emf.facet.efacet.core.exception.DerivedTypedElementException;
import org.eclipse.papyrus.emf.facet.query.java.core.IJavaQuery2;
import org.eclipse.papyrus.emf.facet.query.java.core.IParameterValueList2;
import org.eclipse.papyrus.infra.constraints.ConstraintsPackage;
import org.eclipse.papyrus.infra.properties.contexts.Context;
import org.eclipse.papyrus.infra.properties.contexts.ContextsPackage;
import org.eclipse.papyrus.infra.properties.contexts.util.ContextsSwitch;
import org.eclipse.papyrus.infra.properties.ui.UiPackage;

/**
 * @author Camille Letavernier
 *
 */
public class GetVisibleFeaturesQuery implements IJavaQuery2<EObject, List<EReference>> {

	static Set<EReference> allExcludedReferences = new HashSet<>();

	static {
		allExcludedReferences.add(ConstraintsPackage.Literals.DISPLAY_UNIT__CONSTRAINTS);
		allExcludedReferences.add(EcorePackage.Literals.EMODEL_ELEMENT__EANNOTATIONS);

		allExcludedReferences.add(ContextsPackage.Literals.CONTEXT__DEPENDENCIES);
		allExcludedReferences.add(ContextsPackage.Literals.DATA_CONTEXT_ROOT__MODEL_ELEMENT_FACTORY);
		allExcludedReferences.add(ContextsPackage.Literals.DATA_CONTEXT_ELEMENT__PACKAGE);
		allExcludedReferences.add(ContextsPackage.Literals.DATA_CONTEXT_ELEMENT__SUPERTYPES);
		allExcludedReferences.add(ContextsPackage.Literals.PROPERTY__CONTEXT_ELEMENT);
		allExcludedReferences.add(ContextsPackage.Literals.ABSTRACT_SECTION__TAB);
		allExcludedReferences.add(ContextsPackage.Literals.SECTION__OWNER);
		allExcludedReferences.add(ContextsPackage.Literals.TAB__AFTER_TAB);
		allExcludedReferences.add(ContextsPackage.Literals.TAB__SECTIONS);
		allExcludedReferences.add(ContextsPackage.Literals.TAB__ALL_SECTIONS);
		allExcludedReferences.add(ContextsPackage.Literals.VIEW__CONTEXT);
		allExcludedReferences.add(ContextsPackage.Literals.VIEW__DATACONTEXTS);

		allExcludedReferences.add(UiPackage.Literals.COMPOSITE_WIDGET__WIDGET_TYPE);
		allExcludedReferences.add(UiPackage.Literals.UI_COMPONENT__ATTRIBUTES);
		allExcludedReferences.add(UiPackage.Literals.LAYOUT__LAYOUT_TYPE);
		allExcludedReferences.add(UiPackage.Literals.PROPERTY_EDITOR__PROPERTY);
		allExcludedReferences.add(UiPackage.Literals.PROPERTY_EDITOR__WIDGET_TYPE);
		allExcludedReferences.add(UiPackage.Literals.PROPERTY_EDITOR__UNRESOLVED_PROPERTY);
		allExcludedReferences.add(UiPackage.Literals.STANDARD_WIDGET__WIDGET_TYPE);
	}

	/**
	 * @see org.eclipse.papyrus.emf.facet.query.java.core.IJavaQuery2#evaluate(org.eclipse.emf.ecore.EObject, org.eclipse.papyrus.emf.facet.query.java.core.IParameterValueList2, org.eclipse.papyrus.emf.facet.efacet.core.IFacetManager)
	 *
	 * @param source
	 * @param parameterValues
	 * @param facetManager
	 * @return
	 * @throws DerivedTypedElementException
	 */
	@Override
	public List<EReference> evaluate(EObject source, IParameterValueList2 parameterValues, IFacetManager facetManager) throws DerivedTypedElementException {
		if (source.eClass().getEPackage() == ContextsPackage.eINSTANCE) {
			return new ContextsSwitch<List<EReference>>() {
				@Override
				public List<EReference> caseContext(Context object) {
					return filter(object.eClass(), filterContextReferences(object));
				}

				@Override
				public java.util.List<EReference> defaultCase(EObject object) {
					return filter(object.eClass(), ref -> true);
				};

			}.doSwitch(source);
		} else if (source.eClass().getEPackage() == UiPackage.eINSTANCE) {
			return filter(source.eClass(), ref -> true);
		}

		return Collections.emptyList();
	}

	protected Predicate<EReference> filterContextReferences(Context object) {
		return ref -> {
			if (ref == ContextsPackage.Literals.CONTEXT__PROTOTYPE) { // Show the Prototype reference only if it is set
				return object.getPrototype() != null;
			}
			return true;
		};
	}

	private List<EReference> filter(EClass eClass, Predicate<EReference> filter) {
		return eClass.getEAllReferences().stream()
				.filter(ref -> !allExcludedReferences.contains(ref))
				.filter(filter)
				.collect(Collectors.toList());
	}

}

Back to the top