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