1 package org.simantics.document.swt.core.widget;
\r
3 import org.eclipse.swt.SWT;
\r
4 import org.eclipse.swt.widgets.Composite;
\r
5 import org.eclipse.ui.PlatformUI;
\r
6 import org.simantics.document.server.JSONObject;
\r
7 import org.simantics.document.server.handler.AbstractEventHandler;
\r
8 import org.simantics.document.server.io.CommandContextImpl;
\r
9 import org.simantics.document.swt.core.SWTDocument;
\r
10 import org.simantics.document.swt.core.base.LeafWidgetManager;
\r
11 import org.simantics.scl.ui.editor.SCLTextEditorNew;
\r
12 import org.simantics.scl.ui.editor.TextAndErrors;
\r
13 import org.simantics.utils.ui.workbench.FocusContextActivator;
\r
15 public class SCLTextEditor extends LeafWidgetManager<SCLTextEditorNew> {
\r
17 public static class State {
\r
19 final private SWTDocument document;
\r
20 final private SCLTextEditorNew editor;
\r
21 public AbstractEventHandler onModify;
\r
23 public State(SWTDocument document, SCLTextEditorNew editor) {
\r
24 this.document = document;
\r
25 this.editor = editor;
\r
28 public void fireText() {
\r
29 if(onModify != null) {
\r
30 CommandContextImpl parameters = new CommandContextImpl();
\r
31 parameters.putString("text", editor.getContent());
\r
32 document.post(onModify, parameters);
\r
39 protected SCLTextEditorNew doCreateControl(SWTDocument document, Composite parent, JSONObject object) {
\r
41 SCLTextEditorNew editor = new SCLTextEditorNew(parent, SWT.NONE);
\r
42 State state = new State(document, editor);
\r
44 editor.viewer.getTextWidget().setData("state", state);
\r
45 new FocusContextActivator("org.simantics.document.swt.core.widget.SCLTextEditor", editor.viewer.getTextWidget(), PlatformUI.getWorkbench().getActiveWorkbenchWindow());
\r
52 protected void doUpdateProperties(SWTDocument document, SCLTextEditorNew control, JSONObject object) {
\r
54 if(control.isDisposed()) return;
\r
56 TextAndErrors textAndErrors = object.getValue("textAndErrors");
\r
57 if(textAndErrors == null) return;
\r
59 // Disable updates during setContent
\r
61 int caret = control.viewer.getTextWidget().getCaretOffset();
\r
63 control.setContent(textAndErrors.text, textAndErrors.errors);
\r
65 // For updates, restore caret
\r
66 control.viewer.getTextWidget().setCaretOffset(caret);
\r
68 final AbstractEventHandler onModify = object.getValue("onModify");
\r
69 State state = (State)control.viewer.getTextWidget().getData("state");
\r
70 state.onModify = onModify;
\r