]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Add missing visualization trigger code
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 15 Jun 2020 09:01:09 +0000 (12:01 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 15 Jun 2020 10:58:50 +0000 (13:58 +0300)
gitlab #91

Change-Id: I99e6c8f256fb4de4cc84e83599f951be8238694e
(cherry picked from commit 165a995bf446be802c4623b46706a257a5367c69)

org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DistrictFinderVisualisationParticipant.java
org.simantics.district.network/META-INF/MANIFEST.MF
org.simantics.district.network/scl/Simantics/District/VisualisationTriggers.scl [new file with mode: 0644]
org.simantics.district.network/src/org/simantics/district/network/visualisations/triggers/VisualisationTrigger.java [new file with mode: 0644]
org.simantics.district.network/src/org/simantics/district/network/visualisations/triggers/VisualisationTriggerComponent.java [new file with mode: 0644]
org.simantics.district.network/src/org/simantics/district/network/visualisations/triggers/VisualisationTriggersContributions.java [new file with mode: 0644]

index 4d460f5d13bf5c8615bd6a26edc2f8ba66210462..270320284de8beadd47f221ed3e50724eeda2615 100644 (file)
@@ -40,6 +40,7 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
     private Color color;
     private Double stroke;
     private transient Color dynamicColor = null;
+    private transient Color eventColor = null;
 
     // Dimensions for shut-off valve symbol
     private static final double left = -0.25;
@@ -112,6 +113,7 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
             }
     
             g2d.setColor(dynamicColor != null ? dynamicColor : color);
+            g2d.setColor(eventColor != null ? eventColor : g2d.getColor());
             g2d.setStroke(bs);
             g2d.draw(path);
     
@@ -269,6 +271,12 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
         this.dynamicColor = color;
     }
 
+    @PropertySetter(value = "eventColor")
+    public void setEventColor(Color color) {
+        this.eventColor = color;
+    }
+
+    
     @PropertySetter(value = "arrowLength")
     public void setArrowLength(Double length) {
         // find if there is a child deferred arrow node
index 4ea8bbf1ccc5d366c7e2ca83dc5338d7d047bc2c..8742fa9219251c925ffb37e2556336a2e0745ddb 100644 (file)
@@ -46,6 +46,7 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti
 
     private Color color;
     private transient Color dynamicColor;
+    private transient Color eventColor;
 
     private Rectangle2D bounds;
     private transient Point2D point;
@@ -93,7 +94,7 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti
             }
     
             Color oldColor = g2d.getColor();
-            Color newColor = dynamicColor != null ? dynamicColor : color;
+            Color newColor = eventColor != null ? eventColor : dynamicColor != null ? dynamicColor : color;
             boolean changeColor = !oldColor.equals(newColor);
     
             double scaleRecip = viewScaleRecip * nodeSize;
@@ -243,6 +244,11 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti
         this.dynamicColor = color;
     }
 
+    @PropertySetter(value = "eventColor")
+       public void setEventColor(Color colorr) {
+               this.eventColor = colorr;
+       }
+    
     @PropertySetter(value = "hidden")
     public void setHidden(Boolean value) {
         this.hidden = value;
@@ -279,4 +285,5 @@ public class DistrictNetworkVertexNode extends G2DParentNode implements ISelecti
             child.setPoints(points);
         }
     }
+
 }
index 5f689de994aeabc64d00510397611c53edf7daac..a88d7ba94d362ab8c4c752d9682e34f92c474df0 100644 (file)
@@ -17,12 +17,14 @@ import org.simantics.g2d.canvas.ICanvasContext;
 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
 import org.simantics.g2d.element.IElement;
 import org.simantics.scenegraph.g2d.G2DParentNode;
