]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui.editor/src/org/simantics/scl/ui/editor/TestTextEditor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.ui.editor / src / org / simantics / scl / ui / editor / TestTextEditor.java
1 package org.simantics.scl.ui.editor;\r
2 \r
3 import org.eclipse.swt.SWT;\r
4 import org.eclipse.swt.custom.SashForm;\r
5 import org.eclipse.swt.events.KeyAdapter;\r
6 import org.eclipse.swt.events.KeyEvent;\r
7 import org.eclipse.swt.graphics.Color;\r
8 import org.eclipse.swt.graphics.Font;\r
9 import org.eclipse.swt.layout.FillLayout;\r
10 import org.eclipse.swt.widgets.Display;\r
11 import org.eclipse.swt.widgets.Event;\r
12 import org.eclipse.swt.widgets.Listener;\r
13 import org.eclipse.swt.widgets.Shell;\r
14 import org.eclipse.swt.widgets.Text;\r
15 import org.simantics.scl.compiler.SCLCompiler;\r
16 import org.simantics.scl.compiler.StandardSCLCompilerConfiguration;\r
17 \r
18 public class TestTextEditor {\r
19 \r
20     public static void main(String[] args) {\r
21         Display display = new Display();\r
22         final Shell shell = new Shell(display);\r
23         shell.setText("SCL text widget example");\r
24         shell.setLayout(new FillLayout());\r
25         \r
26         SashForm sashForm = new SashForm(shell, SWT.VERTICAL);        \r
27         \r
28         final SCLTextEditor editor = new SCLTextEditor(sashForm, 0, StandardSCLCompilerConfiguration.INSTANCE);\r
29         editor.setContent(\r
30              "// Difficulty: 0\n" +\r
31              "// Expected: [2.0, 3.0, 5.0, 7.0, 11.0, 13.0, 17.0, 19.0]\n\n" +\r
32              "// and : [Boolean] -> Boolean\n" +\r
33              "and = fold (&&) True\n\n" +\r
34              "// isPrime : Integer -> Boolean\n" +\r
35              "isPrime p = and (\n" +\r
36              "    d <- [2 .. sqrt p]\n" +\r
37              "    [p % d > 0]\n" +\r
38              ")\n\n" +\r
39              "x <- [2 .. 20]\n" +\r
40              "isPrime x\n" +\r
41              "[x]"\r
42           );\r
43         \r
44         final Text resultArea = new Text(sashForm, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);\r
45         resultArea.setText("Ctrl-S executes the code.");\r
46         resultArea.setEditable(false);\r
47         resultArea.setBackground(new Color(shell.getDisplay(), 255, 255, 255));\r
48         resultArea.setFont(new Font(shell.getDisplay(), "Courier New", 10, SWT.NORMAL));\r
49         \r
50         sashForm.setWeights(new int[] {5, 1});\r
51         \r
52         display.addFilter(SWT.KeyDown, new Listener() {\r
53             @Override\r
54             public void handleEvent(Event event) {\r
55                 if(event.keyCode == 's' && event.stateMask == SWT.CTRL) {\r
56                     final String code = editor.getContent();\r
57                     new Thread() {\r
58                         public void run() {\r
59                             try {\r
60                                 final String result = SCLCompiler.compileExpression(StandardSCLCompilerConfiguration.INSTANCE, code).execute().toString();\r
61                                 shell.getDisplay().asyncExec(new Runnable() {\r
62                                     public void run() {\r
63                                         resultArea.setText(result);    \r
64                                     }                                                \r
65                                 });\r
66                             } catch (Exception e) {\r
67                             }\r
68                         }\r
69                     }.start();\r
70                     event.doit = false;\r
71                 }\r
72             }\r
73         });\r
74         shell.addKeyListener(new KeyAdapter() {\r
75             @Override\r
76             public void keyPressed(KeyEvent e) {\r
77                 System.out.println(e);\r
78             }\r
79         });\r
80         \r
81         shell.open();\r
82         while (!shell.isDisposed ()) {\r
83             if (!display.readAndDispatch ()) display.sleep ();\r
84         }\r
85         display.dispose ();\r
86     }\r
87     \r
88 }\r