--- /dev/null
+bin\r
+target\r
+*.class\r
+.settings\r
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>\r
+<classpath>\r
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>\r
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>\r
+ <classpathentry kind="src" path="src"/>\r
+ <classpathentry kind="output" path="bin"/>\r
+</classpath>\r
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>\r
+<projectDescription>\r
+ <name>org.simantics.district.network.ontology</name>\r
+ <comment></comment>\r
+ <projects>\r
+ </projects>\r
+ <buildSpec>\r
+ <buildCommand>\r
+ <name>org.simantics.graph.builder</name>\r
+ <arguments>\r
+ </arguments>\r
+ </buildCommand>\r
+ <buildCommand>\r
+ <name>org.eclipse.jdt.core.javabuilder</name>\r
+ <arguments>\r
+ </arguments>\r
+ </buildCommand>\r
+ <buildCommand>\r
+ <name>org.eclipse.pde.ManifestBuilder</name>\r
+ <arguments>\r
+ </arguments>\r
+ </buildCommand>\r
+ <buildCommand>\r
+ <name>org.eclipse.pde.SchemaBuilder</name>\r
+ <arguments>\r
+ </arguments>\r
+ </buildCommand>\r
+ </buildSpec>\r
+ <natures>\r
+ <nature>org.eclipse.pde.PluginNature</nature>\r
+ <nature>org.eclipse.jdt.core.javanature</nature>\r
+ <nature>org.simantics.graph.nature</nature>\r
+ </natures>\r
+</projectDescription>\r
--- /dev/null
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Simantics District Network Ontology
+Bundle-SymbolicName: org.simantics.district.network.ontology
+Bundle-Version: 1.0.0.qualifier
+Require-Bundle: org.simantics.layer0,
+ org.simantics.diagram.ontology;bundle-version="2.2.0",
+ org.simantics.structural.ontology;bundle-version="1.2.0",
+ org.simantics.modeling.ontology;bundle-version="1.2.0",
+ org.simantics.viewpoint.ontology;bundle-version="1.2.0",
+ org.simantics.image2.ontology;bundle-version="1.2.0"
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
+Export-Package: org.simantics.district.network.ontology
--- /dev/null
+source.. = src/\r
+output.. = bin/\r
+bin.includes = plugin.xml,\\r
+ META-INF/,\\r
+ .,\\r
+ graph.tg\r
--- /dev/null
+L0 = <http://www.simantics.org/Layer0-1.1>
+DIA = <http://www.simantics.org/Diagram-2.2>
+STR = <http://www.simantics.org/Structural-1.2>
+MOD = <http://www.simantics.org/Modeling-1.2>
+VP = <http://www.simantics.org/Viewpoint-1.2>
+IMAGE = <http://www.simantics.org/Image2-1.2>
+
+MBC = MOD.ModelingBrowseContext
+
+DN = <http://www.simantics.org/DistrictNetwork-1.0> : L0.Ontology
+ @L0.new
+ L0.HasResourceClass "org.simantics.district.network.ontology.DistrictNetworkResource"
+
+DN.Edge <T DIA.Element : L0.Type
+
+DN.Vertex <T DIA.Element : L0.Type
+
+DN.Connection <T DIA.Connection
+
+DN.Diagram <T DIA.Diagram
+
+
+DN.Composite <T STR.Composite
+ @L0.assert MOD.HasModelingTemplate
+ _ : MOD.DiagramTemplate
+ MOD.HasDiagramType DN.Diagram
+
+DN.HasStartVertex <R L0.HasProperty
+ L0.HasRange DN.Vertex
+
+DN.HasEndVertex <R L0.HasProperty
+ L0.HasRange DN.Vertex
+
+DNAC.newDiagramContribution : L0.Template
+ @template %actionContext %label %image %action
+ %actionContext
+ VP.BrowseContext.HasActionContribution _ : VP.ActionContribution
+ L0.HasLabel %label
+ VP.ActionContribution.HasImage %image
+ VP.ActionContribution.HasCategory VP.NewActionCategory
+ VP.ActionContribution.HasNodeType MBC.Configuration
+ VP.ActionContribution.HasAction %action
+
+DNAC = DN.ActionContext : VP.BrowseContext
+ VP.BrowseContext.IsIncludedIn MOD.ModelingActionContext
+ @DNAC.newDiagramContribution "DN Diagram" IMAGES.ConfigurationFolder ACTIONS.NewDNDiagram
+
+ACTIONS = DN.Actions : L0.Library
+
+ACTIONS.NewDNDiagram : MOD.NewComposite
+ MOD.NewComposite.HasCompositeType DN.Composite
+ MOD.NewComposite.HasDefaultName "NewDNComposite"
+
+IMAGES = DN.Images : L0.Library
+
+IMAGES.ConfigurationFolder : IMAGE.PngImage
+ @L0.loadBytes "images/Configuration.png"
--- /dev/null
+package org.simantics.district.network.ontology;\r
+\r
+import org.simantics.db.RequestProcessor;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.service.QueryControl;\r
+\r
+public class DistrictNetworkResource {\r
+ \r
+ public final Resource ActionContext;\r
+ public final Resource ActionContext_newDiagramContribution;\r
+ public final Resource Actions;\r
+ public final Resource Actions_NewDNDiagram;\r
+ public final Resource Composite;\r
+ public final Resource Connection;\r
+ public final Resource Diagram;\r
+ public final Resource Edge;\r
+ public final Resource HasEndVertex;\r
+ public final Resource HasEndVertex_Inverse;\r
+ public final Resource HasStartVertex;\r
+ public final Resource HasStartVertex_Inverse;\r
+ public final Resource Images;\r
+ public final Resource Images_ConfigurationFolder;\r
+ public final Resource Vertex;\r
+ \r
+ public static class URIs {\r
+ public static final String ActionContext = "http://www.simantics.org/DistrictNetwork-1.0/ActionContext";\r
+ public static final String ActionContext_newDiagramContribution = "http://www.simantics.org/DistrictNetwork-1.0/ActionContext/newDiagramContribution";\r
+ public static final String Actions = "http://www.simantics.org/DistrictNetwork-1.0/Actions";\r
+ public static final String Actions_NewDNDiagram = "http://www.simantics.org/DistrictNetwork-1.0/Actions/NewDNDiagram";\r
+ public static final String Composite = "http://www.simantics.org/DistrictNetwork-1.0/Composite";\r
+ public static final String Connection = "http://www.simantics.org/DistrictNetwork-1.0/Connection";\r
+ public static final String Diagram = "http://www.simantics.org/DistrictNetwork-1.0/Diagram";\r
+ public static final String Edge = "http://www.simantics.org/DistrictNetwork-1.0/Edge";\r
+ public static final String HasEndVertex = "http://www.simantics.org/DistrictNetwork-1.0/HasEndVertex";\r
+ public static final String HasEndVertex_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/HasEndVertex/Inverse";\r
+ public static final String HasStartVertex = "http://www.simantics.org/DistrictNetwork-1.0/HasStartVertex";\r
+ public static final String HasStartVertex_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/HasStartVertex/Inverse";\r
+ public static final String Images = "http://www.simantics.org/DistrictNetwork-1.0/Images";\r
+ public static final String Images_ConfigurationFolder = "http://www.simantics.org/DistrictNetwork-1.0/Images/ConfigurationFolder";\r
+ public static final String Vertex = "http://www.simantics.org/DistrictNetwork-1.0/Vertex";\r
+ }\r
+ \r
+ public static Resource getResourceOrNull(ReadGraph graph, String uri) {\r
+ try {\r
+ return graph.getResource(uri);\r
+ } catch(DatabaseException e) {\r
+ System.err.println(e.getMessage());\r
+ return null;\r
+ }\r
+ }\r
+ \r
+ public DistrictNetworkResource(ReadGraph graph) {\r
+ ActionContext = getResourceOrNull(graph, URIs.ActionContext);\r
+ ActionContext_newDiagramContribution = getResourceOrNull(graph, URIs.ActionContext_newDiagramContribution);\r
+ Actions = getResourceOrNull(graph, URIs.Actions);\r
+ Actions_NewDNDiagram = getResourceOrNull(graph, URIs.Actions_NewDNDiagram);\r
+ Composite = getResourceOrNull(graph, URIs.Composite);\r
+ Connection = getResourceOrNull(graph, URIs.Connection);\r
+ Diagram = getResourceOrNull(graph, URIs.Diagram);\r
+ Edge = getResourceOrNull(graph, URIs.Edge);\r
+ HasEndVertex = getResourceOrNull(graph, URIs.HasEndVertex);\r
+ HasEndVertex_Inverse = getResourceOrNull(graph, URIs.HasEndVertex_Inverse);\r
+ HasStartVertex = getResourceOrNull(graph, URIs.HasStartVertex);\r
+ HasStartVertex_Inverse = getResourceOrNull(graph, URIs.HasStartVertex_Inverse);\r
+ Images = getResourceOrNull(graph, URIs.Images);\r
+ Images_ConfigurationFolder = getResourceOrNull(graph, URIs.Images_ConfigurationFolder);\r
+ Vertex = getResourceOrNull(graph, URIs.Vertex);\r
+ }\r
+ \r
+ public static DistrictNetworkResource getInstance(ReadGraph graph) {\r
+ Session session = graph.getSession();\r
+ DistrictNetworkResource ret = session.peekService(DistrictNetworkResource.class);\r
+ if(ret == null) {\r
+ QueryControl qc = graph.getService(QueryControl.class);\r
+ ret = new DistrictNetworkResource(qc.getIndependentGraph(graph));\r
+ session.registerService(DistrictNetworkResource.class, ret);\r
+ }\r
+ return ret;\r
+ }\r
+ \r
+ public static DistrictNetworkResource getInstance(RequestProcessor session) throws DatabaseException {\r
+ DistrictNetworkResource ret = session.peekService(DistrictNetworkResource.class);\r
+ if(ret == null) {\r
+ ret = session.syncRequest(new Read<DistrictNetworkResource>() {\r
+ public DistrictNetworkResource perform(ReadGraph graph) throws DatabaseException {\r
+ QueryControl qc = graph.getService(QueryControl.class);\r
+ return new DistrictNetworkResource(qc.getIndependentGraph(graph));\r
+ }\r
+ });\r
+ session.registerService(DistrictNetworkResource.class, ret);\r
+ }\r
+ return ret;\r
+ }\r
+ \r
+}\r
+\r
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>\r
+<classpath>\r
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>\r
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>\r
+ <classpathentry kind="src" path="src"/>\r
+ <classpathentry kind="output" path="bin"/>\r
+</classpath>\r
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>\r
+<projectDescription>\r
+ <name>org.simantics.district.network.ui</name>\r
+ <comment></comment>\r
+ <projects>\r
+ </projects>\r
+ <buildSpec>\r
+ <buildCommand>\r
+ <name>org.eclipse.jdt.core.javabuilder</name>\r
+ <arguments>\r
+ </arguments>\r
+ </buildCommand>\r
+ <buildCommand>\r
+ <name>org.eclipse.pde.ManifestBuilder</name>\r
+ <arguments>\r
+ </arguments>\r
+ </buildCommand>\r
+ <buildCommand>\r
+ <name>org.eclipse.pde.SchemaBuilder</name>\r
+ <arguments>\r
+ </arguments>\r
+ </buildCommand>\r
+ </buildSpec>\r
+ <natures>\r
+ <nature>org.eclipse.pde.PluginNature</nature>\r
+ <nature>org.eclipse.jdt.core.javanature</nature>\r
+ </natures>\r
+</projectDescription>\r
--- /dev/null
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Simantics District Network UI
+Bundle-SymbolicName: org.simantics.district.network.ui;singleton:=true
+Bundle-Version: 1.0.0.qualifier
+Require-Bundle: org.eclipse.e4.ui.model.workbench;bundle-version="1.1.100.v20150407-1430",
+ org.eclipse.swt,
+ org.simantics.g2d,
+ org.eclipse.e4.core.di.annotations,
+ org.eclipse.e4.ui.workbench,
+ javax.inject,
+ org.simantics.ui,
+ org.simantics.modeling.ui,
+ org.simantics.district.network.ontology,
+ org.simantics.utils.datastructures,
+ org.simantics.district.network;bundle-version="1.0.0"
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
+Export-Package: org.simantics.district.network.ui.adapters
+Bundle-ActivationPolicy: lazy
+Bundle-Activator: org.simantics.district.network.ui.internal.Activator
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>\r
+<adapters>\r
+ <target interface="org.simantics.diagram.adapter.ElementFactory">\r
+ <resource uri="http://www.simantics.org/DistrictNetwork-1.0/Edge"\r
+ class="org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElementFactory">\r
+ <graph />\r
+ </resource>\r
+ <type uri="http://www.simantics.org/DistrictNetwork-1.0/Edge"\r
+ class="org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElementFactory">\r
+ <graph />\r
+ </type>\r
+ <resource uri="http://www.simantics.org/DistrictNetwork-1.0/Vertex"\r
+ class="org.simantics.district.network.ui.adapters.DistrictNetworkVertexElementFactory">\r
+ <graph />\r
+ </resource>\r
+ <type uri="http://www.simantics.org/DistrictNetwork-1.0/Vertex"\r
+ class="org.simantics.district.network.ui.adapters.DistrictNetworkVertexElementFactory">\r
+ <graph />\r
+ </type>\r
+ </target>\r
+</adapters>
\ No newline at end of file
--- /dev/null
+output.. = bin/\r
+bin.includes = META-INF/,\\r
+ .,\\r
+ fragment.e4xmi,\\r
+ plugin.xml,\\r
+ adapters.xml\r
+source.. = src/\r
--- /dev/null
+<?xml version="1.0" encoding="ASCII"?>\r
+<fragment:ModelFragments xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:basic="http://www.eclipse.org/ui/2010/UIModel/application/descriptor/basic" xmlns:commands="http://www.eclipse.org/ui/2010/UIModel/application/commands" xmlns:fragment="http://www.eclipse.org/ui/2010/UIModel/fragment" xmlns:menu="http://www.eclipse.org/ui/2010/UIModel/application/ui/menu" xmi:id="_lkvWIML0Eea1mdgpHNVHMA">\r
+ <fragments xsi:type="fragment:StringModelFragment" xmi:id="_nMVscML8Eea1mdgpHNVHMA" featurename="descriptors" parentElementId="xpath:/">\r
+ <elements xsi:type="basic:PartDescriptor" xmi:id="_bSU8EML9Eea1mdgpHNVHMA" elementId="org.simantics.district.network.ui.partdescriptor.districteditor" label="District Editor" tooltip="Test" contributionURI="bundleclass://org.simantics.district.network.ui/org.simantics.district.network.ui.DistrictEditor"/>\r
+ </fragments>\r
+ <fragments xsi:type="fragment:StringModelFragment" xmi:id="_4mZBIML_Eea1mdgpHNVHMA" featurename="commands" parentElementId="xpath:/">\r
+ <elements xsi:type="commands:Command" xmi:id="_6lrYUML_Eea1mdgpHNVHMA" elementId="org.simantics.district.network.ui.command.openDistrictEditor" commandName="Open District Editor"/>\r
+ </fragments>\r
+ <fragments xsi:type="fragment:StringModelFragment" xmi:id="_Js7rUMMAEea1mdgpHNVHMA" featurename="menuContributions" parentElementId="xpath:/">\r
+ <elements xsi:type="menu:MenuContribution" xmi:id="_MbrJgMMAEea1mdgpHNVHMA" elementId="org.simantics.district.network.ui.menucontribution.0" parentId="help">\r
+ <children xsi:type="menu:HandledMenuItem" xmi:id="_0D7tIMMAEea1mdgpHNVHMA" elementId="org.simantics.district.network.ui.handledmenuitem.opendistricteditor" label="Open District Editor" command="_6lrYUML_Eea1mdgpHNVHMA"/>\r
+ </elements>\r
+ </fragments>\r
+ <fragments xsi:type="fragment:StringModelFragment" xmi:id="_dbiHcMMBEea1mdgpHNVHMA" featurename="handlers" parentElementId="xpath:/">\r
+ <elements xsi:type="commands:Handler" xmi:id="_ffHv0MMBEea1mdgpHNVHMA" elementId="org.simantics.district.network.ui.handler.0"/>\r
+ </fragments>\r
+</fragment:ModelFragments>\r
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>\r
+<plugin>\r
+\r
+ <extension\r
+ id="org.simantics.district.network.ui.fragment"\r
+ point="org.eclipse.e4.workbench.model">\r
+ <fragment\r
+ uri="fragment.e4xmi">\r
+ </fragment>\r
+ </extension>\r
+ <extension\r
+ point="org.eclipse.ui.editors">\r
+ <editor\r
+ name="District Network Diagram Editor"\r
+ icon="icons/diagramEditor.png"\r
+ class="org.simantics.district.network.ui.DistrictDiagramEditor:viewer=org.simantics.district.network.ui.DistrictDiagramViewer"\r
+ contributorClass="org.simantics.modeling.ui.diagramEditor.DiagramViewerActionContributor"\r
+ matchingStrategy="org.simantics.ui.workbench.editor.input.ResourceEditorInputMatchingStrategy"\r
+ id="org.simantics.district.network.ui.diagrameditor">\r
+ </editor>\r
+ </extension>\r
+ <extension\r
+ point="org.simantics.ui.resourceEditorAdapter">\r
+ <adapterClass\r
+ groupId="org.simantics.diagramEditor.group"\r
+ priority="300"\r
+ class="org.simantics.district.network.ui.OpenDiagramFromConfigurationAdapter">\r
+ </adapterClass>\r
+ </extension>\r
+</plugin>\r
--- /dev/null
+package org.simantics.district.network.ui;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.diagram.synchronization.graph.AddElement;\r
+import org.simantics.district.network.DistrictNetworkUtil;\r
+import org.simantics.district.network.ontology.DistrictNetworkResource;\r
+\r
+public class DNEdgeBuilder {\r
+ \r
+ private Resource diagramResource;\r
+\r
+ public DNEdgeBuilder(Resource diagramResource) {\r
+ this.diagramResource = diagramResource;\r
+ }\r
+\r
+ public void create(WriteGraph graph, double[] start, double[] end) throws DatabaseException {\r
+ \r
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);\r
+ \r
+ // 1. Get diagram edge to construct\r
+ Resource edge = getOrCreateEdge(graph);\r
+ \r
+ // 1.1 Give running name to connection and increment the counter attached to the diagram.\r
+ AddElement.claimFreshElementName(graph, diagramResource, edge);\r
+ \r
+ // 2. Add vertices\r
+ Resource startVertex = getOrCreateVertex(graph, start);\r
+ Resource endVertex = getOrCreateVertex(graph, end);\r
+ \r
+ graph.claim(edge, DN.HasStartVertex, startVertex);\r
+ graph.claim(edge, DN.HasEndVertex, endVertex);\r
+ \r
+ Layer0Utils.addCommentMetadata(graph, "Added edge " + edge);\r
+ graph.markUndoPoint();\r
+ }\r
+\r
+ private Resource getOrCreateVertex(WriteGraph graph, double[] coords) throws DatabaseException {\r
+ // TODO: check if vertex exists already, for now create always new\r
+ Resource vertex = null;\r
+ if (vertex == null) {\r
+ vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords); \r
+ AddElement.claimFreshElementName(graph, diagramResource, vertex);\r
+ }\r
+ return vertex;\r
+ }\r
+\r
+ private Resource getOrCreateEdge(WriteGraph graph) throws DatabaseException {\r
+ return DistrictNetworkUtil.createEdge(graph, diagramResource);\r
+ }\r
+\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui;\r
+\r
+import org.eclipse.ui.PartInitException;\r
+import org.simantics.modeling.ui.diagramEditor.DiagramEditor;\r
+import org.simantics.modeling.ui.diagramEditor.DiagramViewer;\r
+\r
+public class DistrictDiagramEditor extends DiagramEditor {\r
+\r
+ public static final String ID = "org.simantics.district.network.ui.diagrameditor";\r
+ \r
+ @Override\r
+ protected DiagramViewer createViewer() throws PartInitException {\r
+ return new DistrictDiagramViewer();\r
+ }\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui;\r
+\r
+import java.util.concurrent.TimeUnit;\r
+\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.handler.PickRequest.PickFilter;\r
+import org.simantics.g2d.diagram.participant.DelayedBatchElementPainter;\r
+import org.simantics.g2d.diagram.participant.ElementPainter;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.g2d.diagram.participant.ZOrderHandler;\r
+import org.simantics.g2d.participant.RenderingQualityInteractor;\r
+import org.simantics.modeling.ui.diagramEditor.DiagramViewer;\r
+\r
+public class DistrictDiagramViewer extends DiagramViewer {\r
+\r
+ @Override\r
+ protected void addDiagramParticipants(ICanvasContext ctx) {\r
+ ctx.add(new ZOrderHandler());\r
+ ctx.add(new Selection());\r
+ ctx.add(new ElementPainter());\r
+ \r
+ ctx.add(new NetworkDrawingParticipant());\r
+ }\r
+\r
+ @Override\r
+ protected void addPainterParticipants(ICanvasContext ctx) {\r
+ ctx.add(new RenderingQualityInteractor());\r
+ ctx.add(new DelayedBatchElementPainter(PickFilter.FILTER_MONITORS, 500, TimeUnit.MILLISECONDS));\r
+ }\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui;\r
+\r
+import java.awt.geom.Path2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+public class DistrictNetworkEdge {\r
+\r
+ private Path2D path;\r
+ \r
+ public DistrictNetworkEdge(Path2D path) {\r
+ this.path = path;\r
+ }\r
+\r
+ public Path2D getPath() {\r
+ return path;\r
+ }\r
+\r
+ public Rectangle2D getBounds(Rectangle2D rect) {\r
+ return path.getBounds2D();\r
+ }\r
+}\r
--- /dev/null
+\r
+package org.simantics.district.network.ui;\r
+\r
+import java.awt.Color;\r
+import java.awt.Graphics2D;\r
+import java.awt.Shape;\r
+import java.awt.geom.Path2D;\r
+import java.awt.geom.Point2D;\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.HashSet;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Set;\r
+\r
+import org.simantics.Simantics;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.ui.DiagramModelHints;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
+import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.diagram.handler.PickContext;\r
+import org.simantics.g2d.diagram.handler.PickRequest;\r
+import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;\r
+import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.participant.TransformUtil;\r
+import org.simantics.g2d.utils.GeometryUtils;\r
+import org.simantics.scenegraph.g2d.G2DNode;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.events.EventTypes;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent;\r
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;\r
+import org.simantics.scenegraph.utils.NodeUtil;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+\r
+public class NetworkDrawingParticipant extends AbstractDiagramParticipant {\r
+\r
+ /**\r
+ * A hint key for terminal pick distance in control pixels.\r
+ * @see #PICK_DIST\r
+ */\r
+ public static final Key KEY_PICK_DISTANCE = new KeyOf(Double.class, "PICK_DISTANCE");\r
+\r
+ /**\r
+ * Default terminal pick distance in control pixels.\r
+ * @see #DEFAULT_PICK_DISTANCE\r
+ */\r
+ public static final double PICK_DIST = 10;\r
+ \r
+ private NetworkDrawingNode node;\r
+ \r
+ @Dependency Selection selection;\r
+ @Dependency TransformUtil util;\r
+ @Dependency PickContext pickContext;\r
+ \r
+ @SGInit\r
+ public void initSG(G2DParentNode parent) {\r
+ node = parent.addNode("networkDrawingNode", NetworkDrawingNode.class);\r
+ }\r
+ \r
+ @Override\r
+ protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) {\r
+ node.setDiagram(newDiagram);\r
+ }\r
+ \r
+ @EventHandler(priority = 1 << 21)\r
+ public boolean handleClick(MouseClickEvent me) {\r
+ \r
+ boolean isLeft = me.button == MouseEvent.LEFT_BUTTON;\r
+ boolean isRight = me.button == MouseEvent.RIGHT_BUTTON;\r
+ if (!isLeft && !isRight)\r
+ return false;\r
+ boolean isShiftPressed = me.hasAllModifiers(MouseEvent.SHIFT_MASK);\r
+ \r
+ int selectionId = me.mouseId;\r
+ \r
+ double pickDist = getPickDistance();\r
+ Rectangle2D controlPickRect = new Rectangle2D.Double(me.controlPosition.getX()-pickDist, me.controlPosition.getY()-pickDist, pickDist*2+1, pickDist*2+1);\r
+ Shape canvasPickRect = GeometryUtils.transformShape(controlPickRect, util.getInverseTransform());\r
+ \r
+ PickRequest req = new PickRequest(canvasPickRect);\r
+ req.pickPolicy = PickPolicy.PICK_INTERSECTING_OBJECTS;\r
+ //req.pickSorter = PickRequest.PickSorter.CONNECTIONS_LAST;\r
+ List<IElement> pickables = new ArrayList<IElement>();\r
+ pickContext.pick(diagram, req, pickables);\r
+ \r
+ Set<IElement> currentSelection = selection.getSelection(selectionId);\r
+ \r
+ if (!pickables.isEmpty()) {\r
+ /*\r
+ * Select the one object the mouse points to. If multiple object\r
+ * are picked, select the one that is after the earliest by\r
+ * index of the current selection when shift is pressed. Otherwise\r
+ * always pick the topmost element.\r
+ */\r
+ IElement selectedPick = isShiftPressed\r
+ ? rotatingPick(currentSelection, pickables)\r
+ : pickables.get(pickables.size() - 1);\r
+ \r
+ // Only select when\r
+ // 1. the selection would actually change\r
+ // AND\r
+ // 2.1. left button was pressed\r
+ // OR\r
+ // 2.2. right button was pressed and the element to-be-selected\r
+ // is NOT a part of the current selection\r
+ if (!Collections.singleton(selectedPick).equals(currentSelection)\r
+ && (isLeft || (isRight && !currentSelection.contains(selectedPick)))) {\r
+ selection.setSelection(selectionId, selectedPick);\r
+ }\r
+ }\r
+ \r
+ return false;\r
+ }\r
+ \r
+ private double getPickDistance() {\r
+ Double pickDistance = getHint(KEY_PICK_DISTANCE);\r
+ return pickDistance == null ? PICK_DIST : Math.max(pickDistance, 0);\r
+ }\r
+ \r
+ private IElement rotatingPick(int selectionId, List<IElement> pickables) {\r
+ Set<IElement> sel = selection.getSelection(selectionId);\r
+ return rotatingPick(sel, pickables);\r
+ }\r
+\r
+ private IElement rotatingPick(Set<IElement> sel, List<IElement> pickables) {\r
+ int earliestIndex = pickables.size();\r
+ for (int i = pickables.size() - 1; i >= 0; --i) {\r
+ if (sel.contains(pickables.get(i))) {\r
+ earliestIndex = i;\r
+ break;\r
+ }\r
+ }\r
+ if (earliestIndex == 0)\r
+ earliestIndex = pickables.size();\r
+ IElement selectedPick = pickables.get(earliestIndex - 1);\r
+ return selectedPick;\r
+ }\r
+\r
+ public static class NetworkDrawingNode extends G2DNode {\r
+\r
+ private static final long serialVersionUID = -3475301184009620573L;\r
+ \r
+ private List<Point2D> nodes = new ArrayList<>();\r
+\r
+ private Rectangle2D rect = new Rectangle2D.Double(10, 10, 10, 10);\r
+\r
+ private Set<Path2D> paths = new HashSet<>();\r
+\r
+ private Resource diagramResource;\r
+\r
+ private boolean committed;\r
+ \r
+ @Override\r
+ public void init() {\r
+ super.init();\r
+ addEventHandler(this);\r
+ }\r
+ \r
+ public void setDiagram(IDiagram diagram) {\r
+ if (diagram != null)\r
+ this.diagramResource = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);\r
+ }\r
+\r
+ @Override\r
+ public void render(Graphics2D g2d) {\r
+ Color old = g2d.getColor();\r
+ g2d.setColor(Color.BLUE);\r
+ \r
+ paths.forEach(p -> {\r
+ g2d.draw(p);\r
+ });\r
+ \r
+ g2d.setColor(old);\r
+ }\r
+\r
+ @Override\r
+ public Rectangle2D getBoundsInLocal() {\r
+ return rect.getBounds2D();\r
+ }\r
+ \r
+ @Override\r
+ public int getEventMask() {\r
+ return EventTypes.MouseMask;\r
+ }\r
+ \r
+ @Override\r
+ protected boolean mouseDoubleClicked(MouseDoubleClickedEvent e) {\r
+ // nodes to path2d\r
+ Point2D start = null;\r
+ Point2D end = null;\r
+ Iterator<Point2D> nodeIter = nodes.iterator();\r
+ while (nodeIter.hasNext()) {\r
+ if (end == null) {\r
+ start = nodeIter.next();\r
+ } else {\r
+ start = end;\r
+ }\r
+ end = nodeIter.next();\r
+ \r
+ createEdge(start, end);\r
+ }\r
+ \r
+ nodes.clear();\r
+ committed = true;\r
+ \r
+ repaint();\r
+ \r
+ return true;\r
+ }\r
+ \r
+ private void createEdge(Point2D start, Point2D end) {\r
+ double[] startCoords = new double[] { start.getX(), start.getY() };\r
+ double[] endCoords = new double[] { end.getX(), end.getY() };\r
+ \r
+ DNEdgeBuilder builder = new DNEdgeBuilder(diagramResource);\r
+ Simantics.getSession().asyncRequest(new WriteRequest() {\r
+ \r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ builder.create(graph, startCoords, endCoords);\r
+ }\r
+ });\r
+ \r
+ }\r
+ \r
+ @Override\r
+ protected boolean mouseClicked(MouseClickEvent e) {\r
+ if (committed) {\r
+ committed = false;\r
+ return false;\r
+ }\r
+ Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double());\r
+ nodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));\r
+ return super.mouseClicked(e);\r
+ }\r
+ \r
+ @Override\r
+ protected boolean mouseMoved(MouseMovedEvent e) {\r
+ return super.mouseMoved(e);\r
+ }\r
+ }\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui;\r
+\r
+import java.util.Collections;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.district.network.ontology.DistrictNetworkResource;\r
+\r
+public class OpenDiagramFromConfigurationAdapter\r
+ extends org.simantics.modeling.ui.diagramEditor.OpenDiagramFromConfigurationAdapter {\r
+\r
+ @Override\r
+ public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {\r
+ Resource diagram = getDiagram(g, r, Collections.emptySet());\r
+ if (diagram == null)\r
+ return false;\r
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(g);\r
+ return g.isInstanceOf(diagram, DN.Diagram) && !isLocked(g, diagram);\r
+ }\r
+\r
+ @Override\r
+ protected String getEditorId() {\r
+ return DistrictDiagramEditor.ID;\r
+ }\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui;\r
+\r
+import javax.inject.Inject;\r
+\r
+import org.eclipse.e4.core.di.annotations.CanExecute;\r
+import org.eclipse.e4.core.di.annotations.Execute;\r
+import org.simantics.ui.workbench.e4.E4WorkbenchUtils;\r
+\r
+public class OpenDistrictEditor {\r
+\r
+ @CanExecute\r
+ public boolean canExecute() {\r
+ return true;\r
+ }\r
+ \r
+ @Execute\r
+ @Inject\r
+ public void execute() {\r
+ E4WorkbenchUtils.openAndShowPart(DistrictDiagramEditor.ID);\r
+ }\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui.adapters;\r
+\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.district.network.ui.DistrictNetworkEdge;\r
+import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.SceneGraphNodeKey;\r
+import org.simantics.g2d.element.handler.InternalSize;\r
+import org.simantics.g2d.element.handler.SceneGraph;\r
+import org.simantics.g2d.elementclass.connection.EdgeClass.FixedTransform;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+\r
+public class DistrictNetworkEdgeElement {\r
+\r
+ public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE");\r
+ public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE");\r
+ \r
+ public static final ElementClass CLASS =\r
+ ElementClass.compile(\r
+ FixedTransform.INSTANCE,\r
+ DNEdgeInternalSize.INSTANCE,\r
+ DNEdgeSceneGraph.INSTANCE\r
+ ).setId(DistrictNetworkEdgeElement.class.getSimpleName());\r
+ \r
+ static final class DNEdgeSceneGraph implements SceneGraph {\r
+ \r
+ public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph();\r
+\r
+ private static final long serialVersionUID = 8894367073815556871L;\r
+\r
+ @Override\r
+ public void init(IElement edgeElement, G2DParentNode parent) {\r
+ DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE);\r
+ if (edge == null) {\r
+ cleanup(edgeElement);\r
+ } else {\r
+ DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE);\r
+ if (node == null) {\r
+ node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class);\r
+ edgeElement.setHint(KEY_DN_EDGE_NODE, node);\r
+ }\r
+ node.setDNEdge(edge);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void cleanup(IElement edge) {\r
+ ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE);\r
+ edge.removeHint(KEY_DN_EDGE_NODE);\r
+ }\r
+ }\r
+ \r
+ static final class DNEdgeInternalSize implements InternalSize {\r
+\r
+ private static final long serialVersionUID = -2725017034692179676L;\r
+ \r
+ public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize();\r
+\r
+ @Override\r
+ public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
+ DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);\r
+ return edge.getBounds(size);\r
+ }\r
+ \r
+ }\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui.adapters;\r
+\r
+import java.awt.geom.Path2D;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.diagram.adapter.SyncElementFactory;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.diagram.ui.DiagramModelHints;\r
+import org.simantics.district.network.ontology.DistrictNetworkResource;\r
+import org.simantics.district.network.ui.DistrictNetworkEdge;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
+\r
+public class DistrictNetworkEdgeElementFactory extends SyncElementFactory {\r
+\r
+ public static final ElementClass CLASS = DistrictNetworkEdgeElement.CLASS;\r
+ \r
+ private DistrictNetworkResource DN;\r
+ private DiagramResource DIA;\r
+ \r
+ public DistrictNetworkEdgeElementFactory(ReadGraph graph) {\r
+ this.DN = DistrictNetworkResource.getInstance(graph);\r
+ this.DIA = DiagramResource.getInstance(graph);\r
+ }\r
+ \r
+ @Override\r
+ public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure<ElementClass> procedure) {\r
+ procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));\r
+ }\r
+ \r
+ @Override\r
+ protected Resource getElementClassBaseType(AsyncReadGraph graph) {\r
+ return DN.Edge;\r
+ }\r
+ \r
+ @Override\r
+ public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource edgeResource, IElement element) throws DatabaseException {\r
+ \r
+ Resource diagramRuntime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);\r
+ \r
+ Resource startVertex = graph.getSingleObject(edgeResource, DN.HasStartVertex);\r
+ Resource endVertex = graph.getSingleObject(edgeResource, DN.HasEndVertex);\r
+ \r
+ double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation);\r
+ double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation);\r
+ \r
+ Path2D path = new Path2D.Double();\r
+ path.moveTo(startCoords[0], startCoords[1]);\r
+ path.lineTo(endCoords[0], endCoords[1]);\r
+ \r
+ DistrictNetworkEdge edge = new DistrictNetworkEdge(path);\r
+ \r
+ element.setHint(DistrictNetworkEdgeElement.KEY_DN_EDGE, edge);\r
+ }\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui.adapters;\r
+\r
+import java.awt.geom.Point2D;\r
+\r
+public class DistrictNetworkVertex {\r
+\r
+ private Point2D point;\r
+ \r
+ public DistrictNetworkVertex(double[] coords) {\r
+ this.point = new Point2D.Double(coords[0], coords[1]);\r
+ }\r
+ \r
+ public Point2D getPoint() {\r
+ return point;\r
+ }\r
+\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui.adapters;\r
+\r
+import java.awt.Dimension;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.SceneGraphNodeKey;\r
+import org.simantics.g2d.element.handler.InternalSize;\r
+import org.simantics.g2d.element.handler.SceneGraph;\r
+import org.simantics.g2d.elementclass.connection.EdgeClass.FixedTransform;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+\r
+public class DistrictNetworkVertexElement {\r
+\r
+ public static final Key KEY_DN_VERTEX = new KeyOf(DistrictNetworkVertex.class, "DN_VERTEX");\r
+ public static final Key KEY_DN_VERTEX_NODE = new SceneGraphNodeKey(DistrictNetworkVertexNode.class, "DN_VERTEX_NODE");\r
+ \r
+ public static final ElementClass CLASS =\r
+ ElementClass.compile(\r
+ FixedTransform.INSTANCE,\r
+ DNVertexInternalSize.INSTANCE,\r
+ DNVertexSceneGraph.INSTANCE\r
+ ).setId(DistrictNetworkVertexElement.class.getSimpleName());\r
+ \r
+ static final class DNVertexSceneGraph implements SceneGraph {\r
+ \r
+ public static final DNVertexSceneGraph INSTANCE = new DNVertexSceneGraph();\r
+\r
+ private static final long serialVersionUID = 8894367073815556871L;\r
+\r
+ @Override\r
+ public void init(IElement edgeElement, G2DParentNode parent) {\r
+ DistrictNetworkVertex vertex = edgeElement.getHint(KEY_DN_VERTEX);\r
+ if (vertex == null) {\r
+ cleanup(edgeElement);\r
+ } else {\r
+ DistrictNetworkVertexNode node = edgeElement.getHint(KEY_DN_VERTEX_NODE);\r
+ if (node == null) {\r
+ node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkVertexNode.class);\r
+ edgeElement.setHint(KEY_DN_VERTEX_NODE, node);\r
+ }\r
+ node.setVertex(vertex);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void cleanup(IElement edge) {\r
+ ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_NODE);\r
+ edge.removeHint(KEY_DN_VERTEX_NODE);\r
+ }\r
+ }\r
+ \r
+ static final class DNVertexInternalSize implements InternalSize {\r
+\r
+ public static final DNVertexInternalSize INSTANCE = new DNVertexInternalSize();\r
+ \r
+ private static final long serialVersionUID = 2625702114278956461L;\r
+\r
+ @Override\r
+ public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
+ DistrictNetworkVertex vertex = e.getHint(KEY_DN_VERTEX);\r
+ if (size == null)\r
+ size = new Rectangle2D.Double();\r
+ size.setFrame(vertex.getPoint(), new Dimension(1, 1));\r
+ return size;\r
+ }\r
+ \r
+ }\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui.adapters;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.diagram.adapter.SyncElementFactory;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.district.network.ontology.DistrictNetworkResource;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
+\r
+public class DistrictNetworkVertexElementFactory extends SyncElementFactory {\r
+\r
+ public static final ElementClass CLASS = DistrictNetworkVertexElement.CLASS;\r
+ private DistrictNetworkResource DN;\r
+ private DiagramResource DIA;\r
+\r
+ public DistrictNetworkVertexElementFactory(ReadGraph graph) {\r
+ this.DN = DistrictNetworkResource.getInstance(graph);\r
+ this.DIA = DiagramResource.getInstance(graph);\r
+ }\r
+\r
+ @Override\r
+ public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType,\r
+ AsyncProcedure<ElementClass> procedure) {\r
+ procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));\r
+ }\r
+\r
+ @Override\r
+ protected Resource getElementClassBaseType(AsyncReadGraph graph) {\r
+ return DN.Vertex;\r
+ }\r
+\r
+ @Override\r
+ public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource vertexResource, IElement element) throws DatabaseException {\r
+ \r
+ double[] coords = graph.getRelatedValue(vertexResource, DIA.HasLocation);\r
+ \r
+ DistrictNetworkVertex vertex = new DistrictNetworkVertex(coords);\r
+ \r
+ element.setHint(DistrictNetworkVertexElement.KEY_DN_VERTEX, vertex);\r
+ }\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui.internal;\r
+\r
+import org.osgi.framework.BundleActivator;\r
+import org.osgi.framework.BundleContext;\r
+\r
+public class Activator implements BundleActivator {\r
+\r
+ @Override\r
+ public void start(BundleContext context) throws Exception {\r
+\r
+ }\r
+\r
+ @Override\r
+ public void stop(BundleContext context) throws Exception {\r
+\r
+ }\r
+\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui.nodes;\r
+\r
+import java.awt.BasicStroke;\r
+import java.awt.Color;\r
+import java.awt.Graphics2D;\r
+import java.awt.RenderingHints;\r
+import java.awt.Shape;\r
+import java.awt.Stroke;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Path2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.district.network.ui.DistrictNetworkEdge;\r
+import org.simantics.scenegraph.g2d.G2DNode;\r
+import org.simantics.scenegraph.utils.NodeUtil;\r
+\r
+public class DistrictNetworkEdgeNode extends G2DNode {\r
+\r
+ private static final long serialVersionUID = 8049769475036519806L;\r
+ \r
+ private static final Stroke SELECTION_STROKE = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);\r
+ private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);\r
+ \r
+ private DistrictNetworkEdge edge;\r
+ private Rectangle2D bounds;\r
+\r
+ @Override\r
+ public void render(Graphics2D g2d) {\r
+ \r
+ AffineTransform ot = null;\r
+ AffineTransform t = getTransform();\r
+ if (t != null && !t.isIdentity()) {\r
+ ot = g2d.getTransform();\r
+ g2d.transform(getTransform());\r
+ }\r
+ \r
+ Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);\r
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);\r
+\r
+ Color oldColor = g2d.getColor();\r
+ boolean selected = isSelected();\r
+ if (selected) {\r
+ Path2D selectionPath = edge.getPath();\r
+ Shape selectionShape = SELECTION_STROKE.createStrokedShape(selectionPath);\r
+ g2d.setColor(SELECTION_COLOR);\r
+ g2d.fill(selectionShape);\r
+ }\r
+ \r
+ g2d.setColor(Color.BLUE);\r
+ // render\r
+ g2d.draw(edge.getPath());\r
+ \r
+ // Reset\r
+ g2d.setColor(oldColor);\r
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);\r
+ \r
+ if (ot != null)\r
+ g2d.setTransform(ot);\r
+ }\r
+\r
+ private boolean isSelected() {\r
+ return NodeUtil.isSelected(this, 1);\r
+ }\r
+\r
+ @Override\r
+ public Rectangle2D getBoundsInLocal() {\r
+ return bounds;\r
+ }\r
+ \r
+ private void updateBounds() {\r
+ Rectangle2D oldBounds = bounds;\r
+ if (oldBounds == null)\r
+ oldBounds = new Rectangle2D.Double();\r
+ bounds = calculateBounds(oldBounds);\r
+ }\r
+\r
+ private Rectangle2D calculateBounds(Rectangle2D rect) {\r
+ return edge.getBounds(rect);\r
+ }\r
+\r
+ public void setDNEdge(DistrictNetworkEdge edge) {\r
+ this.edge = edge;\r
+ }\r
+\r
+}\r
--- /dev/null
+package org.simantics.district.network.ui.nodes;\r
+\r
+import java.awt.Color;\r
+import java.awt.Graphics2D;\r
+import java.awt.RenderingHints;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;\r
+import org.simantics.scenegraph.g2d.G2DNode;\r
+\r
+public class DistrictNetworkVertexNode extends G2DNode {\r
+\r
+ private static final long serialVersionUID = -2641639101400236719L;\r
+ private DistrictNetworkVertex vertex;\r
+\r
+ @Override\r
+ public void render(Graphics2D g2d) {\r
+ AffineTransform ot = null;\r
+ AffineTransform t = getTransform();\r
+ if (t != null && !t.isIdentity()) {\r
+ ot = g2d.getTransform();\r
+ g2d.transform(getTransform());\r
+ }\r
+ \r
+ Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);\r
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);\r
+ \r
+ Color oldColor = g2d.getColor();\r
+ g2d.setColor(Color.RED);\r
+ // render\r
+ \r
+ Rectangle2D.Double rect = new Rectangle2D.Double(vertex.getPoint().getX() - 0.5, vertex.getPoint().getY() - 0.5, 1, 1);\r
+ g2d.draw(rect);\r
+ \r
+ // Reset stats\r
+ g2d.setColor(oldColor);\r
+ \r
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);\r
+ \r
+ if (ot != null)\r
+ g2d.setTransform(ot);\r
+ }\r
+\r
+ @Override\r
+ public Rectangle2D getBoundsInLocal() {\r
+ return new Rectangle2D.Double(vertex.getPoint().getX(), vertex.getPoint().getY(), 1, 1);\r
+ }\r
+\r
+ public void setVertex(DistrictNetworkVertex vertex) {\r
+ this.vertex = vertex;\r
+ }\r
+\r
+}\r
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>\r
+<classpath>\r
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>\r
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>\r
+ <classpathentry kind="src" path="src"/>\r
+ <classpathentry kind="output" path="bin"/>\r
+</classpath>\r
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>\r
+<projectDescription>\r
+ <name>org.simantics.district.network</name>\r
+ <comment></comment>\r
+ <projects>\r
+ </projects>\r
+ <buildSpec>\r
+ <buildCommand>\r
+ <name>org.eclipse.jdt.core.javabuilder</name>\r
+ <arguments>\r
+ </arguments>\r
+ </buildCommand>\r
+ <buildCommand>\r
+ <name>org.eclipse.pde.ManifestBuilder</name>\r
+ <arguments>\r
+ </arguments>\r
+ </buildCommand>\r
+ <buildCommand>\r
+ <name>org.eclipse.pde.SchemaBuilder</name>\r
+ <arguments>\r
+ </arguments>\r
+ </buildCommand>\r
+ </buildSpec>\r
+ <natures>\r
+ <nature>org.eclipse.pde.PluginNature</nature>\r
+ <nature>org.eclipse.jdt.core.javanature</nature>\r
+ </natures>\r
+</projectDescription>\r
--- /dev/null
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Simantics District Network
+Bundle-SymbolicName: org.simantics.district.network
+Bundle-Version: 1.0.0.qualifier
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
+Require-Bundle: org.simantics.db,
+ org.simantics.layer0;bundle-version="1.1.0",
+ org.simantics.district.network.ontology,
+ org.simantics.diagram.ontology;bundle-version="2.2.0",
+ org.simantics.db.common
+Export-Package: org.simantics.district.network
--- /dev/null
+source.. = src/\r
+output.. = bin/\r
+bin.includes = META-INF/,\\r
+ .,\\r
+ adapters.xml\r
--- /dev/null
+package org.simantics.district.network;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.OrderedSetUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.district.network.ontology.DistrictNetworkResource;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class DistrictNetworkUtil {\r
+\r
+ public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ \r
+ Resource edge = graph.newResource();\r
+ graph.claim(edge, L0.InstanceOf, DN.Edge);\r
+ OrderedSetUtils.addFirst(graph, composite, edge);\r
+ graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);\r
+ return edge;\r
+ }\r
+ \r
+ public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ Resource vertex = graph.newResource();\r
+ graph.claim(vertex, L0.InstanceOf, DN.Vertex);\r
+ graph.claimLiteral(vertex, DIA.HasLocation, coords);\r
+ OrderedSetUtils.addFirst(graph, composite, vertex);\r
+ graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);\r
+ \r
+ return vertex;\r
+ }\r
+}\r