]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java
Merge changes I78c3a258,I7bf72f04
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / ComponentTypeViewerData.java
index 820d45f2e1ecf9365a65aa3895771b68519ee8ee..01c799d45832b824835b1a477c20c3ecd3dfce16 100644 (file)
@@ -3,9 +3,9 @@ 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.HashSet;
 import java.util.List;
-import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
@@ -42,22 +42,22 @@ 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.db.layer0.util.Layer0Utils;
 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.datastructures.Pair;
 import org.simantics.utils.threads.ThreadUtils;
 import org.simantics.utils.ui.ErrorLogger;
 
@@ -225,26 +225,6 @@ public class ComponentTypeViewerData {
         editor.setEditor(text, selectedItem, column);
     }
 
-    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);
@@ -282,11 +262,40 @@ public class ComponentTypeViewerData {
                             throws DatabaseException {
                         graph.markUndoPoint();
 
+                        String newValue2 = newValue;
+
+                        Resource possibleGraphType = null;
                         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);
+                        Resource L0Res = graph.getResource("http://www.simantics.org/Layer0-1.1");
+                        Layer0 L0 = Layer0.getInstance(graph);
+
+                        Collection<Resource> graphTypes1 = QueryIndexUtils.searchByTypeAndName(graph, L0Res, L0.ValueType, newValue);
+                        Collection<Resource> graphTypes2 = QueryIndexUtils.searchByTypeAndName(graph, root, L0.ValueType, newValue);
+
+                        Collection<Resource> graphTypes = new HashSet<>(graphTypes1);
+                        graphTypes.addAll(graphTypes2);
+
+                        Set<Pair<Resource, String>> candidates = new HashSet<>();
+                        for (Resource graphType : graphTypes) {
+                            Collection<Statement> stms = graph.getAssertedStatements(graphType, L0.HasValueType);
+                            if(stms.size() == 1) {
+                                // Only accept valueType if it asserts HasValueType with the same name
+                                String hasValueType = graph.getValue(stms.iterator().next().getObject(), Bindings.STRING);
+                                if (hasValueType.equals(newValue)) {
+                                    candidates.add(new Pair<>(graphType, hasValueType));
+                                }
+                            }
+                        }
+
+                        // We support only graph types with unique name at this point. Later we could implement UI to let the user to select from multiple graph types.
+                        if (candidates.size() == 1) {
+                            Pair<Resource, String> result = candidates.iterator().next();
+                            possibleGraphType = result.first;
+                            newValue2 = result.second;
+                        }
+
+                        ComponentTypeCommands.editType(graph, componentType, propertyInfo.resource, convertDefaultValue, newValue2, possibleGraphType);
                         if (range != null) ComponentTypeCommands.setRange(graph, componentType, propertyInfo.resource, range);
                     }
                 });