--- /dev/null
+package org.simantics.document.swt.core.scl;\r
+\r
+import java.util.List;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.document.server.handler.AbstractEventHandler;\r
+import org.simantics.document.server.handler.EventHandler;\r
+import org.simantics.document.server.handler.WriteEventHandler;\r
+import org.simantics.document.server.io.CommandContext;\r
+import org.simantics.document.server.io.CommandResult;\r
+import org.simantics.document.server.serverResponse.ServerResponse;\r
+import org.simantics.document.swt.core.SWTViews;\r
+import org.simantics.scl.runtime.SCLContext;\r
+import org.simantics.scl.runtime.function.Function1;\r
+import org.simantics.scl.runtime.reporting.SCLReportingHandler;\r
+import org.simantics.scl.runtime.tuple.Tuple0;\r
+import org.simantics.ui.selection.WorkbenchSelectionElement;\r
+import org.simantics.ui.selection.WorkbenchSelectionUtils;\r
+\r
+public class SCL {\r
+ \r
+ public static final String SCL_VALUE_PROPERTY = "sclValueProperty";\r
+ \r
+ public static EventHandler eventHandler(final Function1<Object, Object> fn) {\r
+ return new EventHandler() {\r
+ \r
+ @Override\r
+ protected ServerResponse handle(ReadGraph graph, CommandContext parameters) throws DatabaseException {\r
+\r
+ final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER); \r
+ graph.async(new WriteRequest() {\r
+ \r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ SCLContext sclContext = SCLContext.getCurrent();\r
+ Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);\r
+ Object oldGraph = sclContext.put("graph", graph);\r
+ fn.apply(Tuple0.INSTANCE);\r
+ sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);\r
+ sclContext.put("graph", oldGraph);\r
+ }\r
+ \r
+ });\r
+ \r
+ return null;\r
+ \r
+ }\r
+ \r
+ };\r
+ }\r
+ \r
+ public static String getURI(ReadGraph graph, Variable variable) throws DatabaseException {\r
+ return variable.getURI(graph);\r
+ }\r
+ \r
+ public static String propertyDisplayValue(ReadGraph graph, Variable variable, String property) throws DatabaseException {\r
+ Variable var = variable.getProperty(graph, property);\r
+ Variable var2 = var.getProperty(graph, Variables.DISPLAY_VALUE);\r
+ return var2.getValue(graph);\r
+ }\r
+ \r
+ public static AbstractEventHandler propertyValueSetter(ReadGraph graph, Variable variable, String property) throws DatabaseException {\r
+ String uri = variable.getURI(graph);\r
+ return new ValueSetter(uri, property);\r
+ }\r
+ \r
+ public static String propertyGetter(ReadGraph graph, Variable variable) throws DatabaseException {\r
+ String value = variable.getPossiblePropertyValue(graph, SCL_VALUE_PROPERTY);\r
+ return value;\r
+ }\r
+ \r
+ static class ValueSetter extends WriteEventHandler {\r
+ \r
+ private String varUri;\r
+ private String property;\r
+ \r
+ public ValueSetter(String varUri, String property) {\r
+ this.varUri = varUri;\r
+ this.property = property;\r
+ }\r
+ \r
+ @Override\r
+ protected CommandResult handle(WriteGraph graph, CommandContext parameters) throws DatabaseException {\r
+ Variable variable = Variables.getVariable(graph, varUri);\r
+ Variable var = variable.getPossibleProperty(graph, property);\r
+ Variable var2 = var.getPossibleProperty(graph, Variables.DISPLAY_VALUE);\r
+ var2.setValue(graph, parameters.getString("text"), Bindings.STRING);\r
+ return null;\r
+ }\r
+ };\r
+\r
+ @SuppressWarnings("unchecked")\r
+ public static List<WorkbenchSelectionElement> decodeWSES(String key) {\r
+ return (List<WorkbenchSelectionElement>)SWTViews.decode(key);\r
+ }\r
+ \r
+ public static Resource wseResource(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {\r
+ return WorkbenchSelectionUtils.getPossibleResource(wse);\r
+ }\r
+\r
+}\r