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.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 {
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);
}
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 =
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) {
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;
});
}
};
+ if (nameFilter != null)
+ text.addListener(SWT.Verify, listener);
text.addListener(SWT.Modify, listener);
text.addListener(SWT.Deactivate, listener);
text.addListener(SWT.Traverse, listener);
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;
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);
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)
{