]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/contentassist/AbstractContentAssistModifier.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / contentassist / AbstractContentAssistModifier.java
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/contentassist/AbstractContentAssistModifier.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/contentassist/AbstractContentAssistModifier.java
new file mode 100644 (file)
index 0000000..e578d6a
--- /dev/null
@@ -0,0 +1,212 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 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.browsing.ui.swt.contentassist;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Event;\r
+import org.eclipse.swt.widgets.Listener;\r
+import org.eclipse.swt.widgets.TreeItem;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.common.modifiers.EnumeratedValue;\r
+import org.simantics.browsing.ui.common.modifiers.Enumeration;\r
+import org.simantics.browsing.ui.content.Labeler.CustomModifier;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+abstract public class AbstractContentAssistModifier<T> implements CustomModifier {\r
+\r
+    protected final Enumeration<T>     enumeration;\r
+    protected final T value;\r
+\r
+    protected ContentAssistTextField   text;\r
+\r
+    public AbstractContentAssistModifier(Enumeration<T> enumeration, T value) {\r
+        if (enumeration == null)\r
+            throw new NullPointerException("null enumeration");\r
+        if (enumeration.size() == 0)\r
+            throw new IllegalArgumentException("");\r
+\r
+        this.enumeration = enumeration;\r
+        this.value = value;\r
+    }\r
+\r
+    @Override\r
+    public String getValue() {\r
+        if (value != null)\r
+            return value.toString();\r
+        return enumeration.values().get(0).getName();\r
+    }\r
+\r
+    @Override\r
+    public String isValid(String label) {\r
+        if (enumeration.findByName(label) == null)\r
+            return "Value '" + label + "' is not among the enumerated values " + enumeration.values();\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public void modify(String label) {\r
+       String error = isValid(label); \r
+       if(error != null)\r
+            throw new IllegalArgumentException(error);\r
+        modifyWithValue(this.value, label);\r
+    }\r
+\r
+    abstract void modifyWithValue(T oldEnumValue, String enumValue);\r
+\r
+    /**\r
+     * Override to customize the content assist proposal objects created from\r
+     * the enumerated values.\r
+     * \r
+     * @param value\r
+     * @return\r
+     */\r
+    protected NamedObject<T> createNamedObject(EnumeratedValue<T> value) {\r
+        return new NamedObject<T>(value.getObject(), value.getName());\r
+    }\r
+\r
+    protected List<NamedObject<T>> toNamedObjects(Enumeration<T> enumeration) {\r
+        List<NamedObject<T>> namedObjects = new ArrayList<NamedObject<T>>();\r
+        for (EnumeratedValue<T> v : enumeration.values())\r
+            namedObjects.add(createNamedObject(v));\r
+        return namedObjects;\r
+    }\r
+\r
+    @Override\r
+    public Object createControl(Object parentControl, Object controlItem, final int columnIndex, NodeContext context) {\r
+        Composite parent = (Composite) parentControl;\r
+        final TreeItem item = (TreeItem) controlItem;\r
+\r
+        List<NamedObject<T>> possibleValues = toNamedObjects(enumeration);\r
+        \r
+        EnumeratedValue<T> enuValue = enumeration.find(value);\r
+        \r
+        NamedObject<T> selectedValue = enuValue != null ? createNamedObject(enuValue) : null;\r
+\r
+        text = new ContentAssistTextField(parent, selectedValue, possibleValues, SWT.NONE);\r
+\r
+        Listener textListener = new Listener() {\r
+            String error;\r
+\r
+            @Override\r
+            public void handleEvent(final Event e) {\r
+                switch (e.type) {\r
+                    case SWT.Modify: {\r
+                        String newText = text.getControl().getText();\r
+//                        System.out.println("VALIDATE NEW TEXT: " + newText);\r
+                        error = isValid(newText);\r
+                        if (error != null) {\r
+                            text.getControl().setBackground(text.getDisplay().getSystemColor(SWT.COLOR_RED));\r
+//                                System.out.println("validation error: " + error);\r
+                        } else {\r
+                            text.getControl().setBackground(null);\r
+                        }\r
+//                        text.getDisplay().asyncExec(new Runnable() {\r
+//                            @Override\r
+//                            public void run() {\r
+                        //if (!text.getControl().isDisposed())\r
+                        //text.getControl().traverse(SWT.TRAVERSE_ARROW_NEXT);\r
+                        //text.getControl().setSelection(text.getControl().getCaretPosition());\r
+//                            }\r
+//                        });\r
+                        break;\r
+                    }\r
+                    case SWT.Verify:\r
+                        // Safety check since it seems that this may happen with\r
+                        // virtual trees.\r
+//                        if (item.isDisposed())\r
+//                            return;\r
+\r
+//                        newText = text.getControl().getText();\r
+//                        String leftText = newText.substring(0, e.start);\r
+//                        String rightText = newText.substring(e.end, newText.length());\r
+//                        GC gc = new GC(text.getControl());\r
+//                        Point size = gc.textExtent(leftText + e.text + rightText);\r
+//                        gc.dispose();\r
+//                        size = text.getControl().computeSize(size.x, SWT.DEFAULT);\r
+//                        Rectangle itemRect = item.getBounds(columnIndex),\r
+//                        rect = tree.getClientArea();\r
+//                        editor.minimumWidth = Math.max(size.x, itemRect.width) + insetX * 2;\r
+//                        int left = itemRect.x,\r
+//                        right = rect.x + rect.width;\r
+//                        editor.minimumWidth = Math.min(editor.minimumWidth, right - left);\r
+//                        editor.minimumHeight = size.y + insetY * 2;\r
+//                        editor.layout();\r
+                        break;\r
+//                    case SWT.FocusOut: {\r
+//                        System.out.println("focus out");\r
+//                        String t = text.getControl().getText();\r
+//                        modify(t);\r
+//\r
+//                        // Item may be disposed if the tree gets reset after a previous editing.\r
+//                        if (!item.isDisposed()) {\r
+//                            item.setText(columnIndex, t);\r
+//                            //queueSelectionRefresh(context);\r
+//                        }\r
+//                        text.dispose();\r
+//                        break;\r
+//                    }\r
+                    case SWT.Traverse: {\r
+                        //System.out.println(AbstractContentAssistEnumerationModifier.class.getSimpleName() + " TRAVERSE: " + e.detail);\r
+                        switch (e.detail) {\r
+                            case SWT.TRAVERSE_RETURN:\r
+                                //System.out.println("TRAVERSE: RETURN");\r
+                                INamedObject obj = text.getResult();\r
+                                String txt = obj != null ? obj.getName() : text.getControl().getText();\r
+                                if (txt ==  null || error != null) {\r
+                                    e.doit = false;\r
+                                    return;\r
+                                }\r
+                                modify(txt);\r
+                                if (!item.isDisposed()) {\r
+                                    item.setText(columnIndex, txt);\r
+                                    //queueSelectionRefresh(context);\r
+                                }\r
+                                // FALL THROUGH\r
+                            case SWT.TRAVERSE_ESCAPE:\r
+                                //System.out.println("TRAVERSE: ESCAPE");\r
+                                text.dispose();\r
+                                e.doit = false;\r
+                                break;\r
+                            default:\r
+                                //System.out.println("unhandled traversal: " + e.detail);\r
+                                break;\r
+                        }\r
+                        break;\r
+                    }\r
+                }\r
+            }\r
+        };\r
+        text.getControl().addListener(SWT.Modify, textListener);\r
+//        text.getControl().addListener(SWT.Verify, textListener);\r
+//        text.getControl().addListener(SWT.FocusOut, textListener);\r
+        text.getControl().addListener(SWT.Traverse, textListener);\r
+\r
+        text.setFocus();\r
+        text.text.selectAll();\r
+        text.getDisplay().asyncExec(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                if (!text.isDisposed())\r
+                    text.openAssist();\r
+            }\r
+        });\r
+\r
+        return text;\r
+    }\r
+\r
+}\r