X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FcomponentTypeEditor%2FComponentTypeViewerData.java;h=ea10d6d8d4e44d700cf6cc789b97ca49ed9e6e6c;hp=b64380b285d7b76593060b2e40a6a7d09f502b8f;hb=1b2114e1f1cbae2f85ae396ef37a3199aaf67a3c;hpb=1d887773b04e7fa984a2c3da4699faea17f9c872 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 b64380b28..ea10d6d8d 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,12 +3,16 @@ 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; +import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.IMessageProvider; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.osgi.util.NLS; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.custom.TableEditor; @@ -45,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 { @@ -52,29 +57,29 @@ public class ComponentTypeViewerData { * Used to validate property names. */ public static final Pattern PROPERTY_NAME_PATTERN = - Pattern.compile("([a-z]|_[0-9a-zA-Z_])[0-9a-zA-Z_]*"); + Pattern.compile("([a-z]|_[0-9a-zA-Z_])[0-9a-zA-Z_]*"); //$NON-NLS-1$ public static final String[] PROPERTY_TYPE_SUGGESTIONS = new String[] { - "Double", - "Integer", - "Float", - "String", - "Boolean", - "Long", - "[Double]", - "[Integer]", - "[Float]", - "[String]", - "[Boolean]", - "[Long]", - "Vector Double", - "Vector Integer", - "Vector Float", - "Vector String", - "Vector Boolean", - "Vector Long" + "Double", //$NON-NLS-1$ + "Integer", //$NON-NLS-1$ + "Float", //$NON-NLS-1$ + "String", //$NON-NLS-1$ + "Boolean", //$NON-NLS-1$ + "Long", //$NON-NLS-1$ + "[Double]", //$NON-NLS-1$ + "[Integer]", //$NON-NLS-1$ + "[Float]", //$NON-NLS-1$ + "[String]", //$NON-NLS-1$ + "[Boolean]", //$NON-NLS-1$ + "[Long]", //$NON-NLS-1$ + "Vector Double", //$NON-NLS-1$ + "Vector Integer", //$NON-NLS-1$ + "Vector Float", //$NON-NLS-1$ + "Vector String", //$NON-NLS-1$ + "Vector Boolean", //$NON-NLS-1$ + "Vector Long" //$NON-NLS-1$ }; - + public Resource componentType; public FormToolkit tk; public Form form; @@ -180,7 +185,7 @@ public class ComponentTypeViewerData { graph.markUndoPoint(); Layer0 L0 = Layer0.getInstance(graph); String prevName = graph.getPossibleRelatedValue2(propertyInfo.resource, L0.HasName); - String oldCamelCasedLabel = prevName != null ? ComponentTypeCommands.camelCaseNameToLabel(prevName) : ""; + String oldCamelCasedLabel = prevName != null ? ComponentTypeCommands.camelCaseNameToLabel(prevName) : ""; //$NON-NLS-1$ String oldLabel = graph.getPossibleRelatedValue(propertyInfo.resource, L0.HasLabel); boolean setLabel = oldLabel == null || oldLabel.isEmpty() @@ -211,7 +216,7 @@ public class ComponentTypeViewerData { editor.setEditor(text, selectedItem, column); } - public void editType(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column, final boolean convertDefaultValue) { + public void editType(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column, String range, final boolean convertDefaultValue) { int extraStyle = propertyInfo.immutable ? SWT.READ_ONLY : 0; final Combo combo = new Combo(table, SWT.NONE | extraStyle); combo.setText(selectedItem.getText(column)); @@ -247,6 +252,7 @@ public class ComponentTypeViewerData { throws DatabaseException { graph.markUndoPoint(); ComponentTypeCommands.editType(graph, componentType, propertyInfo.resource, convertDefaultValue, newValue); + if (range != null) ComponentTypeCommands.setRange(graph, componentType, propertyInfo.resource, range); } }); } @@ -371,18 +377,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); @@ -391,7 +411,7 @@ public class ComponentTypeViewerData { private Range parseRange(NumberType numberType, String minStr, String maxStr, boolean lowInclusive, boolean highInclusive) throws RangeException { try { - String rangeStr = (lowInclusive ? "[" : "(") + minStr + ".." + maxStr + (highInclusive ? "]" : ")"); + String rangeStr = (lowInclusive ? "[" : "(") + minStr + ".." + maxStr + (highInclusive ? "]" : ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ return Range.valueOf(rangeStr); } catch (IllegalArgumentException e) { // Limits are invalid @@ -401,8 +421,8 @@ public class ComponentTypeViewerData { private static Combo createRangeInclusionCombo(Composite parent, boolean inclusive) { Combo rng = new Combo(parent, SWT.READ_ONLY); - rng.add("Inclusive"); - rng.add("Exclusive"); + rng.add(Messages.ComponentTypeViewerData_Inclusive); + rng.add(Messages.ComponentTypeViewerData_Exclusive); rng.select(inclusive ? 0 : 1); return rng; } @@ -431,12 +451,12 @@ public class ComponentTypeViewerData { GridLayoutFactory.swtDefaults().numColumns(3).applyTo(composite); Label low = new Label(composite, SWT.NONE); - low.setText("Minimum Value:"); + low.setText(Messages.ComponentTypeViewerData_MinimumValue); final Text lowText = new Text(composite, SWT.BORDER | extraTextStyle); GridDataFactory.fillDefaults().grab(true, false).hint(100, SWT.DEFAULT).applyTo(lowText); final Combo lowSelector = createRangeInclusionCombo(composite, !range.getLower().isExclusive()); Label high = new Label(composite, SWT.NONE); - high.setText("Maximum Value:"); + high.setText(Messages.ComponentTypeViewerData_MaximumValue); final Text highText = new Text(composite, SWT.BORDER | extraTextStyle); GridDataFactory.fillDefaults().grab(true, false).hint(100, SWT.DEFAULT).applyTo(highText); final Combo highSelector = createRangeInclusionCombo(composite, !range.getUpper().isExclusive()); @@ -446,10 +466,10 @@ public class ComponentTypeViewerData { GridLayoutFactory.swtDefaults().numColumns(2).equalWidth(true).applyTo(buttonComposite); Button ok = new Button(buttonComposite, SWT.NONE); - ok.setText("&OK"); + ok.setText(IDialogConstants.OK_LABEL); GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).applyTo(ok); Button cancel = new Button(buttonComposite, SWT.NONE); - cancel.setText("&Cancel"); + cancel.setText(IDialogConstants.CANCEL_LABEL); GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).applyTo(ok); if (range.getLower().getValue() != null) @@ -565,10 +585,10 @@ public class ComponentTypeViewerData { } }; - String helpText = propertyInfo.immutable ? "ESC to close." : "Ctrl+Enter to apply changes, ESC to cancel."; + String helpText = propertyInfo.immutable ? Messages.ComponentTypeViewerData_ESCToClose : Messages.ComponentTypeViewerData_CtrlEnterApplyChanges; Label help = tk.createLabel(shell, helpText, SWT.BORDER | SWT.FLAT); GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(help); - help.setForeground( tk.getColors().createColor( "fg", tk.getColors().getSystemColor(SWT.COLOR_LIST_SELECTION) ) ); + help.setForeground( tk.getColors().createColor( "fg", tk.getColors().getSystemColor(SWT.COLOR_LIST_SELECTION) ) ); //$NON-NLS-1$ Display display = table.getDisplay(); Rectangle clientArea = display.getClientArea(); @@ -599,16 +619,15 @@ public class ComponentTypeViewerData { return null; for (ComponentTypeViewerPropertyInfo info : properties) { if (propertyName.equals(info.name)) - return "Property name '" + propertyName + "' is already in use."; + return NLS.bind(Messages.ComponentTypeViewerData_PropertyNameInUse, propertyName); } for (NamedResource cp : connectionPoints) { if (propertyName.equals(cp.getName())) - return "Name '" + propertyName + "' is already used for a terminal."; + return NLS.bind(Messages.ComponentTypeViewerData_NameInUse, propertyName ); } Matcher m = namePattern.matcher(propertyName); if (!m.matches()) - return "Property name '" + propertyName + "' contains invalid characters, does not match pattern " - + namePattern.pattern() + "."; + return NLS.bind(Messages.ComponentTypeViewerData_ContainsInvalidCharacters, propertyName, namePattern.pattern()); return null; }