]> gerrit.simantics Code Review - simantics/platform.git/blob
6187159a4bdfba185c92d550fa644f8cd12d6ce5
[simantics/platform.git] /
1 package org.simantics.issues.ui.contribution;
2
3 import java.util.List;
4
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.Resource;
7 import org.simantics.db.Session;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.common.request.WriteRequest;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.SelectionHints;
12 import org.simantics.db.layer0.adapter.DropActionFactory;
13 import org.simantics.issues.ontology.IssueResource;
14 import org.simantics.modeling.PropertyVariables;
15 import org.simantics.utils.datastructures.Callback;
16 import org.simantics.utils.ui.ErrorLogger;
17 import org.simantics.utils.ui.ISelectionUtils;
18
19 /**
20  * @author Tuukka Lehtonen
21  */
22 public class IssueContextDropAction implements DropActionFactory {
23
24     @Override
25     public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {
26         //System.out.println("DROP: " + source + " -> " + target);
27
28         final Resource issue= ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);
29         if (issue == null)
30             return null;
31
32         final List<PropertyVariables> vars = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, PropertyVariables.class);
33         if (!vars.isEmpty()) {
34             //System.out.println("TODO: add property container to context");
35         } else {
36             List<Resource> ctx = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);
37             if (!ctx.isEmpty())
38                 addToIssueContext(g.getSession(), issue, ctx);
39         }
40
41         return null;
42     }
43
44     private void addToIssueContext(Session session, final Resource issue, final List<Resource> contexts) {
45         session.asyncRequest(new WriteRequest() {
46             @Override
47             public void perform(WriteGraph graph) throws DatabaseException {
48                 final IssueResource ISSUE = IssueResource.getInstance(graph);
49                 for (Resource ctx : contexts) {
50                     graph.claim(issue, ISSUE.Issue_HasContext, ISSUE.Issue_HasContext, ctx);
51                 }
52             }
53         }, new Callback<DatabaseException>() {
54             @Override
55             public void run(DatabaseException e) {
56                 if (e != null)
57                     ErrorLogger.defaultLogError(e);
58             }
59         });
60     }
61
62 }