]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/SCLDropAction.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / SCLDropAction.java
1 package org.simantics.modeling;
2
3 import org.simantics.Simantics;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.common.request.ResourceRead;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.layer0.adapter.DropActionFactory;
9 import org.simantics.db.layer0.variable.Variable;
10 import org.simantics.db.layer0.variable.Variables;
11 import org.simantics.diagram.Logger;
12 import org.simantics.scl.runtime.function.Function3;
13
14 public class SCLDropAction implements DropActionFactory {
15
16         final private Resource rule;
17
18         public SCLDropAction(ReadGraph graph, Resource rule) throws DatabaseException {
19                 this.rule = rule;
20         }
21
22         static class RuleFunctionRequest extends ResourceRead<Function3<Resource,Object,Integer,Object>> {
23
24                 protected RuleFunctionRequest(Resource rule) {
25                         super(rule);
26                 }
27
28                 @Override
29                 public Function3<Resource, Object, Integer, Object> perform(ReadGraph graph) throws DatabaseException {
30                         Variable ruleVariable = Variables.getVariable(graph, resource);
31                         ModelingResources MOD = ModelingResources.getInstance(graph);
32                         return ruleVariable.getPossiblePropertyValue(graph, MOD.SCLDropAction_action);
33                 }
34
35         }
36
37         public static class SCLDropActionRunnable implements Runnable {
38
39                 public Resource rule;
40                 public Resource target;
41                 public Object source;
42                 public int operation;
43
44                 public SCLDropActionRunnable(Resource rule, Resource target, Object source, int operation) {
45                         this.rule = rule;
46                         this.target = target;
47                         this.source = source;
48                         this.operation = operation;
49                 }
50
51                 @Override
52                 public void run() {
53                         Simantics.getSession().markUndoPoint();
54                         try {
55                                 Function3<Resource, Object, Integer, Object> function = Simantics.getSession().syncRequest(new RuleFunctionRequest(rule));
56                                 function.apply(target, source, operation);
57                         } catch (DatabaseException e) {
58                                 Logger.defaultLogError(e);
59                         }
60                 }
61
62         }
63
64         @Override
65         public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {
66                 return new SCLDropActionRunnable(rule, (Resource)target, source, operation);
67         }
68
69 }