]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java
Add default mappings for right/left click & prevent edge drawing
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / function / Functions.java
index 04cd94cf46658f2d3c0bbb8431275d06f8640ad0..89878c5b60c59041986f3a572fec0d96bc558236 100644 (file)
@@ -1,5 +1,7 @@
 package org.simantics.district.network.ui.function;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -42,6 +44,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.RuntimeDatabaseException;
 import org.simantics.db.exception.ServiceException;
 import org.simantics.db.layer0.QueryIndexUtils;
+import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.layer0.variable.Variables;
 import org.simantics.db.layer0.variable.Variables.Role;
@@ -52,10 +55,15 @@ import org.simantics.modeling.ModelingResources;
 import org.simantics.modeling.adapters.NewCompositeActionFactory;
 import org.simantics.modeling.typicals.TypicalUtil;
 import org.simantics.operation.Layer0X;
+import org.simantics.scl.compiler.commands.CommandSession;
+import org.simantics.scl.compiler.commands.CommandSessionImportEntry;
+import org.simantics.scl.compiler.errors.CompilationError;
+import org.simantics.scl.osgi.SCLOsgi;
 import org.simantics.scl.reflection.annotations.SCLValue;
 import org.simantics.scl.runtime.SCLContext;
 import org.simantics.scl.runtime.function.Function1;
 import org.simantics.scl.runtime.function.FunctionImpl1;
+import org.simantics.scl.runtime.reporting.SCLReportingHandler;
 import org.simantics.ui.workbench.action.DefaultActions;
 import org.simantics.utils.ui.SWTUtils;
 import org.slf4j.Logger;
@@ -85,14 +93,25 @@ public class Functions {
     
     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
     public static Object defaultVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
-        System.out.println(graph.getURI(resource));
-        System.out.println(context.getURI(graph));
-        
         Resource diagram = resolveElement(graph, context);
         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
         return baseMappingModifier(graph, diagram, DN.VertexDefaultMapping, DN.Mapping_VertexMapping, context);
     }
-    
+
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+    public static Object rightClickVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
+        Resource diagram = resolveElement(graph, context);
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        return baseMappingModifier(graph, diagram, DN.RightClickDefaultMapping, DN.Mapping_VertexMapping, context);
+    }
+
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+    public static Object leftClickVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
+        Resource diagram = resolveElement(graph, context);
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        return baseMappingModifier(graph, diagram, DN.LeftClickDefaultMapping, DN.Mapping_VertexMapping, context);
+    }
+
     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
     public static Object mappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
         
@@ -101,24 +120,23 @@ public class Functions {
         return baseMappingModifier(graph, element, DistrictNetworkResource.getInstance(graph).HasMapping, mappingType, context);
     }
 
-    public static Map<String, Resource> getVertexMappings(ReadGraph graph, Resource resource) throws DatabaseException {
-        Map<String, Resource> second = getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping);
+    public static Map<String, Resource> getVertexMappings(ReadGraph graph, Resource indexRoot) throws DatabaseException {
+        Map<String, Resource> second = getNetworkMappingsByType(graph, indexRoot, DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping);
         return second;
     }
 
-    public static Map<String, Resource> getEdgeMappings(ReadGraph graph, Resource resource) throws DatabaseException {
-        Map<String, Resource> second = getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping);
+    public static Map<String, Resource> getEdgeMappings(ReadGraph graph, Resource indexRoot) throws DatabaseException {
+        Map<String, Resource> second = getNetworkMappingsByType(graph, indexRoot, 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);
+        Map<String, Resource> result = getNetworkMappingsByType(graph, graph.sync(new IndexRoot(resource)), DistrictNetworkResource.getInstance(graph).SpatialRefSystem);
         return result;
         
     }
 
