]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/CopyDistrictVertexHandler.java
Initial version of dh element cut/paste (copy still WIP)
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / contributions / CopyDistrictVertexHandler.java
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();
+    }
+}