]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/SCLEditor.java
fd52bc55877fdfbb10c9a476e885abf86d14aa8e
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / SCLEditor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt.widgets;\r
13 \r
14 import org.eclipse.swt.widgets.Composite;\r
15 import org.eclipse.swt.widgets.Control;\r
16 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;\r
17 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
18 import org.simantics.db.management.ISessionContext;\r
19 import org.simantics.db.procedure.Listener;\r
20 import org.simantics.scl.compiler.StandardSCLCompilerConfiguration;\r
21 import org.simantics.scl.ui.editor.SCLTextEditor;\r
22 import org.simantics.utils.ui.SWTUtils;\r
23 \r
24 public class SCLEditor extends WidgetImpl {\r
25 \r
26         private ReadFactory<?, String> textFactory;\r
27         \r
28         final private SCLTextEditor editor;\r
29         \r
30         public SCLEditor(Composite parent, WidgetSupport support, int style) {\r
31                 super(support);\r
32                 editor = new SCLTextEditor(parent, style, StandardSCLCompilerConfiguration.INSTANCE);\r
33                 support.register(this);\r
34         }\r
35         \r
36         public void setTextFactory(ReadFactory<?, String> textFactory) {\r
37                 this.textFactory = textFactory;\r
38         }\r
39         \r
40         public SCLTextEditor getWidget() {\r
41                 return editor;\r
42         }\r
43         \r
44         @Override\r
45         public Control getControl() {\r
46                 return editor;\r
47         }\r
48 \r
49         @Override\r
50         public void setInput(ISessionContext context, Object input) {\r
51 \r
52                 if(textFactory != null) {\r
53                         textFactory.listen(context, input, new Listener<String>() {\r
54 \r
55                                 public void exception(final Throwable t) {\r
56                                         SWTUtils.asyncExec(editor, new Runnable() {\r
57 \r
58                                                 @Override\r
59                                                 public void run() {\r
60                                                         if(isDisposed()) return;\r
61 //                                                      System.out.println("Button received new text: " + text);\r
62                                                         editor.setContent(t.toString());\r
63                                                 }\r
64 \r
65                                         });\r
66                                 }\r
67 \r
68                                 @Override\r
69                                 public void execute(final String s) {\r
70                                         SWTUtils.asyncExec(editor, new Runnable() {\r
71 \r
72                                                 @Override\r
73                                                 public void run() {\r
74                                                         if(isDisposed()) return;\r
75                                                         editor.setContent(s);\r
76                                                 }\r
77 \r
78                                         });\r
79                                 }\r
80 \r
81                                 @Override\r
82                                 public boolean isDisposed() {\r
83                                         return editor.isDisposed();\r
84                                 }\r
85 \r
86                         });\r
87                 \r
88                 }\r
89                 \r
90         }\r
91         \r
92         public void setContent(String s) {\r
93                 editor.setContent(s);\r
94         }\r
95         \r
96         public String getContent() {\r
97                 return editor.getContent();\r
98         }\r
99 \r
100 }\r