Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierre-Charles David2016-01-21 16:04:47 +0000
committerStephane Begaudeau2016-01-28 08:47:48 +0000
commit404eb5b44343089184cdc8ec404adefcd09bc03d (patch)
treef14c135323f5fcdb40f3f1cdb83edbce61e6a0d8
parent2d5f6afc4125743eaea5ad6c2045c296b95a5a0e (diff)
downloadorg.eclipse.eef-404eb5b44343089184cdc8ec404adefcd09bc03d.tar.gz
org.eclipse.eef-404eb5b44343089184cdc8ec404adefcd09bc03d.tar.xz
org.eclipse.eef-404eb5b44343089184cdc8ec404adefcd09bc03d.zip
[482528] Refactor EEPageImpl
Refactor EEPageImpl using utilities extracted from EEFViewImpl into the new Util class. Bug: 482528 Change-Id: I351e7b55b6a566749b69f4dc4350971451dd02bb Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
-rw-r--r--plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/EEFPageImpl.java40
-rw-r--r--plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/EEFViewImpl.java36
-rw-r--r--plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/Util.java78
3 files changed, 95 insertions, 59 deletions
diff --git a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/EEFPageImpl.java b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/EEFPageImpl.java
index 70fe6f9d6..fdeb12430 100644
--- a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/EEFPageImpl.java
+++ b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/EEFPageImpl.java
@@ -19,6 +19,7 @@ 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.EEFView;
+import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.sirius.common.interpreter.api.IEvaluationResult;
import org.eclipse.sirius.common.interpreter.api.IInterpreter;
@@ -87,34 +88,16 @@ public class EEFPageImpl implements EEFPage {
* Initialize the variables of the EEFPage.
*/
public void initialize() {
- List<EEFGroupDescription> eefGroupDescriptions = this.eefPageDescription.getGroups();
- for (EEFGroupDescription eefGroupDescription : eefGroupDescriptions) {
- String semanticCandidatesExpression = eefGroupDescription.getSemanticCandidateExpression();
- if (semanticCandidatesExpression != null && semanticCandidatesExpression.trim().length() > 0) {
- IEvaluationResult evaluationResult = this.interpreter.evaluateExpression(this.getVariableManager().getVariables(),
- semanticCandidatesExpression);
- if (evaluationResult.getValue() instanceof Iterable<?>) {
- @SuppressWarnings("unchecked")
- Iterable<Object> groupSemanticCandidates = (Iterable<Object>) evaluationResult.getValue();
- for (Object groupSemanticCandidate : groupSemanticCandidates) {
- IVariableManager childVariableManager = this.getVariableManager().createChild();
- childVariableManager.put(EEFExpressionUtils.SELF, groupSemanticCandidate);
- EEFGroupImpl eefGroupImpl = new EEFGroupImpl(this, eefGroupDescription, childVariableManager, this.interpreter,
- this.editingDomain);
- eefGroups.add(eefGroupImpl);
- }
- } else {
- Object groupSemanticCandidate = evaluationResult.getValue();
+ for (EEFGroupDescription eefGroupDescription : eefPageDescription.getGroups()) {
+ String semanticCandidatesExpression = Util.firstNonBlank(eefGroupDescription.getSemanticCandidateExpression(), "var:self"); //$NON-NLS-1$
+ IEvaluationResult result = interpreter.evaluateExpression(this.getVariableManager().getVariables(), semanticCandidatesExpression);
+ if (result.success()) {
+ for (EObject groupSemanticCandidate : result.asEObjects()) {
IVariableManager childVariableManager = this.getVariableManager().createChild();
childVariableManager.put(EEFExpressionUtils.SELF, groupSemanticCandidate);
- EEFGroupImpl eefGroupImpl = new EEFGroupImpl(this, eefGroupDescription, childVariableManager, this.interpreter,
- this.editingDomain);
+ EEFGroupImpl eefGroupImpl = new EEFGroupImpl(this, eefGroupDescription, childVariableManager, interpreter, editingDomain);
eefGroups.add(eefGroupImpl);
}
- } else {
- IVariableManager childVariableManager = this.getVariableManager().createChild();
- EEFGroupImpl eefGroupImpl = new EEFGroupImpl(this, eefGroupDescription, childVariableManager, this.interpreter, this.editingDomain);
- eefGroups.add(eefGroupImpl);
}
}
}
@@ -129,8 +112,8 @@ public class EEFPageImpl implements EEFPage {
String labelExpression = this.eefPageDescription.getLabelExpression();
if (labelExpression != null) {
IEvaluationResult evaluationResult = this.interpreter.evaluateExpression(this.getVariableManager().getVariables(), labelExpression);
- if (evaluationResult.getValue() != null) {
- return evaluationResult.getValue().toString();
+ if (evaluationResult.success()) {
+ return evaluationResult.asString();
}
}
return labelExpression;
@@ -156,6 +139,11 @@ public class EEFPageImpl implements EEFPage {
return this.eefPageDescription;
}
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.eclipse.eef.core.api.EEFPage#getView()
+ */
@Override
public EEFView getView() {
return this.eefView;
diff --git a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/EEFViewImpl.java b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/EEFViewImpl.java
index 109e453fc..01dc7cd50 100644
--- a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/EEFViewImpl.java
+++ b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/EEFViewImpl.java
@@ -118,7 +118,7 @@ public class EEFViewImpl implements EEFView {
* @return an actual {@link EEFPage} setup according to the description.
*/
private EEFPageImpl createPage(EEFPageDescription description) {
- Object candidate = computeCandidate(this.variableManager, description.getSemanticCandidateExpression());
+ Object candidate = Util.computeCandidate(this.interpreter, this.variableManager, description.getSemanticCandidateExpression());
IVariableManager childVariableManager = this.variableManager.createChild();
if (candidate != null) {
childVariableManager.put(EEFExpressionUtils.SELF, candidate);
@@ -141,7 +141,7 @@ public class EEFViewImpl implements EEFView {
for (EEFPage eefPage : this.eefPages) {
String pageSemanticCandidateExpression = eefPage.getDescription().getSemanticCandidateExpression();
- if (!isBlank(pageSemanticCandidateExpression)) {
+ if (!Util.isBlank(pageSemanticCandidateExpression)) {
IEvaluationResult evaluationResult = this.interpreter.evaluateExpression(this.variableManager.getVariables(),
pageSemanticCandidateExpression);
if (evaluationResult.success()) {
@@ -154,7 +154,7 @@ public class EEFViewImpl implements EEFView {
List<EEFGroup> groups = eefPage.getGroups();
for (EEFGroup eefGroup : groups) {
String groupSemanticCandidateExpression = eefGroup.getDescription().getSemanticCandidateExpression();
- if (!isBlank(groupSemanticCandidateExpression)) {
+ if (!Util.isBlank(groupSemanticCandidateExpression)) {
IEvaluationResult evaluationResult = this.interpreter.evaluateExpression(eefPage.getVariableManager().getVariables(),
groupSemanticCandidateExpression);
if (evaluationResult.success()) {
@@ -188,34 +188,4 @@ public class EEFViewImpl implements EEFView {
return this.eefViewDescription;
}
- /**
- * Helper to evaluate a SemanticCandidateExpression.
- *
- * @param context
- * the evaluation context (variables).
- * @param expression
- * the expression to evaluate.
- * @return the result, or <code>null</code> if the evaluation failed.
- */
- private Object computeCandidate(IVariableManager context, String expression) {
- if (!isBlank(expression)) {
- IEvaluationResult evaluationResult = interpreter.evaluateExpression(context.getVariables(), expression);
- if (evaluationResult.success()) {
- return evaluationResult.getValue();
- }
- }
- return null;
- }
-
- /**
- * Tests if a string is blank (i.e. null, empty, or containing only whitespace).
- *
- * @param s
- * the string to test.
- * @return <code>true</code> if and only if the string is blank.
- */
- private boolean isBlank(String s) {
- return s == null || s.trim().length() == 0;
- }
-
}
diff --git a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/Util.java b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/Util.java
new file mode 100644
index 000000000..b7a8198db
--- /dev/null
+++ b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/Util.java
@@ -0,0 +1,78 @@
+/*******************************************************************************
+ * Copyright (c) 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.core.internal;
+
+import org.eclipse.sirius.common.interpreter.api.IEvaluationResult;
+import org.eclipse.sirius.common.interpreter.api.IInterpreter;
+import org.eclipse.sirius.common.interpreter.api.IVariableManager;
+
+/**
+ * Shared utility methods.
+ *
+ * @author Pierre-Charles David <pierre-charles.david@obeo.fr>
+ */
+public final class Util {
+ /**
+ * Prevent instantiation.
+ */
+ private Util() {
+ // Prevent instantiation.
+ }
+
+ /**
+ * Helper to evaluate a SemanticCandidateExpression.
+ *
+ * @param itp
+ * the interpreter to use.
+ * @param context
+ * the evaluation context (variables).
+ * @param expression
+ * the expression to evaluate.
+ * @return the result, or <code>null</code> if the evaluation failed.
+ */
+ public static Object computeCandidate(IInterpreter itp, IVariableManager context, String expression) {
+ if (!isBlank(expression)) {
+ IEvaluationResult evaluationResult = itp.evaluateExpression(context.getVariables(), expression);
+ if (evaluationResult.success()) {
+ return evaluationResult.getValue();
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns the first of a series of alternative string values which is not {@link Util#blank blank}.
+ *
+ * @param alternatives
+ * the alernative strings, in order of preference.
+ * @return the first non-blank alternative, or <code>null</code> if they are all blank.
+ */
+ public static String firstNonBlank(String... alternatives) {
+ for (String s : alternatives) {
+ if (!isBlank(s)) {
+ return s;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Tests if a string is blank (i.e. null, empty, or containing only whitespace).
+ *
+ * @param s
+ * the string to test.
+ * @return <code>true</code> if and only if the string is blank.
+ */
+ public static boolean isBlank(String s) {
+ return s == null || s.trim().length() == 0;
+ }
+
+}

Back to the top