]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java
Pushing some (very) old changes to remote.. should have done long ago
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / function / Functions.java
index 0ea5adf7f7638173ba324b27a610bc931c946c33..d80082ec65279d8281c2f29590897fddf2a65e1b 100644 (file)
@@ -1,9 +1,12 @@
 package org.simantics.district.network.ui.function;
 
+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.stream.Collectors;
 
 import org.eclipse.jface.dialogs.Dialog;
@@ -32,6 +35,7 @@ import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.common.request.ObjectsWithType;
 import org.simantics.db.common.request.ReadRequest;
 import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.exception.DatabaseException;
@@ -40,14 +44,15 @@ import org.simantics.db.exception.ServiceException;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.layer0.variable.Variables.Role;
 import org.simantics.db.procedure.Procedure;
+import org.simantics.diagram.stubs.DiagramResource;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.layer0.Layer0;
 import org.simantics.modeling.ModelingResources;
 import org.simantics.modeling.ModelingUtils;
 import org.simantics.modeling.adapters.NewCompositeActionFactory;
 import org.simantics.modeling.typicals.TypicalUtil;
+import org.simantics.operation.Layer0X;
 import org.simantics.scl.reflection.annotations.SCLValue;
-import org.simantics.structural.stubs.StructuralResource2;
 import org.simantics.ui.workbench.action.DefaultActions;
 import org.simantics.utils.ui.SWTUtils;
 import org.slf4j.Logger;
@@ -117,7 +122,10 @@ public class Functions {
         mappings.forEach(mapping -> {
             try {
                 String name = graph.getRelatedValue2(mapping, L0.HasName);
-                result.put(name, mapping);
+                Resource existing = result.put(name, mapping);
+                if (existing != null) {
+                    LOGGER.warn("Duplicate mapping name! {} {} and existing is {}", name, mapping, existing);
+                }
             } catch (DatabaseException e) {
                 e.printStackTrace();
             }
@@ -280,9 +288,11 @@ public class Functions {
         }
 
         protected Map<String, Resource> getComposites(ReadGraph graph, Resource element) throws DatabaseException {
+            
             Resource indexRoot = graph.sync(new IndexRoot(element));
-            List<Resource> composites = ModelingUtils.searchByType(graph, indexRoot, StructuralResource2.getInstance(graph).Composite);
-            List<Resource> nonDistrictComposites = composites.stream().filter(comp -> {
+            List<Resource> diagrams = ModelingUtils.searchByType(graph, indexRoot, DiagramResource.getInstance(graph).Diagram);
+            
+            List<Resource> nonDistrictComposites = composites.values().stream().filter(comp -> {
                 try {
                     return !graph.isInstanceOf(comp, DistrictNetworkResource.getInstance(graph).Composite);
                 } catch (ServiceException e1) {
@@ -297,7 +307,7 @@ public class Functions {
                     String name = graph.getRelatedValue2(mapping, L0.HasName);
                     result.put(name, mapping);
                 } catch (DatabaseException e) {
-                    e.printStackTrace();
+                    LOGGER.error("Could not read name of " + mapping, e);
                 }
             });
             return result;
@@ -428,6 +438,10 @@ public class Functions {
                                 graph.claim(diagram, DN.EdgeDefaultMapping, dialog.getDefaultEdgeMapping());
                                 graph.claim(diagram, DN.VertexDefaultMapping, dialog.getDefaultVertexMapping());
                                 graph.claim(diagram, DN.HasSpatialRefSystem, dialog.getCRS());
+                                
+                                // Generated name prefix from composite name
+                                String compositeName = graph.getRelatedValue2(composite, Layer0.getInstance(graph).HasName, Bindings.STRING);
+                                graph.claimLiteral(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix, "N" + compositeName.substring(compositeName.length() - 1, compositeName.length()));
                             }
                         });
                         DefaultActions.asyncPerformDefaultAction(Simantics.getSession(), composite, false, false, true);
@@ -441,4 +455,16 @@ public class Functions {
                     }
                 });
     }
+
+    public static Collection<Resource> getDistrictDiagrams(ReadGraph graph) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        Collection<Resource> indexRoots = graph.sync(new ObjectsWithType(Simantics.getProjectResource(), L0.ConsistsOf, L0.IndexRoot));
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        Set<Resource> results = new HashSet<>();
+        for (Resource indexRoot : indexRoots) {
+            Collection<Resource> diagrams = ModelingUtils.searchByType(graph, indexRoot, DN.Diagram);
+            results.addAll(diagrams);
+        }
+        return results;
+    }
 }