]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.event/src/org/simantics/event/view/handler/TagAction.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / view / handler / TagAction.java
diff --git a/bundles/org.simantics.event/src/org/simantics/event/view/handler/TagAction.java b/bundles/org.simantics.event/src/org/simantics/event/view/handler/TagAction.java
new file mode 100644 (file)
index 0000000..0de4415
--- /dev/null
@@ -0,0 +1,78 @@
+package org.simantics.event.view.handler;\r
+\r
+import java.util.List;\r
+\r
+import org.eclipse.jface.action.Action;\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.VirtualGraph;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.service.VirtualGraphSupport;\r
+import org.simantics.event.ontology.EventResource;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class TagAction extends Action {\r
+\r
+    private final String virtualGraphId;\r
+    private final String tagURI;\r
+    private final boolean tag;\r
+    private final List<Resource> resources;\r
+\r
+    /**\r
+     * @param label\r
+     * @param image\r
+     * @param virtualGraphId\r
+     * @param tagURI\r
+     * @param tag <code>true</code> to add tag, <code>false</code> to remove tag\r
+     * @param input\r
+     */\r
+    public TagAction(String label, ImageDescriptor image, String virtualGraphId, String tagURI, boolean tag, List<Resource> input) {\r
+        super(label, image);\r
+\r
+        this.virtualGraphId = virtualGraphId;\r
+        this.tagURI = tagURI;\r
+        this.tag = tag;\r
+        this.resources = input;\r
+    }\r
+\r
+    @Override\r
+    public void run() {\r
+        Session session = Simantics.peekSession();\r
+        if (session == null) return;\r
+\r
+        final VirtualGraph vg = virtualGraphId == null ? null :\r
+            session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId);\r
+        session.asyncRequest(new WriteRequest(vg) {\r
+            @Override\r
+            public void perform(WriteGraph graph) throws DatabaseException {\r
+                // Write tags\r
+                Resource tr = graph.getResource(tagURI);\r
+                for (Resource r : resources) {\r
+                    if (tag)\r
+                        graph.claim(r, tr, tr, r);\r
+                    else\r
+                        graph.deny(r, tr, r);\r
+                }\r
+\r
+                // Set milestone labels for all events of the event log\r
+                if (tagURI.equals(EventResource.URIs.Milestone)) {\r
+                    CorrectMilestoneLabelsAction.correctMilestoneLabels(graph, vg, resources);\r
+                }\r
+\r
+                // Perform any extra writes desired\r
+                postTagWrite(graph);\r
+            }\r
+        });\r
+\r
+    }\r
+\r
+    public void postTagWrite(WriteGraph graph) throws DatabaseException {\r
+    }\r
+\r
+}\r