]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui.editor/src/org/simantics/scl/ui/editor/TestTextEditor.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scl.ui.editor / src / org / simantics / scl / ui / editor / TestTextEditor.java
1 package org.simantics.scl.ui.editor;
2
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;
17
18 public class TestTextEditor {
19
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());
25         
26         SashForm sashForm = new SashForm(shell, SWT.VERTICAL);        
27         
28         final SCLTextEditor editor = new SCLTextEditor(sashForm, 0, StandardSCLCompilerConfiguration.INSTANCE);
29         editor.setContent(
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" +
37              "    [p % d > 0]\n" +
38              ")\n\n" +
39              "x <- [2 .. 20]\n" +
40              "isPrime x\n" +
41              "[x]"
42           );
43         
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));
49         
50         sashForm.setWeights(new int[] {5, 1});
51         
52         display.addFilter(SWT.KeyDown, new Listener() {
53             @Override
54             public void handleEvent(Event event) {
55                 if(event.keyCode == 's' && event.stateMask == SWT.CTRL) {
56                     final String code = editor.getContent();
57                     new Thread() {
58                         public void run() {
59                             try {
60                                 final String result = SCLCompiler.compileExpression(StandardSCLCompilerConfiguration.INSTANCE, code).execute().toString();
61                                 shell.getDisplay().asyncExec(new Runnable() {
62                                     public void run() {
63                                         resultArea.setText(result);    
64                                     }                                                
65                                 });
66                             } catch (Exception e) {
67                             }
68                         }
69                     }.start();
70                     event.doit = false;
71                 }
72             }
73         });
74         shell.addKeyListener(new KeyAdapter() {
75             @Override
76             public void keyPressed(KeyEvent e) {
77                 System.out.println(e);
78             }
79         });
80         
81         shell.open();
82         while (!shell.isDisposed ()) {
83             if (!display.readAndDispatch ()) display.sleep ();
84         }
85         display.dispose ();
86     }
87     
88 }