]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.scl.ui.editor/src/org/simantics/scl/ui/editor/TestTextEditor.java b/bundles/org.simantics.scl.ui.editor/src/org/simantics/scl/ui/editor/TestTextEditor.java
new file mode 100644 (file)
index 0000000..513b6a1
--- /dev/null
@@ -0,0 +1,88 @@
+package org.simantics.scl.ui.editor;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.custom.SashForm;\r
+import org.eclipse.swt.events.KeyAdapter;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.graphics.Color;\r
+import org.eclipse.swt.graphics.Font;\r
+import org.eclipse.swt.layout.FillLayout;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Event;\r
+import org.eclipse.swt.widgets.Listener;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.scl.compiler.SCLCompiler;\r
+import org.simantics.scl.compiler.StandardSCLCompilerConfiguration;\r
+\r
+public class TestTextEditor {\r
+\r
+    public static void main(String[] args) {\r
+        Display display = new Display();\r
+        final Shell shell = new Shell(display);\r
+        shell.setText("SCL text widget example");\r
+        shell.setLayout(new FillLayout());\r
+        \r
+        SashForm sashForm = new SashForm(shell, SWT.VERTICAL);        \r
+        \r
+        final SCLTextEditor editor = new SCLTextEditor(sashForm, 0, StandardSCLCompilerConfiguration.INSTANCE);\r
+        editor.setContent(\r
+             "// Difficulty: 0\n" +\r
+             "// Expected: [2.0, 3.0, 5.0, 7.0, 11.0, 13.0, 17.0, 19.0]\n\n" +\r
+             "// and : [Boolean] -> Boolean\n" +\r
+             "and = fold (&&) True\n\n" +\r
+             "// isPrime : Integer -> Boolean\n" +\r
+             "isPrime p = and (\n" +\r
+             "    d <- [2 .. sqrt p]\n" +\r
+             "    [p % d > 0]\n" +\r
+             ")\n\n" +\r
+             "x <- [2 .. 20]\n" +\r
+             "isPrime x\n" +\r
+             "[x]"\r
+          );\r
+        \r
+        final Text resultArea = new Text(sashForm, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);\r
+        resultArea.setText("Ctrl-S executes the code.");\r
+        resultArea.setEditable(false);\r
+        resultArea.setBackground(new Color(shell.getDisplay(), 255, 255, 255));\r
+        resultArea.setFont(new Font(shell.getDisplay(), "Courier New", 10, SWT.NORMAL));\r
+        \r
+        sashForm.setWeights(new int[] {5, 1});\r
+        \r
+        display.addFilter(SWT.KeyDown, new Listener() {\r
+            @Override\r
+            public void handleEvent(Event event) {\r
+                if(event.keyCode == 's' && event.stateMask == SWT.CTRL) {\r
+                    final String code = editor.getContent();\r
+                    new Thread() {\r
+                        public void run() {\r
+                            try {\r
+                                final String result = SCLCompiler.compileExpression(StandardSCLCompilerConfiguration.INSTANCE, code).execute().toString();\r
+                                shell.getDisplay().asyncExec(new Runnable() {\r
+                                    public void run() {\r
+                                        resultArea.setText(result);    \r
+                                    }                                                \r
+                                });\r
+                            } catch (Exception e) {\r
+                            }\r
+                        }\r
+                    }.start();\r
+                    event.doit = false;\r
+                }\r
+            }\r
+        });\r
+        shell.addKeyListener(new KeyAdapter() {\r
+            @Override\r
+            public void keyPressed(KeyEvent e) {\r
+                System.out.println(e);\r
+            }\r
+        });\r
+        \r
+        shell.open();\r
+        while (!shell.isDisposed ()) {\r
+            if (!display.readAndDispatch ()) display.sleep ();\r
+        }\r
+        display.dispose ();\r
+    }\r
+    \r
+}\r