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