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=d6b312426253fa33226aeb13f36505455ddea238;hpb=c1278c0248e5f2f93c1debb83c7ba5c8fd296b4b;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 d6b312426..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; @@ -40,10 +42,12 @@ 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.threads.ThreadUtils; import org.simantics.utils.ui.ErrorLogger; public class ComponentTypeViewerData { @@ -90,12 +94,44 @@ public class ComponentTypeViewerData { 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 extraWriter) { editName(table, editor, propertyInfo, selectedItem, column, - (pInfo, name) -> validatePropertyName(pInfo, name, namePattern)); + 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); + } + + public void editName(Table table, TableEditor editor, ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column, + Function2 nameValidator) + { + editName(table, editor, propertyInfo, selectedItem, column, nameValidator, null); } public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column, - Function2 nameValidator) { + 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 = @@ -105,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) { @@ -118,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; @@ -157,10 +193,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); @@ -219,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; @@ -333,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); @@ -481,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) {