X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Fnodes%2FConnectionLineStyle.java;h=5b3d2b43b4c1e4da8582f811323e31d102268fc2;hb=refs%2Fchanges%2F67%2F2767%2F1;hp=75a46fd4ef0f94dd77eac0ecc31cb59d32cb5b89;hpb=6ca29dac1f155f35b5d246e4ad5dc8aa1d6198aa;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ConnectionLineStyle.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ConnectionLineStyle.java index 75a46fd4..5b3d2b43 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ConnectionLineStyle.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ConnectionLineStyle.java @@ -19,6 +19,7 @@ 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.layer0.Layer0; import org.simantics.modeling.ModelingResources; @@ -27,6 +28,10 @@ import org.simantics.scenegraph.g2d.G2DNode; import org.simantics.scenegraph.profile.EvaluationContext; import org.simantics.scenegraph.profile.common.ProfileVariables; import org.simantics.scenegraph.utils.GeometryUtils; +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; public class ConnectionLineStyle extends StyleBase> { @@ -94,7 +99,48 @@ public class ConnectionLineStyle extends StyleBase> { @Override public List calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException { - return graph.syncRequest(new ElementConnectionRequest(groupItem), TransientCacheListener.instance()); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + ModelingResources MOD = ModelingResources.getInstance(graph); + StructuralResource2 STR = StructuralResource2.getInstance(graph); + + Resource vertex = groupItem; + 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 @@ -118,63 +164,35 @@ public class ConnectionLineStyle extends StyleBase> { ProfileVariables.denyChild(parent, "*", "districtNetworkConnection"); } - private static final class ElementConnectionRequest extends ResourceRead> { - private ElementConnectionRequest(Resource resource) { + 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 List perform(ReadGraph graph) throws DatabaseException { - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - ModelingResources MOD = ModelingResources.getInstance(graph); - StructuralResource2 STR = StructuralResource2.getInstance(graph); - - Resource vertex = resource; - if (!graph.isInstanceOf(vertex, DN.Vertex)) - return Collections.emptyList(); - - double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation); - - Resource element = graph.getPossibleObject(vertex, DN.MappedComponent); - if (element == null) - return Collections.emptyList(); - Resource component = graph.getPossibleObject(element, MOD.ElementToComponent); - if (component == null) - return Collections.emptyList(); - Resource componentType = graph.getPossibleType(component, STR.Component); - if (componentType == null) - return Collections.emptyList(); - - Resource actionsModule = Layer0Utils.getPossibleChild(graph, componentType, "Actions"); + 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 Collections.emptyList(); + return null; - List components; + String uri = graph.getURI(actionsModule); + SCLContext sclContext = SCLContext.getCurrent(); + Object oldGraph = sclContext.get("graph"); try { - components = Simantics.applySCL(graph.getURI(actionsModule), "getConnectedComponents", graph, component); + sclContext.put("graph", graph); + return (Function1>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "getConnectedComponents"); + } catch (ValueNotFound e1) { + return null; + } finally { + sclContext.put("graph", oldGraph); } - catch (DatabaseException e) { - return Collections.emptyList(); - } - - 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; } } }