1 package org.simantics.scl.ui.editor;
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.custom.SashForm;
5 import org.eclipse.swt.events.KeyAdapter;
6 import org.eclipse.swt.events.KeyEvent;
7 import org.eclipse.swt.graphics.Color;
8 import org.eclipse.swt.graphics.Font;
9 import org.eclipse.swt.layout.FillLayout;
10 import org.eclipse.swt.widgets.Display;
11 import org.eclipse.swt.widgets.Event;
12 import org.eclipse.swt.widgets.Listener;
13 import org.eclipse.swt.widgets.Shell;
14 import org.eclipse.swt.widgets.Text;
15 import org.simantics.scl.compiler.SCLCompiler;
16 import org.simantics.scl.compiler.StandardSCLCompilerConfiguration;
18 public class TestTextEditor {
20 public static void main(String[] args) {
21 Display display = new Display();
22 final Shell shell = new Shell(display);
23 shell.setText("SCL text widget example");
24 shell.setLayout(new FillLayout());
26 SashForm sashForm = new SashForm(shell, SWT.VERTICAL);
28 final SCLTextEditor editor = new SCLTextEditor(sashForm, 0, StandardSCLCompilerConfiguration.INSTANCE);
30 "// Difficulty: 0\n" +
31 "// Expected: [2.0, 3.0, 5.0, 7.0, 11.0, 13.0, 17.0, 19.0]\n\n" +
32 "// and : [Boolean] -> Boolean\n" +
33 "and = fold (&&) True\n\n" +
34 "// isPrime : Integer -> Boolean\n" +
35 "isPrime p = and (\n" +
36 " d <- [2 .. sqrt p]\n" +
44 final Text resultArea = new Text(sashForm, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
45 resultArea.setText("Ctrl-S executes the code.");
46 resultArea.setEditable(false);
47 resultArea.setBackground(new Color(shell.getDisplay(), 255, 255, 255));
48 resultArea.setFont(new Font(shell.getDisplay(), "Courier New", 10, SWT.NORMAL));
50 sashForm.setWeights(new int[] {5, 1});
52 display.addFilter(SWT.KeyDown, new Listener() {
54 public void handleEvent(Event event) {
55 if(event.keyCode == 's' && event.stateMask == SWT.CTRL) {
56 final String code = editor.getContent();
60 final String result = SCLCompiler.compileExpression(StandardSCLCompilerConfiguration.INSTANCE, code).execute().toString();
61 shell.getDisplay().asyncExec(new Runnable() {
63 resultArea.setText(result);
66 } catch (Exception e) {
74 shell.addKeyListener(new KeyAdapter() {
76 public void keyPressed(KeyEvent e) {
77 System.out.println(e);
82 while (!shell.isDisposed ()) {
83 if (!display.readAndDispatch ()) display.sleep ();