]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/widgets/TrackedCCombo.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / widgets / TrackedCCombo.java
diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/widgets/TrackedCCombo.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/widgets/TrackedCCombo.java
new file mode 100644 (file)
index 0000000..8a5d38c
--- /dev/null
@@ -0,0 +1,352 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.ui.widgets;\r
+\r
+import org.eclipse.core.runtime.Assert;\r
+import org.eclipse.core.runtime.ListenerList;\r
+import org.eclipse.jface.dialogs.IInputValidator;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.custom.CCombo;\r
+import org.eclipse.swt.events.DisposeEvent;\r
+import org.eclipse.swt.events.DisposeListener;\r
+import org.eclipse.swt.events.FocusEvent;\r
+import org.eclipse.swt.events.FocusListener;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.events.KeyListener;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.events.MouseEvent;\r
+import org.eclipse.swt.events.MouseListener;\r
+import org.eclipse.swt.events.MouseTrackListener;\r
+import org.eclipse.swt.graphics.Color;\r
+import org.eclipse.swt.widgets.Composite;\r
+\r
+/**\r
+ * This is a TrackedTest SWT Text-widget 'decorator'.\r
+ * \r
+ * It implements the necessary listeners to achieve the text widget behaviour\r
+ * needed by Simantics. User notification about modifications is provided via an\r
+ * Action instance given by the user.\r
+ * \r
+ * @see org.simantics.utils.ui.widgets.TrackedTextTest\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class TrackedCCombo {\r
+    private static final int EDITING = 1 << 0;\r
+    private static final int MODIFIED_DURING_EDITING = 1 << 1;\r
+\r
+    /**\r
+     * Used to tell whether or not a mouseDown has occured after a focusGained\r
+     * event to be able to select the whole text field when it is pressed for\r
+     * the first time while the widget holds focus.\r
+     */\r
+    private static final int MOUSE_DOWN_FIRST_TIME = 1 << 2;\r
+    \r
+    private int               state;\r
+\r
+    private String            textBeforeEdit;\r
+\r
+    private CCombo            combo;\r
+\r
+    private CompositeListener listener;\r
+\r
+    private ListenerList      modifyListeners;\r
+\r
+    private IInputValidator   validator;\r
+\r
+    //private static Color highlightColor = new Color(null, 250, 250, 250);\r
+    //private static Color inactiveColor = new Color(null, 240, 240, 240);\r
+    private static Color highlightColor = new Color(null, 254, 255, 197);\r
+    private static Color inactiveColor = new Color(null, 245, 246, 190);\r
+    private static Color invalidInputColor = new Color(null, 255, 128, 128);\r
+\r
+    /**\r
+     * A composite of many UI listeners for creating the functionality of this\r
+     * class.\r
+     */\r
+    private class CompositeListener\r
+    implements ModifyListener, DisposeListener, KeyListener, MouseTrackListener,\r
+            MouseListener, FocusListener\r
+    {\r
+        // Keyboard/editing events come in the following order:\r
+        //   1. keyPressed\r
+        //   2. verifyText\r
+        //   3. modifyText\r
+        //   4. keyReleased\r
+        \r
+        public void modifyText(ModifyEvent e) {\r
+            //System.out.println("modifyText: " + e);\r
+            if (isEditing())\r
+                setModified(true);\r
+\r
+            String valid = isTextValid();\r
+            if (valid != null) {\r
+                getWidget().setBackground(invalidInputColor);\r
+            } else {\r
+                if (isEditing())\r
+                    getWidget().setBackground(null);\r
+                else\r
+                    getWidget().setBackground(inactiveColor);\r
+            }\r
+        }\r
+\r
+        public void widgetDisposed(DisposeEvent e) {\r
+            combo.removeModifyListener(this);\r
+        }\r
+\r
+        public void keyPressed(KeyEvent e) {\r
+            //System.out.println("keyPressed: " + e);\r
+            if (!isEditing()) {\r
+                // ESC, ENTER & keypad ENTER must not start editing \r
+                if (e.keyCode == SWT.ESC)\r
+                    return;\r
+\r
+                if (e.keyCode == SWT.F2 || e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {\r
+                    startEdit(true);\r
+                } else if (e.character != '\0') {\r
+                    startEdit(false);\r
+                }\r
+            } else {\r
+                // ESC reverts any changes made during this edit\r
+                if (e.keyCode == SWT.ESC) {\r
+                    revertEdit();\r
+                }\r
+                if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {\r
+                    applyEdit();\r
+                }                    \r
+            }\r
+        }\r
+\r
+        public void keyReleased(KeyEvent e) {\r
+            //System.out.println("keyReleased: " + e);\r
+        }\r
+\r
+        public void mouseEnter(MouseEvent e) {\r
+            //System.out.println("mouseEnter");\r
+            if (!isEditing()) {\r
+                getWidget().setBackground(highlightColor);\r
+            }\r
+        }\r
+\r
+        public void mouseExit(MouseEvent e) {\r
+            //System.out.println("mouseExit");\r
+            if (!isEditing()) {\r
+                getWidget().setBackground(inactiveColor);\r
+            }\r
+        }\r
+\r
+        public void mouseHover(MouseEvent e) {\r
+            //System.out.println("mouseHover");\r
+        }\r
+\r
+        public void mouseDoubleClick(MouseEvent e) {\r
+            //System.out.println("mouseDoubleClick: " + e);\r
+//            if (e.button == 1) {\r
+//                getWidget().selectAll();\r
+//            }\r
+        }\r
+\r
+        public void mouseDown(MouseEvent e) {\r
+            //System.out.println("mouseDown: " + e);\r
+            if (!isEditing()) {\r
+                // In reality we should never get here, since focusGained\r
+                // always comes before mouseDown, but let's keep this\r
+                // fallback just to be safe.\r
+                if (e.button == 1) {\r
+                    startEdit(true);\r
+                }\r
+            } else {\r
+                if (e.button == 1 && (state & MOUSE_DOWN_FIRST_TIME) != 0) {\r
+//                    getWidget().selectAll();\r
+                    state &= ~MOUSE_DOWN_FIRST_TIME;\r
+                }\r
+            }\r
+        }\r
+\r
+        public void mouseUp(MouseEvent e) {\r
+        }\r
+\r
+        public void focusGained(FocusEvent e) {\r
+            //System.out.println("focusGained");\r
+            if (!isEditing()) {\r
+                startEdit(true);\r
+            }\r
+        }\r
+\r
+        public void focusLost(FocusEvent e) {\r
+            //System.out.println("focusLost");\r
+            if (isEditing()) {\r
+                applyEdit();\r
+            }\r
+        }\r
+    }\r
+    \r
+    public TrackedCCombo(CCombo combo) {\r
+        Assert.isNotNull(combo);\r
+        this.state = 0;\r
+        this.combo = combo;\r
+\r
+        initialize();\r
+    }\r
+\r
+    public TrackedCCombo(Composite parent, int style) {\r
+        this.state = 0;\r
+        this.combo = new CCombo(parent, style);\r
+\r
+        initialize();\r
+    }\r
+\r
+    /**\r
+     * Common initialization. Assumes that text is already created.\r
+     */\r
+    private void initialize() {\r
+        Assert.isNotNull(combo);\r
+        \r
+        combo.setBackground(inactiveColor);\r
+        \r
+        listener = new CompositeListener();\r
+        \r
+        combo.addModifyListener(listener);\r
+        combo.addDisposeListener(listener);\r
+        combo.addKeyListener(listener);\r
+        combo.addMouseTrackListener(listener);\r
+        combo.addMouseListener(listener);\r
+        combo.addFocusListener(listener);\r
+    }    \r
+    \r
+    private void startEdit(boolean selectAll) {\r
+        if (isEditing()) {\r
+            // Print some debug incase we end are in an invalid state\r
+            try {\r
+                throw new Exception("TrackedText: BUG: startEdit called when in editing state");\r
+            } catch (Exception e) {\r
+                System.out.println(e);\r
+            }\r
+        }\r
+        //System.out.println("start edit: selectall=" + selectAll + ", text=" + text.getText() + ", caretpos=" + caretPositionBeforeEdit);\r
+\r
+        // Backup text-field data for reverting purposes\r
+        textBeforeEdit = combo.getText();\r
+\r
+        // Signal editing state\r
+        combo.setBackground(null);\r
+        \r
+//        if (selectAll) {\r
+//            text.selectAll();\r
+//        }\r
+        state |= EDITING | MOUSE_DOWN_FIRST_TIME;\r
+    }\r
+\r
+    private void applyEdit() {\r
+        try {\r
+            if (isTextValid() != null) {\r
+                // Just discard the edit.\r
+                combo.setText(textBeforeEdit);\r
+            } else if (isModified() && !combo.getText().equals(textBeforeEdit)) {\r
+                //System.out.println("apply");\r
+                if (modifyListeners != null) {\r
+                    TrackedModifyEvent event = new TrackedModifyEvent(combo, combo.getText());\r
+                    for (Object o : modifyListeners.getListeners()) {\r
+                        ((TrackedModifyListener) o).modifyText(event);\r
+                    }\r
+                }\r
+            }\r
+        } finally {\r
+            endEdit();\r
+        }\r
+    }\r
+    \r
+    private void endEdit() {\r
+        if (!isEditing()) {\r
+            // Print some debug incase we end are in an invalid state\r
+            //ExceptionUtils.logError(new Exception("BUG: endEdit called when not in editing state"));\r
+            System.out.println("BUG: endEdit called when not in editing state");\r
+        }\r
+        combo.setBackground(inactiveColor);\r
+        //System.out.println("endEdit: " + text.getText() + ", caret: " + text.getCaretLocation() + ", selection: " + text.getSelection());\r
+        // Always move the caret to the end of the string\r
+//        text.setSelection(text.getCharCount());\r
+        \r
+        state &= ~(EDITING | MOUSE_DOWN_FIRST_TIME);\r
+        setModified(false);\r
+    }\r
+\r
+    private void revertEdit() {\r
+        if (!isEditing()) {\r
+            // Print some debug incase we end are in an invalid state\r
+            //ExceptionUtils.logError(new Exception("BUG: revertEdit called when not in editing state"));\r
+            System.out.println("BUG: revertEdit called when not in editing state");\r
+        }\r
+        combo.setText(textBeforeEdit);\r
+//        text.setSelection(caretPositionBeforeEdit);\r
+        combo.setBackground(inactiveColor);\r
+        state &= ~(EDITING | MOUSE_DOWN_FIRST_TIME);\r
+        setModified(false);\r
+    }\r
+    \r
+    private boolean isEditing() {\r
+        return (state & EDITING) != 0;\r
+    }\r
+    \r
+    private void setModified(boolean modified) {\r
+        if (modified) {\r
+            state |= MODIFIED_DURING_EDITING;\r
+        } else {\r
+            state &= ~MODIFIED_DURING_EDITING;\r
+        }\r
+    }\r
+    \r
+    private boolean isModified() {\r
+        return (state & MODIFIED_DURING_EDITING) != 0;\r
+    }\r
+    \r
+    public void setText(String text) {\r
+        this.combo.setText(text);\r
+    }\r
+    \r
+    public void setTextWithoutNotify(String text) {\r
+        this.combo.removeModifyListener(listener);\r
+        setText(text);\r
+        this.combo.addModifyListener(listener);\r
+    }\r
+\r
+    public CCombo getWidget() {\r
+        return combo;\r
+    }\r
+    \r
+    public synchronized void addModifyListener(TrackedModifyListener listener) {\r
+        if (modifyListeners == null) {\r
+            modifyListeners = new ListenerList(ListenerList.IDENTITY);\r
+        }\r
+        modifyListeners.add(listener);\r
+    }\r
+    \r
+    public synchronized void removeModifyListener(TrackedModifyListener listener) {\r
+        if (modifyListeners == null)\r
+            return;\r
+        modifyListeners.remove(listener);\r
+    }\r
+    \r
+    public void setInputValidator(IInputValidator validator) {\r
+        if (validator != this.validator) {\r
+            this.validator = validator;\r
+        }\r
+    }\r
+    \r
+    private String isTextValid() {\r
+        if (validator != null) {\r
+            return validator.isValid(getWidget().getText());\r
+        }\r
+        return null;\r
+    }\r
+}\r