]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Initial version of dh element cut/paste (copy still WIP)
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 8 Apr 2019 16:42:10 +0000 (19:42 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Sat, 31 Aug 2019 20:07:22 +0000 (23:07 +0300)
gitlab #43
APROS-15307

Change-Id: Ie1652086c689416e052aed38f5e523ab16ce7b2e

org.simantics.district.network.ui/fragment.e4xmi
org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/CopyDistrictVertexHandler.java [new file with mode: 0644]
org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/PasteDistrictVertexHandler.java [new file with mode: 0644]

index fb9bde4c8db2e9276a0e098753a526ff1e4dd436..93ff59c6a0050cc30d2c1ebf7adb961eb4c63beb 100644 (file)
@@ -9,6 +9,10 @@
     <elements xsi:type="commands:Command" xmi:id="_Uv6NEOJdEei2MexsDMErvQ" elementId="org.simantics.district.network.ui.command.changemapbackgroundcolor" commandName="Change Map Background Color"/>
     <elements xsi:type="commands:Command" xmi:id="_eTLe0PhxEeiSu98FclGtqA" elementId="org.simantics.district.network.ui.command.toggletrackchanges" commandName="Toggle Track Changes"/>
     <elements xsi:type="commands:Command" xmi:id="_f4ugUPhxEeiSu98FclGtqA" elementId="org.simantics.district.network.ui.command.splittomultiplediagrams" commandName="Split to multiple diagrams"/>
+    <elements xsi:type="commands:Command" xmi:id="_RJIeMFn_Eem2uLDOwusbsQ" elementId="org.simantics.district.network.ui.command.copydistrictvertex" commandName="Copy District Vertex">
+      <parameters xmi:id="_lH-1cFoDEem2uLDOwusbsQ" elementId="org.simantics.district.network.ui.commandparameter.0" name="cut"/>
+    </elements>
+    <elements xsi:type="commands:Command" xmi:id="_T3or0Fn_Eem2uLDOwusbsQ" elementId="org.simantics.district.network.ui.command.pastedistrictvertex" commandName="Paste District Vertex"/>
   </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">
       <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"/>
       <children xsi:type="menu:DynamicMenuContribution" xmi:id="_jK8QQD9EEemjkrTOYZxVwQ" elementId="org.simantics.district.network.ui.dynamicmenucontribution.elementActions" label="Element Actions" contributionURI="bundleclass://org.simantics.district.network.ui/org.simantics.district.network.ui.contributions.NetworkElementActionMenuContribution"/>
+      <children xsi:type="menu:HandledMenuItem" xmi:id="_aqopoFn_Eem2uLDOwusbsQ" elementId="org.simantics.district.network.ui.handledmenuitem.copy" label="Copy (WIP)" enabled="false" command="_RJIeMFn_Eem2uLDOwusbsQ"/>
+      <children xsi:type="menu:HandledMenuItem" xmi:id="_dGH0oFn_Eem2uLDOwusbsQ" elementId="org.simantics.district.network.ui.handledmenuitem.paste" label="Paste" command="_T3or0Fn_Eem2uLDOwusbsQ"/>
+      <children xsi:type="menu:HandledMenuItem" xmi:id="_ajCQAFoDEem2uLDOwusbsQ" elementId="org.simantics.district.network.ui.handledmenuitem.cut" label="Cut" command="_RJIeMFn_Eem2uLDOwusbsQ">
+        <parameters xmi:id="_qNYB8FoDEem2uLDOwusbsQ" elementId="org.simantics.district.network.ui.parameter.0" name="org.simantics.district.network.ui.commandparameter.0" value="true"/>
+      </children>
     </elements>
   </fragments>
   <fragments xsi:type="fragment:StringModelFragment" xmi:id="_dbiHcMMBEea1mdgpHNVHMA" featurename="handlers" parentElementId="xpath:/">
@@ -29,5 +38,7 @@
     <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"/>
     <elements xsi:type="commands:Handler" xmi:id="_h3ZzgPhxEeiSu98FclGtqA" elementId="org.simantics.district.network.ui.handler.3" contributionURI="bundleclass://org.simantics.district.network.ui/org.simantics.district.network.ui.contributions.ToggleTrackChanges" command="_eTLe0PhxEeiSu98FclGtqA"/>
     <elements xsi:type="commands:Handler" xmi:id="_kc-uoPhxEeiSu98FclGtqA" elementId="org.simantics.district.network.ui.handler.4" contributionURI="bundleclass://org.simantics.district.network.ui/org.simantics.district.network.ui.contributions.ToggleSplitToMultipleDiagrams" command="_f4ugUPhxEeiSu98FclGtqA"/>
+    <elements xsi:type="commands:Handler" xmi:id="_WEwqsFn_Eem2uLDOwusbsQ" elementId="org.simantics.district.network.ui.handler.5" contributionURI="bundleclass://org.simantics.district.network.ui/org.simantics.district.network.ui.contributions.CopyDistrictVertexHandler" command="_RJIeMFn_Eem2uLDOwusbsQ"/>
+    <elements xsi:type="commands:Handler" xmi:id="_X7QSQFn_Eem2uLDOwusbsQ" elementId="org.simantics.district.network.ui.handler.6" contributionURI="bundleclass://org.simantics.district.network.ui/org.simantics.district.network.ui.contributions.PasteDistrictVertexHandler" command="_T3or0Fn_Eem2uLDOwusbsQ"/>
   </fragments>
 </fragment:ModelFragments>
diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/CopyDistrictVertexHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/CopyDistrictVertexHandler.java
new file mode 100644 (file)
index 0000000..3f99f60
--- /dev/null
@@ -0,0 +1,60 @@
+package org.simantics.district.network.ui.contributions;
+
+import java.util.List;
+
+import javax.inject.Named;
+
+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.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.db.request.Read;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.utils.ui.ISelectionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CopyDistrictVertexHandler {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(CopyDistrictVertexHandler.class);
+    static List<Resource> elements;
+    static boolean cut = true;
+
+    @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 {
+                    DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                    for (Resource selection : elements) {
+                        if (!graph.isInstanceOf(selection, DN.Element)) {
+                            return false;
+                        }
+                    }
+                    return true;
+                }
+            });
+        } catch (DatabaseException e) {
+            LOGGER.error("Could not evaluate if mapping can be changed for selection {}", elements, e);
+            return false;
+        }
+    }
+
+    @Execute
+    public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
+        final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
+        // we store these to a static variable for pasting.. maybe not the best solution 
+        CopyDistrictVertexHandler.elements = elements;
+        //CopyDistrictVertexHandler.cut = cut != null && !cut.isEmpty();
+    }
+}
diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/PasteDistrictVertexHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/PasteDistrictVertexHandler.java
new file mode 100644 (file)
index 0000000..b9aedef
--- /dev/null
@@ -0,0 +1,158 @@
+package org.simantics.district.network.ui.contributions;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.TimeUnit;
+
+import javax.inject.Named;
+
+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.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+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.db.layer0.util.RemoverUtil;
+import org.simantics.db.request.Read;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.utils.threads.ThreadUtils;
+import org.simantics.utils.ui.ISelectionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PasteDistrictVertexHandler {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(PasteDistrictVertexHandler.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 {
+                    DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                    for (Resource selection : elements) {
+                        if (!graph.isInstanceOf(selection, DN.Element)) {
+                            return false;
+                        }
+                    }
+                    return true;
+                }
+            });
+        } catch (DatabaseException e) {
+            LOGGER.error("Could not evaluate if mapping can be changed for selection {}", elements, e);
+            return false;
+        }
+    }
+
+    @Execute
+    public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
+        final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
+        final List<Resource> copiedElements = CopyDistrictVertexHandler.elements; // TODO: this could be implemented more nicely with clipboard ?
+        boolean cut = CopyDistrictVertexHandler.cut;
+        
+        Resource targetElement = elements.get(0);
+        Resource sourceElement = copiedElements.get(0);
+        
+        try {
+            Simantics.getSession().syncRequest(new WriteRequest() {
+                
+                @Override
+                public void perform(WriteGraph graph) throws DatabaseException {
+                    DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                    
+                    Resource sourceMappedElement = graph.getPossibleObject(sourceElement, DN.MappedComponent);
+                    Resource targetMappedElement = graph.getPossibleObject(targetElement, DN.MappedComponent);
+                    if (sourceMappedElement != null)
+                        RemoverUtil.remove(graph, sourceMappedElement);
+                    if (targetMappedElement != null)
+                        RemoverUtil.remove(graph, targetMappedElement);
+                    ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
+                        Simantics.getSession().asyncRequest(new WriteRequest() {
+                            
+                            @Override
+                            public void perform(WriteGraph graph) throws DatabaseException {
+                                //copyMapping(graph, DN, sourceElement, targetElement);
+                                copyLocation(graph, DN, sourceElement, targetElement, cut);
+                                copyVertexInverses(graph, DN, sourceElement, targetElement, cut);
+                                copyElevation(graph, DN, sourceElement, targetElement, cut);
+                            }
+                        });                        
+                    }, 200, TimeUnit.MILLISECONDS);
+                }
+                
+                private void copyMapping(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement) throws DatabaseException {
+                    Resource sourceMapping = graph.getSingleObject(sourceElement, DN.Mapping);
+                    Resource targetMapping = graph.getSingleObject(targetElement, DN.Mapping);
+                    if (cut) {
+                        graph.deny(sourceElement, DN.Mapping);
+                        graph.claim(sourceElement, DN.Mapping, targetMapping);
+                    }
+                    graph.deny(targetElement, DN.Mapping);
+                    graph.claim(targetElement, DN.Mapping, sourceMapping);
+                }
+
+                private void copyElevation(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
+                    double sourceElevation = graph.getRelatedValue(sourceElement, DN.Vertex_HasElevation, Bindings.DOUBLE);
+                    double targetElevation = graph.getRelatedValue(targetElement, DN.Vertex_HasElevation, Bindings.DOUBLE);
+                    if (cut) {
+                        graph.deny(sourceElement, DN.Vertex_HasElevation);
+                        graph.claimLiteral(sourceElement, DN.Vertex_HasElevation, targetElevation, Bindings.DOUBLE);
+                    }
+                    graph.deny(targetElement, DN.Vertex_HasElevation);
+                    graph.claimLiteral(targetElement, DN.Vertex_HasElevation, sourceElevation, Bindings.DOUBLE);
+                }
+
+                private void copyLocation(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
+                    DiagramResource DIA = DiagramResource.getInstance(graph);
+                    double[] sourceLocation = graph.getRelatedValue(sourceElement, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
+                    double[] targetLocation = graph.getRelatedValue(targetElement, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
+                    if (cut) {
+                        graph.deny(sourceElement, DIA.HasLocation);
+                        graph.claimLiteral(sourceElement, DIA.HasLocation, targetLocation, Bindings.DOUBLE_ARRAY);
+                    }
+                    graph.deny(targetElement, DIA.HasLocation);
+                    graph.claimLiteral(targetElement, DIA.HasLocation, sourceLocation, Bindings.DOUBLE_ARRAY);
+                }
+                
+                private void copyVertexInverses(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
+                    Collection<Resource> sourceStartVertex_inverse = graph.getObjects(sourceElement, DN.HasStartVertex_Inverse);
+                    Collection<Resource> sourceEndVertex_inverse = graph.getObjects(sourceElement, DN.HasEndVertex_Inverse);
+                    Collection<Resource> targetStartVertex_inverse = graph.getObjects(targetElement, DN.HasStartVertex_Inverse);
+                    Collection<Resource> targetEndVertex_inverse = graph.getObjects(targetElement, DN.HasEndVertex_Inverse);
+                    graph.deny(sourceElement, DN.HasStartVertex_Inverse);
+                    graph.deny(sourceElement, DN.HasEndVertex_Inverse);
+                    graph.deny(targetElement, DN.HasStartVertex_Inverse);
+                    graph.deny(targetElement, DN.HasEndVertex_Inverse);
+                    for (Resource startVertexInverse : sourceStartVertex_inverse) {
+                        graph.claim(targetElement, DN.HasStartVertex_Inverse, startVertexInverse);
+                    }
+                    for (Resource targetVertexInverse : targetStartVertex_inverse) {
+                        graph.claim(sourceElement, DN.HasStartVertex_Inverse, targetVertexInverse);
+                    }
+                    for (Resource endVertexInverse : sourceEndVertex_inverse) {
+                        graph.claim(targetElement, DN.HasEndVertex_Inverse, endVertexInverse);
+                    }
+                    for (Resource targetVertexInverse : targetEndVertex_inverse) {
+                        graph.claim(sourceElement, DN.HasEndVertex_Inverse, targetVertexInverse);
+                    }
+                }
+            });
+        } catch (DatabaseException e) {
+            e.printStackTrace();
+        }
+        
+    }
+}