]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Change map background color in network diagram 23/2523/2
authorjsimomaa <jani.simomaa@gmail.com>
Wed, 5 Dec 2018 09:32:00 +0000 (11:32 +0200)
committerJani Simomaa <jani.simomaa@semantum.fi>
Wed, 5 Dec 2018 09:32:17 +0000 (09:32 +0000)
gitlab #20

Change-Id: I4d81104cf61a99aeb4a02c9ade40337d253c6404

org.simantics.district.network.ontology/META-INF/MANIFEST.MF
org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph
org.simantics.district.network.ontology/graph/DistrictNetworkDiagramSettings.pgraph
org.simantics.district.network.ontology/src/org/simantics/district/network/ontology/DistrictNetworkResource.java
org.simantics.district.network.ui/fragment.e4xmi
org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeMapBackgroundColorHandler.java [new file with mode: 0644]
org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeMappingTypeHandler.java
org.simantics.district.network/META-INF/MANIFEST.MF
org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java

index a4cb4e141a7edd2a477b148a2b85e0c2a69942e6..9653d5749b02fdbc4768d5d062d3391fa689d56c 100644 (file)
@@ -1,6 +1,6 @@
 Manifest-Version: 1.0
 Bundle-ManifestVersion: 2
-Bundle-Name: Simantics District Network Ontology
+Bundle-Name: http://www.simantics.org/DistrictNetwork-1.0
 Bundle-SymbolicName: org.simantics.district.network.ontology
 Bundle-Version: 1.0.0.qualifier
 Require-Bundle: org.simantics.layer0,
@@ -12,6 +12,7 @@ Require-Bundle: org.simantics.layer0,
  org.simantics.selectionview.ontology;bundle-version="1.2.0",
  org.simantics.selectionview.ui.ontology;bundle-version="1.1.0",
  org.simantics.g2d.ontology;bundle-version="1.1.0",
- org.simantics.layer0x.ontology;bundle-version="1.1.0"
+ org.simantics.layer0x.ontology;bundle-version="1.1.0",
+ org.simantics.datatypes.ontology;bundle-version="1.1.0"
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Export-Package: org.simantics.district.network.ontology
index 64c1b6cd8328f9ad63218bb7a28ef2d953c8b10b..c5195291285a76b7c0d4aaed51e452090dd69637 100644 (file)
@@ -154,6 +154,14 @@ DN.Diagram.MappedDiagram <R L0.IsWeaklyRelatedTo
 DN.MappedComponent <R L0.IsWeaklyRelatedTo
     L0.InverseOf DN.MappedFromElement <R L0.IsWeaklyRelatedTo
 
+DN.Diagram.trackChangesEnabled <R L0.HasProperty : SEL.GenericParameterType
+    L0.HasRange L0.Boolean
+    L0.HasLabel "Track changes"
+
+DN.Diagram.splitToMultipleEnabled <R L0.HasProperty : SEL.GenericParameterType
+    L0.HasRange L0.Boolean
+    L0.HasLabel "Split To Multiple Diagrams"
+
 DN.AddLayerToDNDiagramTemplate <T L0.Template
 
 DN.EPSG_4326 : DN.SpatialRefSystem
@@ -253,3 +261,25 @@ DN.Mapping.EdgeMapping <T DN.Mapping.Base
     >-- DN.Mapping.EdgeMapping.KSupplyAttribute --> L0.String <R L0.HasProperty
     >-- DN.Mapping.EdgeMapping.TGroundAttribute --> L0.String <R L0.HasProperty
     >-- DN.Mapping.EdgeMapping.ElevationAttribute --> L0.String <R L0.HasProperty
