diff options
author | cletavernie | 2011-10-10 11:50:53 +0000 |
---|---|---|
committer | cletavernie | 2011-10-10 11:50:53 +0000 |
commit | 2a5fd9b1c37dc6bb03c6711cbc7873dbd27266dc (patch) | |
tree | 97e8f4eb6b48b25cab336caee17516932fc23ad8 | |
parent | aeccbc2827dd515f772fc5d3b71b0972de3729bb (diff) | |
download | org.eclipse.papyrus-2a5fd9b1c37dc6bb03c6711cbc7873dbd27266dc.tar.gz org.eclipse.papyrus-2a5fd9b1c37dc6bb03c6711cbc7873dbd27266dc.tar.xz org.eclipse.papyrus-2a5fd9b1c37dc6bb03c6711cbc7873dbd27266dc.zip |
360409: [Widgets] The StringEditor should provide a feature to auto-commit after a delay
https://bugs.eclipse.org/bugs/show_bug.cgi?id=360409
-rw-r--r-- | plugins/core/org.eclipse.papyrus.widgets/src/org/eclipse/papyrus/widgets/editors/StringEditor.java | 93 |
1 files changed, 92 insertions, 1 deletions
diff --git a/plugins/core/org.eclipse.papyrus.widgets/src/org/eclipse/papyrus/widgets/editors/StringEditor.java b/plugins/core/org.eclipse.papyrus.widgets/src/org/eclipse/papyrus/widgets/editors/StringEditor.java index 7c7be29b75a..4cc4d4d0755 100644 --- a/plugins/core/org.eclipse.papyrus.widgets/src/org/eclipse/papyrus/widgets/editors/StringEditor.java +++ b/plugins/core/org.eclipse.papyrus.widgets/src/org/eclipse/papyrus/widgets/editors/StringEditor.java @@ -11,11 +11,16 @@ *****************************************************************************/
package org.eclipse.papyrus.widgets.editors;
+import java.util.Timer;
+import java.util.TimerTask;
+
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.papyrus.widgets.databinding.TextObservableValue;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
@@ -32,13 +37,21 @@ import org.eclipse.swt.widgets.Text; *
* @author Camille Letavernier
*/
-public class StringEditor extends AbstractValueEditor implements KeyListener {
+public class StringEditor extends AbstractValueEditor implements KeyListener, ModifyListener {
/**
* The text box for editing this editor's value
*/
protected Text text;
+ private int delay = 600;
+
+ private boolean validateOnDelay = false;
+
+ private final Timer timer;
+
+ private TimerTask currentValidateTask;
+
/**
*
* Constructor.
@@ -83,6 +96,8 @@ public class StringEditor extends AbstractValueEditor implements KeyListener { text.addKeyListener(this);
setCommitOnFocusLost(text);
+
+ timer = new Timer(true);
}
@Override
@@ -186,4 +201,80 @@ public class StringEditor extends AbstractValueEditor implements KeyListener { this.text.setText(""); //$NON-NLS-1$;
}
}
+
+ /**
+ * Indicates that this editor should be automatically validated after
+ * a timer.
+ *
+ * @param validateOnDelay
+ */
+ public void setValidateOnDelay(boolean validateOnDelay) {
+ this.validateOnDelay = validateOnDelay;
+
+ if(validateOnDelay) {
+ text.addModifyListener(this);
+ } else {
+ text.removeModifyListener(this);
+ cancelCurrentTask();
+ }
+ }
+
+ /**
+ * Indicates that this editor should be automatically validated after
+ * the given timer
+ *
+ * @param millis
+ * The delay after which the editor should be automatically validated,
+ * in milliseconds. The default is 600ms
+ */
+ public void setValidateOnDelay(int millis) {
+ this.delay = millis;
+ setValidateOnDelay(true);
+ if(delay == 0) {
+ cancelCurrentTask();
+ }
+ }
+
+ private void cancelCurrentTask() {
+ if(currentValidateTask != null) {
+ currentValidateTask.cancel();
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void modifyText(ModifyEvent e) {
+ //SWT Thread
+ if(validateOnDelay) {
+ if(delay == 0) {
+ commit(); //Direct commit on edition, to avoid creating useless threads
+ return;
+ }
+ cancelCurrentTask();
+
+ currentValidateTask = new TimerTask() {
+
+ //Timer thread
+ @Override
+ public void run() {
+ StringEditor.this.getDisplay().syncExec(new Runnable() {
+
+ //SWT Thread
+ public void run() {
+ commit();
+ }
+ });
+ }
+ };
+ timer.schedule(currentValidateTask, delay);
+ }
+ }
+
+ @Override
+ public void dispose() {
+ cancelCurrentTask();
+ timer.cancel();
+ super.dispose();
+ }
}
|