+import org.simantics.scl.runtime.tuple.Tuple2;
 import org.simantics.utils.datastructures.hints.IHintContext.Key;
 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
 
 public class DistrictFinderVisualisationParticipant extends AbstractDiagramParticipant {
 
     private static final String TOPIC = "org/simantics/district/selection/elementQuerySelection";
+    private static final String TOPIC2 = "org/simantics/district/visualisationTrigger";
     
     private IEventBroker broker;
 
@@ -56,6 +58,22 @@ public class DistrictFinderVisualisationParticipant extends AbstractDiagramParti
                 }
             }
         });
+        broker.subscribe(TOPIC2, event -> {
+               Tuple2 data = (Tuple2) event.getProperty(IEventBroker.DATA);
+               Resource resource = (Resource) data.c0;
+               Color colorr = (Color) data.c1;
+            IElement element = DiagramNodeUtil.findElement(ctx, resource);
+            final G2DParentNode node = element.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE);
+            if (node != null) {
+                // color is a bit special - if not present then reset with null value
+                ((DistrictNetworkEdgeNode) node).setEventColor(colorr);
+                node.repaint();
+            } else {
+                final G2DParentNode vertexNode = element.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE);
+                ((DistrictNetworkVertexNode) vertexNode).setEventColor(colorr);
+                vertexNode.repaint();
+            }
+        });
     }
     
     @Override
index f49358b930c22c1b77c4c87d6a12a0430b507a5c..345276f73830ab471bc44fcef1635f1f136c8d17 100644 (file)
@@ -26,4 +26,5 @@ Export-Package: org.simantics.district.network,
  org.simantics.district.network.changeset,
  org.simantics.district.network.profile,
  org.simantics.district.network.visualisations,
