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);
45 fn.apply(Tuple0.INSTANCE);
46 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
47 sclContext.put("graph", oldGraph);
59 public static String getURI(ReadGraph graph, Variable variable) throws DatabaseException {
60 return variable.getURI(graph);
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);
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);
74 public static String propertyGetter(ReadGraph graph, Variable variable) throws DatabaseException {
75 String value = variable.getPossiblePropertyValue(graph, SCL_VALUE_PROPERTY);
79 static class ValueSetter extends WriteEventHandler {
81 private String varUri;
82 private String property;
84 public ValueSetter(String varUri, String property) {
86 this.property = property;
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);
99 @SuppressWarnings("unchecked")
100 public static List<WorkbenchSelectionElement> decodeWSES(String key) {
101 return (List<WorkbenchSelectionElement>)SWTViews.decode(key);
104 public static Resource wseResource(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
105 return WorkbenchSelectionUtils.getPossibleResource(wse);