]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/scl/SCL.java
Revert of SCL context using try-finally
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / scl / SCL.java
1 package org.simantics.document.swt.core.scl;
2
3 import java.util.List;
4
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
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.variable.Variable;
12 import org.simantics.db.layer0.variable.Variables;
13 import org.simantics.document.server.handler.AbstractEventHandler;
14 import org.simantics.document.server.handler.EventHandler;
15 import org.simantics.document.server.handler.WriteEventHandler;
16 import org.simantics.document.server.io.CommandContext;
17 import org.simantics.document.server.io.CommandResult;
18 import org.simantics.document.server.serverResponse.ServerResponse;
19 import org.simantics.document.swt.core.SWTViews;
20 import org.simantics.scl.runtime.SCLContext;
21 import org.simantics.scl.runtime.function.Function1;
22 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
23 import org.simantics.scl.runtime.tuple.Tuple0;
24 import org.simantics.ui.selection.WorkbenchSelectionElement;
25 import org.simantics.ui.selection.WorkbenchSelectionUtils;
26
27 public class SCL {
28     
29     public static final String SCL_VALUE_PROPERTY = "sclValueProperty";
30     
31     public static EventHandler eventHandler(final Function1<Object, Object> fn) {
32         return new EventHandler() {
33                         
34                         @Override
35                         protected ServerResponse handle(ReadGraph graph, CommandContext parameters) throws DatabaseException {
36
37                             final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER); 
38                                 graph.async(new WriteRequest() {
39                                         
40                                         @Override
41                                         public void perform(WriteGraph graph) throws DatabaseException {
42                                             SCLContext sclContext = SCLContext.getCurrent();
43                                             Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
44                                                 Object oldGraph = sclContext.put("graph", graph);
45                                                 try {
46                                                         fn.apply(Tuple0.INSTANCE);
47                                                 } finally {
48                                                         sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
49                                                         sclContext.put("graph", oldGraph);
50                                                 }
51                                         }
52                                         
53                                 });
54                                 
55                                 return null;
56                                 
57                         }
58                         
59                 };
60     }
61     
62     public static String getURI(ReadGraph graph, Variable variable) throws DatabaseException {
63         return variable.getURI(graph);
64     }
65     
66     public static String propertyDisplayValue(ReadGraph graph, Variable variable, String property) throws DatabaseException {
67         Variable var = variable.getProperty(graph, property);
68         Variable var2 = var.getProperty(graph, Variables.DISPLAY_VALUE);
69         return var2.getValue(graph);
70     }
71     
72     public static AbstractEventHandler propertyValueSetter(ReadGraph graph, Variable variable, String property) throws DatabaseException {
73         String uri = variable.getURI(graph);
74         return new ValueSetter(uri, property);
75     }
76     
77     public static String propertyGetter(ReadGraph graph, Variable variable) throws DatabaseException {
78         String value = variable.getPossiblePropertyValue(graph, SCL_VALUE_PROPERTY);
79         return value;
80     }
81     
82     static class ValueSetter extends WriteEventHandler {
83         
84         private String varUri;
85         private String property;
86         
87         public ValueSetter(String varUri, String property) {
88             this.varUri = varUri;
89             this.property = property;
90         }
91         
92         @Override
93         protected CommandResult handle(WriteGraph graph, CommandContext parameters) throws DatabaseException {
94             Variable variable = Variables.getVariable(graph, varUri);
95             Variable var = variable.getPossibleProperty(graph, property);
96             Variable var2 = var.getPossibleProperty(graph, Variables.DISPLAY_VALUE);
97             var2.setValue(graph, parameters.getString("text"), Bindings.STRING);
98             return null;
99         }
100     };
101
102     @SuppressWarnings("unchecked")
103     public static List<WorkbenchSelectionElement> decodeWSES(String key) {
104         return (List<WorkbenchSelectionElement>)SWTViews.decode(key);
105     }
106     
107     public static Resource wseResource(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
108         return WorkbenchSelectionUtils.getPossibleResource(wse);
109     }
110
111 }