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