]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java
Initial version of validating derived properties formulas
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / ComponentTypeViewerData.java
index ea3f61e80fe0904251ac2cd4af2a44581717f392..bc7345ab178483efcb0e7177802e556cace8602f 100644 (file)
@@ -3,6 +3,8 @@ package org.simantics.modeling.ui.componentTypeEditor;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -45,6 +47,7 @@ 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.threads.ThreadUtils;
 import org.simantics.utils.ui.ErrorLogger;
 
 public class ComponentTypeViewerData {
@@ -97,6 +100,14 @@ public class ComponentTypeViewerData {
     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);
     }
@@ -109,6 +120,18 @@ public class ComponentTypeViewerData {
 
     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 = 
@@ -118,9 +141,11 @@ 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 = nameValidator.apply(propertyInfo, text.getText());
                     if (error != null) {
@@ -131,9 +156,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;
@@ -177,6 +200,8 @@ public class ComponentTypeViewerData {
                 });
             }
         };
+        if (nameFilter != null)
+            text.addListener(SWT.Verify, listener);
         text.addListener(SWT.Modify, listener);
         text.addListener(SWT.Deactivate, listener);
         text.addListener(SWT.Traverse, listener);
@@ -349,18 +374,32 @@ public class ComponentTypeViewerData {
 
         if (validator != null) {
             org.eclipse.swt.widgets.Listener validationListener = new org.eclipse.swt.widgets.Listener() {
+                
+                private ScheduledFuture<?> future;
+                
                 @Override
                 public void handleEvent(Event e) {
                     final String newValue = text.getText();
-                    String error = validator.apply(Simantics.getSession(), componentType, propertyInfo.resource, newValue);
-                    if (error != null) {
-                        text.setBackground(text.getDisplay().getSystemColor(SWT.COLOR_RED));
-                        text.setToolTipText(error);
-                        return;
-                    } else {
-                        text.setBackground(null);
-                        text.setToolTipText(null);
-                    }
+                    if (future != null && !future.isCancelled())
+                        future.cancel(true);
+                    future = ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
+                        String error = validator.apply(Simantics.getSession(), componentType, propertyInfo.resource, newValue);
+                        if (!text.isDisposed()) {
+                            text.getDisplay().asyncExec(() -> {
+                                if (!text.isDisposed()) {
+                                    if (error != null) {
+                                        text.setBackground(text.getDisplay().getSystemColor(SWT.COLOR_RED));
+                                        text.setToolTipText(error);
+                                        return;
+                                    } else {
+                                        text.setBackground(null);
+                                        text.setToolTipText(null);
+                                    }
+                                }
+                                
+                            });
+                        }
+                    }, 500, TimeUnit.MILLISECONDS);
                 }
             };
             text.addListener(SWT.Modify, validationListener);
@@ -497,7 +536,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)
     {