]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network/src/org/simantics/district/network/profile/DiagramSettingsRequest.java
Make defaults to work & remove apply button in dynamic visualisations
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / profile / DiagramSettingsRequest.java
index 726005eef7e088dfcd0024153988d0c2f2b50fc9..eac9ce0814982b3d4bbd309f8532f8469fb8b4bc 100644 (file)
@@ -5,45 +5,62 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.request.ResourceRead;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.Variables;
 import org.simantics.diagram.stubs.DiagramResource;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.scl.runtime.function.Function1;
 
 /**
  * @author Tuukka Lehtonen
  */
 public class DiagramSettingsRequest extends ResourceRead<DiagramSettings> {
 
-       protected DiagramSettingsRequest(Resource runtimeDiagram) {
+       public DiagramSettingsRequest(Resource runtimeDiagram) {
                super(runtimeDiagram);
        }
 
-       @Override
+       @SuppressWarnings("unchecked")
+    @Override
        public DiagramSettings perform(ReadGraph graph) throws DatabaseException {
                DiagramResource DIA = DiagramResource.getInstance(graph);
                DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
 
-               Resource edgeThicknessProperty = null;
-               Resource nodeScalingProperty = null;
-               double edgeThicknessScale = 1;
-               double nodeScalingScale = 1;
+               Function1<Resource, Double> arrowLengthProperty = null;
+               double arrowLengthGain = 1;
+               double arrowLengthBias = 0;
 
                Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
                if (diagram != null) {
-                       Resource etp = graph.getPossibleObject(diagram, DN.Diagram_edgeThicknessProperty);
-                       edgeThicknessProperty = graph.getPossibleObject(etp, DN.Edge_ThicknessProperty_value);
-//                     Resource nsp = graph.getPossibleObject(diagram, DN.Diagram_nodeScalingProperty);
-//                     nodeScalingProperty = graph.getPossibleObject(nsp, DN.Vertex_ScaleProperty_value);
-
-                       edgeThicknessScale =
-                                       safeDoubleProperty(graph, etp, DN.Edge_ThicknessProperty_scale, 1)
-                                       * safeDoubleProperty(graph, diagram, DN.Diagram_edgeThicknessScale, 1);
-
-//                     nodeScalingScale =
-//                                     safeDoubleProperty(graph, nsp, DN.Vertex_ScaleProperty_scale, 1)
-//                                     * safeDoubleProperty(graph, diagram, DN.Diagram_nodeScale, 1);
+                       Variable dv = Variables.getPossibleVariable(graph, diagram);
+
+                       Resource alp = graph.getPossibleObject(diagram, DN.Diagram_arrowLengthProperty);
+                       //System.out.println("alp: " + NameUtils.getURIOrSafeNameInternal(graph, alp));
+                       if (alp != null) {
+                               Variable alpv = Variables.getPossibleVariable(graph, alp);
+                               if (alpv != null) {
+                                       //System.out.println("alpv: " + alpv.getURI(graph));
+                                       arrowLengthProperty = alpv.getPropertyValue(graph, DN.Edge_ArrowLengthProperty_value);
+                               }
+
+                               arrowLengthGain =
+                                               safeDoubleProperty(graph, alp, DN.Edge_ArrowLengthProperty_gain, 1)
+                                               * safeDoubleProperty(graph, diagram, DN.Diagram_arrowLengthGain, 1);
+                               arrowLengthBias =
+                                               safeDoubleProperty(graph, alp, DN.Edge_ArrowLengthProperty_bias, 0)
+                                               + safeDoubleProperty(graph, diagram, DN.Diagram_arrowLengthBias, 0);
+                       }
+
                }
 
-               return new DiagramSettings(nodeScalingProperty, nodeScalingScale, edgeThicknessProperty, edgeThicknessScale);
+               DiagramSettings ds = new DiagramSettings( arrowLengthProperty, arrowLengthGain, arrowLengthBias);
+               //System.out.println("new diagram settings: " + ds);
+               return ds;
+       }
+
+       private static float safeFloatProperty(ReadGraph graph, Resource r, Resource property, float defaultValue) throws DatabaseException {
+               Float d = graph.getPossibleRelatedValue(r, property, Bindings.FLOAT);
+               return d != null ? d : defaultValue;
        }
 
        private static double safeDoubleProperty(ReadGraph graph, Resource r, Resource property, double defaultValue) throws DatabaseException {
@@ -51,4 +68,13 @@ public class DiagramSettingsRequest extends ResourceRead<DiagramSettings> {
                return d != null ? d : defaultValue;
        }
 
+       private static float limit(float min, float max, float value) {
+               return Math.max(min, Math.min(value,  max));
+       }
+
+       @SuppressWarnings("unused")
+       private static double limit(double min, double max, double value) {
+               return Math.max(min, Math.min(value,  max));
+       }
+
 }