+
+
+// Allowed connection types
+DN.SupplyConnectionType <T STR.ConnectionType
+
+DN.SupplyInConnectionType : DN.SupplyConnectionType : STR.InputDirection
+    STR.SupportsDirection DN.SupplyOutConnectionType
+    STR.ConnectionType.isUserSelectable true
+
+DN.SupplyOutConnectionType : DN.SupplyConnectionType : STR.OutputDirection
+    STR.SupportsDirection DN.SupplyInConnectionType
+    STR.ConnectionType.isUserSelectable true
+
+DN.ReturnConnectionType <T STR.ConnectionType
+
+DN.ReturnInConnectionType : DN.ReturnConnectionType : STR.InputDirection
+    STR.SupportsDirection DN.ReturnOutConnectionType
+    STR.ConnectionType.isUserSelectable true
+
+DN.ReturnOutConnectionType : DN.ReturnConnectionType : STR.OutputDirection
+    STR.SupportsDirection DN.ReturnInConnectionType
+    STR.ConnectionType.isUserSelectable true
index eb2416e96f6589f08cd5c1077ca3b9e8de71c7cd..334adbedd9f0461debeae348c0b3d517703afe69 100644 (file)
@@ -9,6 +9,7 @@ SEL = <http://www.simantics.org/SelectionView-1.2>
 SEL_UI = <http://www.simantics.org/SelectionViewUI-1.1>
 G2D = <http://www.simantics.org/G2D-1.1>
 DN = <http://www.simantics.org/DistrictNetwork-1.0>
+DATATYPES = <http://www.simantics.org/Datatypes-1.1>
 
 // ----------------------------------------------------------------------------
 
@@ -78,7 +79,7 @@ DN.Diagram
         L0.HasLabel "Node Scale Bias"
     >-- DN.Diagram.nodeScaleProperty --> DN.Vertex.ScaleProperty <R L0.HasProperty : DN.VertexScalePropertyParameterType
         L0.HasLabel "Node Scale Property"
-    >-- DN.Diagram.backgroundColor ==> "String" <R L0.HasProperty : SEL.GenericParameterType
+    >-- DN.Diagram.backgroundColor --> DATATYPES.RGB.Integer <R L0.HasProperty : SEL.ColorParameterType
         L0.HasLabel "Diagram Background Color"
     >-- DN.Diagram.drawMapEnabled ==> "Boolean" <R L0.HasProperty : SEL.GenericParameterType
         L0.HasLabel "Draw Map"
