]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java
Added inline filtering support for property name editing in UC editor
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / ComponentTypeViewerData.java
index a3e29debd21348990b8286a31727799428e837a2..b64380b285d7b76593060b2e40a6a7d09f502b8f 100644 (file)
@@ -40,8 +40,10 @@ import org.simantics.db.WriteGraph;
 import org.simantics.db.common.NamedResource;
 import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.function.DbConsumer;
 import org.simantics.layer0.Layer0;
 import org.simantics.modeling.userComponent.ComponentTypeCommands;
+import org.simantics.scl.runtime.function.Function2;
 import org.simantics.scl.runtime.function.Function4;
 import org.simantics.utils.ui.ErrorLogger;
 
@@ -86,9 +88,47 @@ public class ComponentTypeViewerData {
         this.componentType = componentType;
         this.form = form;
     }
-    
+
     public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
             Pattern namePattern) {
+        editName(table, editor, propertyInfo, selectedItem, column, namePattern, null);
+    }
+
+    public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
+            Pattern namePattern, DbConsumer<WriteGraph> extraWriter) {
+        editName(table, editor, propertyInfo, selectedItem, column,
+                null,
+                (pInfo, name) -> validatePropertyName(pInfo, name, namePattern),
+                extraWriter);
+    }
+
+    public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
+            Function2<ComponentTypeViewerPropertyInfo, String, String> nameFilter, Pattern namePattern, DbConsumer<WriteGraph> extraWriter) {
+        editName(table, editor, propertyInfo, selectedItem, column, nameFilter,
+                (pInfo, name) -> validatePropertyName(pInfo, name, namePattern),
+                extraWriter);
+    }
+
+    public void editName(Table table, TableEditor editor, ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
+            Function2<ComponentTypeViewerPropertyInfo, String, String> nameValidator)
+    {
+        editName(table, editor, propertyInfo, selectedItem, column, nameValidator, null);
+    }
+
+    public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
+            Function2<ComponentTypeViewerPropertyInfo, String, String> nameValidator, DbConsumer<WriteGraph> extraWriter) {
+        editName(table, editor, propertyInfo, selectedItem, column, null, nameValidator, extraWriter);
+    }
+
+    public void editName(
+            Table table,
+            TableEditor editor,
+            final ComponentTypeViewerPropertyInfo propertyInfo,
+            TableItem selectedItem,
+            int column,
+            Function2<ComponentTypeViewerPropertyInfo, String, String> nameFilter,
+            Function2<ComponentTypeViewerPropertyInfo, String, String> nameValidator,
+            DbConsumer<WriteGraph> extraWriter) {
         int extraStyle = propertyInfo.immutable ? SWT.READ_ONLY : 0;
         final Text text = new Text(table, SWT.NONE | extraStyle);
         org.eclipse.swt.widgets.Listener listener = 
@@ -98,11 +138,13 @@ public class ComponentTypeViewerData {
                 if (e.type == SWT.Dispose) {
                     form.setMessage(null);
                     return;
-                }
-
-                if (e.type == SWT.Modify) {
+                } else if (e.type == SWT.Verify) {
+                    // Filter input if necessary
+                    e.text = nameFilter != null ? nameFilter.apply(propertyInfo, e.text) : e.text;
+                    return;
+                } else if (e.type == SWT.Modify) {
                     // validate current name
-                    String error = validatePropertyName(propertyInfo, text.getText(), namePattern);
+                    String error = nameValidator.apply(propertyInfo, text.getText());
                     if (error != null) {
                         text.setBackground(text.getDisplay().getSystemColor(SWT.COLOR_RED));
                         form.setMessage(error, IMessageProvider.ERROR);
@@ -111,9 +153,7 @@ public class ComponentTypeViewerData {
                         form.setMessage(null);
                     }
                     return;
-                }
-
-                if (e.type == SWT.Traverse) {
+                } else if (e.type == SWT.Traverse) {
                     if (e.detail == SWT.TRAVERSE_ESCAPE) {
                         text.dispose();
                         e.doit = false;
@@ -126,7 +166,7 @@ public class ComponentTypeViewerData {
                 final String newValue = text.getText();
                 text.dispose();
 
-                String error = validatePropertyName(propertyInfo, newValue, namePattern);
+                String error = nameValidator.apply(propertyInfo, newValue);
                 if (error != null)
                     return;
 
@@ -150,10 +190,15 @@ public class ComponentTypeViewerData {
                         ComponentTypeCommands.rename(graph, propertyInfo.resource, newValue);
                         if (setLabel)
                             ComponentTypeCommands.setLabel(graph, propertyInfo.resource, ComponentTypeCommands.camelCaseNameToLabel(newValue));
+
+                        if (extraWriter != null)
+                            extraWriter.accept(graph);
                     }
                 });
             }
         };
+        if (nameFilter != null)
+            text.addListener(SWT.Verify, listener);
         text.addListener(SWT.Modify, listener);
         text.addListener(SWT.Deactivate, listener);
         text.addListener(SWT.Traverse, listener);
@@ -212,7 +257,7 @@ public class ComponentTypeViewerData {
         combo.addListener(SWT.Traverse, listener);
     }
 
-    protected void editUnit(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column) {
+    public void editUnit(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column) {
         // Disallow unit editing for non-numeric configuration properties
         if (propertyInfo.numberType == null && propertyInfo.sectionSpecificData == null)
             return;
@@ -302,6 +347,12 @@ public class ComponentTypeViewerData {
                 }
                 text.dispose();
 
+                if (validator != null) {
+                    String error = validator.apply(Simantics.getSession(), componentType, propertyInfo.resource, newValue);
+                    if (error != null)
+                        return;
+                }
+
                 if (writer != null) {
                     Simantics.getSession().async(new WriteRequest() {
                         @Override
@@ -468,7 +519,7 @@ public class ComponentTypeViewerData {
         shell.open();
     }
 
-    protected void editMultilineText(Table table, TableEditor editor,
+    public void editMultilineText(Table table, TableEditor editor,
             final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem,
             Rectangle selectedItemBounds, int column, final StringWriter writer)
     {