1 package org.simantics.document.swt.core.scl;
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;
29 public static final String SCL_VALUE_PROPERTY = "sclValueProperty";
31 public static EventHandler eventHandler(final Function1<Object, Object> fn) {
32 return new EventHandler() {
35 protected ServerResponse handle(ReadGraph graph, CommandContext parameters) throws DatabaseException {
37 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
38 graph.async(new WriteRequest() {
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);
46 fn.apply(Tuple0.INSTANCE);
48 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
49 sclContext.put("graph", oldGraph);
62 public static String getURI(ReadGraph graph, Variable variable) throws DatabaseException {
63 return variable.getURI(graph);
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);
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);
77 public static String propertyGetter(ReadGraph graph, Variable variable) throws DatabaseException {
78 String value = variable.getPossiblePropertyValue(graph, SCL_VALUE_PROPERTY);
82 static class ValueSetter extends WriteEventHandler {
84 private String varUri;
85 private String property;
87 public ValueSetter(String varUri, String property) {
89 this.property = property;
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);
102 @SuppressWarnings("unchecked")
103 public static List<WorkbenchSelectionElement> decodeWSES(String key) {
104 return (List<WorkbenchSelectionElement>)SWTViews.decode(key);
107 public static Resource wseResource(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
108 return WorkbenchSelectionUtils.getPossibleResource(wse);