From: jsimomaa Date: Wed, 1 Feb 2017 09:26:31 +0000 (+0200) Subject: Some more added functionality to simantics district editor etc X-Git-Tag: v1.31.0~17 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=16ee01dc5a40981c58fd5b478b89552e5814e8bb;p=simantics%2Fdistrict.git Some more added functionality to simantics district editor etc refs #6958 Change-Id: I92a39fffe451b11fe5fa3ab57414f9729637fce9 --- diff --git a/org.simantics.district.feature/test.csv b/org.simantics.district.feature/test.csv new file mode 100644 index 00000000..96e4e390 --- /dev/null +++ b/org.simantics.district.feature/test.csv @@ -0,0 +1,3 @@ +x1;y1;z1;x2;y2;z2 +1;2;3;2;4;6 +3;5;3;3;4;5 \ No newline at end of file diff --git a/org.simantics.district.network.ontology/META-INF/MANIFEST.MF b/org.simantics.district.network.ontology/META-INF/MANIFEST.MF index 18b8713d..6ac026c7 100644 --- a/org.simantics.district.network.ontology/META-INF/MANIFEST.MF +++ b/org.simantics.district.network.ontology/META-INF/MANIFEST.MF @@ -9,6 +9,7 @@ Require-Bundle: org.simantics.layer0, 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", - org.simantics.selectionview.ontology;bundle-version="1.2.0" + org.simantics.selectionview.ontology;bundle-version="1.2.0", + org.simantics.selectionview.ui.ontology;bundle-version="1.1.0" Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Export-Package: org.simantics.district.network.ontology diff --git a/org.simantics.district.network.ontology/graph.tg b/org.simantics.district.network.ontology/graph.tg index 73e40a0a..eb85b78f 100644 Binary files a/org.simantics.district.network.ontology/graph.tg and b/org.simantics.district.network.ontology/graph.tg differ diff --git a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph index 2c6ac584..ade6b1c5 100644 --- a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph +++ b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph @@ -5,8 +5,7 @@ MOD = VP = IMAGE = SEL = - -MBC = MOD.ModelingBrowseContext +SEL_UI = DN = : L0.Ontology @L0.new @@ -16,12 +15,12 @@ DN = : L0.Ontology DN.Layer -- DN.Mapping.InputTerminal --> DN.Mapping.Terminal -- DN.Mapping.OutputTerminal --> DN.Mapping.Terminal -- DN.Mapping.ComponentType --> STR.ComponentType -- DN.Mapping.ComponentType --> STR.ComponentType -- DN.Mapping.VertexMapping.ElevationAttribute --> L0.String -- DN.Mapping.Terminals --> DN.Mapping.TerminalPair -- DN.Mapping.ComponentType >-- DN.Mapping.EdgeMapping.Bidirectional --> L0.Boolean -- DN.Mapping.EdgeMapping.LengthAttribute --> L0.String () { - 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; - } - -} - +package org.simantics.district.network.ontology; + +import org.simantics.db.RequestProcessor; +import org.simantics.db.Resource; +import org.simantics.db.ReadGraph; +import org.simantics.db.request.Read; +import org.simantics.db.Session; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.service.QueryControl; + +public class DistrictNetworkResource { + + public final Resource ActionContext; + public final Resource ActionContext_newDiagramContribution; + public final Resource Actions; + public final Resource Actions_NewDNDiagram; + public final Resource AddLayerToDNDiagramTemplate; + public final Resource Bidirectional; + public final Resource Bidirectional_Inverse; + public final Resource BrowseContext; + public final Resource Composite; + public final Resource Connection; + public final Resource Diagram; + public final Resource EPSG_4326; + public final Resource Edge; + public final Resource EdgeDefaultMapping; + public final Resource EdgeDefaultMapping_Inverse; + public final Resource EdgeMappingParameterType; + public final Resource Edge_HasDiameter; + public final Resource Edge_HasDiameter_Inverse; + public final Resource Functions; + public final Resource Functions_compositeInstantiator; + public final Resource Functions_convertToValue; + public final Resource Functions_defaultEdgeMappingModifier; + public final Resource Functions_defaultVertexMappingModifier; + public final Resource Functions_enumerationValues; + public final Resource Functions_mappingModifier; + public final Resource HasEndVertex; + public final Resource HasEndVertex_Inverse; + public final Resource HasInLayerTag; + public final Resource HasInLayerTag_Inverse; + public final Resource HasMapping; + public final Resource HasMapping_Inverse; + public final Resource HasSRID; + public final Resource HasSRID_Inverse; + public final Resource HasSRTEXT; + public final Resource HasSRTEXT_Inverse; + public final Resource HasSpatialRefSystem; + public final Resource HasSpatialRefSystem_Inverse; + public final Resource HasStartVertex; + public final Resource HasStartVertex_Inverse; + public final Resource Images; + public final Resource Images_ConfigurationFolder; + public final Resource InLayer; + public final Resource Layer; + public final Resource Mapping; + public final Resource MappingParameterType; + public final Resource Mapping_Base; + public final Resource Mapping_ComponentType; + public final Resource Mapping_ComponentType_Inverse; + public final Resource Mapping_Dummy; + public final Resource Mapping_EdgeMapping; + public final Resource Mapping_EdgeMapping_Bidirectional; + public final Resource Mapping_EdgeMapping_Bidirectional_Inverse; + public final Resource Mapping_EdgeMapping_DiameterAttribute; + public final Resource Mapping_EdgeMapping_DiameterAttribute_Inverse; + public final Resource Mapping_EdgeMapping_LengthAttribute; + public final Resource Mapping_EdgeMapping_LengthAttribute_Inverse; + public final Resource Mapping_InputTerminal; + public final Resource Mapping_InputTerminal_Inverse; + public final Resource Mapping_OutputTerminal; + public final Resource Mapping_OutputTerminal_Inverse; + public final Resource Mapping_Terminal; + public final Resource Mapping_TerminalPair; + public final Resource Mapping_Terminals; + public final Resource Mapping_Terminals_Inverse; + public final Resource Mapping_VertexMapping; + public final Resource Mapping_VertexMapping_ElevationAttribute; + public final Resource Mapping_VertexMapping_ElevationAttribute_Inverse; + public final Resource SpatialRefSystem; + public final Resource Vertex; + public final Resource VertexDefaultMapping; + public final Resource VertexDefaultMapping_Inverse; + public final Resource VertexMappingParameterType; + + public static class URIs { + public static final String ActionContext = "http://www.simantics.org/DistrictNetwork-1.0/ActionContext"; + public static final String ActionContext_newDiagramContribution = "http://www.simantics.org/DistrictNetwork-1.0/ActionContext/newDiagramContribution"; + public static final String Actions = "http://www.simantics.org/DistrictNetwork-1.0/Actions"; + public static final String Actions_NewDNDiagram = "http://www.simantics.org/DistrictNetwork-1.0/Actions/NewDNDiagram"; + public static final String AddLayerToDNDiagramTemplate = "http://www.simantics.org/DistrictNetwork-1.0/AddLayerToDNDiagramTemplate"; + public static final String Bidirectional = "http://www.simantics.org/DistrictNetwork-1.0/Bidirectional"; + public static final String Bidirectional_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Bidirectional/Inverse"; + public static final String BrowseContext = "http://www.simantics.org/DistrictNetwork-1.0/BrowseContext"; + public static final String Composite = "http://www.simantics.org/DistrictNetwork-1.0/Composite"; + public static final String Connection = "http://www.simantics.org/DistrictNetwork-1.0/Connection"; + public static final String Diagram = "http://www.simantics.org/DistrictNetwork-1.0/Diagram"; + public static final String EPSG_4326 = "http://www.simantics.org/DistrictNetwork-1.0/EPSG_4326"; + public static final String Edge = "http://www.simantics.org/DistrictNetwork-1.0/Edge"; + public static final String EdgeDefaultMapping = "http://www.simantics.org/DistrictNetwork-1.0/EdgeDefaultMapping"; + public static final String EdgeDefaultMapping_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/EdgeDefaultMapping/Inverse"; + public static final String EdgeMappingParameterType = "http://www.simantics.org/DistrictNetwork-1.0/EdgeMappingParameterType"; + public static final String Edge_HasDiameter = "http://www.simantics.org/DistrictNetwork-1.0/Edge/HasDiameter"; + public static final String Edge_HasDiameter_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Edge/HasDiameter/Inverse"; + public static final String Functions = "http://www.simantics.org/DistrictNetwork-1.0/Functions"; + public static final String Functions_compositeInstantiator = "http://www.simantics.org/DistrictNetwork-1.0/Functions/compositeInstantiator"; + public static final String Functions_convertToValue = "http://www.simantics.org/DistrictNetwork-1.0/Functions/convertToValue"; + public static final String Functions_defaultEdgeMappingModifier = "http://www.simantics.org/DistrictNetwork-1.0/Functions/defaultEdgeMappingModifier"; + public static final String Functions_defaultVertexMappingModifier = "http://www.simantics.org/DistrictNetwork-1.0/Functions/defaultVertexMappingModifier"; + public static final String Functions_enumerationValues = "http://www.simantics.org/DistrictNetwork-1.0/Functions/enumerationValues"; + public static final String Functions_mappingModifier = "http://www.simantics.org/DistrictNetwork-1.0/Functions/mappingModifier"; + public static final String HasEndVertex = "http://www.simantics.org/DistrictNetwork-1.0/HasEndVertex"; + public static final String HasEndVertex_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/HasEndVertex_Inverse"; + public static final String HasInLayerTag = "http://www.simantics.org/DistrictNetwork-1.0/HasInLayerTag"; + public static final String HasInLayerTag_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/HasInLayerTag/Inverse"; + public static final String HasMapping = "http://www.simantics.org/DistrictNetwork-1.0/HasMapping"; + public static final String HasMapping_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/HasMapping/Inverse"; + public static final String HasSRID = "http://www.simantics.org/DistrictNetwork-1.0/HasSRID"; + public static final String HasSRID_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/HasSRID/Inverse"; + public static final String HasSRTEXT = "http://www.simantics.org/DistrictNetwork-1.0/HasSRTEXT"; + public static final String HasSRTEXT_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/HasSRTEXT/Inverse"; + public static final String HasSpatialRefSystem = "http://www.simantics.org/DistrictNetwork-1.0/HasSpatialRefSystem"; + public static final String HasSpatialRefSystem_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/HasSpatialRefSystem/Inverse"; + public static final String HasStartVertex = "http://www.simantics.org/DistrictNetwork-1.0/HasStartVertex"; + public static final String HasStartVertex_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/HasStartVertex_Inverse"; + public static final String Images = "http://www.simantics.org/DistrictNetwork-1.0/Images"; + public static final String Images_ConfigurationFolder = "http://www.simantics.org/DistrictNetwork-1.0/Images/ConfigurationFolder"; + public static final String InLayer = "http://www.simantics.org/DistrictNetwork-1.0/InLayer"; + public static final String Layer = "http://www.simantics.org/DistrictNetwork-1.0/Layer"; + public static final String Mapping = "http://www.simantics.org/DistrictNetwork-1.0/Mapping"; + public static final String MappingParameterType = "http://www.simantics.org/DistrictNetwork-1.0/MappingParameterType"; + public static final String Mapping_Base = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/Base"; + public static final String Mapping_ComponentType = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/ComponentType"; + public static final String Mapping_ComponentType_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/ComponentType/Inverse"; + public static final String Mapping_Dummy = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/Dummy"; + public static final String Mapping_EdgeMapping = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/EdgeMapping"; + public static final String Mapping_EdgeMapping_Bidirectional = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/EdgeMapping/Bidirectional"; + public static final String Mapping_EdgeMapping_Bidirectional_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/EdgeMapping/Bidirectional/Inverse"; + public static final String Mapping_EdgeMapping_DiameterAttribute = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/EdgeMapping/DiameterAttribute"; + public static final String Mapping_EdgeMapping_DiameterAttribute_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/EdgeMapping/DiameterAttribute/Inverse"; + public static final String Mapping_EdgeMapping_LengthAttribute = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/EdgeMapping/LengthAttribute"; + public static final String Mapping_EdgeMapping_LengthAttribute_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/EdgeMapping/LengthAttribute/Inverse"; + public static final String Mapping_InputTerminal = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/InputTerminal"; + public static final String Mapping_InputTerminal_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/InputTerminal/Inverse"; + public static final String Mapping_OutputTerminal = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/OutputTerminal"; + public static final String Mapping_OutputTerminal_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/OutputTerminal/Inverse"; + public static final String Mapping_Terminal = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/Terminal"; + public static final String Mapping_TerminalPair = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/TerminalPair"; + public static final String Mapping_Terminals = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/Terminals"; + public static final String Mapping_Terminals_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/Terminals/Inverse"; + public static final String Mapping_VertexMapping = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/VertexMapping"; + public static final String Mapping_VertexMapping_ElevationAttribute = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/VertexMapping/ElevationAttribute"; + public static final String Mapping_VertexMapping_ElevationAttribute_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/VertexMapping/ElevationAttribute/Inverse"; + public static final String SpatialRefSystem = "http://www.simantics.org/DistrictNetwork-1.0/SpatialRefSystem"; + public static final String Vertex = "http://www.simantics.org/DistrictNetwork-1.0/Vertex"; + public static final String VertexDefaultMapping = "http://www.simantics.org/DistrictNetwork-1.0/VertexDefaultMapping"; + public static final String VertexDefaultMapping_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/VertexDefaultMapping/Inverse"; + public static final String VertexMappingParameterType = "http://www.simantics.org/DistrictNetwork-1.0/VertexMappingParameterType"; + } + + public static Resource getResourceOrNull(ReadGraph graph, String uri) { + try { + return graph.getResource(uri); + } catch(DatabaseException e) { + System.err.println(e.getMessage()); + return null; + } + } + + public DistrictNetworkResource(ReadGraph graph) { + ActionContext = getResourceOrNull(graph, URIs.ActionContext); + ActionContext_newDiagramContribution = getResourceOrNull(graph, URIs.ActionContext_newDiagramContribution); + Actions = getResourceOrNull(graph, URIs.Actions); + Actions_NewDNDiagram = getResourceOrNull(graph, URIs.Actions_NewDNDiagram); + AddLayerToDNDiagramTemplate = getResourceOrNull(graph, URIs.AddLayerToDNDiagramTemplate); + Bidirectional = getResourceOrNull(graph, URIs.Bidirectional); + Bidirectional_Inverse = getResourceOrNull(graph, URIs.Bidirectional_Inverse); + BrowseContext = getResourceOrNull(graph, URIs.BrowseContext); + Composite = getResourceOrNull(graph, URIs.Composite); + Connection = getResourceOrNull(graph, URIs.Connection); + Diagram = getResourceOrNull(graph, URIs.Diagram); + EPSG_4326 = getResourceOrNull(graph, URIs.EPSG_4326); + Edge = getResourceOrNull(graph, URIs.Edge); + EdgeDefaultMapping = getResourceOrNull(graph, URIs.EdgeDefaultMapping); + EdgeDefaultMapping_Inverse = getResourceOrNull(graph, URIs.EdgeDefaultMapping_Inverse); + EdgeMappingParameterType = getResourceOrNull(graph, URIs.EdgeMappingParameterType); + Edge_HasDiameter = getResourceOrNull(graph, URIs.Edge_HasDiameter); + Edge_HasDiameter_Inverse = getResourceOrNull(graph, URIs.Edge_HasDiameter_Inverse); + Functions = getResourceOrNull(graph, URIs.Functions); + Functions_compositeInstantiator = getResourceOrNull(graph, URIs.Functions_compositeInstantiator); + Functions_convertToValue = getResourceOrNull(graph, URIs.Functions_convertToValue); + Functions_defaultEdgeMappingModifier = getResourceOrNull(graph, URIs.Functions_defaultEdgeMappingModifier); + Functions_defaultVertexMappingModifier = getResourceOrNull(graph, URIs.Functions_defaultVertexMappingModifier); + Functions_enumerationValues = getResourceOrNull(graph, URIs.Functions_enumerationValues); + Functions_mappingModifier = getResourceOrNull(graph, URIs.Functions_mappingModifier); + HasEndVertex = getResourceOrNull(graph, URIs.HasEndVertex); + HasEndVertex_Inverse = getResourceOrNull(graph, URIs.HasEndVertex_Inverse); + HasInLayerTag = getResourceOrNull(graph, URIs.HasInLayerTag); + HasInLayerTag_Inverse = getResourceOrNull(graph, URIs.HasInLayerTag_Inverse); + HasMapping = getResourceOrNull(graph, URIs.HasMapping); + HasMapping_Inverse = getResourceOrNull(graph, URIs.HasMapping_Inverse); + HasSRID = getResourceOrNull(graph, URIs.HasSRID); + HasSRID_Inverse = getResourceOrNull(graph, URIs.HasSRID_Inverse); + HasSRTEXT = getResourceOrNull(graph, URIs.HasSRTEXT); + HasSRTEXT_Inverse = getResourceOrNull(graph, URIs.HasSRTEXT_Inverse); + HasSpatialRefSystem = getResourceOrNull(graph, URIs.HasSpatialRefSystem); + HasSpatialRefSystem_Inverse = getResourceOrNull(graph, URIs.HasSpatialRefSystem_Inverse); + HasStartVertex = getResourceOrNull(graph, URIs.HasStartVertex); + HasStartVertex_Inverse = getResourceOrNull(graph, URIs.HasStartVertex_Inverse); + Images = getResourceOrNull(graph, URIs.Images); + Images_ConfigurationFolder = getResourceOrNull(graph, URIs.Images_ConfigurationFolder); + InLayer = getResourceOrNull(graph, URIs.InLayer); + Layer = getResourceOrNull(graph, URIs.Layer); + Mapping = getResourceOrNull(graph, URIs.Mapping); + MappingParameterType = getResourceOrNull(graph, URIs.MappingParameterType); + Mapping_Base = getResourceOrNull(graph, URIs.Mapping_Base); + Mapping_ComponentType = getResourceOrNull(graph, URIs.Mapping_ComponentType); + Mapping_ComponentType_Inverse = getResourceOrNull(graph, URIs.Mapping_ComponentType_Inverse); + Mapping_Dummy = getResourceOrNull(graph, URIs.Mapping_Dummy); + Mapping_EdgeMapping = getResourceOrNull(graph, URIs.Mapping_EdgeMapping); + Mapping_EdgeMapping_Bidirectional = getResourceOrNull(graph, URIs.Mapping_EdgeMapping_Bidirectional); + Mapping_EdgeMapping_Bidirectional_Inverse = getResourceOrNull(graph, URIs.Mapping_EdgeMapping_Bidirectional_Inverse); + Mapping_EdgeMapping_DiameterAttribute = getResourceOrNull(graph, URIs.Mapping_EdgeMapping_DiameterAttribute); + Mapping_EdgeMapping_DiameterAttribute_Inverse = getResourceOrNull(graph, URIs.Mapping_EdgeMapping_DiameterAttribute_Inverse); + Mapping_EdgeMapping_LengthAttribute = getResourceOrNull(graph, URIs.Mapping_EdgeMapping_LengthAttribute); + Mapping_EdgeMapping_LengthAttribute_Inverse = getResourceOrNull(graph, URIs.Mapping_EdgeMapping_LengthAttribute_Inverse); + Mapping_InputTerminal = getResourceOrNull(graph, URIs.Mapping_InputTerminal); + Mapping_InputTerminal_Inverse = getResourceOrNull(graph, URIs.Mapping_InputTerminal_Inverse); + Mapping_OutputTerminal = getResourceOrNull(graph, URIs.Mapping_OutputTerminal); + Mapping_OutputTerminal_Inverse = getResourceOrNull(graph, URIs.Mapping_OutputTerminal_Inverse); + Mapping_Terminal = getResourceOrNull(graph, URIs.Mapping_Terminal); + Mapping_TerminalPair = getResourceOrNull(graph, URIs.Mapping_TerminalPair); + Mapping_Terminals = getResourceOrNull(graph, URIs.Mapping_Terminals); + Mapping_Terminals_Inverse = getResourceOrNull(graph, URIs.Mapping_Terminals_Inverse); + Mapping_VertexMapping = getResourceOrNull(graph, URIs.Mapping_VertexMapping); + Mapping_VertexMapping_ElevationAttribute = getResourceOrNull(graph, URIs.Mapping_VertexMapping_ElevationAttribute); + Mapping_VertexMapping_ElevationAttribute_Inverse = getResourceOrNull(graph, URIs.Mapping_VertexMapping_ElevationAttribute_Inverse); + SpatialRefSystem = getResourceOrNull(graph, URIs.SpatialRefSystem); + Vertex = getResourceOrNull(graph, URIs.Vertex); + VertexDefaultMapping = getResourceOrNull(graph, URIs.VertexDefaultMapping); + VertexDefaultMapping_Inverse = getResourceOrNull(graph, URIs.VertexDefaultMapping_Inverse); + VertexMappingParameterType = getResourceOrNull(graph, URIs.VertexMappingParameterType); + } + + public static DistrictNetworkResource getInstance(ReadGraph graph) { + Session session = graph.getSession(); + DistrictNetworkResource ret = session.peekService(DistrictNetworkResource.class); + if(ret == null) { + QueryControl qc = graph.getService(QueryControl.class); + ret = new DistrictNetworkResource(qc.getIndependentGraph(graph)); + session.registerService(DistrictNetworkResource.class, ret); + } + return ret; + } + + public static DistrictNetworkResource getInstance(RequestProcessor session) throws DatabaseException { + DistrictNetworkResource ret = session.peekService(DistrictNetworkResource.class); + if(ret == null) { + ret = session.syncRequest(new Read() { + 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/META-INF/MANIFEST.MF b/org.simantics.district.network.ui/META-INF/MANIFEST.MF index 1a7ea47d..49df92f5 100644 --- a/org.simantics.district.network.ui/META-INF/MANIFEST.MF +++ b/org.simantics.district.network.ui/META-INF/MANIFEST.MF @@ -3,6 +3,8 @@ Bundle-ManifestVersion: 2 Bundle-Name: Simantics District Network UI Bundle-SymbolicName: org.simantics.district.network.ui;singleton:=true Bundle-Version: 1.0.0.qualifier +Bundle-Activator: org.simantics.district.network.ui.internal.Activator +Export-Package: org.simantics.district.network.ui.adapters Require-Bundle: org.eclipse.e4.ui.model.workbench;bundle-version="1.1.100.v20150407-1430", org.eclipse.swt, org.simantics.g2d, @@ -15,8 +17,14 @@ Require-Bundle: org.eclipse.e4.ui.model.workbench;bundle-version="1.1.100.v20150 org.simantics.utils.datastructures, org.simantics.district.network;bundle-version="1.0.0", org.simantics.layer0.utils, - org.simantics.district.maps + org.simantics.district.maps, + org.slf4j.api, + org.simantics.db.indexing, + org.eclipse.e4.ui.services, + org.eclipse.e4.core.di, + org.eclipse.e4.ui.di, + org.eclipse.e4.core.commands, + org.eclipse.e4.core.contexts Bundle-RequiredExecutionEnvironment: JavaSE-1.8 -Export-Package: org.simantics.district.network.ui.adapters +Import-Package: javax.inject;version="1.0.0" 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 index 8fdfa320..eda331ba 100644 --- a/org.simantics.district.network.ui/adapters.xml +++ b/org.simantics.district.network.ui/adapters.xml @@ -1,47 +1,52 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/org.simantics.district.network.ui/fragment.e4xmi b/org.simantics.district.network.ui/fragment.e4xmi index 526195f2..e4a2fbcb 100644 --- a/org.simantics.district.network.ui/fragment.e4xmi +++ b/org.simantics.district.network.ui/fragment.e4xmi @@ -1,17 +1,35 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.simantics.district.network.ui/plugin.xml b/org.simantics.district.network.ui/plugin.xml index 7e6e8c13..826432f9 100644 --- a/org.simantics.district.network.ui/plugin.xml +++ b/org.simantics.district.network.ui/plugin.xml @@ -1,30 +1,41 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + 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 index 687ca337..ebdb9e87 100644 --- 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 @@ -34,7 +34,7 @@ public class DNEdgeBuilder { glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER); } - public void create(WriteGraph graph, double[] start, double[] end) throws DatabaseException { + public void create(WriteGraph graph, double[] start, double[] end, double padding) throws DatabaseException { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); @@ -46,8 +46,8 @@ public class DNEdgeBuilder { // 2. Add vertices Collection vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex)); - Resource startVertex = getOrCreateVertex(graph, vertices, start); - Resource endVertex = getOrCreateVertex(graph, vertices, end); + Resource startVertex = getOrCreateVertex(graph, vertices, start, padding); + Resource endVertex = getOrCreateVertex(graph, vertices, end, padding); graph.claim(edge, DN.HasStartVertex, startVertex); graph.claim(edge, DN.HasEndVertex, endVertex); @@ -68,12 +68,13 @@ public class DNEdgeBuilder { glm.putElementOnVisibleLayers(diagram, graph, res); } - private Resource getOrCreateVertex(WriteGraph graph, Collection vertices, double[] coords) throws DatabaseException { + private Resource getOrCreateVertex(WriteGraph graph, Collection vertices, double[] coords, double padding) throws DatabaseException { Resource vertex = null; + double halfPadding = padding / 2; for (Resource vertx : vertices) { double[] existingCoords = graph.getRelatedValue2(vertx, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY); - Rectangle2D existing = new Rectangle2D.Double(existingCoords[0], existingCoords[1], 1, 1); - Rectangle2D tobecreated = new Rectangle2D.Double(coords[0], coords[1], 1, 1); + Rectangle2D existing = new Rectangle2D.Double(existingCoords[0] - halfPadding, existingCoords[1] - halfPadding, padding, padding); + Rectangle2D tobecreated = new Rectangle2D.Double(coords[0] - halfPadding, coords[1] - halfPadding, padding, padding); if (existing.intersects(tobecreated)) { vertex = vertx; break; diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNGraphLayerUtil.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNGraphLayerUtil.java index 27905a55..a036f78d 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNGraphLayerUtil.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNGraphLayerUtil.java @@ -1,66 +1,83 @@ -package org.simantics.district.network.ui; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; -import org.simantics.diagram.stubs.DiagramResource; -import org.simantics.diagram.synchronization.graph.layer.GraphLayer; -import org.simantics.diagram.synchronization.graph.layer.GraphLayerUtil; -import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil; -import org.simantics.district.network.ontology.DistrictNetworkResource; -import org.simantics.layer0.Layer0; - -public class DNGraphLayerUtil implements IGraphLayerUtil { - - public DNGraphLayerUtil(Resource layer) { - - } - - @Override - public GraphLayer loadLayer(ReadGraph graph, Resource layer) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - String name = graph.getRelatedValue(layer, L0.HasName); - Resource inLayer = graph.getSingleObject(layer, DN.HasInLayerTag); - - Map properties = new HashMap<>(); - properties.put("IN_LAYER", inLayer); - - return new GraphLayer(name, layer, properties); - } - - @Override - public GraphLayer createLayer(WriteGraph graph, String layerName, boolean active) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - DiagramResource DIA = DiagramResource.getInstance(graph); - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - - - Resource layer = graph.newResource(); - graph.claim(layer, L0.InstanceOf, null, DN.Layer); - - // Assign tagging relations - Resource inLayerTag = GraphLayerUtil.newTag(graph, L0, DN.InLayer); - graph.claim(layer, DN.HasInLayerTag, inLayerTag); - - // Assign shared name property for all, the layer and the tags - Resource name = graph.newResource(); - graph.claim(name, L0.InstanceOf, null, L0.String); - graph.claimValue(name, layerName); - - graph.claim(layer, L0.HasName, name); - graph.claim(inLayerTag, L0.HasName, name); - - graph.claim(layer, DN.HasSpatialRefSystem, DN.EPSG_4326); - - GraphLayerUtil.setLayerActive(graph, DIA, layer, active); - - return new GraphLayer(layerName, layer, Collections.emptyMap()); - } - -} +package org.simantics.district.network.ui; + +import java.util.HashMap; +import java.util.Map; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.diagram.synchronization.graph.layer.GraphLayer; +import org.simantics.diagram.synchronization.graph.layer.GraphLayerUtil; +import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil; +import org.simantics.district.network.ontology.DistrictNetworkResource; +import org.simantics.layer0.Layer0; + +public class DNGraphLayerUtil implements IGraphLayerUtil { + + public DNGraphLayerUtil(Resource layer) { + + } + + @Override + public GraphLayer loadLayer(ReadGraph graph, Resource layer) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); + String name = graph.getRelatedValue(layer, L0.HasName); + Resource inLayer = graph.getSingleObject(layer, DN.HasInLayerTag); + Resource visible = graph.getSingleObject(layer, DIA.HasVisibleTag); + Resource focusable = graph.getSingleObject(layer, DIA.HasFocusableTag); + + Map properties = new HashMap<>(); + properties.put(GraphLayer.PROP_FOCUSABLE, focusable); + properties.put(GraphLayer.PROP_VISIBLE, visible); + properties.put("IN_LAYER", inLayer); + + return new GraphLayer(name, layer, properties); + } + + @Override + public GraphLayer createLayer(WriteGraph graph, String layerName, boolean active) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + + + Resource layer = graph.newResource(); + graph.claim(layer, L0.InstanceOf, null, DN.Layer); + + // Assign tagging relations + Resource inLayerTag = GraphLayerUtil.newTag(graph, L0, DN.InLayer); + // for backwards compatibility + Resource visibleTag = GraphLayerUtil.newTag(graph, L0, DIA.IsVisible); + Resource focusableTag = GraphLayerUtil.newTag(graph, L0, DIA.IsFocusable); + graph.claim(layer, DN.HasInLayerTag, inLayerTag); + // for backwards compatibility + graph.claim(layer, DIA.HasVisibleTag, visibleTag); + graph.claim(layer, DIA.HasFocusableTag, focusableTag); + + // Assign shared name property for all, the layer and the tags + Resource name = graph.newResource(); + graph.claim(name, L0.InstanceOf, null, L0.String); + graph.claimValue(name, layerName); + + graph.claim(layer, L0.HasName, name); + graph.claim(inLayerTag, L0.HasName, name); + graph.claim(visibleTag, L0.HasName, name); + graph.claim(focusableTag, L0.HasName, name); + + graph.claim(layer, DN.HasSpatialRefSystem, DN.EPSG_4326); + + GraphLayerUtil.setLayerActive(graph, DIA, layer, active); + + Map properties = new HashMap<>(); + properties.put("IN_LAYER", inLayerTag); + properties.put(GraphLayer.PROP_FOCUSABLE, focusableTag); + properties.put(GraphLayer.PROP_VISIBLE, visibleTag); + + return new GraphLayer(layerName, layer, properties); + } + +} 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 index 5155ccd2..57480180 100644 --- 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 @@ -30,6 +30,10 @@ public class DistrictDiagramViewer extends DiagramViewer { ctx.add(new NetworkDrawingParticipant()); } + protected String getPopupId() { + return "#DistrictDiagramPopup"; + } + @Override public void initializeCanvasContext(CanvasContext ctx) { super.initializeCanvasContext(ctx); 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 index 6ae6097c..a340d859 100644 --- 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 @@ -1,39 +1,83 @@ - -package org.simantics.district.network.ui; - -import org.simantics.district.network.ui.nodes.NetworkDrawingNode; -import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit; -import org.simantics.g2d.diagram.IDiagram; -import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant; -import org.simantics.scenegraph.g2d.G2DParentNode; -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; - - @SGInit - public void initSG(G2DParentNode parent) { - node = parent.addNode("networkDrawingNode", NetworkDrawingNode.class); - node.setNetworkDrawingParticipant(this); - } - - @Override - protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) { - node.setDiagram(newDiagram); - } - -} + +package org.simantics.district.network.ui; + +import java.awt.geom.Point2D; +import java.util.ArrayList; +import java.util.List; + +import org.simantics.district.network.ui.adapters.DistrictNetworkVertexElement; +import org.simantics.district.network.ui.nodes.DistrictNetworkVertexNode; +import org.simantics.district.network.ui.nodes.NetworkDrawingNode; +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.participant.AbstractDiagramParticipant; +import org.simantics.g2d.element.IElement; +import org.simantics.scenegraph.g2d.G2DNode; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.utils.datastructures.hints.IHintContext.Key; +import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; + +public class NetworkDrawingParticipant extends AbstractDiagramParticipant { + + @Dependency + PickContext pick; + + /** + * 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; + + @SGInit + public void initSG(G2DParentNode parent) { + node = parent.addNode("networkDrawingNode", NetworkDrawingNode.class); + node.setNetworkDrawingParticipant(this); + } + + @Override + protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) { + node.setDiagram(newDiagram); + } + + public boolean pickHoveredElement(Point2D currentMousePos) { + PickRequest req = new PickRequest(currentMousePos.getX(), currentMousePos.getY()); + List pickables = new ArrayList(); + pick.pick(diagram, req, pickables); + + List snap = new ArrayList<>(diagram.getSnapshot()); + + snap.removeAll(pickables); + + boolean changed = false; + for (IElement sn : snap) { + G2DNode node = sn.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE); + if (node instanceof DistrictNetworkVertexNode) { + if (((DistrictNetworkVertexNode) node).hover(false) && !changed) { + changed = true; + } + } + } + + for (IElement elem : pickables) { + G2DNode node = elem.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE); + if (node instanceof DistrictNetworkVertexNode) { + if (((DistrictNetworkVertexNode) node).hover(true) && !changed) { + changed = true; + } + } + } + return changed; + } + +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictDiagramClassAdapter.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictDiagramClassAdapter.java new file mode 100644 index 00000000..fe3ed935 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictDiagramClassAdapter.java @@ -0,0 +1,89 @@ +package org.simantics.district.network.ui.adapters; + +import org.simantics.Simantics; +import org.simantics.db.AsyncReadGraph; +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.db.procedure.AsyncProcedure; +import org.simantics.diagram.adapter.DiagramClassAdapter; +import org.simantics.diagram.synchronization.IModifiableSynchronizationContext; +import org.simantics.diagram.synchronization.SynchronizationHints; +import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; +import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints; +import org.simantics.diagram.synchronization.graph.layer.GraphLayer; +import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager; +import org.simantics.g2d.diagram.DiagramClass; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.diagram.handler.SubstituteElementClass; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementHints; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.ElementLayerListener; +import org.simantics.g2d.layers.ILayer; + +public class DistrictDiagramClassAdapter extends DiagramClassAdapter { + + static final SubstituteElementClass DINSTANCE = new DistrictDiagramSubstituteElementClass(); + + @Override + public void adapt(AsyncReadGraph g, Resource source, Resource r, AsyncProcedure procedure) { + procedure.execute(g, INSTANCE.newClassWith(DINSTANCE)); + } + + static class DistrictDiagramElementLayerListenerImpl implements ElementLayerListener { + + private static final long serialVersionUID = 7723435000661141433L; + + @Override + public void visibilityChanged(IElement e, ILayer layer, boolean visible) { + IModifiableSynchronizationContext context = e.getDiagram().getHint(SynchronizationHints.CONTEXT); + GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER); + GraphLayer gl = layerManager.getGraphLayer(layer.getName()); + if (gl != null) { + changeTag(e, gl.getVisible(), visible); + } + } + + @Override + public void focusabilityChanged(IElement e, ILayer layer, boolean focusable) { + IModifiableSynchronizationContext context = e.getDiagram().getHint(SynchronizationHints.CONTEXT); + GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER); + GraphLayer gl = layerManager.getGraphLayer(layer.getName()); + if (gl != null) { + changeTag(e, gl.getFocusable(), focusable); + } + } + + void changeTag(IElement e, Resource tag, boolean set) { + Object object = e.getHint(ElementHints.KEY_OBJECT); + Resource tagged; + if (object instanceof Resource) + tagged = (Resource) object; + else + tagged = null; + if (tagged == null) + return; + + Simantics.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + DiagramGraphUtil.tag(graph, tagged, tag, set); + } + }); + } + } + + static class DistrictDiagramSubstituteElementClass implements SubstituteElementClass { + + static final ElementLayerListener LAYER_LISTENER = new DistrictDiagramElementLayerListenerImpl(); + + @Override + public ElementClass substitute(IDiagram d, ElementClass ec) { + return ec.newClassWith(LAYER_LISTENER); + } + + } +} 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 index a53c02d2..12f26182 100644 --- 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 @@ -1,75 +1,116 @@ -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.element.handler.impl.DefaultTransform; -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( - DefaultTransform.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); - if (size == null) - size = new Rectangle2D.Double(); - edge.getBounds(size); - - return size; - } - - } -} +package org.simantics.district.network.ui.adapters; + +import java.awt.geom.Rectangle2D; +import java.util.Collection; +import java.util.Collections; + +import org.simantics.district.network.ui.DistrictNetworkEdge; +import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode; +import org.simantics.g2d.connection.handler.ConnectionHandler; +import org.simantics.g2d.diagram.handler.Topology.Connection; +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.element.handler.impl.DefaultTransform; +import org.simantics.g2d.element.handler.impl.SimpleElementLayers; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.utils.datastructures.hints.IHintContext.Key; +import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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( + DefaultTransform.INSTANCE, + DNEdgeInternalSize.INSTANCE, + DNEdgeSceneGraph.INSTANCE, + DNEdgeConnectionHandler.INSTANCE, + SimpleElementLayers.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 Logger LOGGER = LoggerFactory.getLogger(DNEdgeInternalSize.class); + + 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); + if (size == null) + size = new Rectangle2D.Double(); + if (edge != null) + edge.getBounds(size); + else + LOGGER.debug("Element {} does not have edge!", e); + + return size; + } + + } + + static class DNEdgeConnectionHandler implements ConnectionHandler { + + private static final long serialVersionUID = -410377314637446238L; + + public static final DNEdgeConnectionHandler INSTANCE = new DNEdgeConnectionHandler(); + + @Override + public Collection getChildren(IElement connection, Collection result) { + return Collections.emptyList(); + } + + @Override + public Collection getBranchPoints(IElement connection, Collection result) { + return Collections.emptyList(); + } + + @Override + public Collection getSegments(IElement connection, Collection result) { + return Collections.emptyList(); + } + + @Override + public Collection getTerminalConnections(IElement connection, Collection result) { + return Collections.emptyList(); + } + } +} 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 index ff1d844f..63590990 100644 --- 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 @@ -1,62 +1,75 @@ -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); - } -} +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.synchronization.IModifiableSynchronizationContext; +import org.simantics.diagram.synchronization.SynchronizationHints; +import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints; +import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager; +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.DiagramHints; +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; +import org.simantics.g2d.layers.ILayersEditor; + +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); + + IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT); + GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER); + + ILayersEditor layersEditor = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR); + layerManager.loadLayersForElement(graph, layersEditor, element, edgeResource); + + } +} 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 index 339d51d4..3e64bc31 100644 --- 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 @@ -1,78 +1,79 @@ -package org.simantics.district.network.ui.adapters; - -import java.awt.Dimension; -import java.awt.geom.AffineTransform; -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.element.handler.impl.DefaultTransform; -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( - DefaultTransform.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); - - AffineTransform at = ElementUtils.getTransform(edgeElement); - if(at != null) - node.setTransform(at); - } - } - - @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) { - if (size == null) - size = new Rectangle2D.Double(); - size.setFrame(-0.5, -0.5, 1, 1); - return size; - } - - } -} +package org.simantics.district.network.ui.adapters; + +import java.awt.geom.AffineTransform; +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.element.handler.impl.DefaultTransform; +import org.simantics.g2d.element.handler.impl.SimpleElementLayers; +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( + DefaultTransform.INSTANCE, + DNVertexInternalSize.INSTANCE, + DNVertexSceneGraph.INSTANCE, + SimpleElementLayers.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); + + AffineTransform at = ElementUtils.getTransform(edgeElement); + if(at != null) + node.setTransform(at); + } + } + + @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) { + if (size == null) + size = new Rectangle2D.Double(); + size.setFrame(-0.5, -0.5, 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 index c1971646..976abd48 100644 --- 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 @@ -1,52 +1,64 @@ -package org.simantics.district.network.ui.adapters; - -import java.awt.geom.AffineTransform; - -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.ElementUtils; -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); - - // set element transform based on layer SRS and coords - AffineTransform at = new AffineTransform(1, 0, 0, 1, coords[0], coords[1]); - ElementUtils.setTransform(element, at); - } -} +package org.simantics.district.network.ui.adapters; + +import java.awt.geom.AffineTransform; + +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.synchronization.IModifiableSynchronizationContext; +import org.simantics.diagram.synchronization.SynchronizationHints; +import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints; +import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager; +import org.simantics.district.network.ontology.DistrictNetworkResource; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.DiagramHints; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementUtils; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.impl.StaticObjectAdapter; +import org.simantics.g2d.layers.ILayersEditor; + +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); + + // set element transform based on layer SRS and coords + AffineTransform at = new AffineTransform(1, 0, 0, 1, coords[0], coords[1]); + ElementUtils.setTransform(element, at); + + IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT); + GraphLayerManager layerManager = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER); + + ILayersEditor layersEditor = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR); + layerManager.loadLayersForElement(graph, layersEditor, element, vertexResource); + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/JoinVerticesHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/JoinVerticesHandler.java new file mode 100644 index 00000000..9a8c1d25 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/JoinVerticesHandler.java @@ -0,0 +1,60 @@ +package org.simantics.district.network.ui.contributions; + +import java.util.List; + +import javax.inject.Named; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.e4.core.di.annotations.CanExecute; +import org.eclipse.e4.core.di.annotations.Execute; +import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.jface.viewers.ISelection; +import org.simantics.DatabaseJob; +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.db.layer0.SelectionHints; +import org.simantics.district.network.DistrictNetworkUtil; +import org.simantics.district.network.ui.internal.Activator; +import org.simantics.utils.ui.ISelectionUtils; + +public class JoinVerticesHandler { + + @CanExecute + public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) { + List vertices = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class); + return vertices.size() > 1; + } + + @Execute + public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) { + final List vertices = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class); + if (vertices.size() <= 1) + return; + Job job = new DatabaseJob("Join selected vertices") { + + @Override + protected IStatus run(IProgressMonitor monitor) { + try { + Simantics.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + DistrictNetworkUtil.joinVertices(graph, vertices); + } + }); + } catch (DatabaseException e) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e); + } + return Status.OK_STATUS; + } + }; + job.setUser(true); + job.schedule(); + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/RunMappingHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/RunMappingHandler.java new file mode 100644 index 00000000..5ae0c0d0 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/RunMappingHandler.java @@ -0,0 +1,56 @@ +package org.simantics.district.network.ui.contributions; + +import javax.inject.Named; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.e4.core.di.annotations.CanExecute; +import org.eclipse.e4.core.di.annotations.Execute; +import org.eclipse.e4.ui.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.ui.IEditorPart; +import org.simantics.DatabaseJob; +import org.simantics.Simantics; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.district.network.ui.internal.Activator; +import org.simantics.ui.workbench.IResourceEditorInput; +import org.simantics.ui.workbench.e4.E4WorkbenchUtils; + +public class RunMappingHandler { + + @CanExecute + public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) { + IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart); + return activeEditorPart != null; + } + + @Execute + public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) { + Job job = new DatabaseJob("Map network to Apros") { + + @Override + protected IStatus run(IProgressMonitor monitor) { + try { + IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart); + IResourceEditorInput input = (IResourceEditorInput) activeEditorPart.getEditorInput(); + Simantics.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Object result = Simantics.applySCL("Apros/District/DistrictMapping", "mapNetworkToAprosProgress", graph, monitor, input.getResource()); + } + }); + } catch (DatabaseException e) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e); + } + return Status.OK_STATUS; + } + }; + job.setUser(true); + job.schedule(); + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/SetFocusableDynamicMenuContribution.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/SetFocusableDynamicMenuContribution.java new file mode 100644 index 00000000..d2c2ed5c --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/SetFocusableDynamicMenuContribution.java @@ -0,0 +1,93 @@ +package org.simantics.district.network.ui.contributions; + +import java.awt.geom.Point2D; +import java.util.ArrayList; +import java.util.List; + +import javax.inject.Named; + +import org.eclipse.e4.core.contexts.IEclipseContext; +import org.eclipse.e4.ui.di.AboutToHide; +import org.eclipse.e4.ui.di.AboutToShow; +import org.eclipse.e4.ui.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem; +import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement; +import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory; +import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.IEditorPart; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.DiagramHints; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.diagram.handler.PickContext; +import org.simantics.g2d.diagram.handler.PickRequest; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.ElementLayers; +import org.simantics.g2d.layers.ILayer; +import org.simantics.g2d.layers.ILayersEditor; +import org.simantics.ui.workbench.e4.E4WorkbenchUtils; + +public class SetFocusableDynamicMenuContribution { + + public static final String SET_FOCUSABLE_ID = "org.simantics.district.network.ui.setFocusable"; + public static final String FOCUSABLE_ELEMENTS = "org.simantics.district.network.ui.focusableElements"; + + @AboutToShow + public void aboutToShow(IEclipseContext popupContext, @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection, List items, @Named(IServiceConstants.ACTIVE_PART) MPart part) { + + IEditorPart editorPart = E4WorkbenchUtils.getActiveIEditorPart(part); + IDiagram diagram = editorPart.getAdapter(IDiagram.class); + if (diagram == null) + return; + + PickContext pick = diagram.getDiagramClass().getSingleItem(PickContext.class); + if (pick == null) + return; + + ICanvasContext ctx = (ICanvasContext) editorPart.getAdapter(ICanvasContext.class); + if (ctx == null) + return; + + Point2D canvasPosition = ctx.getHintStack().getHint(DiagramHints.POPUP_MENU_CANVAS_POSITION); + if (canvasPosition == null) + return; + + List result = new ArrayList<>(); + + ILayersEditor le = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR); + // Quite hackish solution.. + boolean oldIgnore = le.getIgnoreFocusSettings(); + le.setIgnoreFocusSettings(true); + pick.pick(diagram, new PickRequest(canvasPosition.getX(), canvasPosition.getY()), result); + le.setIgnoreFocusSettings(oldIgnore); + + boolean allow = false; + + for (IElement elem : result) { + ElementLayers el = elem.getElementClass().getAtMostOneItemOfClass(ElementLayers.class); + for (ILayer layer : le.getVisibleLayers()) { + boolean focusable = el.isFocusable(elem, layer); + if (!focusable) + allow = true; + } + } + + MHandledMenuItem dynamicItem = MMenuFactory.INSTANCE.createHandledMenuItem(); + if (allow) + dynamicItem.setLabel("Allow focusing"); + else + dynamicItem.setLabel("Disallow focusing"); + + dynamicItem.setContributorURI("platform:/plugin/org.simantics.district.network.ui"); + dynamicItem.setIconURI("platform:/plugin/org.simantics.modeling.ui/icons/shape_handles.png"); + dynamicItem.setCommand(E4WorkbenchUtils.getMCommandById(SET_FOCUSABLE_ID)); + items.add(dynamicItem); + + popupContext.set(FOCUSABLE_ELEMENTS, result); + } + + @AboutToHide + public void aboutToHide(IEclipseContext popupContext) { + popupContext.remove(FOCUSABLE_ELEMENTS); + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/SetFocusableHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/SetFocusableHandler.java new file mode 100644 index 00000000..b0cfad3c --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/SetFocusableHandler.java @@ -0,0 +1,53 @@ + +package org.simantics.district.network.ui.contributions; + +import java.util.Collection; + +import javax.inject.Named; + +import org.eclipse.e4.core.contexts.IEclipseContext; +import org.eclipse.e4.core.di.annotations.CanExecute; +import org.eclipse.e4.core.di.annotations.Execute; +import org.eclipse.e4.ui.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.IEditorPart; +import org.simantics.g2d.diagram.DiagramHints; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.ElementLayers; +import org.simantics.g2d.layers.ILayer; +import org.simantics.g2d.layers.ILayers; +import org.simantics.ui.workbench.e4.E4WorkbenchUtils; + +public class SetFocusableHandler { + + @CanExecute + public boolean canExecute(IEclipseContext popupContext, @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) { + @SuppressWarnings("unchecked") + Collection elements = (Collection) popupContext.get(SetFocusableDynamicMenuContribution.FOCUSABLE_ELEMENTS); + return !elements.isEmpty(); + } + + @Execute + public void execute(IEclipseContext popupContext, @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection, @Named(IServiceConstants.ACTIVE_PART) MPart activePart) { + IEditorPart editorPart = E4WorkbenchUtils.getActiveIEditorPart(activePart); + + IDiagram diagram = editorPart.getAdapter(IDiagram.class); + if (diagram == null) + return; + + @SuppressWarnings("unchecked") + Collection selectedElements = (Collection) popupContext.get(SetFocusableDynamicMenuContribution.FOCUSABLE_ELEMENTS); + + ILayers le = diagram.getHint(DiagramHints.KEY_LAYERS); + for (IElement elem : selectedElements) { + ElementLayers el = elem.getElementClass().getAtMostOneItemOfClass(ElementLayers.class); + for (ILayer layer : le.getVisibleLayers()) { + boolean focusable = el.isFocusable(elem, layer); + el.setFocusability(elem, layer, !focusable); + } + } + } + +} \ No newline at end of file diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java new file mode 100644 index 00000000..016a1420 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java @@ -0,0 +1,329 @@ +package org.simantics.district.network.ui.function; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Combo; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Group; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.dialogs.SelectionStatusDialog; +import org.simantics.NameLabelUtil; +import org.simantics.Simantics; +import org.simantics.browsing.ui.common.modifiers.EnumeratedValue; +import org.simantics.browsing.ui.common.modifiers.Enumeration; +import org.simantics.browsing.ui.graph.impl.GraphEnumerationModifier; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.IndexRoot; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.RuntimeDatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables.Role; +import org.simantics.db.procedure.Procedure; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.district.network.ontology.DistrictNetworkResource; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.ModelingResources; +import org.simantics.modeling.ModelingUtils; +import org.simantics.modeling.adapters.NewCompositeActionFactory; +import org.simantics.modeling.typicals.TypicalUtil; +import org.simantics.scl.reflection.annotations.SCLValue; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.ui.workbench.action.DefaultActions; +import org.simantics.utils.ui.SWTUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Functions { + + private static final Logger LOGGER = LoggerFactory.getLogger(Functions.class); + + private Functions() { + } + + private static class HasMappingEnumerationModifier extends GraphEnumerationModifier { + + public HasMappingEnumerationModifier(Session session, Resource subject, Resource relation, Enumeration enumeration, Resource value) { + super(session, subject, relation, enumeration, value); + } + + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Object defaultEdgeMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException { + Resource diagram = resolveElement(graph, context); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + return baseMappingModifier(graph, diagram, DN.EdgeDefaultMapping, DN.Mapping_EdgeMapping, context); + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Object defaultVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException { + System.out.println(graph.getURI(resource)); + System.out.println(context.getURI(graph)); + + Resource diagram = resolveElement(graph, context); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + return baseMappingModifier(graph, diagram, DN.VertexDefaultMapping, DN.Mapping_VertexMapping, context); + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Object mappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException { + + Resource element = resolveElement(graph, context); + Resource mappingType = resolveMappingType(graph, element); + return baseMappingModifier(graph, element, DistrictNetworkResource.getInstance(graph).HasMapping, mappingType, context); + } + + public static Map getVertexMappings(ReadGraph graph, Resource resource) throws DatabaseException { + return getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping); + } + + public static Map getEdgeMappings(ReadGraph graph, Resource resource) throws DatabaseException { + return getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping); + } + + public static Map getNetworkMappingsByType(ReadGraph graph, Resource element, Resource mappingType) throws DatabaseException { + Resource indexRoot = graph.sync(new IndexRoot(element)); + List mappings = ModelingUtils.searchByType(graph, indexRoot, mappingType); + Map result = new HashMap<>(mappings.size()); + Layer0 L0 = Layer0.getInstance(graph); + mappings.forEach(mapping -> { + try { + String name = graph.getRelatedValue2(mapping, L0.HasName); + result.put(name, mapping); + } catch (DatabaseException e) { + e.printStackTrace(); + } + }); + return result; + } + + private static Object baseMappingModifier(ReadGraph graph, Resource element, Resource property, Resource mappingType, Variable context) throws DatabaseException { + Resource indexRoot = graph.sync(new IndexRoot(element)); + List mappings = ModelingUtils.searchByType(graph, indexRoot, mappingType); + Enumeration enums = Enumeration + .make(mappings.stream().map(m -> createEnumeratedValue(graph, m)).collect(Collectors.toList())); + + Resource currentMapping = graph.getSingleObject(element, property); + + return new HasMappingEnumerationModifier(Simantics.getSession(), element, property, enums, currentMapping); + } + + private static Resource resolveMappingType(ReadGraph graph, Resource element) throws DatabaseException { + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + if (graph.isInstanceOf(element, DN.Edge)) + return DN.Mapping_EdgeMapping; + else if (graph.isInstanceOf(element, DN.Vertex)) + return DN.Mapping_VertexMapping; + throw new IllegalStateException("No mapping type found for element " + element + " : " + graph.getPossibleURI(element)); + } + + private static Resource resolveElement(ReadGraph graph, Variable variable) throws DatabaseException { + Role role = variable.getPossibleRole(graph); + if (role.equals(Role.PROPERTY)) + return resolveElement(graph, variable.getParent(graph)); + else + return variable.getRepresents(graph); + } + + private static EnumeratedValue createEnumeratedValue(ReadGraph graph, Resource resource) { + try { + String label = NameLabelUtil.modalName(graph, resource); + return new EnumeratedValue(label, resource); + } catch (DatabaseException e) { + throw new RuntimeDatabaseException(e); + } + } + + @SCLValue(type = "ReadGraph -> Resource -> a -> b") + public static Object enumerationValues(ReadGraph graph, Resource resource, Object context) throws DatabaseException { + Variable var = (Variable) context; + System.out.println(graph.getURI(resource)); + System.out.println(var.getURI(graph)); + return Collections.emptyList(); + } + + @SCLValue(type = "ReadGraph -> Resource -> a -> b") + public static Object convertToValue(ReadGraph graph, Resource resource, Object context) throws DatabaseException { + return graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING); +// return null; + } + + + @SCLValue(type = "Resource -> String -> Resource -> Resource") + public static Resource compositeInstantiator(final Resource compositeType, final String defaultName, final Resource target) throws DatabaseException { + + return TypicalUtil.syncExec(procedure -> { + if (!SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> { + try { + queryInitialValuesAndCreateComposite(compositeType, target, defaultName, procedure); + } catch (Throwable t) { + procedure.exception(t); + } + })) { + procedure.execute(null); + } + }); + } + + private static class DefaultMappingsDialog extends SelectionStatusDialog { + + private Combo vertexMappingCombo; + private Combo edgeMappingCombo; + private Combo crsCombo; + private Composite composite; + + private Resource configuration; + private Map vertexMappings = new HashMap<>(); + private Map edgeMappings = new HashMap<>(); + + private Resource defaultVertexMapping; + private Resource defaultEdgeMapping; + + protected DefaultMappingsDialog(Shell parentShell, Resource configuration) { + super(parentShell); + this.configuration = configuration; + setTitle("Select mappings for new DN diagram"); + } + + public Resource getDefaultVertexMapping() { + return defaultVertexMapping; + } + + public Resource getDefaultEdgeMapping() { + return defaultEdgeMapping; + } + + @Override + protected Control createDialogArea(Composite parent) { + composite = (Composite) super.createDialogArea(parent); + + createMappingsGroup(composite); + createCRSSettingsGroup(composite); + + // compute default values + Simantics.getSession().asyncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + + vertexMappings = getVertexMappings(graph, configuration); + edgeMappings = getEdgeMappings(graph, configuration); + + composite.getDisplay().asyncExec(() -> { + + vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()])); + edgeMappingCombo.setItems(edgeMappings.keySet().toArray(new String[edgeMappings.size()])); + vertexMappingCombo.select(0); + edgeMappingCombo.select(0); + }); + + } + }); + return composite; + } + + private void createMappingsGroup(Composite parent) { + Group group= new Group(parent, SWT.NONE); + group.setFont(parent.getFont()); + group.setText("Default mappings"); + GridDataFactory.fillDefaults().grab(true, false).applyTo(group); + group.setLayout(new GridLayout(1, false)); + + Composite cmposite = new Composite(group, SWT.NONE); + cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); + cmposite.setLayout(new GridLayout(2, false)); + + Label vertexMappingLabel = new Label(cmposite, SWT.NONE); + vertexMappingLabel.setText("Default vertex mapping"); + + vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); + GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo); + + Label edgeMappingLabel = new Label(cmposite, SWT.NONE); + edgeMappingLabel.setText("Default edge mapping"); + + edgeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); + GridDataFactory.fillDefaults().grab(true, false).applyTo(edgeMappingCombo); + } + + private void createCRSSettingsGroup(Composite parent) { + Group group= new Group(parent, SWT.NONE); + group.setFont(parent.getFont()); + group.setText("CRS settings"); + GridDataFactory.fillDefaults().grab(true, false).applyTo(group); + group.setLayout(new GridLayout(1, false)); + + Composite cmposite = new Composite(group, SWT.NONE); + cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); + cmposite.setLayout(new GridLayout(2, false)); + + Label vertexMappingLabel = new Label(cmposite, SWT.NONE); + vertexMappingLabel.setText("Default CRS"); + + crsCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); + GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false); + crsCombo.setLayoutData(textData); + } + + + @Override + protected void computeResult() { + defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex())); + defaultEdgeMapping = edgeMappings.get(edgeMappingCombo.getItem(edgeMappingCombo.getSelectionIndex())); + } + + } + + private static void queryInitialValuesAndCreateComposite(final Resource compositeType, final Resource target, + String defaultName, final Procedure procedure) { + DefaultMappingsDialog dialog = new DefaultMappingsDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), target); + + if (dialog.open() != Dialog.OK) { + procedure.execute(null); + return; + } + Simantics.getSession().asyncRequest( + NewCompositeActionFactory.createCompositeRequest(target, defaultName, compositeType), + new Procedure() { + @Override + public void execute(Resource composite) { + Simantics.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + Resource diagram = graph.getSingleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram); + graph.claim(diagram, DN.EdgeDefaultMapping, dialog.getDefaultEdgeMapping()); + graph.claim(diagram, DN.VertexDefaultMapping, dialog.getDefaultVertexMapping()); + } + }); + DefaultActions.asyncPerformDefaultAction(Simantics.getSession(), composite, false, false, true); + procedure.execute(composite); + } + + @Override + public void exception(Throwable t) { + LOGGER.error("Failed to create composite, see exception for details.", t); + procedure.exception(t); + } + }); + } +} 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 index 1e7a4d51..977fe30c 100644 --- 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 @@ -1,18 +1,25 @@ -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 { - - } - -} +package org.simantics.district.network.ui.internal; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; + +public class Activator implements BundleActivator { + + public static final String PLUGIN_ID = "org.simantics.district.network.ui"; + private static BundleContext context; + + @Override + public void start(BundleContext context) throws Exception { + Activator.context = context; + } + + @Override + public void stop(BundleContext context) throws Exception { + Activator.context = null; + } + + public static BundleContext getContext() { + return context; + } + +} 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 index 4abeee8b..408625f5 100644 --- 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 @@ -23,7 +23,7 @@ public class DistrictNetworkEdgeNode extends G2DNode { private DistrictNetworkEdge edge; private Rectangle2D bounds; - private Stroke stroke = new BasicStroke(2); + private static final Stroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); private boolean scaleStroke = true; @Override @@ -42,7 +42,7 @@ public class DistrictNetworkEdgeNode extends G2DNode { } Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Color oldColor = g2d.getColor(); BasicStroke oldStroke = (BasicStroke) g2d.getStroke(); @@ -55,12 +55,12 @@ public class DistrictNetworkEdgeNode extends G2DNode { // } // g2d.setColor(Color.BLUE); - if (stroke != null) { - if (scaleStroke && stroke instanceof BasicStroke) { - BasicStroke bs = GeometryUtils.scaleStroke(stroke, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform()))); + if (STROKE != null) { + if (scaleStroke && STROKE instanceof BasicStroke) { + BasicStroke bs = GeometryUtils.scaleStroke(STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform()))); g2d.setStroke(bs); } else { - g2d.setStroke(stroke); + g2d.setStroke(STROKE); } } // render 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 index a126ee59..8dcd5183 100644 --- 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 @@ -11,21 +11,27 @@ import java.awt.geom.Rectangle2D; import org.simantics.district.network.ui.adapters.DistrictNetworkVertex; import org.simantics.scenegraph.g2d.G2DNode; import org.simantics.scenegraph.utils.GeometryUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DistrictNetworkVertexNode extends G2DNode { + private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class); + private static final long serialVersionUID = -2641639101400236719L; private DistrictNetworkVertex vertex; + + private static final double left = -0.5; + private static final double top = -0.5; + private static final double width = 1; + private static final double height = 1; - private static final double scale = 1; - - private double left = -0.5; - private double top = -0.5; - private double width = 1; - private double height = 1; + private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height); + private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 2, top * 2, width * 2, height * 2); private Stroke stroke = new BasicStroke(2); private boolean scaleStroke = true; + private boolean hover; @Override public void init() { @@ -48,18 +54,24 @@ public class DistrictNetworkVertexNode extends G2DNode { Stroke oldStroke = g2d.getStroke(); g2d.setColor(Color.RED); - if (stroke != null) { - if (scaleStroke && stroke instanceof BasicStroke) { - BasicStroke bs = GeometryUtils.scaleStroke(stroke, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform()))); - g2d.setStroke(bs); - } else { - g2d.setStroke(stroke); - } + + double scaleRecip = 1; + if (scaleStroke) { + double scale = GeometryUtils.getScale(g2d.getTransform()); + + //System.out.println("scale: " + scale); + scaleRecip = 1.0 / scale; } - // render + scaleRecip = 8.0 * scaleRecip; - Rectangle2D.Double rect = new Rectangle2D.Double(left / scale, top / scale, width / scale, height / scale); - g2d.draw(rect); + Rectangle2D toDraw; + if (hover) { + toDraw = new Rectangle2D.Double(HOVERED.getX() * scaleRecip, HOVERED.getY() * scaleRecip, HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip); + } else { + toDraw = new Rectangle2D.Double(NORMAL.getX() * scaleRecip, NORMAL.getY() * scaleRecip, NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip); + } + // render + g2d.fill(toDraw); // Reset stats g2d.setColor(oldColor); @@ -73,11 +85,22 @@ public class DistrictNetworkVertexNode extends G2DNode { @Override public Rectangle2D getBoundsInLocal() { - return new Rectangle2D.Double(left / scale, top / scale, width / scale, height / scale); + return NORMAL; } public void setVertex(DistrictNetworkVertex vertex) { this.vertex = vertex; } + public boolean hover(boolean hover) { +// if (hover && LOGGER.isDebugEnabled()) +// LOGGER.debug("Hovering " + this); + boolean changed = false; + if (this.hover != hover) { + this.hover = hover; + changed = true; + } + return changed; + } + } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java index a9c96e9b..14744234 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java @@ -24,6 +24,7 @@ import org.simantics.g2d.canvas.IToolMode; import org.simantics.g2d.diagram.IDiagram; import org.simantics.scenegraph.g2d.G2DNode; import org.simantics.scenegraph.g2d.events.EventTypes; +import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent; import org.simantics.scenegraph.g2d.events.MouseEvent; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent; @@ -35,6 +36,8 @@ public class NetworkDrawingNode extends G2DNode { private static final long serialVersionUID = -3475301184009620573L; + private Point2D currentMousePos = null; + private List nodes = new ArrayList<>(); private Resource diagramResource; @@ -73,6 +76,8 @@ public class NetworkDrawingNode extends G2DNode { @Override public void render(Graphics2D g2d) { + if (nodes.isEmpty()) + return; Path2D path = new Path2D.Double(); Iterator nodeIter = nodes.iterator(); @@ -84,6 +89,8 @@ public class NetworkDrawingNode extends G2DNode { Point2D node = nodeIter.next(); path.lineTo(node.getX(), node.getY()); } + if (currentMousePos != null) + path.lineTo(currentMousePos.getX(), currentMousePos.getY()); Color old = g2d.getColor(); Stroke oldStroke = g2d.getStroke(); @@ -112,7 +119,7 @@ public class NetworkDrawingNode extends G2DNode { @Override public int getEventMask() { - return EventTypes.MouseMask; + return EventTypes.AnyMask; } @Override @@ -145,6 +152,9 @@ public class NetworkDrawingNode extends G2DNode { } private void createEdge(Point2D start, Point2D end) { + + double padding = GeometryUtils.getScale(getTransform()); + double[] startCoords = new double[] { start.getX(), start.getY() }; double[] endCoords = new double[] { end.getX(), end.getY() }; @@ -153,12 +163,12 @@ public class NetworkDrawingNode extends G2DNode { @Override public void perform(WriteGraph graph) throws DatabaseException { - builder.create(graph, startCoords, endCoords); + builder.create(graph, startCoords, endCoords, padding); } }); } - + @Override protected boolean mouseClicked(MouseClickEvent e) { // check ToolMode @@ -168,9 +178,12 @@ public class NetworkDrawingNode extends G2DNode { committed = false; return false; } - Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double()); - nodes.add(new Point2D.Double(localPos.getX(), localPos.getY())); - + if (e.button == MouseEvent.RIGHT_BUTTON && !nodes.isEmpty()) { + nodes.remove(nodes.size() - 1); + } else if (e.button == MouseEvent.LEFT_BUTTON) { + Point2D localPos = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double()); + nodes.add(new Point2D.Double(localPos.getX(), localPos.getY())); + } repaint(); return true; } @@ -183,6 +196,34 @@ public class NetworkDrawingNode extends G2DNode { @Override protected boolean mouseMoved(MouseMovedEvent e) { + IToolMode mode = getToolMode(); + if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) { + boolean repaint = false; + Point2D p = NodeUtil.worldToLocal(this, e.controlPosition, new Point2D.Double()); + if (participant.pickHoveredElement(p)) { + repaint = true; + } + if (!nodes.isEmpty()) { + currentMousePos = p; + + repaint(); + return true; + } + currentMousePos = null; + if (repaint == true) + repaint(); + } return super.mouseMoved(e); } + + @Override + protected boolean keyPressed(KeyPressedEvent e) { + if (e.keyCode == java.awt.event.KeyEvent.VK_ESCAPE) { + nodes.clear(); + repaint(); + return true; + } + return super.keyPressed(e); + + } } \ No newline at end of file diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DNPointerInteractor.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DNPointerInteractor.java index acb0acac..a1a8ae5a 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DNPointerInteractor.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DNPointerInteractor.java @@ -11,8 +11,10 @@ import org.simantics.g2d.canvas.ICanvasParticipant; import org.simantics.g2d.diagram.handler.PickRequest.PickSorter; import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor; import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo; +import org.simantics.g2d.element.ElementHints; import org.simantics.g2d.element.IElement; -import org.simantics.scenegraph.g2d.G2DNode; +import org.simantics.scenegraph.g2d.IG2DNode; +import org.simantics.scenegraph.g2d.nodes.SingleElementNode; public class DNPointerInteractor extends PointerInteractor { @@ -21,8 +23,8 @@ public class DNPointerInteractor extends PointerInteractor { @Override public void sort(List elements) { Collections.sort(elements, (e1, e2) -> { - G2DNode e1node = getNode(e1); - G2DNode e2node = getNode(e2); + IG2DNode e1node = getNode(e1); + IG2DNode e2node = getNode(e2); if (e1node.getZIndex() < e2node.getZIndex()) return -1; else if (e1node.getZIndex() > e2node.getZIndex()) @@ -31,10 +33,17 @@ public class DNPointerInteractor extends PointerInteractor { }); } - private static G2DNode getNode(IElement element) { - G2DNode node = element.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE); + private static IG2DNode getNode(IElement element) { + IG2DNode node = element.getHint(DistrictNetworkEdgeElement.KEY_DN_EDGE_NODE); if (node == null) node = element.getHint(DistrictNetworkVertexElement.KEY_DN_VERTEX_NODE); + if (node == null) { + node = element.getHint(ElementHints.KEY_SG_NODE); + if (node instanceof SingleElementNode) { + SingleElementNode snode = (SingleElementNode) node; + node = snode.getNodes().iterator().next(); + } + } return node; } } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DNTranslateMode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DNTranslateMode.java index 7d8af3d6..27de17e9 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DNTranslateMode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/participants/DNTranslateMode.java @@ -11,6 +11,7 @@ import org.simantics.db.WriteGraph; import org.simantics.db.common.CommentMetadata; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.elements.ElementTransforms; import org.simantics.diagram.elements.ElementTransforms.TransformedObject; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.district.network.ontology.DistrictNetworkResource; @@ -46,24 +47,26 @@ public class DNTranslateMode extends TranslateMode { Object obj = ElementUtils.getObject(e); if (obj instanceof Resource) { Resource res = (Resource) obj; + AffineTransform at = ElementUtils.getLocalTransform(e, new AffineTransform()); if (graph.isInstanceOf(res, DN.Vertex)) { - AffineTransform at = ElementUtils.getLocalTransform(e, new AffineTransform()); - transformed.add( new TransformedObject((Resource) obj, at) ); - - // write to db double[] coords = new double[] { at.getTranslateX(), at.getTranslateY() }; graph.claimLiteral(res, DIA.HasLocation, coords); + } else { + // fallback to normal HasTransforms + transformed.add( new TransformedObject((Resource) obj, at) ); } } } - if (!transformed.isEmpty()) { + graph.markUndoPoint(); + // Normal transforms + ElementTransforms.setTransformRequest(transformed).perform(graph); + CommentMetadata cm = graph.getMetadata(CommentMetadata.class); graph.addMetadata(cm.add("Translated " + transformed.size() + " " + (transformed.size() == 1 ? "element" : "elements") + " by (" + dx + "," + dy + ") mm.")); - graph.markUndoPoint(); } } }); diff --git a/org.simantics.district.network/META-INF/MANIFEST.MF b/org.simantics.district.network/META-INF/MANIFEST.MF index 03d4574c..2aaaa26e 100644 --- a/org.simantics.district.network/META-INF/MANIFEST.MF +++ b/org.simantics.district.network/META-INF/MANIFEST.MF @@ -8,5 +8,6 @@ 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 + org.simantics.db.common, + org.simantics.db.layer0 Export-Package: org.simantics.district.network 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 index 25503407..9bdec1b5 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java +++ b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java @@ -1,36 +1,64 @@ -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); - - 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; - } -} +package org.simantics.district.network; + +import java.util.Collection; +import java.util.Iterator; + +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.db.layer0.util.RemoverUtil; +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); + + Resource edge = graph.newResource(); + graph.claim(edge, L0.InstanceOf, DN.Edge); + OrderedSetUtils.add(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.add(graph, composite, vertex); + graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex); + + return vertex; + } + + public static Resource joinVertices(WriteGraph graph, Collection vertices) throws DatabaseException { + if (vertices.isEmpty()) + throw new IllegalArgumentException("vertices-collection should not be empty for joining vertices!"); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + Iterator verticeIterator = vertices.iterator(); + Resource master = verticeIterator.next(); + while (verticeIterator.hasNext()) { + Resource slave = verticeIterator.next(); + Resource composite = graph.getSingleObject(slave, Layer0.getInstance(graph).PartOf); + Collection startVertexEdges = graph.getObjects(slave, DN.HasStartVertex_Inverse); + for (Resource startVertexEdge : startVertexEdges) { + graph.deny(startVertexEdge, DN.HasStartVertex); + graph.claim(startVertexEdge, DN.HasStartVertex, master); + } + Collection endVertexEdges = graph.getObjects(slave, DN.HasEndVertex_Inverse); + for (Resource endVertexEdge : endVertexEdges) { + graph.deny(endVertexEdge, DN.HasEndVertex); + graph.claim(endVertexEdge, DN.HasEndVertex, master); + } + OrderedSetUtils.remove(graph, composite, slave); + } + return master; + } +}