-    public static Map<String, Resource> getNetworkMappingsByType(ReadGraph graph, Resource element, Resource mappingType) throws DatabaseException {
-        Resource indexRoot = graph.sync(new IndexRoot(element));
+    public static Map<String, Resource> getNetworkMappingsByType(ReadGraph graph, Resource indexRoot, Resource mappingType) throws DatabaseException {
         List<Resource> mappings = QueryIndexUtils.searchByType(graph, indexRoot, mappingType);
         Map<String, Resource> result = new HashMap<>(mappings.size());
         Layer0 L0 = Layer0.getInstance(graph);
@@ -176,8 +194,6 @@ public class Functions {
     @SCLValue(type = "ReadGraph -> Resource -> a -> b")
     public static Object enumerationValues(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
         Variable var = (Variable) context;
-        System.out.println(graph.getURI(resource));
-        System.out.println(var.getURI(graph));
         return Collections.emptyList();
     }
     
@@ -210,8 +226,10 @@ public class Functions {
     private static class DefaultMappingsDialog extends SelectionStatusDialog {
 
         private Combo vertexMappingCombo;
+        private Combo rightClickMappingCombo;
+        private Combo leftClickMappingCombo;
         private Combo edgeMappingCombo;
-        private Combo crsCombo;
+        //private Combo crsCombo;
         private Composite composite;
         
         private Resource configuration;
@@ -222,7 +240,9 @@ public class Functions {
         
         private Resource defaultVertexMapping;
         private Resource defaultEdgeMapping;
-        private Resource defaultCRS;
+        private Resource rightClickVertexMapping;
+        private Resource leftClickVertexMapping;
+        //private Resource defaultCRS;
         
         private Combo compositeMappingCombo;
         private Combo componentMappingCombo;
@@ -237,6 +257,14 @@ public class Functions {
             return defaultVertexMapping;
         }
 
+               public Resource getRightClickVertexMapping() {
+                       return rightClickVertexMapping;
+               }
+
+               public Resource getLeftClickVertexMapping() {
+                       return leftClickVertexMapping;
+               }
+
         public Resource getDefaultEdgeMapping() {
             return defaultEdgeMapping;
         }
@@ -246,17 +274,17 @@ public class Functions {
             composite = (Composite) super.createDialogArea(parent);
             
             createMappingsGroup(composite);
-            createExistingCompositeGroup(composite);
-            createCRSSettingsGroup(composite);
+            //createExistingCompositeGroup(composite);
+            //createCRSSettingsGroup(composite);
             
             // compute default values
             Simantics.getSession().asyncRequest(new ReadRequest() {
 
                 @Override
                 public void run(ReadGraph graph) throws DatabaseException {
-                    
-                    vertexMappings = getVertexMappings(graph, configuration);
-                    edgeMappings = getEdgeMappings(graph, configuration);
+                    Resource indexRoot = graph.sync(new IndexRoot(configuration));
+                    vertexMappings = getVertexMappings(graph, indexRoot);
+                    edgeMappings = getEdgeMappings(graph, indexRoot);
                     
                     composites = getComposites(graph, configuration);
                   
@@ -265,15 +293,19 @@ public class Functions {
                     composite.getDisplay().asyncExec(() -> {
                         
                         vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
+                        rightClickMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
+                        leftClickMappingCombo.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()]));
+                        //crsCombo.setItems(crss.keySet().toArray(new String[crss.size()]));
                         
                         compositeMappingCombo.setItems(composites.keySet().toArray(new String[composites.size()]));
                         vertexMappingCombo.select(0);
+                        rightClickMappingCombo.select(0);
+                        leftClickMappingCombo.select(0);
                         edgeMappingCombo.select(0);
                         
-                        crsCombo.select(0);
+                        //crsCombo.select(0);
                         
                         if (!composites.isEmpty())
                             compositeMappingCombo.select(0);
@@ -322,7 +354,19 @@ public class Functions {
 
             vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
             GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
-            
+
+            Label rightClickMappingLabel = new Label(cmposite, SWT.NONE);
+            rightClickMappingLabel.setText("Default right click mapping");
+
+            rightClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
+            GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
+
+            Label leftClickMappingLabel = new Label(cmposite, SWT.NONE);
+            leftClickMappingLabel.setText("Default left click mapping");
+
+            leftClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
+            GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
+
             Label edgeMappingLabel = new Label(cmposite, SWT.NONE);
             edgeMappingLabel.setText("Default edge mapping");
 
@@ -390,9 +434,9 @@ public class Functions {
             Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
             vertexMappingLabel.setText("Default CRS");
 
-            crsCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
-            GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false);
-            crsCombo.setLayoutData(textData);
+            //crsCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
+            //GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false);
+            //crsCombo.setLayoutData(textData);
         }
         
 
@@ -400,11 +444,13 @@ 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()));
+            rightClickVertexMapping = vertexMappings.get(rightClickMappingCombo.getItem(rightClickMappingCombo.getSelectionIndex()));
+            leftClickVertexMapping = vertexMappings.get(leftClickMappingCombo.getItem(leftClickMappingCombo.getSelectionIndex()));
+            //defaultCRS = crss.get(crsCombo.getItem(crsCombo.getSelectionIndex()));
         }
 
         public Resource getCRS() {
-            return defaultCRS;
+            return crss.get("EPSG_4326"); // this is only supported
         }
         
     }
@@ -430,6 +476,8 @@ 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.RightClickDefaultMapping, dialog.getRightClickVertexMapping());
+                                graph.claim(diagram, DN.LeftClickDefaultMapping, dialog.getLeftClickVertexMapping());
                                 graph.claim(diagram, DN.HasSpatialRefSystem, dialog.getCRS());
                                 
                                 // Generated name prefix from composite name
@@ -476,7 +524,13 @@ public class Functions {
         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
         return listInstanceNames(graph, context, DN.Edge_ThicknessProperty);
     }
-
+    
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+    public static Object arrowLengthPropertyEnumerationValues(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        return listInstanceNames(graph, context, DN.Edge_ArrowLengthProperty);
+    }
+    
     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
     public static Object nodeScalePropertyEnumerationValues(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
@@ -490,6 +544,13 @@ public class Functions {
         return baseMappingModifier(graph, diagram, DN.Diagram_edgeThicknessProperty, DN.Edge_ThicknessProperty, context);
     }
 
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+    public static Object arrowLengthPropertyModifier(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+        Resource diagram = resolveElement(graph, context);
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        return baseMappingModifier(graph, diagram, DN.Diagram_arrowLengthProperty, DN.Edge_ArrowLengthProperty, context);
+    }
+    
     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
     public static Object nodeScalePropertyModifier(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
         Resource diagram = resolveElement(graph, context);
@@ -523,6 +584,10 @@ public class Functions {
         public Double apply(Resource edge) {
             return ONE;
         }
+        @Override
+        public String toString() {
+            return "1";
+        }
     };
 
     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
@@ -547,4 +612,86 @@ public class Functions {
         };
     }
 
+    private static class RangeValidator implements Function1<String, String> {
+        private double min;
+        private double max;
+        public RangeValidator(double min, double max) {
+            this.min = min;
+            this.max = max;
+        }
+        @Override
+        public String apply(String s) {
+            try {
+                double d = Double.parseDouble(s);
+                if (d < min)
+                    return "Value must be greater than or equal to " + min;
+                if (d > max)
+                    return "Value must be less than or equal to " + max;
+                return null;
+            } catch (NumberFormatException e) {
+                return "Specified value is not a number";
+            }
+        }
+    }
+
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+    public static Object hueValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
+        return new RangeValidator(0, 360);
+    }
+
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+    public static Object saturationValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
+        return new RangeValidator(0, 100);
+    }
+
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+    public static Object brightnessValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
+        String importEntry = null;
+        Resource root = Variables.getPossibleIndexRoot(graph, context);
+        if (root != null) {
+            Resource sclmain = Layer0Utils.getPossibleChild(graph, root, "SCLMain");
+            if (sclmain != null) {
+                importEntry = graph.getPossibleURI(sclmain);
+            }
+        }
+        SCLContext ctx = SCLContext.getCurrent();
+        Object oldGraph = ctx.put("graph", graph);
+        try {
+            return new BrightnessExpressionValidator(
+                    importEntry != null
+                    ? Arrays.asList(importEntry)
+                    : Collections.emptyList());
+        } finally {
+            ctx.put("graph", oldGraph);
+        }
+    }
+
+    private static class BrightnessExpressionValidator implements Function1<String, String> {
+        private CommandSession session;
+
+        public BrightnessExpressionValidator(List<String> importEntries) {
+            this.session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, SCLReportingHandler.DEFAULT);
+            this.session.setImportEntries(imports(importEntries));
+        }
+
+        private ArrayList<CommandSessionImportEntry> imports(List<String> entries) {
+            ArrayList<CommandSessionImportEntry> result = new ArrayList<>();
+            entries.stream().map(CommandSessionImportEntry::new).forEach(result::add);
+            if (entries.isEmpty())
+                result.add(new CommandSessionImportEntry("Simantics/District/SCLMain"));
+            return result;
+        }
+
+        @Override
+        public String apply(String s) {
+            s = s.trim();
+            if (!s.startsWith("="))
+                return "Expression expected, must start with '='";
+            CompilationError[] errors = session.validate(s.substring(1));
+            if(errors.length == 0)
+                return null;
+            return errors[0].description;
+        }
+    }
+
 }