Removed unnecessary dependencies on org.apache.log4j
[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.scl.runtime.function.Function3;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 public class SCLDropAction implements DropActionFactory {
16
17         private static final Logger LOGGER = LoggerFactory.getLogger(SCLDropAction.class);
18
19         final private Resource rule;
20
21         public SCLDropAction(ReadGraph graph, Resource rule) throws DatabaseException {
22                 this.rule = rule;
23         }
24
25         static class RuleFunctionRequest extends ResourceRead<Function3<Resource,Object,Integer,Object>> {
26
27                 protected RuleFunctionRequest(Resource rule) {
28                         super(rule);
29                 }
30
31                 @Override
32                 public Function3<Resource, Object, Integer, Object> perform(ReadGraph graph) throws DatabaseException {
33                         Variable ruleVariable = Variables.getVariable(graph, resource);
34                         ModelingResources MOD = ModelingResources.getInstance(graph);
35                         return ruleVariable.getPossiblePropertyValue(graph, MOD.SCLDropAction_action);
36                 }
37
38         }
39
40         public static class SCLDropActionRunnable implements Runnable {
41
42                 public Resource rule;
43                 public Resource target;
44                 public Object source;
45                 public int operation;
46
47                 public SCLDropActionRunnable(Resource rule, Resource target, Object source, int operation) {
48                         this.rule = rule;
49                         this.target = target;
50                         this.source = source;
51                         this.operation = operation;
52                 }
53
54                 @Override
55                 public void run() {
56                         Simantics.getSession().markUndoPoint();
57                         try {
58                                 Function3<Resource, Object, Integer, Object> function = Simantics.getSession().syncRequest(new RuleFunctionRequest(rule));
59                                 function.apply(target, source, operation);
60                         } catch (DatabaseException e) {
61                                 LOGGER.error("SCLDropActionRunnable failed to request/apply RuleFunction {}", rule, e);
62                         }
63                 }
64
65         }
66
67         @Override
68         public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {
69                 return new SCLDropActionRunnable(rule, (Resource)target, source, operation);
70         }
71
72 }