package org.simantics.modeling.ui.componentTypeEditor;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
import org.simantics.databoard.type.NumberType;
import org.simantics.databoard.units.internal.library.UnitLibrary;
import org.simantics.databoard.util.Limit;
import org.simantics.databoard.util.Range;
import org.simantics.databoard.util.RangeException;
+import org.simantics.db.ReadGraph;
import org.simantics.db.RequestProcessor;
import org.simantics.db.Resource;
+import org.simantics.db.Statement;
import org.simantics.db.WriteGraph;
import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.common.request.ResourceRead;
import org.simantics.db.common.request.WriteRequest;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.function.DbConsumer;
+import org.simantics.db.layer0.QueryIndexUtils;
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 {
editor.setEditor(text, selectedItem, column);
}
- public void editType(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column, final boolean convertDefaultValue) {
+ private static class TypeDefinitionMapRequest extends ResourceRead<Map<String,Resource>> {
+ public TypeDefinitionMapRequest(Resource resource) {
+ super(resource);
+ }
+
+ @Override
+ public Map<String,Resource> perform(ReadGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ Map<String,Resource> result = new HashMap<>();
+ for(Resource valueType : QueryIndexUtils.searchByType(graph, resource, L0.ValueType)) {
+ Collection<Statement> stms = graph.getAssertedStatements(valueType, L0.HasValueType);
+ if(stms.size() == 1) {
+ String sclValueType = graph.getValue(stms.iterator().next().getObject(), Bindings.STRING);
+ result.put(sclValueType, valueType);
+ }
+ }
+ return result;
+ }
+ }
+
+ 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));
if (propertyInfo.immutable)
return;
+
Simantics.getSession().async(new WriteRequest() {
@Override
public void perform(WriteGraph graph)
throws DatabaseException {
graph.markUndoPoint();
- ComponentTypeCommands.editType(graph, componentType, propertyInfo.resource, convertDefaultValue, newValue);
+
+ Resource root = graph.syncRequest(new IndexRoot(componentType));
+ Map<String,Resource> typeDefinitionMap = graph.syncRequest(new TypeDefinitionMapRequest(root));
+ Resource possibleGraphType = typeDefinitionMap.get(newValue);
+
+ ComponentTypeCommands.editType(graph, componentType, propertyInfo.resource, convertDefaultValue, newValue, possibleGraphType);
+ if (range != null) ComponentTypeCommands.setRange(graph, componentType, propertyInfo.resource, range);
}
});
}
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);