- org.simantics.district.network.visualisations.model
+ org.simantics.district.network.visualisations.model,
+ org.simantics.district.network.visualisations.triggers
diff --git a/org.simantics.district.network/scl/Simantics/District/VisualisationTriggers.scl b/org.simantics.district.network/scl/Simantics/District/VisualisationTriggers.scl
new file mode 100644 (file)
index 0000000..3eea008
--- /dev/null
@@ -0,0 +1,5 @@
+importJava "org.simantics.district.network.visualisations.triggers.VisualisationTrigger" where
+    data VisualisationTrigger
+    
+    @JavaName "<init>"
+    visualisationTriggerContribution :: String -> String -> Double -> String -> VisualisationTrigger
\ No newline at end of file
diff --git a/org.simantics.district.network/src/org/simantics/district/network/visualisations/triggers/VisualisationTrigger.java b/org.simantics.district.network/src/org/simantics/district/network/visualisations/triggers/VisualisationTrigger.java
new file mode 100644 (file)
index 0000000..7a9614e
--- /dev/null
@@ -0,0 +1,46 @@
+package org.simantics.district.network.visualisations.triggers;
+
+import java.awt.Color;
+
+public class VisualisationTrigger {
+
+    private String moduleName;
+    private String attributeName;
+    private double threshold;
+    private Color color;
+
+    public VisualisationTrigger(String moduleName, String attributeName, double threshold, String colorHex) {
+        this.moduleName = moduleName;
+        this.attributeName = attributeName;
+        this.threshold = threshold;
+        this.color = hex2Rgb(colorHex);
+    }
+
+    public String getModuleName() {
+        return moduleName;
+    }
+
+    public String getAttributeName() {
+        return attributeName;
+    }
+
+    public double getThreshold() {
+        return threshold;
+    }
+    
+    public Color getColor() {
+               return color;
+       }
+    
+    /**
+     * 
+     * @param colorStr e.g. "#FFFFFF"
+     * @return 
+     */
+    private static Color hex2Rgb(String colorStr) {
+        return new Color(
+                Integer.valueOf( colorStr.substring( 1, 3 ), 16 ),
+                Integer.valueOf( colorStr.substring( 3, 5 ), 16 ),
+                Integer.valueOf( colorStr.substring( 5, 7 ), 16 ) );
+    }
+}
diff --git a/org.simantics.district.network/src/org/simantics/district/network/visualisations/triggers/VisualisationTriggerComponent.java b/org.simantics.district.network/src/org/simantics/district/network/visualisations/triggers/VisualisationTriggerComponent.java
new file mode 100644 (file)
index 0000000..b18fede
--- /dev/null
@@ -0,0 +1,30 @@
+package org.simantics.district.network.visualisations.triggers;
+
+import org.simantics.db.Resource;
+import org.simantics.structural.synchronization.base.ComponentBase;
+
+public class VisualisationTriggerComponent {
+
+    private Resource element;
+    private ComponentBase<?> component;
+    private VisualisationTrigger trigger;
+
+    public VisualisationTriggerComponent(Resource element, ComponentBase<?> component, VisualisationTrigger activeTrigger) {
+        this.element = element;
+        this.component = component;
+        this.trigger = activeTrigger;
+    }
+    
+    public Resource getElement() {
+               return element;
+       }
+
+    public ComponentBase<?> getComponent() {
+        return component;
+    }
+    
+    public VisualisationTrigger getTrigger() {
+        return trigger;
+    }
+
+}
diff --git a/org.simantics.district.network/src/org/simantics/district/network/visualisations/triggers/VisualisationTriggersContributions.java b/org.simantics.district.network/src/org/simantics/district/network/visualisations/triggers/VisualisationTriggersContributions.java
new file mode 100644 (file)
index 0000000..8120a97
--- /dev/null
@@ -0,0 +1,73 @@
+package org.simantics.district.network.visualisations.triggers;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.simantics.NameLabelUtil;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.request.ObjectsWithSupertype;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.layer0.Layer0;
+import org.simantics.scl.compiler.top.ValueNotFound;
+import org.simantics.scl.osgi.SCLOsgi;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.tuple.Tuple0;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class VisualisationTriggersContributions {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(VisualisationTriggersContributions.class);
+    private static final String VISUALISATIONS_TRIGGERS_MODULE = "VisualisationTriggers";
+    private static final String VISUALISATION_TRIGGER_CONTRIBUTION = "visualisationTriggers";
+    
+    public static Map<String, List<VisualisationTrigger>> visualisationTriggers(ReadGraph graph) throws DatabaseException {
+        List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
+        
+        Map<String, List<VisualisationTrigger>> results = new HashMap<>();
+        Layer0 L0 = Layer0.getInstance(graph);
+        
+        Object oldGraph = SCLContext.getCurrent().get("graph");
+        try {
+            SCLContext.getCurrent().put("graph", graph);
+            for (Resource sharedOntology : sharedOntologies) {
+                
+                Collection<Resource> userComponents = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
+                
+                for (Resource userComponent : userComponents) {
+                    NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, userComponent), userComponent);
+                    List<VisualisationTrigger> visualisationTriggerContributions = visualisationTriggerContribution(graph, moduleType);
+                    if (visualisationTriggerContributions != null)
+                        results.put(moduleType.getName(), visualisationTriggerContributions);
+                }
+            }
+        } finally {
+            SCLContext.getCurrent().put("graph", oldGraph);
+        }
+        return results;
+    }
+    
+    private static List<VisualisationTrigger> visualisationTriggerContribution(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, VISUALISATIONS_TRIGGERS_MODULE);
+        if (sclModule != null) {
+            String moduleURI = graph.getURI(sclModule);
+            try {
+                List<VisualisationTrigger> result = (List<VisualisationTrigger>) SCLOsgi.MODULE_REPOSITORY.getValue(moduleURI, VISUALISATION_TRIGGER_CONTRIBUTION);
+                return result;
+            } catch (ValueNotFound e) {
+                LOGGER.error("Could not find contributions for module {} and expression {}", moduleURI, VISUALISATION_TRIGGER_CONTRIBUTION, e);
+            }
+        }
+        return null;
+    }
+
+}
+