]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/scl/SCL.java
e62d544911ab54b74b2b65398e6eeabae9948df4
[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                                                 fn.apply(Tuple0.INSTANCE);
46                                                 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
47                                                 sclContext.put("graph", oldGraph);
48                                         }
49                                         
50                                 });
51                                 
52                                 return null;
53                                 
54                         }
55                         
56                 };
57     }
58     
59     public static String getURI(ReadGraph graph, Variable variable) throws DatabaseException {
60         return variable.getURI(graph);
61     }
62     
63     public static String propertyDisplayValue(ReadGraph graph, Variable variable, String property) throws DatabaseException {
64         Variable var = variable.getProperty(graph, property);
65         Variable var2 = var.getProperty(graph, Variables.DISPLAY_VALUE);
66         return var2.getValue(graph);
67     }
68     
69     public static AbstractEventHandler propertyValueSetter(ReadGraph graph, Variable variable, String property) throws DatabaseException {
70         String uri = variable.getURI(graph);
71         return new ValueSetter(uri, property);
72     }
73     
74     public static String propertyGetter(ReadGraph graph, Variable variable) throws DatabaseException {
75         String value = variable.getPossiblePropertyValue(graph, SCL_VALUE_PROPERTY);
76         return value;
77     }
78     
79     static class ValueSetter extends WriteEventHandler {
80         
81         private String varUri;
82         private String property;
83         
84         public ValueSetter(String varUri, String property) {
85             this.varUri = varUri;
86             this.property = property;
87         }
88         
89         @Override
90         protected CommandResult handle(WriteGraph graph, CommandContext parameters) throws DatabaseException {
91             Variable variable = Variables.getVariable(graph, varUri);
92             Variable var = variable.getPossibleProperty(graph, property);
93             Variable var2 = var.getPossibleProperty(graph, Variables.DISPLAY_VALUE);
94             var2.setValue(graph, parameters.getString("text"), Bindings.STRING);
95             return null;
96         }
97     };
98
99     @SuppressWarnings("unchecked")
100     public static List<WorkbenchSelectionElement> decodeWSES(String key) {
101         return (List<WorkbenchSelectionElement>)SWTViews.decode(key);
102     }
103     
104     public static Resource wseResource(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
105         return WorkbenchSelectionUtils.getPossibleResource(wse);
106     }
107
108 }