index 6f9c6122d9f408d5c8f2b8b39abee7261e5477d7..9ef2ef02b2b4ab789ac215102cc26b4a3065ede2 100644 (file)
@@ -43,6 +43,10 @@ public class DistrictNetworkResource {
     public final Resource Diagram_nodeScaleGain_Inverse;
     public final Resource Diagram_nodeScaleProperty;
     public final Resource Diagram_nodeScaleProperty_Inverse;
+    public final Resource Diagram_splitToMultipleEnabled;
+    public final Resource Diagram_splitToMultipleEnabled_Inverse;
+    public final Resource Diagram_trackChangesEnabled;
+    public final Resource Diagram_trackChangesEnabled_Inverse;
     public final Resource DistrictNodeGroup;
     public final Resource DistrictNodeGroup_hasComponentTypeName;
     public final Resource DistrictNodeGroup_hasComponentTypeName_Inverse;
@@ -211,8 +215,14 @@ public class DistrictNetworkResource {
     public final Resource Mapping_VertexMapping_dpAttribute_Inverse;
     public final Resource Mapping_VertexMapping_dtAttribute;
     public final Resource Mapping_VertexMapping_dtAttribute_Inverse;
+    public final Resource ReturnConnectionType;
+    public final Resource ReturnInConnectionType;
+    public final Resource ReturnOutConnectionType;
     public final Resource SCLMain;
     public final Resource SpatialRefSystem;
+    public final Resource SupplyConnectionType;
+    public final Resource SupplyInConnectionType;
+    public final Resource SupplyOutConnectionType;
     public final Resource Vertex;
     public final Resource VertexDefaultMapping;
     public final Resource VertexDefaultMapping_Inverse;
@@ -304,6 +314,10 @@ public class DistrictNetworkResource {
         public static final String Diagram_nodeScaleGain_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/nodeScaleGain/Inverse";
         public static final String Diagram_nodeScaleProperty = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/nodeScaleProperty";
         public static final String Diagram_nodeScaleProperty_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/nodeScaleProperty/Inverse";
+        public static final String Diagram_splitToMultipleEnabled = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/splitToMultipleEnabled";
+        public static final String Diagram_splitToMultipleEnabled_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/splitToMultipleEnabled/Inverse";
+        public static final String Diagram_trackChangesEnabled = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/trackChangesEnabled";
+        public static final String Diagram_trackChangesEnabled_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Diagram/trackChangesEnabled/Inverse";
         public static final String DistrictNodeGroup = "http://www.simantics.org/DistrictNetwork-1.0/DistrictNodeGroup";
         public static final String DistrictNodeGroup_hasComponentTypeName = "http://www.simantics.org/DistrictNetwork-1.0/DistrictNodeGroup/hasComponentTypeName";
         public static final String DistrictNodeGroup_hasComponentTypeName_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/DistrictNodeGroup/hasComponentTypeName/Inverse";
@@ -472,8 +486,14 @@ public class DistrictNetworkResource {
         public static final String Mapping_VertexMapping_dpAttribute_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/VertexMapping/dpAttribute/Inverse";
         public static final String Mapping_VertexMapping_dtAttribute = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/VertexMapping/dtAttribute";
         public static final String Mapping_VertexMapping_dtAttribute_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Mapping/VertexMapping/dtAttribute/Inverse";
+        public static final String ReturnConnectionType = "http://www.simantics.org/DistrictNetwork-1.0/ReturnConnectionType";
+        public static final String ReturnInConnectionType = "http://www.simantics.org/DistrictNetwork-1.0/ReturnInConnectionType";
+        public static final String ReturnOutConnectionType = "http://www.simantics.org/DistrictNetwork-1.0/ReturnOutConnectionType";
         public static final String SCLMain = "http://www.simantics.org/DistrictNetwork-1.0/SCLMain";
         public static final String SpatialRefSystem = "http://www.simantics.org/DistrictNetwork-1.0/SpatialRefSystem";
+        public static final String SupplyConnectionType = "http://www.simantics.org/DistrictNetwork-1.0/SupplyConnectionType";
+        public static final String SupplyInConnectionType = "http://www.simantics.org/DistrictNetwork-1.0/SupplyInConnectionType";
+        public static final String SupplyOutConnectionType = "http://www.simantics.org/DistrictNetwork-1.0/SupplyOutConnectionType";
         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";
@@ -575,6 +595,10 @@ public class DistrictNetworkResource {
         Diagram_nodeScaleGain_Inverse = getResourceOrNull(graph, URIs.Diagram_nodeScaleGain_Inverse);
         Diagram_nodeScaleProperty = getResourceOrNull(graph, URIs.Diagram_nodeScaleProperty);
         Diagram_nodeScaleProperty_Inverse = getResourceOrNull(graph, URIs.Diagram_nodeScaleProperty_Inverse);
+        Diagram_splitToMultipleEnabled = getResourceOrNull(graph, URIs.Diagram_splitToMultipleEnabled);
+        Diagram_splitToMultipleEnabled_Inverse = getResourceOrNull(graph, URIs.Diagram_splitToMultipleEnabled_Inverse);
+        Diagram_trackChangesEnabled = getResourceOrNull(graph, URIs.Diagram_trackChangesEnabled);
+        Diagram_trackChangesEnabled_Inverse = getResourceOrNull(graph, URIs.Diagram_trackChangesEnabled_Inverse);
         DistrictNodeGroup = getResourceOrNull(graph, URIs.DistrictNodeGroup);
         DistrictNodeGroup_hasComponentTypeName = getResourceOrNull(graph, URIs.DistrictNodeGroup_hasComponentTypeName);
         DistrictNodeGroup_hasComponentTypeName_Inverse = getResourceOrNull(graph, URIs.DistrictNodeGroup_hasComponentTypeName_Inverse);
@@ -743,8 +767,14 @@ public class DistrictNetworkResource {
         Mapping_VertexMapping_dpAttribute_Inverse = getResourceOrNull(graph, URIs.Mapping_VertexMapping_dpAttribute_Inverse);
         Mapping_VertexMapping_dtAttribute = getResourceOrNull(graph, URIs.Mapping_VertexMapping_dtAttribute);
         Mapping_VertexMapping_dtAttribute_Inverse = getResourceOrNull(graph, URIs.Mapping_VertexMapping_dtAttribute_Inverse);
+        ReturnConnectionType = getResourceOrNull(graph, URIs.ReturnConnectionType);
+        ReturnInConnectionType = getResourceOrNull(graph, URIs.ReturnInConnectionType);
+        ReturnOutConnectionType = getResourceOrNull(graph, URIs.ReturnOutConnectionType);
         SCLMain = getResourceOrNull(graph, URIs.SCLMain);
         SpatialRefSystem = getResourceOrNull(graph, URIs.SpatialRefSystem);
+        SupplyConnectionType = getResourceOrNull(graph, URIs.SupplyConnectionType);
+        SupplyInConnectionType = getResourceOrNull(graph, URIs.SupplyInConnectionType);
+        SupplyOutConnectionType = getResourceOrNull(graph, URIs.SupplyOutConnectionType);
         Vertex = getResourceOrNull(graph, URIs.Vertex);
         VertexDefaultMapping = getResourceOrNull(graph, URIs.VertexDefaultMapping);
         VertexDefaultMapping_Inverse = getResourceOrNull(graph, URIs.VertexDefaultMapping_Inverse);
index be0b7a40b5970567afbb3cc6853db14c36f37dc2..f51696181c150b759b72da8d609c655c744174cc 100644 (file)
@@ -6,6 +6,7 @@
     <elements xsi:type="commands:Command" xmi:id="_QzdUQOIcEeaJP6VyXP72Jw" elementId="org.simantics.district.network.ui.setFocusable" commandName="Set Focusable"/>
     <elements xsi:type="commands:Command" xmi:id="_4wJtADWxEeeVXaXEq4_qpA" elementId="org.simantics.district.network.ui.command.changemapping" commandName="Change Mapping"/>
     <elements xsi:type="commands:Command" xmi:id="_qillYOGQEeiIMuq1qhQJRQ" elementId="org.simantics.district.network.ui.command.toggledrawmap" commandName="Toggle Draw Map"/>
+    <elements xsi:type="commands:Command" xmi:id="_Uv6NEOJdEei2MexsDMErvQ" elementId="org.simantics.district.network.ui.command.changemapbackgroundcolor" commandName="Change Map Background Color"/>
   </fragments>
   <fragments xsi:type="fragment:StringModelFragment" xmi:id="_Js7rUMMAEea1mdgpHNVHMA" featurename="menuContributions" parentElementId="xpath:/">
     <elements xsi:type="menu:MenuContribution" xmi:id="_T-jiEN8NEeaigNyzMJBOrg" elementId="org.simantics.district.network.ui.menucontribution.districtDiagramPopup" parentId="#DistrictDiagramPopup">
@@ -14,6 +15,7 @@
       <children xsi:type="menu:DynamicMenuContribution" xmi:id="_dieAsOIgEeaJP6VyXP72Jw" elementId="org.simantics.district.network.ui.dynamicmenucontribution.setFocusable" label="Set Focusable" contributionURI="bundleclass://org.simantics.district.network.ui/org.simantics.district.network.ui.contributions.SetFocusableDynamicMenuContribution"/>
       <children xsi:type="menu:HandledMenuItem" xmi:id="_QbF4UDWyEeeVXaXEq4_qpA" elementId="org.simantics.district.network.ui.handledmenuitem.changemapping" label="Change Mapping" iconURI="platform:/plugin/com.famfamfam.silk/icons/arrow_switch.png" tooltip="Change mapping of multiple elements" command="_4wJtADWxEeeVXaXEq4_qpA"/>
       <children xsi:type="menu:HandledMenuItem" xmi:id="_ZE-9sOGeEei2MexsDMErvQ" elementId="org.simantics.district.network.ui.handledmenuitem.toggledrawmap" label="Toggle Draw Map" iconURI="platform:/plugin/com.famfamfam.silk/icons/map_add.png" command="_qillYOGQEeiIMuq1qhQJRQ"/>
+      <children xsi:type="menu:HandledMenuItem" xmi:id="_bd3S0OJdEei2MexsDMErvQ" elementId="org.simantics.district.network.ui.handledmenuitem.changebackgroundcolor" label="Change Background Color" iconURI="platform:/plugin/com.famfamfam.silk/icons/palette.png" command="_Uv6NEOJdEei2MexsDMErvQ"/>
     </elements>
   </fragments>
   <fragments xsi:type="fragment:StringModelFragment" xmi:id="_dbiHcMMBEea1mdgpHNVHMA" featurename="handlers" parentElementId="xpath:/">
@@ -21,5 +23,6 @@
     <elements xsi:type="commands:Handler" xmi:id="_VPgBwOIcEeaJP6VyXP72Jw" elementId="org.simantics.district.network.ui.handler.setFocusable" contributionURI="bundleclass://org.simantics.district.network.ui/org.simantics.district.network.ui.contributions.SetFocusableHandler" command="_QzdUQOIcEeaJP6VyXP72Jw"/>
     <elements xsi:type="commands:Handler" xmi:id="_LojWYDWyEeeVXaXEq4_qpA" elementId="org.simantics.district.network.ui.handler.0" contributionURI="bundleclass://org.simantics.district.network.ui/org.simantics.district.network.ui.contributions.ChangeMappingTypeHandler" command="_4wJtADWxEeeVXaXEq4_qpA"/>
     <elements xsi:type="commands:Handler" xmi:id="_ubB2kOGQEeiIMuq1qhQJRQ" elementId="org.simantics.district.network.ui.handler.1" contributionURI="bundleclass://org.simantics.district.network.ui/org.simantics.district.network.ui.contributions.DrawMapToggleHandler" command="_qillYOGQEeiIMuq1qhQJRQ"/>
+    <elements xsi:type="commands:Handler" xmi:id="_WmQDoOJdEei2MexsDMErvQ" elementId="org.simantics.district.network.ui.handler.2" contributionURI="bundleclass://org.simantics.district.network.ui/org.simantics.district.network.ui.contributions.ChangeMapBackgroundColorHandler" command="_Uv6NEOJdEei2MexsDMErvQ"/>
   </fragments>
 </fragment:ModelFragments>
diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeMapBackgroundColorHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeMapBackgroundColorHandler.java
new file mode 100644 (file)
index 0000000..d52124c
--- /dev/null
@@ -0,0 +1,74 @@
+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.dialogs.Dialog;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.ui.progress.UIJob;
+import org.simantics.DatabaseJob;
+import org.simantics.Simantics;
+import org.simantics.datatypes.literal.RGB;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.db.request.Write;
+import org.simantics.district.network.DistrictNetworkUtil;
+import org.simantics.district.network.ui.internal.Activator;
+import org.simantics.utils.ui.ISelectionUtils;
+import org.simantics.utils.ui.color.Color;
+import org.simantics.utils.ui.workbench.dialogs.ColorDialog;
+import org.slf4j.LoggerFactory;
+
+public class ChangeMapBackgroundColorHandler {
+
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ChangeMapBackgroundColorHandler.class);
+
+    @CanExecute
+    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
+        final List<Resource> diagrams = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
+        return diagrams.size() > 0;
+    }
+
+    @Execute
+    public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
+        final List<Resource> diagrams = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
+        Job uijob = new UIJob("Change Map Background Color") {
+            
+            @Override
+            public IStatus runInUIThread(IProgressMonitor monitor) {
+                ColorDialog dialog = new ColorDialog(getDisplay().getActiveShell());
+                if (dialog.open() == Dialog.OK) {
+                    Color c = dialog.getColor();
+                    
+                    LOGGER.info("Found color {}", c);
+                    Job job = new DatabaseJob("Toggle draw map") {
+
+                        @Override
+                        protected IStatus run(IProgressMonitor monitor) {
+                            try {
+                                Simantics.getSession().syncRequest((Write) graph -> DistrictNetworkUtil.changeMapBackgroundColor(graph, diagrams.get(0), new RGB.Integer(c.getR(), c.getG(), c.getB())));
+                            } catch (DatabaseException e) {
+                                return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);
+                            }
+                            return Status.OK_STATUS;
+                        }
+                    };
+                    job.setUser(true);
+                    job.schedule();
+                }
+                return Status.OK_STATUS;
+            }
+        };
+        uijob.setUser(true);
+        uijob.schedule();
+    }
+}
index 2f3aae4568b8b0bf236a72e96dbc27ca703a67c5..2b3d1888a41b2a0218221444ac8fbf5536f02ce2 100644 (file)
@@ -1,8 +1,12 @@
 package org.simantics.district.network.ui.contributions;
 
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
 
 import javax.inject.Named;
 
@@ -25,110 +29,172 @@ 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.eclipse.ui.progress.UIJob;
 import org.simantics.DatabaseJob;
 import org.simantics.Simantics;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
+import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.request.IndexRoot;
 import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.UniqueRead;
 import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ServiceException;
+import org.simantics.db.exception.ValidationException;
 import org.simantics.db.layer0.SelectionHints;
-import org.simantics.db.request.Read;
+import org.simantics.db.procedure.Procedure;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.district.network.ui.function.Functions;
 import org.simantics.district.network.ui.internal.Activator;
-import org.simantics.layer0.Layer0;
 import org.simantics.utils.ui.ISelectionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class ChangeMappingTypeHandler {
 
-    
+    private static final Logger LOGGER = LoggerFactory.getLogger(ChangeMappingTypeHandler.class);
+
     @CanExecute
     public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
-        List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
-        if (elements.size() < 1)
-            return false;
-        try {
-            return Simantics.getSession().syncRequest(new Read<Boolean>() {
-
-                @Override
-                public Boolean perform(ReadGraph graph) throws DatabaseException {
-                    Layer0 L0 = Layer0.getInstance(graph);
-                    Resource instanceOf = null;
-                    for (Resource element : elements) {
-                        if (instanceOf == null) {
-                            instanceOf = graph.getSingleObject(element, L0.InstanceOf);
-                        } else {
-                            Resource currentInstanceOf = graph.getSingleObject(element, L0.InstanceOf);
-                            if (!currentInstanceOf.equals(instanceOf)) {
-                                return false;
-                            }
-                        }
-                    }
-                    return true;
-                }
-            });
-        } catch (DatabaseException e) {
-            e.printStackTrace();
-            return false;
-        }
+//        List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
+//        if (elements.size() < 1)
+//            return false;
+//        try {
+//            return Simantics.getSession().syncRequest(new Read<Boolean>() {
+//
+//                @Override
+//                public Boolean perform(ReadGraph graph) throws DatabaseException {
+//                    Layer0 L0 = Layer0.getInstance(graph);
+//                    Resource instanceOf = null;
+//                    for (Resource element : elements) {
+//                        if (instanceOf == null) {
+//                            instanceOf = graph.getSingleObject(element, L0.InstanceOf);
+//                        } else {
+//                            Resource currentInstanceOf = graph.getSingleObject(element, L0.InstanceOf);
+//                            if (!currentInstanceOf.equals(instanceOf)) {
+//                                return false;
+//                            }
+//                        }
+//                    }
+//                    return true;
+//                }
+//            });
+//        } catch (DatabaseException e) {
+//            e.printStackTrace();
+//            return false;
+//        }
+        return true;
     }
     
     @Execute
     public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
         final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
-        if (elements.size() < 1)
-            return;
-        
-        SelectMappingDialog dialog = new SelectMappingDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), elements);
-        if (dialog.open() != Dialog.OK)
-            return;
+//        if (elements.size() < 1)
+//            return;
         
