1 package org.simantics.scl.ui.editor;
\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
18 public class TestTextEditor {
\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
26 SashForm sashForm = new SashForm(shell, SWT.VERTICAL);
\r
28 final SCLTextEditor editor = new SCLTextEditor(sashForm, 0, StandardSCLCompilerConfiguration.INSTANCE);
\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
39 "x <- [2 .. 20]\n" +
\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
50 sashForm.setWeights(new int[] {5, 1});
\r
52 display.addFilter(SWT.KeyDown, new Listener() {
\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
60 final String result = SCLCompiler.compileExpression(StandardSCLCompilerConfiguration.INSTANCE, code).execute().toString();
\r
61 shell.getDisplay().asyncExec(new Runnable() {
\r
63 resultArea.setText(result);
\r
66 } catch (Exception e) {
\r
74 shell.addKeyListener(new KeyAdapter() {
\r
76 public void keyPressed(KeyEvent e) {
\r
77 System.out.println(e);
\r
82 while (!shell.isDisposed ()) {
\r
83 if (!display.readAndDispatch ()) display.sleep ();
\r