package org.simantics.district.network.ui.styles; import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.simantics.Simantics; import org.simantics.db.ReadGraph; 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.util.Layer0Utils; import org.simantics.diagram.profile.StyleBase; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.district.network.ui.nodes.ConnectionLineNode; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; import org.simantics.scenegraph.INode; import org.simantics.scenegraph.profile.EvaluationContext; import org.simantics.scenegraph.profile.common.ProfileVariables; 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; @Deprecated public class ConnectionLineStyle extends StyleBase> { @Override public List calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException { return doCalculateConnectedComponentPoints(graph, groupItem); } public static List doCalculateConnectedComponentPoints(ReadGraph graph, Resource vertex) throws DatabaseException { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); ModelingResources MOD = ModelingResources.getInstance(graph); StructuralResource2 STR = StructuralResource2.getInstance(graph); if (!graph.isInstanceOf(vertex, DN.Vertex)) return Collections.emptyList(); double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation); Resource component = DistrictNetworkUtil.getMappedComponentCached(graph, vertex); if (component == null) return Collections.emptyList(); Resource componentType = graph.getPossibleType(component, STR.Component); if (componentType == null) return Collections.emptyList(); Function1> fun = getConnectedComponentsFunctionCached(graph, componentType); if (fun == null) return Collections.emptyList(); List components = Simantics.applySCLRead(graph, fun, component); if (components == null || components.isEmpty()) return Collections.emptyList(); List result = new ArrayList<>(components.size() + 1); result.add(new Point2D.Double(coords[0], coords[1])); for (Resource comp : components) { Resource e = comp != null ? graph.getPossibleObject(comp, MOD.ComponentToElement) : null; Resource mappingElement = e != null ? graph.getPossibleObject(e, DN.MappedFromElement) : null; if (mappingElement != null) { double[] coords2 = graph.getRelatedValue(mappingElement, DiagramResource.getInstance(graph).HasLocation); result.add(new Point2D.Double(coords2[0], coords2[1])); } else { result.add(null); } } return result; } @Override public void applyStyleForNode(EvaluationContext observer, INode parent, List result) { if (result == null || result.size() < 2) { ProfileVariables.denyChild(parent, "*", ConnectionLineNode.NODE_NAME); return; } ConnectionLineNode node = ProfileVariables.claimChild(parent, "*", ConnectionLineNode.NODE_NAME, ConnectionLineNode.class, observer); if (node == null) return; node.setPoints(result); node.setZIndex(0); node.setStrokeWidth(2.f); } @Override protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode parent) { ProfileVariables.denyChild(parent, "*", ConnectionLineNode.NODE_NAME); } private static Function1> getConnectedComponentsFunctionCached(ReadGraph graph, Resource componentType) throws DatabaseException { return graph.syncRequest(new ConnectedComponentsFunctionRequest(componentType), TransientCacheListener.instance()); } private static final class ConnectedComponentsFunctionRequest extends ResourceRead>> { public ConnectedComponentsFunctionRequest(Resource resource) { super(resource); } @SuppressWarnings("unchecked") @Override public Function1> perform(ReadGraph graph) throws DatabaseException { Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, "Actions"); if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule)) return null; String uri = graph.getURI(actionsModule); SCLContext sclContext = SCLContext.getCurrent(); Object oldGraph = sclContext.get("graph"); try { sclContext.put("graph", graph); return (Function1>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "getConnectedComponents"); } catch (ValueNotFound e1) { return null; } finally { sclContext.put("graph", oldGraph); } } } }