-        Resource mapping = dialog.getDefaultVertexMapping();
-        Job job = new DatabaseJob("Join selected vertices") {
+        CompletableFuture<Map<NamedResource, Collection<NamedResource>>> result = new CompletableFuture<>();
+        Simantics.getSession().asyncRequest(new UniqueRead<Map<NamedResource, Collection<NamedResource>>>() {
             
             @Override
-            protected IStatus run(IProgressMonitor monitor) {
-                try {
-                    Simantics.getSession().syncRequest(new WriteRequest() {
-                        
-                        @Override
-                        public void perform(WriteGraph graph) throws DatabaseException {
-                            DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
-                            for (Resource element : elements) {
-                                graph.deny(element, DN.HasMapping);
-                                graph.claim(element, DN.HasMapping, mapping);
-                            }
+            public Map<NamedResource, Collection<NamedResource>> perform(ReadGraph graph) throws DatabaseException {
+                DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                
+                Map<NamedResource, Collection<NamedResource>> currents = new HashMap<>();
+                for (Resource element : elements) {
+                    Resource currentMapping = graph.getPossibleObject(element, DN.HasMapping);
+                    NamedResource mapping = new NamedResource(NameUtils.getSafeName(graph, currentMapping), currentMapping);
+                    currents.compute(mapping, (t, u) -> {
+                        if (u == null)
+                            u = new HashSet<>();
+                        try {
+                            u.add(new NamedResource(NameUtils.getSafeName(graph, element), element));
+                        } catch (ValidationException | ServiceException e) {
+                            LOGGER.error("Could not compute name for {}", element, e);
                         }
+                        return u;
                     });
-                } catch (DatabaseException e) {
-                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);
                 }
+                return currents;
+            }
+        }, new Procedure<Map<NamedResource, Collection<NamedResource>>>() {
+
+            @Override
+            public void execute(Map<NamedResource, Collection<NamedResource>> results) {
+                result.complete(results);
+            }
+
+            @Override
+            public void exception(Throwable t) {
+                LOGGER.error("Could not compute mappings for selection {}", elements, t);
+                result.completeExceptionally(t);
+            }
+        });
+        
+        UIJob uiJob = new UIJob("Change mappings..") {
+            
+            @Override
+            public IStatus runInUIThread(IProgressMonitor monitor) {
+                SelectMappingDialog dialog = new SelectMappingDialog(getDisplay().getActiveShell(), result);
+                if (dialog.open() != Dialog.OK)
+                    return Status.OK_STATUS;
+                
+                Map<Resource, Collection<NamedResource>> results = dialog.getResults();
+                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 {
+                                    DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                                    
+                                    for (Map.Entry<Resource, Collection<NamedResource>> entry : results.entrySet()) {
+                                        Resource newMapping = entry.getKey();
+                                        Collection<NamedResource> elements = entry.getValue();
+                                        for (NamedResource element : elements) {
+                                            graph.deny(element.getResource(), DN.HasMapping);
+                                            graph.claim(element.getResource(), DN.HasMapping, newMapping);
+                                        }
+                                    }
+                                }
+                            });
+                        } catch (DatabaseException e) {
+                            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);
+                        }
+                        return Status.OK_STATUS;
+                    }
+                };
+                job.setUser(true);
+                job.schedule();
                 return Status.OK_STATUS;
             }
         };
