X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Fstyles%2FDistrictNetworkStaticInfoStyle.java;h=d0bc9551f572df106d24b88704af53afd17874c0;hb=3730466cd1e90c50c988292e107639952448d6a6;hp=e452a17b1f52188d8ab43b48de2a4302c5ad2a8a;hpb=38cb4b0b42c4c35a696bb46ffcf8129ca8d89103;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java index e452a17b..d0bc9551 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/styles/DistrictNetworkStaticInfoStyle.java @@ -1,6 +1,7 @@ package org.simantics.district.network.ui.styles; import java.awt.geom.Point2D; +import java.util.Objects; import java.util.Set; import org.simantics.Simantics; @@ -9,6 +10,7 @@ import org.simantics.db.Resource; import org.simantics.db.common.procedure.adapter.TransientCacheListener; import org.simantics.db.common.request.ResourceRead; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.exception.MissingVariableException; import org.simantics.db.layer0.exception.MissingVariableValueException; import org.simantics.db.layer0.exception.PendingVariableException; import org.simantics.db.layer0.util.Layer0Utils; @@ -19,100 +21,136 @@ import org.simantics.diagram.stubs.DiagramResource; import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.district.network.profile.MidBranchEdgeSetRequest; +import org.simantics.district.network.ui.nodes.DeferredRenderingNode; +import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode; import org.simantics.district.network.ui.nodes.DistrictNetworkNodeUtils; import org.simantics.district.network.ui.nodes.DistrictNetworkStaticInfoNode; import org.simantics.layer0.Layer0; import org.simantics.scenegraph.INode; +import org.simantics.scenegraph.ParentNode; +import org.simantics.scenegraph.g2d.IG2DNode; +import org.simantics.scenegraph.g2d.nodes.ConnectionNode; +import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode; import org.simantics.scenegraph.profile.EvaluationContext; import org.simantics.scenegraph.profile.common.ProfileVariables; +import org.simantics.scenegraph.utils.NodeUtil; import org.simantics.scl.compiler.top.ValueNotFound; import org.simantics.scl.osgi.SCLOsgi; import org.simantics.scl.runtime.SCLContext; import org.simantics.scl.runtime.function.Function1; import org.simantics.structural.stubs.StructuralResource2; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +@Deprecated public class DistrictNetworkStaticInfoStyle extends StyleBase { + private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkStaticInfoStyle.class); + private static final String ACTIONS_MODULE = "Actions"; private static final String PIPELINE_INFO = "pipelineInfo"; - + + public static final String STATIC_INFO_DEFERRED = "staticInfo"; + public static class StyleResult { - public StyleResult(Point2D p1, Point2D p2, String info) { + public StyleResult(Resource r, Point2D p1, String info) { + this.r = r; this.p1 = p1; - this.p2 = p2; this.info = info; } - - public Point2D p1; - public Point2D p2; - public String info; + + public final Resource r; + public final Point2D p1; + public final String info; + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + r.hashCode(); + result = prime * result + ((info == null) ? 0 : info.hashCode()); + result = prime * result + ((p1 == null) ? 0 : p1.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + StyleResult other = (StyleResult) obj; + return r.equals(other.r) && Objects.equals(info, other.info) && Objects.equals(p1, other.p1); + } } - + public DistrictNetworkStaticInfoStyle(Resource style) { - super(style); + super(); } - + + private static final Point2D EDGE = new Point2D.Double(); + @Override public StyleResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource mapElement) throws DatabaseException { - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - - boolean isEdge = graph.isInstanceOf(mapElement, DN.Edge); - boolean isVertex = graph.isInstanceOf(mapElement, DN.Vertex); - if (!isEdge && !isVertex) - return null; - - if (isEdge) { - Resource diagram = graph.getSingleObject(mapElement, Layer0.getInstance(graph).PartOf); - Set edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance()); - if (!edgesToUse.contains(mapElement)) - return null; - } - - DiagramResource DIA = DiagramResource.getInstance(graph); - StructuralResource2 STR = StructuralResource2.getInstance(graph); - - Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement); - if (module == null) - return null; - - Resource moduleType = graph.getPossibleType(module, STR.Component); - if (moduleType == null) - return null; - - Function1 function = getUCPipelineInfoFunctionCached(graph, moduleType); - if (function == null) - return null; - - String result; - try { - Variable variable = Variables.getVariable(graph, module); - Variable moduleVariable = Variables.possibleActiveVariable(graph, variable); - if (moduleVariable == null) - moduleVariable = variable; - - result = Simantics.applySCLRead(graph, function, moduleVariable); - } catch (PendingVariableException | MissingVariableValueException e) { - result = null; - } - - if (isVertex) { - double[] coords = graph.getRelatedValue(mapElement, DIA.HasLocation); - Point2D p = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords[0], coords[1]), null); - return new StyleResult(p, p, result); - } - else if (isEdge) { - Resource v1 = graph.getSingleObject(mapElement, DN.HasStartVertex); - double[] coords1 = graph.getRelatedValue(v1, DIA.HasLocation); - Resource v2 = graph.getSingleObject(mapElement, DN.HasEndVertex); - double[] coords2 = graph.getRelatedValue(v2, DIA.HasLocation); - Point2D p1 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords1[0], coords1[1]), null); - Point2D p2 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords2[0], coords2[1]), null); - - return new StyleResult(p1, p2, result); - } - - return null; + return calculateStyle(graph, entry, mapElement); + } + + public static StyleResult calculateStyle(ReadGraph graph, Resource entry, Resource mapElement) throws DatabaseException { + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + Set types = graph.getTypes(mapElement); + boolean isEdge = types.contains(DN.Edge); + boolean isVertex = types.contains(DN.Vertex); + if (!isEdge && !isVertex) + return null; + + if (isEdge) { + Resource diagram = graph.getSingleObject(mapElement, Layer0.getInstance(graph).PartOf); + Set edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance()); + if (!edgesToUse.contains(mapElement)) + return null; + } + + Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement); + if (module == null) + return null; + + StructuralResource2 STR = StructuralResource2.getInstance(graph); + Resource moduleType = graph.getPossibleType(module, STR.Component); + if (moduleType == null) + return null; + + Function1 function = getUCPipelineInfoFunctionCached(graph, moduleType); + if (function == null) + return null; + + String result; + try { + Variable variable = Variables.getVariable(graph, module); + Variable moduleVariable = Variables.possibleActiveVariable(graph, variable); + if (moduleVariable == null) + moduleVariable = variable; + + result = Simantics.applySCLRead(graph, function, moduleVariable); + } catch (PendingVariableException | MissingVariableValueException e) { + result = null; + } catch (MissingVariableException e) { + // the requested variable is missing from the UC + String message = e.getMessage(); + LOGGER.warn("Missing variable for calculating style with function {} {}", function, message); + result = "<" + message + ">"; + } + + if (isVertex) { + DiagramResource DIA = DiagramResource.getInstance(graph); + double[] coords = graph.getRelatedValue(mapElement, DIA.HasLocation); + Point2D p = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords[0], coords[1]), null); + return new StyleResult(mapElement, p, result); + } else if (isEdge) { + return new StyleResult(mapElement, EDGE, result); + } + return null; } @Override @@ -121,33 +159,38 @@ public class DistrictNetworkStaticInfoStyle extends StyleBase root = (ParentNode) NodeUtil.getNearestParentOfType(parent, RTreeNode.class); + if (root != null) { + DeferredRenderingNode deferred = ProfileVariables.claimChild(root, "", STATIC_INFO_DEFERRED, DeferredRenderingNode.class, evaluationContext); + deferred.setZIndex(Integer.MAX_VALUE-1); + } + DistrictNetworkStaticInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class, evaluationContext); if (node == null) return; Point2D p1 = result.p1; - Point2D p2 = result.p2; - - if (p1.equals(p2)) { + if (p1 != EDGE) { node.setLocation(p1, new Point2D.Double(1.0, 0.0)); + } else { + // StaticInfoNode takes care of positioning the text during render + // based on the found DistrictNetworkEdgeNode's information. + for (IG2DNode n : ((ConnectionNode)parent).getNodes()) { + if (n instanceof DistrictNetworkEdgeNode) { + node.setEdgeNode((DistrictNetworkEdgeNode) n); + } + } } - else { - double sign = Math.signum(p1.getX() - p2.getX()); - Point2D.Double origin = new Point2D.Double(0.5 * (p1.getX() + p2.getX()), 0.5 * (p1.getY() + p2.getY())); - Point2D direction = new Point2D.Double(0.5 * sign * (p1.getX() - p2.getX()), 0.5 * sign * (p1.getY() - p2.getY())); - - node.setLocation(origin, direction); - } - + node.setInfo(result.info); } - + private static Function1 getUCPipelineInfoFunctionCached(ReadGraph graph, Resource componentType) throws DatabaseException { return graph.syncRequest(new UCPipelineInfoRequest(componentType), TransientCacheListener.instance()); } - + private static final class UCPipelineInfoRequest extends ResourceRead> { public UCPipelineInfoRequest(Resource resource) { super(resource); @@ -159,7 +202,7 @@ public class DistrictNetworkStaticInfoStyle extends StyleBase