]> 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 016a1420dba6a4a8b5588c4e8e4a7735633f2ae2..d80082ec65279d8281c2f29590897fddf2a65e1b 100644 (file)
@@ -1,14 +1,19 @@
 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;
 import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Combo;
@@ -30,10 +35,12 @@ 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;
 import org.simantics.db.exception.RuntimeDatabaseException;
+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;
@@ -44,8 +51,8 @@ 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;
@@ -92,11 +99,19 @@ public class Functions {
     }
 
     public static Map<String, Resource> getVertexMappings(ReadGraph graph, Resource resource) throws DatabaseException {
-        return getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping);
+        Map<String, Resource> second = getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping);
+        return second;
     }
 
     public static Map<String, Resource> getEdgeMappings(ReadGraph graph, Resource resource) throws DatabaseException {
-        return getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping);
+        Map<String, Resource> second = getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping);
+        return second;
+    }
+    
+    public static Map<String, Resource> getCRSs(ReadGraph graph, Resource resource) throws DatabaseException {
+        Map<String, Resource> result = getNetworkMappingsByType(graph, resource, DistrictNetworkResource.getInstance(graph).SpatialRefSystem);
+        return result;
+        
     }
 
     public static Map<String, Resource> getNetworkMappingsByType(ReadGraph graph, Resource element, Resource mappingType) throws DatabaseException {
@@ -107,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();
             }
@@ -193,9 +211,16 @@ public class Functions {
         private Resource configuration;
         private Map<String, Resource> vertexMappings = new HashMap<>();
         private Map<String, Resource> edgeMappings = new HashMap<>();
+        private Map<String, Resource> composites = new HashMap<>();
+        private Map<String, Resource> crss = new HashMap<>();
+        private Map<String, Map<String, Resource>> components = new HashMap<>();
         
         private Resource defaultVertexMapping;
         private Resource defaultEdgeMapping;
+        private Resource defaultCRS;
+        
+        private Combo compositeMappingCombo;
+        private Combo componentMappingCombo;
 
         protected DefaultMappingsDialog(Shell parentShell, Resource configuration) {
             super(parentShell);
@@ -216,6 +241,7 @@ public class Functions {
             composite = (Composite) super.createDialogArea(parent);
             
             createMappingsGroup(composite);
+            createExistingCompositeGroup(composite);
             createCRSSettingsGroup(composite);
             
             // compute default values
@@ -227,12 +253,28 @@ public class Functions {
                     vertexMappings = getVertexMappings(graph, configuration);
                     edgeMappings = getEdgeMappings(graph, configuration);
                     
+                    composites = getComposites(graph, configuration);
+                    if (composites.size() > 0) {
+                        components = getComponents(graph, composites.get(0));
+                    }
+                    
+                    crss = getCRSs(graph, configuration);
+                    
                     composite.getDisplay().asyncExec(() -> {
                         
                         vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
                         edgeMappingCombo.setItems(edgeMappings.keySet().toArray(new String[edgeMappings.size()]));
+                        
+                        crsCombo.setItems(crss.keySet().toArray(new String[crss.size()]));
+                        
+                        compositeMappingCombo.setItems(composites.keySet().toArray(new String[composites.size()]));
                         vertexMappingCombo.select(0);
                         edgeMappingCombo.select(0);
+                        
+                        crsCombo.select(0);
+                        
+                        if (!composites.isEmpty())
+                            compositeMappingCombo.select(0);
                     }); 
                     
                 }
@@ -240,6 +282,37 @@ public class Functions {
             return composite;
         }
         
+        protected Map<String, Map<String, Resource>> getComponents(ReadGraph graph, Resource resource) {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        protected Map<String, Resource> getComposites(ReadGraph graph, Resource element) throws DatabaseException {
+            
+            Resource indexRoot = graph.sync(new IndexRoot(element));
+            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) {
+                    LOGGER.error("Could not check if composite " + comp + " is instanceOf DistrictNetwork.composite");
+                    return false;
+                }
+            }).collect(Collectors.toList());
+            Map<String, Resource> result = new HashMap<>(nonDistrictComposites.size());
+            Layer0 L0 = Layer0.getInstance(graph);
+            nonDistrictComposites.forEach(mapping -> {
+                try {
+                    String name = graph.getRelatedValue2(mapping, L0.HasName);
+                    result.put(name, mapping);
+                } catch (DatabaseException e) {
+                    LOGGER.error("Could not read name of " + mapping, e);
+                }
+            });
+            return result;
+        }
+
         private void createMappingsGroup(Composite parent) {
             Group group= new Group(parent, SWT.NONE);
             group.setFont(parent.getFont());
@@ -264,6 +337,52 @@ public class Functions {
             GridDataFactory.fillDefaults().grab(true, false).applyTo(edgeMappingCombo);
         }
         
+        private void createExistingCompositeGroup(Composite parent) {
+            Group group= new Group(parent, SWT.NONE);
+            group.setFont(parent.getFont());
+            group.setText("Mapped composite");
+            GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
+            group.setLayout(new GridLayout(1, false));
+            
+            Composite cmposite = new Composite(group, SWT.NONE);
+            cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
+            cmposite.setLayout(new GridLayout(2, false));
+            
+            Label compositeMappingLabel = new Label(cmposite, SWT.NONE);
+            compositeMappingLabel.setText("Select composite");
+
+            compositeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
+            GridDataFactory.fillDefaults().grab(true, false).applyTo(compositeMappingCombo);
+            compositeMappingCombo.addSelectionListener(new SelectionAdapter() {
+                
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    super.widgetSelected(e);
+                    recalculateMappapleComponents();
+                }
+            });
+            
+            Label compojnentMappingLabel = new Label(cmposite, SWT.NONE);
+            compojnentMappingLabel.setText("Select component");
+            
+            componentMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
+            GridDataFactory.fillDefaults().grab(true, false).applyTo(componentMappingCombo);
+        }
+        
+        protected void recalculateMappapleComponents() {
+            Simantics.getSession().asyncRequest(new ReadRequest() {
+                
+                @Override
+                public void run(ReadGraph graph) throws DatabaseException {
+                    
+                    
+                    composite.getDisplay().asyncExec(() -> {
+                        
+                    }); 
+                }
+            });
+        }
+
         private void createCRSSettingsGroup(Composite parent) {
             Group group= new Group(parent, SWT.NONE);
             group.setFont(parent.getFont());
@@ -288,6 +407,11 @@ public class Functions {
         protected void computeResult() {
             defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex()));
             defaultEdgeMapping = edgeMappings.get(edgeMappingCombo.getItem(edgeMappingCombo.getSelectionIndex()));
+            defaultCRS = crss.get(crsCombo.getItem(crsCombo.getSelectionIndex()));
+        }
+
+        public Resource getCRS() {
+            return defaultCRS;
         }
         
     }
@@ -313,6 +437,11 @@ public class Functions {
                                 Resource diagram = graph.getSingleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram);
                                 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);
@@ -326,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;
+    }
 }