From a2e0d3dfba1245a13dcc8aa23c0188926e2a03c5 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Tue, 20 Dec 2016 11:29:41 +0200 Subject: [PATCH] Initial commit of simantics/district --- .gitignore | 4 + .../.classpath | 7 + .../.project | 34 +++ .../META-INF/MANIFEST.MF | 13 + .../build.properties | 6 + .../graph.tg | Bin 0 -> 4543 bytes .../graph/DistrictNetwork.pgraph | 57 ++++ .../graph/images/Configuration.png | Bin 0 -> 268 bytes .../ontology/DistrictNetworkResource.java | 100 +++++++ org.simantics.district.network.ui/.classpath | 7 + org.simantics.district.network.ui/.project | 28 ++ .../META-INF/MANIFEST.MF | 20 ++ .../adapters.xml | 21 ++ .../build.properties | 7 + .../fragment.e4xmi | 17 ++ org.simantics.district.network.ui/plugin.xml | 30 +++ .../district/network/ui/DNEdgeBuilder.java | 54 ++++ .../network/ui/DistrictDiagramEditor.java | 15 ++ .../network/ui/DistrictDiagramViewer.java | 30 +++ .../network/ui/DistrictNetworkEdge.java | 21 ++ .../network/ui/NetworkDrawingParticipant.java | 253 ++++++++++++++++++ .../OpenDiagramFromConfigurationAdapter.java | 26 ++ .../network/ui/OpenDistrictEditor.java | 21 ++ .../adapters/DistrictNetworkEdgeElement.java | 71 +++++ .../DistrictNetworkEdgeElementFactory.java | 62 +++++ .../ui/adapters/DistrictNetworkVertex.java | 17 ++ .../DistrictNetworkVertexElement.java | 74 +++++ .../DistrictNetworkVertexElementFactory.java | 48 ++++ .../network/ui/internal/Activator.java | 18 ++ .../ui/nodes/DistrictNetworkEdgeNode.java | 85 ++++++ .../ui/nodes/DistrictNetworkVertexNode.java | 54 ++++ org.simantics.district.network/.classpath | 7 + org.simantics.district.network/.project | 28 ++ .../META-INF/MANIFEST.MF | 12 + .../build.properties | 5 + .../district/network/DistrictNetworkUtil.java | 37 +++ 36 files changed, 1289 insertions(+) create mode 100644 .gitignore create mode 100644 org.simantics.district.network.ontology/.classpath create mode 100644 org.simantics.district.network.ontology/.project create mode 100644 org.simantics.district.network.ontology/META-INF/MANIFEST.MF create mode 100644 org.simantics.district.network.ontology/build.properties create mode 100644 org.simantics.district.network.ontology/graph.tg create mode 100644 org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph create mode 100644 org.simantics.district.network.ontology/graph/images/Configuration.png create mode 100644 org.simantics.district.network.ontology/src/org/simantics/district/network/ontology/DistrictNetworkResource.java create mode 100644 org.simantics.district.network.ui/.classpath create mode 100644 org.simantics.district.network.ui/.project create mode 100644 org.simantics.district.network.ui/META-INF/MANIFEST.MF create mode 100644 org.simantics.district.network.ui/adapters.xml create mode 100644 org.simantics.district.network.ui/build.properties create mode 100644 org.simantics.district.network.ui/fragment.e4xmi create mode 100644 org.simantics.district.network.ui/plugin.xml create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramEditor.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkEdge.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/OpenDiagramFromConfigurationAdapter.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/OpenDistrictEditor.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElementFactory.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertex.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElementFactory.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/internal/Activator.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java create mode 100644 org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java create mode 100644 org.simantics.district.network/.classpath create mode 100644 org.simantics.district.network/.project create mode 100644 org.simantics.district.network/META-INF/MANIFEST.MF create mode 100644 org.simantics.district.network/build.properties create mode 100644 org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..0d27de2a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin +target +*.class +.settings diff --git a/org.simantics.district.network.ontology/.classpath b/org.simantics.district.network.ontology/.classpath new file mode 100644 index 00000000..b862a296 --- /dev/null +++ b/org.simantics.district.network.ontology/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/org.simantics.district.network.ontology/.project b/org.simantics.district.network.ontology/.project new file mode 100644 index 00000000..07311627 --- /dev/null +++ b/org.simantics.district.network.ontology/.project @@ -0,0 +1,34 @@ + + + org.simantics.district.network.ontology + + + + + + org.simantics.graph.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + org.simantics.graph.nature + + diff --git a/org.simantics.district.network.ontology/META-INF/MANIFEST.MF b/org.simantics.district.network.ontology/META-INF/MANIFEST.MF new file mode 100644 index 00000000..0302088e --- /dev/null +++ b/org.simantics.district.network.ontology/META-INF/MANIFEST.MF @@ -0,0 +1,13 @@ +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 diff --git a/org.simantics.district.network.ontology/build.properties b/org.simantics.district.network.ontology/build.properties new file mode 100644 index 00000000..022de173 --- /dev/null +++ b/org.simantics.district.network.ontology/build.properties @@ -0,0 +1,6 @@ +source.. = src/ +output.. = bin/ +bin.includes = plugin.xml,\ + META-INF/,\ + .,\ + graph.tg diff --git a/org.simantics.district.network.ontology/graph.tg b/org.simantics.district.network.ontology/graph.tg new file mode 100644 index 0000000000000000000000000000000000000000..70e6cc2734910c8eb4ff548f61b94e7a3e617898 GIT binary patch literal 4543 zcmeHK-ESOM72oxKuXp_sJE_y=!$}(FtKQg-+a^uZ#LhbQCeGTi9XI_*n(^*rJFuSF z&dg?QsiGDSMXe&FK7mxAsPFL52jHzANXURtkpfcTjfbiTAp`=XA{D~#cjnIStP|iL zV5PG&zu!6c+T@(=ja5 zNfF2Sm*HR9DC?$cxQ6Z|iG!XxlD;R;Nm;*Tm@u`>PMWG;G`e6}Zd<#ksjceLN7P-f zd2h)n48bHv zlX+wCH`N55RZl#^63yO5qmgrrRSkhDIXTO&K$V~>oGz?t6@6@YG&cq~PA-w0H!DH| z#KbCd*(&Q*!>lN~B-z+@pPjU=hJ)3?wANf`@>D0YAZ{6zx~*~UU>=cHeKVTlH(b(J zYgNo1%HyKU6!pe=Yqe%MhOnb#k9VO-UcaT)t8Nhs0qQ04qltofTelq&BVyPC!)Eg( ztQ0hR#Id+AG;Fd}#qSorJFm#rNREi2Ik|}vhu@l9oa%6P+0Yv`%P?IPl0&59*{z{C zO!`o!;1tYKwO-cC1x8{@*zFi?%e!r?)K#FCM2&4CO&m11Al`cs9n}Cqt626rRMVo` z_CQB7)J-SRkH4M;VN%VWp}#0pCqvirdZ|ekvGgirQIg@VMc1yET&!zV&3z~NXsQ`` zpbjxj&uZ(sJp!{)aOcQ%WYE#Pt(T3GxA4otb}!azx^3&&4T$#KEm%G;3T~ICd2787 ziZU74KsSVm&ug|T%^&u#<~X{|P1)nMD3~wnw&60UIbjmB#)_>;DEE6N=y5?aE8IL+ z$?Om8v#gkKpDp-FsfsRO^&&Odfa!Bh)47!|=_R){_<7N!=WPpfcGqRNlfoq?WVrNq zjl^WY-=J7z7wao+VcI7&m(TZyb|;%;AVSwko(yvq;^VY%v1#=&IV+IC$!o5rw(*3JJOkds!0j_0 zk(5` zN&6tS3&EL~*eG8AsR&*5VqrFo3RFf(et z*y?*o%(AU?;ic?HNe}Hn?3F1KULYlPOS9-yS&qeCLCmR(F3#IBcCo$&2Up1N4J^?C zG31e-GRsY~1+oVMW_@B7ZyNLqZjZ+a&qfj%r+19_L?6c4`p8_cs%6~qfeb=?d>QZQd-&lw4I!R|@Oyex;Y&)NSNNjB7Zffi{FuTs3LgVL51D{G z2ic|YIXs_-Jfg7Q|1-d6AU=Ky?@tHtX}muXz(u^DfIJDg0bzf{e)}BW`Gxl}?S~=0 zJ;zJz>uFDpdHW>@+Y*nc`3xxR+aCvJpFSSPJ7eZ!_BRy3hZH`H=Mv{)d9#LgzLKxLOiF0dx3aAAYF0%h0fy28@K?MRZh*BUQ*2=*GZDkZOOL&Hy$pINj1SP z@C#?&m@iIe(+{TsvW1yEw+lntlVpE;|9Vfu<8C28v9$Kv(zo9G!ygmz{U1j69QfDi z8+Yz(uG|@ne6;)1&)*n5=bk+lIseVj?a1<c-x8Ow&em=gDgT9FM!LhTDcgU*n*418C=|57l@ zB_+JI6qLKn_GY!Twf>RoQSW`FpjS!US42s*8NIKFl9DU-AIqg~p33M0i=&jN9$XyB zbu$$#j{0bwYAcSqgxXRZNhy&c87Ph(RPpR!>y9ngPbr=|+ON#sH9C0P<;R1kp-+7< zm{5s(6lTJg1g9_)GI2&>CT`+jLMKitJriUE5?7cy{+^HnJ0J(sIo@`=%GKoNJ|-4j Ka<6QbeE$Z=zp8Nn literal 0 HcmV?d00001 diff --git a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph new file mode 100644 index 00000000..612fb303 --- /dev/null +++ b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph @@ -0,0 +1,57 @@ +L0 = +DIA = +STR = +MOD = +VP = +IMAGE = + +MBC = MOD.ModelingBrowseContext + +DN = : L0.Ontology + @L0.new + L0.HasResourceClass "org.simantics.district.network.ontology.DistrictNetworkResource" + +DN.Edge -0X8F+bl!88bf*c_XFFc36=o%sjD&%YcD=zI{(xSj#O0mzDBhJe`c z7=Rf76TsyKWKAehNXP~FG-EXYCIK=4pGFh|RJQ&9f9K0bhA9kp;V#GyR3<7tfDM3g z$Vrho49fg)1JGRpif3dnXVX;_ad() { + public DistrictNetworkResource perform(ReadGraph graph) throws DatabaseException { + QueryControl qc = graph.getService(QueryControl.class); + return new DistrictNetworkResource(qc.getIndependentGraph(graph)); + } + }); + session.registerService(DistrictNetworkResource.class, ret); + } + return ret; + } + +} + diff --git a/org.simantics.district.network.ui/.classpath b/org.simantics.district.network.ui/.classpath new file mode 100644 index 00000000..b862a296 --- /dev/null +++ b/org.simantics.district.network.ui/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/org.simantics.district.network.ui/.project b/org.simantics.district.network.ui/.project new file mode 100644 index 00000000..c8969562 --- /dev/null +++ b/org.simantics.district.network.ui/.project @@ -0,0 +1,28 @@ + + + org.simantics.district.network.ui + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/org.simantics.district.network.ui/META-INF/MANIFEST.MF b/org.simantics.district.network.ui/META-INF/MANIFEST.MF new file mode 100644 index 00000000..0e348e6b --- /dev/null +++ b/org.simantics.district.network.ui/META-INF/MANIFEST.MF @@ -0,0 +1,20 @@ +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 diff --git a/org.simantics.district.network.ui/adapters.xml b/org.simantics.district.network.ui/adapters.xml new file mode 100644 index 00000000..57243ce1 --- /dev/null +++ b/org.simantics.district.network.ui/adapters.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/org.simantics.district.network.ui/build.properties b/org.simantics.district.network.ui/build.properties new file mode 100644 index 00000000..1ab42f55 --- /dev/null +++ b/org.simantics.district.network.ui/build.properties @@ -0,0 +1,7 @@ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + fragment.e4xmi,\ + plugin.xml,\ + adapters.xml +source.. = src/ diff --git a/org.simantics.district.network.ui/fragment.e4xmi b/org.simantics.district.network.ui/fragment.e4xmi new file mode 100644 index 00000000..526195f2 --- /dev/null +++ b/org.simantics.district.network.ui/fragment.e4xmi @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/org.simantics.district.network.ui/plugin.xml b/org.simantics.district.network.ui/plugin.xml new file mode 100644 index 00000000..7e6e8c13 --- /dev/null +++ b/org.simantics.district.network.ui/plugin.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java new file mode 100644 index 00000000..475269f8 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java @@ -0,0 +1,54 @@ +package org.simantics.district.network.ui; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.Layer0Utils; +import org.simantics.diagram.synchronization.graph.AddElement; +import org.simantics.district.network.DistrictNetworkUtil; +import org.simantics.district.network.ontology.DistrictNetworkResource; + +public class DNEdgeBuilder { + + private Resource diagramResource; + + public DNEdgeBuilder(Resource diagramResource) { + this.diagramResource = diagramResource; + } + + public void create(WriteGraph graph, double[] start, double[] end) throws DatabaseException { + + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + + // 1. Get diagram edge to construct + Resource edge = getOrCreateEdge(graph); + + // 1.1 Give running name to connection and increment the counter attached to the diagram. + AddElement.claimFreshElementName(graph, diagramResource, edge); + + // 2. Add vertices + Resource startVertex = getOrCreateVertex(graph, start); + Resource endVertex = getOrCreateVertex(graph, end); + + graph.claim(edge, DN.HasStartVertex, startVertex); + graph.claim(edge, DN.HasEndVertex, endVertex); + + Layer0Utils.addCommentMetadata(graph, "Added edge " + edge); + graph.markUndoPoint(); + } + + private Resource getOrCreateVertex(WriteGraph graph, double[] coords) throws DatabaseException { + // TODO: check if vertex exists already, for now create always new + Resource vertex = null; + if (vertex == null) { + vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords); + AddElement.claimFreshElementName(graph, diagramResource, vertex); + } + return vertex; + } + + private Resource getOrCreateEdge(WriteGraph graph) throws DatabaseException { + return DistrictNetworkUtil.createEdge(graph, diagramResource); + } + +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramEditor.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramEditor.java new file mode 100644 index 00000000..9862cde2 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramEditor.java @@ -0,0 +1,15 @@ +package org.simantics.district.network.ui; + +import org.eclipse.ui.PartInitException; +import org.simantics.modeling.ui.diagramEditor.DiagramEditor; +import org.simantics.modeling.ui.diagramEditor.DiagramViewer; + +public class DistrictDiagramEditor extends DiagramEditor { + + public static final String ID = "org.simantics.district.network.ui.diagrameditor"; + + @Override + protected DiagramViewer createViewer() throws PartInitException { + return new DistrictDiagramViewer(); + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java new file mode 100644 index 00000000..e536f3cc --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java @@ -0,0 +1,30 @@ +package org.simantics.district.network.ui; + +import java.util.concurrent.TimeUnit; + +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.handler.PickRequest.PickFilter; +import org.simantics.g2d.diagram.participant.DelayedBatchElementPainter; +import org.simantics.g2d.diagram.participant.ElementPainter; +import org.simantics.g2d.diagram.participant.Selection; +import org.simantics.g2d.diagram.participant.ZOrderHandler; +import org.simantics.g2d.participant.RenderingQualityInteractor; +import org.simantics.modeling.ui.diagramEditor.DiagramViewer; + +public class DistrictDiagramViewer extends DiagramViewer { + + @Override + protected void addDiagramParticipants(ICanvasContext ctx) { + ctx.add(new ZOrderHandler()); + ctx.add(new Selection()); + ctx.add(new ElementPainter()); + + ctx.add(new NetworkDrawingParticipant()); + } + + @Override + protected void addPainterParticipants(ICanvasContext ctx) { + ctx.add(new RenderingQualityInteractor()); + ctx.add(new DelayedBatchElementPainter(PickFilter.FILTER_MONITORS, 500, TimeUnit.MILLISECONDS)); + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkEdge.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkEdge.java new file mode 100644 index 00000000..1a99ae3c --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkEdge.java @@ -0,0 +1,21 @@ +package org.simantics.district.network.ui; + +import java.awt.geom.Path2D; +import java.awt.geom.Rectangle2D; + +public class DistrictNetworkEdge { + + private Path2D path; + + public DistrictNetworkEdge(Path2D path) { + this.path = path; + } + + public Path2D getPath() { + return path; + } + + public Rectangle2D getBounds(Rectangle2D rect) { + return path.getBounds2D(); + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java new file mode 100644 index 00000000..a8cd239b --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/NetworkDrawingParticipant.java @@ -0,0 +1,253 @@ + +package org.simantics.district.network.ui; + +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.Shape; +import java.awt.geom.Path2D; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.simantics.Simantics; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.ui.DiagramModelHints; +import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency; +import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.diagram.handler.PickContext; +import org.simantics.g2d.diagram.handler.PickRequest; +import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy; +import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant; +import org.simantics.g2d.diagram.participant.Selection; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.participant.TransformUtil; +import org.simantics.g2d.utils.GeometryUtils; +import org.simantics.scenegraph.g2d.G2DNode; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.scenegraph.g2d.events.EventTypes; +import org.simantics.scenegraph.g2d.events.MouseEvent; +import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler; +import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent; +import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent; +import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent; +import org.simantics.scenegraph.utils.NodeUtil; +import org.simantics.utils.datastructures.hints.IHintContext.Key; +import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; + +public class NetworkDrawingParticipant extends AbstractDiagramParticipant { + + /** + * A hint key for terminal pick distance in control pixels. + * @see #PICK_DIST + */ + public static final Key KEY_PICK_DISTANCE = new KeyOf(Double.class, "PICK_DISTANCE"); + + /** + * Default terminal pick distance in control pixels. + * @see #DEFAULT_PICK_DISTANCE + */ + public static final double PICK_DIST = 10; + + private NetworkDrawingNode node; + + @Dependency Selection selection; + @Dependency TransformUtil util; + @Dependency PickContext pickContext; + + @SGInit + public void initSG(G2DParentNode parent) { + node = parent.addNode("networkDrawingNode", NetworkDrawingNode.class); + } + + @Override + protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) { + node.setDiagram(newDiagram); + } + + @EventHandler(priority = 1 << 21) + public boolean handleClick(MouseClickEvent me) { + + boolean isLeft = me.button == MouseEvent.LEFT_BUTTON; + boolean isRight = me.button == MouseEvent.RIGHT_BUTTON; + if (!isLeft && !isRight) + return false; + boolean isShiftPressed = me.hasAllModifiers(MouseEvent.SHIFT_MASK); + + int selectionId = me.mouseId; + + double pickDist = getPickDistance(); + Rectangle2D controlPickRect = new Rectangle2D.Double(me.controlPosition.getX()-pickDist, me.controlPosition.getY()-pickDist, pickDist*2+1, pickDist*2+1); + Shape canvasPickRect = GeometryUtils.transformShape(controlPickRect, util.getInverseTransform()); + + PickRequest req = new PickRequest(canvasPickRect); + req.pickPolicy = PickPolicy.PICK_INTERSECTING_OBJECTS; + //req.pickSorter = PickRequest.PickSorter.CONNECTIONS_LAST; + List pickables = new ArrayList(); + pickContext.pick(diagram, req, pickables); + + Set currentSelection = selection.getSelection(selectionId); + + if (!pickables.isEmpty()) { + /* + * Select the one object the mouse points to. If multiple object + * are picked, select the one that is after the earliest by + * index of the current selection when shift is pressed. Otherwise + * always pick the topmost element. + */ + IElement selectedPick = isShiftPressed + ? rotatingPick(currentSelection, pickables) + : pickables.get(pickables.size() - 1); + + // Only select when + // 1. the selection would actually change + // AND + // 2.1. left button was pressed + // OR + // 2.2. right button was pressed and the element to-be-selected + // is NOT a part of the current selection + if (!Collections.singleton(selectedPick).equals(currentSelection) + && (isLeft || (isRight && !currentSelection.contains(selectedPick)))) { + selection.setSelection(selectionId, selectedPick); + } + } + + return false; + } + + private double getPickDistance() { + Double pickDistance = getHint(KEY_PICK_DISTANCE); + return pickDistance == null ? PICK_DIST : Math.max(pickDistance, 0); + } + + private IElement rotatingPick(int selectionId, List pickables) { + Set sel = selection.getSelection(selectionId); + return rotatingPick(sel, pickables); + } + + private IElement rotatingPick(Set sel, List pickables) { + int earliestIndex = pickables.size(); + for (int i = pickables.size() - 1; i >= 0; --i) { + if (sel.contains(pickables.get(i))) { + earliestIndex = i; + break; + } + } + if (earliestIndex == 0) + earliestIndex = pickables.size(); + IElement selectedPick = pickables.get(earliestIndex - 1); + return selectedPick; + } + + public static class NetworkDrawingNode extends G2DNode { + + private static final long serialVersionUID = -3475301184009620573L; + + private List nodes = new ArrayList<>(); + + private Rectangle2D rect = new Rectangle2D.Double(10, 10, 10, 10); + + private Set paths = new HashSet<>(); + + private Resource diagramResource; + + private boolean committed; + + @Override + public void init() { + super.init(); + addEventHandler(this); + } + + public void setDiagram(IDiagram diagram) { + if (diagram != null) + this.diagramResource = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE); + } + + @Override + public void render(Graphics2D g2d) { + Color old = g2d.getColor(); + g2d.setColor(Color.BLUE); + + paths.forEach(p -> { + g2d.draw(p); + }); + + g2d.setColor(old); + } + + @Override + public Rectangle2D getBoundsInLocal() { + return rect.getBounds2D(); + } + + @Override + public int getEventMask() { + return EventTypes.MouseMask; + } + + @Override + protected boolean mouseDoubleClicked(MouseDoubleClickedEvent e) { + // nodes to path2d + Point2D start = null; + Point2D end = null; + Iterator nodeIter = nodes.iterator(); + while (nodeIter.hasNext()) { + if (end == null) { + start = nodeIter.next(); + } else { + start = end; + } + end = nodeIter.next(); + + createEdge(start, end); + } + + nodes.clear(); + committed = true; + + repaint(); + + return true; + } + + private void createEdge(Point2D start, Point2D end) { + double[] startCoords = new double[] { start.getX(), start.getY() }; + double[] endCoords = new double[] { end.getX(), end.getY() }; + + DNEdgeBuilder builder = new DNEdgeBuilder(diagramResource); + Simantics.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + builder.create(graph, startCoords, endCoords); + } + }); + + } + + @Override + protected boolean mouseClicked(MouseClickEvent e) { + if (committed) { + committed = false; + return false; + } + Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double()); + nodes.add(new Point2D.Double(localPos.getX(), localPos.getY())); + return super.mouseClicked(e); + } + + @Override + protected boolean mouseMoved(MouseMovedEvent e) { + return super.mouseMoved(e); + } + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/OpenDiagramFromConfigurationAdapter.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/OpenDiagramFromConfigurationAdapter.java new file mode 100644 index 00000000..1c49acbc --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/OpenDiagramFromConfigurationAdapter.java @@ -0,0 +1,26 @@ +package org.simantics.district.network.ui; + +import java.util.Collections; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.district.network.ontology.DistrictNetworkResource; + +public class OpenDiagramFromConfigurationAdapter + extends org.simantics.modeling.ui.diagramEditor.OpenDiagramFromConfigurationAdapter { + + @Override + public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException { + Resource diagram = getDiagram(g, r, Collections.emptySet()); + if (diagram == null) + return false; + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(g); + return g.isInstanceOf(diagram, DN.Diagram) && !isLocked(g, diagram); + } + + @Override + protected String getEditorId() { + return DistrictDiagramEditor.ID; + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/OpenDistrictEditor.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/OpenDistrictEditor.java new file mode 100644 index 00000000..9abb9a03 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/OpenDistrictEditor.java @@ -0,0 +1,21 @@ +package org.simantics.district.network.ui; + +import javax.inject.Inject; + +import org.eclipse.e4.core.di.annotations.CanExecute; +import org.eclipse.e4.core.di.annotations.Execute; +import org.simantics.ui.workbench.e4.E4WorkbenchUtils; + +public class OpenDistrictEditor { + + @CanExecute + public boolean canExecute() { + return true; + } + + @Execute + @Inject + public void execute() { + E4WorkbenchUtils.openAndShowPart(DistrictDiagramEditor.ID); + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java new file mode 100644 index 00000000..8ec1430b --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java @@ -0,0 +1,71 @@ +package org.simantics.district.network.ui.adapters; + +import java.awt.geom.Rectangle2D; + +import org.simantics.district.network.ui.DistrictNetworkEdge; +import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementUtils; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.SceneGraphNodeKey; +import org.simantics.g2d.element.handler.InternalSize; +import org.simantics.g2d.element.handler.SceneGraph; +import org.simantics.g2d.elementclass.connection.EdgeClass.FixedTransform; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.utils.datastructures.hints.IHintContext.Key; +import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; + +public class DistrictNetworkEdgeElement { + + public static final Key KEY_DN_EDGE = new KeyOf(DistrictNetworkEdge.class, "DN_EDGE"); + public static final Key KEY_DN_EDGE_NODE = new SceneGraphNodeKey(DistrictNetworkEdgeNode.class, "DN_EDGE_NODE"); + + public static final ElementClass CLASS = + ElementClass.compile( + FixedTransform.INSTANCE, + DNEdgeInternalSize.INSTANCE, + DNEdgeSceneGraph.INSTANCE + ).setId(DistrictNetworkEdgeElement.class.getSimpleName()); + + static final class DNEdgeSceneGraph implements SceneGraph { + + public static final DNEdgeSceneGraph INSTANCE = new DNEdgeSceneGraph(); + + private static final long serialVersionUID = 8894367073815556871L; + + @Override + public void init(IElement edgeElement, G2DParentNode parent) { + DistrictNetworkEdge edge = edgeElement.getHint(KEY_DN_EDGE); + if (edge == null) { + cleanup(edgeElement); + } else { + DistrictNetworkEdgeNode node = edgeElement.getHint(KEY_DN_EDGE_NODE); + if (node == null) { + node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkEdgeNode.class); + edgeElement.setHint(KEY_DN_EDGE_NODE, node); + } + node.setDNEdge(edge); + } + } + + @Override + public void cleanup(IElement edge) { + ElementUtils.removePossibleNode(edge, KEY_DN_EDGE_NODE); + edge.removeHint(KEY_DN_EDGE_NODE); + } + } + + static final class DNEdgeInternalSize implements InternalSize { + + private static final long serialVersionUID = -2725017034692179676L; + + public static final DNEdgeInternalSize INSTANCE = new DNEdgeInternalSize(); + + @Override + public Rectangle2D getBounds(IElement e, Rectangle2D size) { + DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE); + return edge.getBounds(size); + } + + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElementFactory.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElementFactory.java new file mode 100644 index 00000000..ff1d844f --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElementFactory.java @@ -0,0 +1,62 @@ +package org.simantics.district.network.ui.adapters; + +import java.awt.geom.Path2D; + +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.AsyncProcedure; +import org.simantics.diagram.adapter.SyncElementFactory; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.diagram.ui.DiagramModelHints; +import org.simantics.district.network.ontology.DistrictNetworkResource; +import org.simantics.district.network.ui.DistrictNetworkEdge; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.impl.StaticObjectAdapter; + +public class DistrictNetworkEdgeElementFactory extends SyncElementFactory { + + public static final ElementClass CLASS = DistrictNetworkEdgeElement.CLASS; + + private DistrictNetworkResource DN; + private DiagramResource DIA; + + public DistrictNetworkEdgeElementFactory(ReadGraph graph) { + this.DN = DistrictNetworkResource.getInstance(graph); + this.DIA = DiagramResource.getInstance(graph); + } + + @Override + public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure procedure) { + procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType))); + } + + @Override + protected Resource getElementClassBaseType(AsyncReadGraph graph) { + return DN.Edge; + } + + @Override + public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource edgeResource, IElement element) throws DatabaseException { + + Resource diagramRuntime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE); + + Resource startVertex = graph.getSingleObject(edgeResource, DN.HasStartVertex); + Resource endVertex = graph.getSingleObject(edgeResource, DN.HasEndVertex); + + double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation); + double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation); + + Path2D path = new Path2D.Double(); + path.moveTo(startCoords[0], startCoords[1]); + path.lineTo(endCoords[0], endCoords[1]); + + DistrictNetworkEdge edge = new DistrictNetworkEdge(path); + + element.setHint(DistrictNetworkEdgeElement.KEY_DN_EDGE, edge); + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertex.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertex.java new file mode 100644 index 00000000..c9981131 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertex.java @@ -0,0 +1,17 @@ +package org.simantics.district.network.ui.adapters; + +import java.awt.geom.Point2D; + +public class DistrictNetworkVertex { + + private Point2D point; + + public DistrictNetworkVertex(double[] coords) { + this.point = new Point2D.Double(coords[0], coords[1]); + } + + public Point2D getPoint() { + return point; + } + +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java new file mode 100644 index 00000000..bb75ab0c --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java @@ -0,0 +1,74 @@ +package org.simantics.district.network.ui.adapters; + +import java.awt.Dimension; +import java.awt.geom.Rectangle2D; + +import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementUtils; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.SceneGraphNodeKey; +import org.simantics.g2d.element.handler.InternalSize; +import org.simantics.g2d.element.handler.SceneGraph; +import org.simantics.g2d.elementclass.connection.EdgeClass.FixedTransform; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.utils.datastructures.hints.IHintContext.Key; +import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; + +public class DistrictNetworkVertexElement { + + public static final Key KEY_DN_VERTEX = new KeyOf(DistrictNetworkVertex.class, "DN_VERTEX"); + public static final Key KEY_DN_VERTEX_NODE = new SceneGraphNodeKey(DistrictNetworkVertexNode.class, "DN_VERTEX_NODE"); + + public static final ElementClass CLASS = + ElementClass.compile( + FixedTransform.INSTANCE, + DNVertexInternalSize.INSTANCE, + DNVertexSceneGraph.INSTANCE + ).setId(DistrictNetworkVertexElement.class.getSimpleName()); + + static final class DNVertexSceneGraph implements SceneGraph { + + public static final DNVertexSceneGraph INSTANCE = new DNVertexSceneGraph(); + + private static final long serialVersionUID = 8894367073815556871L; + + @Override + public void init(IElement edgeElement, G2DParentNode parent) { + DistrictNetworkVertex vertex = edgeElement.getHint(KEY_DN_VERTEX); + if (vertex == null) { + cleanup(edgeElement); + } else { + DistrictNetworkVertexNode node = edgeElement.getHint(KEY_DN_VERTEX_NODE); + if (node == null) { + node = parent.addNode(ElementUtils.generateNodeId(edgeElement), DistrictNetworkVertexNode.class); + edgeElement.setHint(KEY_DN_VERTEX_NODE, node); + } + node.setVertex(vertex); + } + } + + @Override + public void cleanup(IElement edge) { + ElementUtils.removePossibleNode(edge, KEY_DN_VERTEX_NODE); + edge.removeHint(KEY_DN_VERTEX_NODE); + } + } + + static final class DNVertexInternalSize implements InternalSize { + + public static final DNVertexInternalSize INSTANCE = new DNVertexInternalSize(); + + private static final long serialVersionUID = 2625702114278956461L; + + @Override + public Rectangle2D getBounds(IElement e, Rectangle2D size) { + DistrictNetworkVertex vertex = e.getHint(KEY_DN_VERTEX); + if (size == null) + size = new Rectangle2D.Double(); + size.setFrame(vertex.getPoint(), new Dimension(1, 1)); + return size; + } + + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElementFactory.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElementFactory.java new file mode 100644 index 00000000..909f7fb1 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElementFactory.java @@ -0,0 +1,48 @@ +package org.simantics.district.network.ui.adapters; + +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.AsyncProcedure; +import org.simantics.diagram.adapter.SyncElementFactory; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.district.network.ontology.DistrictNetworkResource; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.impl.StaticObjectAdapter; + +public class DistrictNetworkVertexElementFactory extends SyncElementFactory { + + public static final ElementClass CLASS = DistrictNetworkVertexElement.CLASS; + private DistrictNetworkResource DN; + private DiagramResource DIA; + + public DistrictNetworkVertexElementFactory(ReadGraph graph) { + this.DN = DistrictNetworkResource.getInstance(graph); + this.DIA = DiagramResource.getInstance(graph); + } + + @Override + public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, + AsyncProcedure procedure) { + procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType))); + } + + @Override + protected Resource getElementClassBaseType(AsyncReadGraph graph) { + return DN.Vertex; + } + + @Override + public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource vertexResource, IElement element) throws DatabaseException { + + double[] coords = graph.getRelatedValue(vertexResource, DIA.HasLocation); + + DistrictNetworkVertex vertex = new DistrictNetworkVertex(coords); + + element.setHint(DistrictNetworkVertexElement.KEY_DN_VERTEX, vertex); + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/internal/Activator.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/internal/Activator.java new file mode 100644 index 00000000..1e7a4d51 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/internal/Activator.java @@ -0,0 +1,18 @@ +package org.simantics.district.network.ui.internal; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; + +public class Activator implements BundleActivator { + + @Override + public void start(BundleContext context) throws Exception { + + } + + @Override + public void stop(BundleContext context) throws Exception { + + } + +} 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 new file mode 100644 index 00000000..60013653 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java @@ -0,0 +1,85 @@ +package org.simantics.district.network.ui.nodes; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.Shape; +import java.awt.Stroke; +import java.awt.geom.AffineTransform; +import java.awt.geom.Path2D; +import java.awt.geom.Rectangle2D; + +import org.simantics.district.network.ui.DistrictNetworkEdge; +import org.simantics.scenegraph.g2d.G2DNode; +import org.simantics.scenegraph.utils.NodeUtil; + +public class DistrictNetworkEdgeNode extends G2DNode { + + private static final long serialVersionUID = 8049769475036519806L; + + private static final Stroke SELECTION_STROKE = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER); + private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96); + + private DistrictNetworkEdge edge; + private Rectangle2D bounds; + + @Override + public void render(Graphics2D g2d) { + + AffineTransform ot = null; + AffineTransform t = getTransform(); + if (t != null && !t.isIdentity()) { + ot = g2d.getTransform(); + g2d.transform(getTransform()); + } + + Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); + + Color oldColor = g2d.getColor(); + boolean selected = isSelected(); + if (selected) { + Path2D selectionPath = edge.getPath(); + Shape selectionShape = SELECTION_STROKE.createStrokedShape(selectionPath); + g2d.setColor(SELECTION_COLOR); + g2d.fill(selectionShape); + } + + g2d.setColor(Color.BLUE); + // render + g2d.draw(edge.getPath()); + + // Reset + g2d.setColor(oldColor); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint); + + if (ot != null) + g2d.setTransform(ot); + } + + private boolean isSelected() { + return NodeUtil.isSelected(this, 1); + } + + @Override + public Rectangle2D getBoundsInLocal() { + return bounds; + } + + private void updateBounds() { + Rectangle2D oldBounds = bounds; + if (oldBounds == null) + oldBounds = new Rectangle2D.Double(); + bounds = calculateBounds(oldBounds); + } + + private Rectangle2D calculateBounds(Rectangle2D rect) { + return edge.getBounds(rect); + } + + public void setDNEdge(DistrictNetworkEdge edge) { + this.edge = edge; + } + +} 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 new file mode 100644 index 00000000..77038500 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkVertexNode.java @@ -0,0 +1,54 @@ +package org.simantics.district.network.ui.nodes; + +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.geom.AffineTransform; +import java.awt.geom.Rectangle2D; + +import org.simantics.district.network.ui.adapters.DistrictNetworkVertex; +import org.simantics.scenegraph.g2d.G2DNode; + +public class DistrictNetworkVertexNode extends G2DNode { + + private static final long serialVersionUID = -2641639101400236719L; + private DistrictNetworkVertex vertex; + + @Override + public void render(Graphics2D g2d) { + AffineTransform ot = null; + AffineTransform t = getTransform(); + if (t != null && !t.isIdentity()) { + ot = g2d.getTransform(); + g2d.transform(getTransform()); + } + + Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); + + Color oldColor = g2d.getColor(); + g2d.setColor(Color.RED); + // render + + Rectangle2D.Double rect = new Rectangle2D.Double(vertex.getPoint().getX() - 0.5, vertex.getPoint().getY() - 0.5, 1, 1); + g2d.draw(rect); + + // Reset stats + g2d.setColor(oldColor); + + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint); + + if (ot != null) + g2d.setTransform(ot); + } + + @Override + public Rectangle2D getBoundsInLocal() { + return new Rectangle2D.Double(vertex.getPoint().getX(), vertex.getPoint().getY(), 1, 1); + } + + public void setVertex(DistrictNetworkVertex vertex) { + this.vertex = vertex; + } + +} diff --git a/org.simantics.district.network/.classpath b/org.simantics.district.network/.classpath new file mode 100644 index 00000000..b862a296 --- /dev/null +++ b/org.simantics.district.network/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/org.simantics.district.network/.project b/org.simantics.district.network/.project new file mode 100644 index 00000000..db68a584 --- /dev/null +++ b/org.simantics.district.network/.project @@ -0,0 +1,28 @@ + + + org.simantics.district.network + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/org.simantics.district.network/META-INF/MANIFEST.MF b/org.simantics.district.network/META-INF/MANIFEST.MF new file mode 100644 index 00000000..03d4574c --- /dev/null +++ b/org.simantics.district.network/META-INF/MANIFEST.MF @@ -0,0 +1,12 @@ +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 diff --git a/org.simantics.district.network/build.properties b/org.simantics.district.network/build.properties new file mode 100644 index 00000000..d7586c21 --- /dev/null +++ b/org.simantics.district.network/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + adapters.xml diff --git a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java new file mode 100644 index 00000000..f9e0439d --- /dev/null +++ b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java @@ -0,0 +1,37 @@ +package org.simantics.district.network; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.district.network.ontology.DistrictNetworkResource; +import org.simantics.layer0.Layer0; + +public class DistrictNetworkUtil { + + public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); + + Resource edge = graph.newResource(); + graph.claim(edge, L0.InstanceOf, DN.Edge); + OrderedSetUtils.addFirst(graph, composite, edge); + graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge); + return edge; + } + + public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); + Resource vertex = graph.newResource(); + graph.claim(vertex, L0.InstanceOf, DN.Vertex); + graph.claimLiteral(vertex, DIA.HasLocation, coords); + OrderedSetUtils.addFirst(graph, composite, vertex); + graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex); + + return vertex; + } +} -- 2.47.1