Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2153ba4f20a1558bafad0d349361b49c199c67dc (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
/*******************************************************************************
 * Copyright (c) 2015, 2016 Obeo.
 * 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:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.eef.ide.ui.properties.api;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.eclipse.eef.EEFContainerDescription;
import org.eclipse.eef.EEFControlDescription;
import org.eclipse.eef.EEFDynamicMappingFor;
import org.eclipse.eef.EefPackage;
import org.eclipse.eef.core.api.EEFExpressionUtils;
import org.eclipse.eef.core.api.EEFGroup;
import org.eclipse.eef.core.api.EEFPage;
import org.eclipse.eef.core.api.utils.EvalFactory;
import org.eclipse.eef.ide.ui.properties.internal.RefreshIdsHolder;
import org.eclipse.eef.properties.ui.api.AbstractEEFTabDescriptor;
import org.eclipse.eef.properties.ui.api.IEEFSectionDescriptor;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;

/**
 * The implementation of the {@link AbstractEEFTabDescriptor} using the {@link EEFPage}.
 *
 * @author sbegaudeau
 */
public class EEFTabDescriptor extends AbstractEEFTabDescriptor {
	/**
	 * The label used by default.
	 */
	private static final String DEFAULT_PAGE_LABEL = "General"; //$NON-NLS-1$

	/**
	 * The {@link EEFPage}.
	 */
	private EEFPage eefPage;

	/**
	 * The constructor.
	 *
	 * @param eefPage
	 *            The EEFPage
	 */
	public EEFTabDescriptor(EEFPage eefPage) {
		super();
		this.eefPage = eefPage;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.eef.properties.ui.api.IEEFTabDescriptor#getId()
	 */
	@Override
	public String getId() {
		// FIXME [SBE] I am commenting this code because while it's better it breaks the live edition in the workspace
		// if (this.eefPage.isUnique()) {
		// return this.eefPage.getDescription().getIdentifier();
		// }

		StringBuilder identifier = new StringBuilder();

		identifier.append(this.eefPage.getDescription().getIdentifier());
		List<EEFGroup> groups = this.eefPage.getGroups();
		for (EEFGroup eefGroup : groups) {
			identifier.append(eefGroup.getDescription().getIdentifier());

			Object groupSemanticElement = eefGroup.getVariableManager().getVariables().get(EEFExpressionUtils.SELF);
			if (groupSemanticElement instanceof EObject) {
				identifier.append(this.getIdForEObject((EObject) groupSemanticElement));
			}
			identifier.append(groupSemanticElement.hashCode());
		}

		// @formatter:off
		groups.stream().map(EEFGroup::getDescription)
			.flatMap(description -> description.getControls().stream().flatMap(this::getDynamicMappingFors))
			.filter(EEFDynamicMappingFor::isForceRefresh)
			.findFirst()
			.ifPresent(control -> identifier.append(System.currentTimeMillis()));
		// @formatter:on

		Object pageSemanticElement = this.eefPage.getVariableManager().getVariables().get(EEFExpressionUtils.SELF);
		if (pageSemanticElement instanceof EObject) {
			identifier.append(this.getIdForEObject((EObject) pageSemanticElement));
		}
		identifier.append(System.identityHashCode(pageSemanticElement));
		return identifier.toString();
	}

	/**
	 * Returns recursively a stream of the dynamic mappings for the given control.
	 *
	 * @param eefControlDescription
	 *            The description of the control
	 * @return The stream of the dynamic mapping
	 */
	private Stream<EEFDynamicMappingFor> getDynamicMappingFors(EEFControlDescription eefControlDescription) {
		Stream<EEFDynamicMappingFor> stream = Stream.empty();

		if (eefControlDescription instanceof EEFDynamicMappingFor) {
			stream = Stream.of((EEFDynamicMappingFor) eefControlDescription);
		} else if (eefControlDescription instanceof EEFContainerDescription) {
			EEFContainerDescription eefContainerDescription = (EEFContainerDescription) eefControlDescription;
			stream = eefContainerDescription.getControls().stream().flatMap(this::getDynamicMappingFors);
		}

		return stream;
	}

	/**
	 * Returns an unique identifier for the given EObject which will stay the same even after changes which would impact
	 * its URI (for example, changing the name of an EClass).
	 *
	 * @param eObject
	 *            The EObject
	 * @return The unique identifier of the given EObject
	 */
	private Integer getIdForEObject(EObject eObject) {
		return RefreshIdsHolder.getOrCreateID(eObject);
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.eef.properties.ui.api.IEEFTabDescriptor#getLabel()
	 */
	@Override
	public String getLabel() {
		String labelExpression = this.eefPage.getDescription().getLabelExpression();
		EAttribute eAttribute = EefPackage.Literals.EEF_PAGE_DESCRIPTION__LABEL_EXPRESSION;

		return EvalFactory.of(this.eefPage.getInterpreter(), this.eefPage.getVariableManager()).logIfBlank(eAttribute).logIfInvalidType(String.class)
				.defaultValue(DEFAULT_PAGE_LABEL).evaluate(labelExpression);
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.eef.properties.ui.api.AbstractEEFTabDescriptor#getSectionDescriptors()
	 */
	@Override
	public List<IEEFSectionDescriptor> getSectionDescriptors() {
		List<IEEFSectionDescriptor> sectionDescriptors = new ArrayList<IEEFSectionDescriptor>();
		sectionDescriptors.add(new EEFSectionDescriptor(this.eefPage));
		return sectionDescriptors;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.eef.properties.ui.api.AbstractEEFTabDescriptor#getCategory()
	 */
	@Override
	public String getCategory() {
		return "EEF"; //$NON-NLS-1$
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.eef.properties.ui.api.AbstractEEFTabDescriptor#isIndented()
	 */
	@Override
	public boolean isIndented() {
		return this.eefPage.getDescription().isIndented();
	}
}

Back to the top