-        job.setUser(true);
-        job.schedule();
+        uiJob.setUser(true);
+        uiJob.schedule();
     }
     
     private static class SelectMappingDialog extends SelectionStatusDialog {
 
-        private Combo vertexMappingCombo;
+        private Map<NamedResource, Combo> mappingCombos = new HashMap<>();
         
         private Composite composite;
         
-        private List<Resource> elements;
-        private Map<String, Resource> vertexMappings = new HashMap<>();
+        private CompletableFuture<Map<NamedResource, Collection<NamedResource>>> elements;
+        
+        private Map<NamedResource, Map<String, Resource>> possibleMappings = new HashMap<>();
         
         private Resource defaultVertexMapping;
 
-        protected SelectMappingDialog(Shell parentShell, List<Resource> elements) {
+        protected SelectMappingDialog(Shell parentShell, CompletableFuture<Map<NamedResource, Collection<NamedResource>>> elements) {
             super(parentShell);
             this.elements = elements;
-            setTitle("Select mappings for new DN diagram");
+            setTitle("Change mappings");
         }
 
         public Resource getDefaultVertexMapping() {
@@ -147,44 +213,87 @@ public class ChangeMappingTypeHandler {
                 @Override
                 public void run(ReadGraph graph) throws DatabaseException {
                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
-                    Resource element = elements.get(0);
-                    if (graph.isInstanceOf(element, DN.Vertex)) {
-                        vertexMappings = Functions.getVertexMappings(graph, element);
-                    } else if (graph.isInstanceOf(element, DN.Edge)) {
-                        vertexMappings = Functions.getEdgeMappings(graph, element);
+                    try {
+                        for (Map.Entry<NamedResource, Collection<NamedResource>> entry : elements.get().entrySet()) {
+                            NamedResource currentMapping = entry.getKey();
+                            Resource resource = entry.getValue().iterator().next().getResource();
+                            Resource indexRoot = graph.sync(new IndexRoot(resource));
+                            if (graph.isInstanceOf(currentMapping.getResource(), DN.Mapping_VertexMapping)) {
+                                possibleMappings.put(currentMapping, Functions.getVertexMappings(graph, indexRoot));
+                            } else if (graph.isInstanceOf(currentMapping.getResource(), DN.Mapping_EdgeMapping)) {
+                                possibleMappings.put(currentMapping, Functions.getEdgeMappings(graph, indexRoot));
+                            }
+                        }
+                    } catch (InterruptedException | ExecutionException e) {
+                        e.printStackTrace();
                     }
                     composite.getDisplay().asyncExec(() -> {
-                        vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
-                        vertexMappingCombo.select(0);
-                    }); 
-                    
+                        for (Map.Entry<NamedResource, Map<String, Resource>> entry : possibleMappings.entrySet()) {
+                            NamedResource key = entry.getKey();
+                            Map<String, Resource> value = entry.getValue();
+                            Combo combo = mappingCombos.get(key);
+                            combo.setItems(value.keySet().toArray(new String[value.size()]));
+                            combo.select(0);
+                        }
+                    });
                 }
             });
             return composite;
         }
 
