]> 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 048e0840eaa55f07a79da6f82f6620d15b06957d..1c4b22ee0762cbc2f6f2a2b248d4b7d53e4ce895 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;
 
@@ -47,6 +49,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 {
@@ -373,18 +376,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);