X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FcomponentTypeEditor%2FComponentTypeViewerData.java;h=bc7345ab178483efcb0e7177802e556cace8602f;hb=288f81ee7fde62ee1b79a8e5d46a7832630149c2;hp=e1f17996505f6d34ec2131f489f97552f0743327;hpb=6bf51de1941aa1dc5ee0a3aa8c50d53168b0c068;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java index e1f179965..bc7345ab1 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java @@ -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 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 nameFilter, Pattern namePattern, DbConsumer 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 nameValidator, DbConsumer 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 nameFilter, + Function2 nameValidator, + DbConsumer 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); @@ -235,7 +260,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; @@ -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) {