]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/SCLTextEditor.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / widget / SCLTextEditor.java
1 package org.simantics.document.swt.core.widget;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.widgets.Composite;
5 import org.eclipse.ui.PlatformUI;
6 import org.simantics.document.server.JSONObject;
7 import org.simantics.document.server.handler.AbstractEventHandler;
8 import org.simantics.document.server.io.CommandContextImpl;
9 import org.simantics.document.swt.core.SWTDocument;
10 import org.simantics.document.swt.core.base.LeafWidgetManager;
11 import org.simantics.scl.ui.editor.SCLTextEditorNew;
12 import org.simantics.scl.ui.editor.TextAndErrors;
13 import org.simantics.utils.ui.workbench.FocusContextActivator;
14
15 public class SCLTextEditor extends LeafWidgetManager<SCLTextEditorNew> {
16         
17         public static class State {
18                 
19                 final private SWTDocument document;
20                 final private SCLTextEditorNew editor;
21                 public AbstractEventHandler onModify;
22
23                 public State(SWTDocument document, SCLTextEditorNew editor) {
24                         this.document = document;
25                         this.editor = editor;
26                 }
27
28                 public void fireText() {
29                         if(onModify != null) {
30                                 CommandContextImpl parameters = new CommandContextImpl();
31                                 parameters.putString("text", editor.getContent());
32                                 document.post(onModify, parameters);
33                         }
34                 }
35                 
36         }
37         
38         @Override
39         protected SCLTextEditorNew doCreateControl(SWTDocument document, Composite parent, JSONObject object) {
40                 
41                 SCLTextEditorNew editor = new SCLTextEditorNew(parent, SWT.NONE);
42                 State state = new State(document, editor);
43
44                 editor.viewer.getTextWidget().setData("state", state);
45                 new FocusContextActivator("org.simantics.document.swt.core.widget.SCLTextEditor", editor.viewer.getTextWidget(), PlatformUI.getWorkbench().getActiveWorkbenchWindow());
46         
47                 return editor;
48                 
49         }
50         
51         @Override
52         protected void doUpdateProperties(SWTDocument document, SCLTextEditorNew control, JSONObject object) {
53
54                 if(control.isDisposed()) return;
55                 
56                 TextAndErrors textAndErrors = object.getValue("textAndErrors");
57                 if(textAndErrors == null) return;
58
59                 // Disable updates during setContent
60
61                 int caret = control.viewer.getTextWidget().getCaretOffset();
62                 
63                 control.setContent(textAndErrors.text, textAndErrors.errors);
64                 
65                 // For updates, restore caret
66                 control.viewer.getTextWidget().setCaretOffset(caret);
67                 
68                 final AbstractEventHandler onModify = object.getValue("onModify");
69                 State state = (State)control.viewer.getTextWidget().getData("state");
70                 state.onModify = onModify;
71                 
72         }
73         
74 }