From: jsimomaa Date: Mon, 15 Jun 2020 09:01:09 +0000 (+0300) Subject: Add missing visualization trigger code X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F15%2F4315%2F2;p=simantics%2Fdistrict.git Add missing visualization trigger code gitlab #91 Change-Id: I99e6c8f256fb4de4cc84e83599f951be8238694e --- diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java index 4d460f5d..27032028 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java @@ -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 diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java index 4ea8bbf1..8742fa92 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java @@ -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); } } + } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DistrictFinderVisualisationParticipant.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DistrictFinderVisualisationParticipant.java index 5f689de9..a88d7ba9 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DistrictFinderVisualisationParticipant.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DistrictFinderVisualisationParticipant.java @@ -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 diff --git a/org.simantics.district.network/META-INF/MANIFEST.MF b/org.simantics.district.network/META-INF/MANIFEST.MF index f5976e26..79671648 100644 --- a/org.simantics.district.network/META-INF/MANIFEST.MF +++ b/org.simantics.district.network/META-INF/MANIFEST.MF @@ -27,4 +27,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 index 00000000..3eea008e --- /dev/null +++ b/org.simantics.district.network/scl/Simantics/District/VisualisationTriggers.scl @@ -0,0 +1,5 @@ +importJava "org.simantics.district.network.visualisations.triggers.VisualisationTrigger" where + data VisualisationTrigger + + @JavaName "" + 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 index 00000000..7a9614e5 --- /dev/null +++ b/org.simantics.district.network/src/org/simantics/district/network/visualisations/triggers/VisualisationTrigger.java @@ -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 index 00000000..b18fede4 --- /dev/null +++ b/org.simantics.district.network/src/org/simantics/district/network/visualisations/triggers/VisualisationTriggerComponent.java @@ -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 index 00000000..8120a973 --- /dev/null +++ b/org.simantics.district.network/src/org/simantics/district/network/visualisations/triggers/VisualisationTriggersContributions.java @@ -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> visualisationTriggers(ReadGraph graph) throws DatabaseException { + List sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE); + + Map> 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 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 visualisationTriggerContributions = visualisationTriggerContribution(graph, moduleType); + if (visualisationTriggerContributions != null) + results.put(moduleType.getName(), visualisationTriggerContributions); + } + } + } finally { + SCLContext.getCurrent().put("graph", oldGraph); + } + return results; + } + + private static List 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 result = (List) 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; + } + +} +