Sync git svn branch with SVN repository r33324.
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / view / handler / TagAction.java
1 package org.simantics.event.view.handler;\r
2 \r
3 import java.util.List;\r
4 \r
5 import org.eclipse.jface.action.Action;\r
6 import org.eclipse.jface.resource.ImageDescriptor;\r
7 import org.simantics.Simantics;\r
8 import org.simantics.db.Resource;\r
9 import org.simantics.db.Session;\r
10 import org.simantics.db.VirtualGraph;\r
11 import org.simantics.db.WriteGraph;\r
12 import org.simantics.db.common.request.WriteRequest;\r
13 import org.simantics.db.exception.DatabaseException;\r
14 import org.simantics.db.service.VirtualGraphSupport;\r
15 import org.simantics.event.ontology.EventResource;\r
16 \r
17 /**\r
18  * @author Tuukka Lehtonen\r
19  */\r
20 public class TagAction extends Action {\r
21 \r
22     private final String virtualGraphId;\r
23     private final String tagURI;\r
24     private final boolean tag;\r
25     protected final List<Resource> resources;\r
26 \r
27     /**\r
28      * @param label\r
29      * @param image\r
30      * @param virtualGraphId\r
31      * @param tagURI\r
32      * @param tag <code>true</code> to add tag, <code>false</code> to remove tag\r
33      * @param input\r
34      */\r
35     public TagAction(String label, ImageDescriptor image, String virtualGraphId, String tagURI, boolean tag, List<Resource> input) {\r
36         super(label, image);\r
37 \r
38         this.virtualGraphId = virtualGraphId;\r
39         this.tagURI = tagURI;\r
40         this.tag = tag;\r
41         this.resources = input;\r
42     }\r
43 \r
44     @Override\r
45     public void run() {\r
46         Session session = Simantics.peekSession();\r
47         if (session == null) return;\r
48 \r
49         final VirtualGraph vg = virtualGraphId == null ? null :\r
50             session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId);\r
51         session.asyncRequest(new WriteRequest(vg) {\r
52             @Override\r
53             public void perform(WriteGraph graph) throws DatabaseException {\r
54                 // Write tags\r
55                 Resource tr = graph.getResource(tagURI);\r
56                 for (Resource r : resources) {\r
57                     if (tag)\r
58                         graph.claim(r, tr, tr, r);\r
59                     else\r
60                         graph.deny(r, tr, r);\r
61                 }\r
62 \r
63                 // Set milestone labels for all events of the event log\r
64                 if (tagURI.equals(EventResource.URIs.Milestone)) {\r
65                     CorrectMilestoneLabelsAction.correctMilestoneLabels(graph, vg, resources);\r
66                 }\r
67 \r
68                 // Perform any extra writes desired\r
69                 postTagWrite(graph);\r
70             }\r
71         });\r
72 \r
73     }\r
74 \r
75     public void postTagWrite(WriteGraph graph) throws DatabaseException {\r
76     }\r
77 \r
78 }\r