[498748] Trigger the editExpression of a text widget on focus lost

Bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=498748
Change-Id: Idbdc7fd537b7e6272eb69442ebd0defc1dffb4e2
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
diff --git a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/controllers/EEFTextController.java b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/controllers/EEFTextController.java
index ffd9688..12011c4 100644
--- a/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/controllers/EEFTextController.java
+++ b/plugins/org.eclipse.eef.core/src/org/eclipse/eef/core/internal/controllers/EEFTextController.java
@@ -12,10 +12,6 @@
 
 import java.util.HashMap;
 import java.util.Map;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.TimeUnit;
 
 import org.eclipse.eef.EEFTextDescription;
 import org.eclipse.eef.EEFWidgetDescription;
@@ -52,16 +48,6 @@
 	private IConsumer<Object> newValueConsumer;
 
 	/**
-	 * Executor service used to run the update of the text field.
-	 */
-	private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
-
-	/**
-	 * This future contains the update to be performed.
-	 */
-	private ScheduledFuture<?> currentUpdatedValueFuture;
-
-	/**
 	 * The constructor.
 	 *
 	 * @param description
@@ -82,30 +68,19 @@
 
 	@Override
 	public void updateValue(final String text) {
-		if (this.currentUpdatedValueFuture != null && !this.currentUpdatedValueFuture.isDone()) {
-			this.currentUpdatedValueFuture.cancel(true);
-		}
-
-		Runnable runnable = new Runnable() {
+		this.contextAdapter.performModelChange(new Runnable() {
 			@Override
 			public void run() {
-				EEFTextController.this.contextAdapter.performModelChange(new Runnable() {
-					@Override
-					public void run() {
-						String editExpression = EEFTextController.this.description.getEditExpression();
-						EAttribute eAttribute = EefPackage.Literals.EEF_TEXT_DESCRIPTION__EDIT_EXPRESSION;
+				String editExpression = EEFTextController.this.description.getEditExpression();
+				EAttribute eAttribute = EefPackage.Literals.EEF_TEXT_DESCRIPTION__EDIT_EXPRESSION;
 
-						Map<String, Object> variables = new HashMap<String, Object>();
-						variables.putAll(EEFTextController.this.variableManager.getVariables());
-						variables.put(EEFExpressionUtils.EEFText.NEW_VALUE, text);
+				Map<String, Object> variables = new HashMap<String, Object>();
+				variables.putAll(EEFTextController.this.variableManager.getVariables());
+				variables.put(EEFExpressionUtils.EEFText.NEW_VALUE, text);
 
-						EvalFactory.of(EEFTextController.this.interpreter, variables).logIfBlank(eAttribute).call(editExpression);
-					}
-				});
+				EvalFactory.of(EEFTextController.this.interpreter, variables).logIfBlank(eAttribute).call(editExpression);
 			}
-		};
-		final long scheduleTime = 500L;
-		this.currentUpdatedValueFuture = this.executor.schedule(runnable, scheduleTime, TimeUnit.MILLISECONDS);
+		});
 	}
 
 	/**
diff --git a/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFTextLifecycleManager.java b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFTextLifecycleManager.java
index 8d64fb1..c52a550 100644
--- a/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFTextLifecycleManager.java
+++ b/plugins/org.eclipse.eef.ide.ui/src/org/eclipse/eef/ide/ui/internal/widgets/EEFTextLifecycleManager.java
@@ -29,8 +29,8 @@
 import org.eclipse.sirius.common.interpreter.api.IVariableManager;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.StyledText;
-import org.eclipse.swt.events.ModifyEvent;
-import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.FocusEvent;
+import org.eclipse.swt.events.FocusListener;
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.widgets.Composite;
@@ -67,9 +67,9 @@
 	private IEEFTextController controller;
 
 	/**
-	 * The listener on the text.
+	 * The listener on the text field.
 	 */
-	private ModifyListener modifyListener;
+	private FocusListener focusListener;
 
 	/**
 	 * The widget factory.
@@ -177,16 +177,21 @@
 	public void aboutToBeShown() {
 		super.aboutToBeShown();
 
-		this.modifyListener = new ModifyListener() {
+		this.focusListener = new FocusListener() {
 			@Override
-			public void modifyText(ModifyEvent event) {
+			public void focusLost(FocusEvent e) {
 				if (!EEFTextLifecycleManager.this.container.isRenderingInProgress()) {
 					controller.updateValue(text.getText());
 					EEFTextLifecycleManager.this.setStyle();
 				}
 			}
+
+			@Override
+			public void focusGained(FocusEvent e) {
+				// do nothing
+			}
 		};
-		this.text.addModifyListener(this.modifyListener);
+		this.text.addFocusListener(this.focusListener);
 
 		this.controller.onNewValue(new IConsumer<Object>() {
 			@Override
@@ -239,7 +244,7 @@
 		super.aboutToBeHidden();
 
 		if (!text.isDisposed()) {
-			this.text.removeModifyListener(this.modifyListener);
+			this.text.removeFocusListener(this.focusListener);
 		}
 		this.controller.removeNewValueConsumer();
 	}