+        private Map<Resource, Collection<NamedResource>> results = new HashMap<>();
+        
         @Override
         protected void computeResult() {
-            defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex()));
+            for (Map.Entry<NamedResource, Combo> combos : mappingCombos.entrySet()) {
+                NamedResource resource = combos.getKey();
+                Combo c = combos.getValue();
+                String item = c.getItem(c.getSelectionIndex());
+                
+                Map<String, Resource> map = possibleMappings.get(resource);
+                Resource newMapping = map.get(item);
+                results.compute(newMapping, (t, u) -> {
+                    if (u == null) {
+                        u = new HashSet<>();
+                    }
+                    u.add(resource);
+                    return u;
+                });
+            }
         }
-        
+
+        public Map<Resource, Collection<NamedResource>> getResults() {
+            return results;
+        }
+
         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");
+            try {
+                for (Map.Entry<NamedResource, Collection<NamedResource>> entry : elements.get().entrySet()) {
+                    
+                    NamedResource currentMapping = entry.getKey();
+                    
+                    Collection<NamedResource> mappedElements = entry.getValue();
+                    
+                    Group group= new Group(parent, SWT.NONE);
+                    group.setFont(parent.getFont());
+                    group.setText(currentMapping.getName() + " currently mapped to " + mappedElements.size() + " elements");
+                    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("New mapping type");
 
-            vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
-            GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
-            
+                    Combo c = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
+                    GridDataFactory.fillDefaults().grab(true, false).applyTo(c);
+                    mappingCombos.put(entry.getKey(), c);
+                }
+            } catch (InterruptedException | ExecutionException e) {
+                e.printStackTrace();
+            }
         }
     }
 }
index 8fb9329ffa5cf4dcde370ce991237ca476a56a9c..58fd3ab49511a1dadf7bfa2d4f4ff463f92a06b9 100644 (file)
@@ -14,6 +14,8 @@ Require-Bundle: org.simantics.db,
  org.simantics.district.geotools;bundle-version="1.0.0",
  org.simantics.diagram,
  org.simantics.scenegraph.profile;bundle-version="1.0.0",
- org.simantics;bundle-version="1.0.0"
+ org.simantics;bundle-version="1.0.0",
+ org.slf4j.api
 Export-Package: org.simantics.district.network,
+ org.simantics.district.network.changeset,
  org.simantics.district.network.profile
index 821085175a91886b25807fde8d30133b805b3ca3..550553f1b88083249877ed8f20c41073adf3efc3 100644 (file)
@@ -4,6 +4,8 @@ import java.util.Collection;
 import java.util.Iterator;
 
 import org.simantics.databoard.Bindings;
+import org.simantics.datatypes.literal.RGB;
+import org.simantics.datatypes.literal.RGB.Integer;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
@@ -220,4 +222,14 @@ public class DistrictNetworkUtil {
         Boolean current = graph.getPossibleRelatedValue(diagram, DN.Diagram_drawMapEnabled, Bindings.BOOLEAN);
         return current != null ? current : true;
     }
+
+    public static void changeMapBackgroundColor(WriteGraph graph, Resource resource, Integer integer) throws DatabaseException {
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        graph.claimLiteral(resource, DN.Diagram_backgroundColor, integer, Bindings.getBindingUnchecked(RGB.Integer.class));
+    }
+    
+    public static Boolean trackChangesEnabled(ReadGraph graph, Resource diagram) throws DatabaseException {
+        return Boolean.TRUE.equals(graph.getPossibleRelatedValue(diagram,
+                DistrictNetworkResource.getInstance(graph).Diagram_trackChangesEnabled));
+    }
 }