Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>The Groovy Swing Console allows a user to enter and run Groovy scripts. This page documents the features of this user interface.</p> <h1>Basics</h1> <p>The Groovy Console: <img class="confluence-embedded-image image-center" src="/download/attachments/231080140/GroovyConsole.gif?version=1&modificationDate=1369383397942" data-image-src="/download/attachments/231080140/GroovyConsole.gif?version=1&modificationDate=1369383397942" data-linked-resource-id="231375672" data-linked-resource-type="attachment" data-linked-resource-default-alias="GroovyConsole.gif" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231080140" title="null > GroovyConsole.gif"><br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p> <ol> <li>Groovy Console is launched via "groovyConsole" or "groovyConsole.bat", both located in "$GROOVY_HOME/bin"</li> <li>The Console has an input area and an output area.</li> <li>You type a Groovy script in the input area.</li> <li>When you select "Run" from the "Actions" menu, the console compiles the script and runs it.</li> <li>Anything that would normally be printed on System.out is printed in the output area.</li> <li>If the script returns a non-null result, that result is printed.</li> </ol> <h1>Features</h1> <h2>Running Scripts</h2> <p>Handy tips for running scripts:</p> <ul class="alternate"> <li>Ctrl+Enter and Ctrl+R are both shortcut keys for "Run Script".</li> <li>If you highight just part of the text in the input area, then Groovy runs just that text.</li> <li>The result of a script is the the value of the last expression executed.</li> <li>You can turn the System.out capture on and off by selecting "Capture System.out" from the "Actions" menu</li> </ul> <h2>Editing Files</h2> <p>You can open any text file, edit it, run it (as a Groovy Script) and then save it again when you are finished.</p> <ul class="alternate"> <li>Select File -> Open (shortcut key ctrl+O) to open a file</li> <li>Select File -> Save (shortcut key ctrl+S) to save a file</li> <li>Select File -> New File (shortcut key ctrl+Q) to start again with a blank<br /> input area</li> </ul> <h2>History and results</h2> <ul> <li>You can pop-up a gui inspector on the last (non-null) result by selecting "Inspect Last" from the "Actions" menu. The inspector is a convenient way to view lists and maps.</li> <li>The console remembers the last ten script runs. You can scroll back and forth through the history by selecting "Next" and "Previous" from the "Edit" menu. Ctrl-N and ctrl-P are convenient shortcut keys.</li> <li>The last (non-null) result is bound to a variable named '_' (an underscore).</li> <li>The last result (null and non-null) for every run in the history is bound into a list variable named '<code>__</code>' (two underscores). The result of the last run is <code>_<em>[-1]</em></code><em>, the result of the second to last run is</em> <code>___</code><code>[-2]</code> and so forth.</li> </ul> <h2>And more</h2> <ul> <li>You can attempt to interrupt a long running task by clicking the "interrupt" button on the small dialog box that pops up when a script is executing.</li> <li>You can change the font size by selecting "Smaller Font" or "Larger Font" from the "Actions menu"</li> <li>The console can be run as an Applet thanks to groovy.ui.ConsoleApplet</li> <li>Code is auto indented when you hit return</li> <li>You can drag'n drop a Groovy script over the text area to open a file</li> <li>You can modify the classpath with which the script in the console is being run by adding a new JAR or a directory to the classpath from the Script menu</li> <li>Error hyperlinking from the output area when a compilation error is expected or when an exception is thrown</li> </ul> <h1>Embedding the Console</h1> <p>To embed a Swing console in your application, simply create the Console object,<br /> load some variables, and then launch it. The console can be embedded in either Java or Groovy code.<br /> The Java code for this is:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import groovy.ui.Console; ... Console console = new Console(); console.setVariable("var1", getValueOfVar1()); console.setVariable("var2", getValueOfVar2()); console.run(); ... </pre></td></tr></table> <p>Once the console is launched, you can use the variable values in Groovy code.</p> <p>An example of how to embed either the GroovyConsole or GroovyShell in a Spring Web application can be found at <a href="http://groovy.codehaus.org/Embedding+a+Groovy+Console+in+a+Java+Server+Application">Embedding a Groovy Console in a Java Server Application</a></p> <h1>Visualizing script output results</h1> <p> You can customize the way script output results are visualized. Let's see how we can customize this. For example, viewing a map result would show something like this:</p> <p><img class="confluence-embedded-image" src="/download/attachments/231080140/sc-without-visu.png?version=1&modificationDate=1369383397941" data-image-src="/download/attachments/231080140/sc-without-visu.png?version=1&modificationDate=1369383397941" data-linked-resource-id="231375670" data-linked-resource-type="attachment" data-linked-resource-default-alias="sc-without-visu.png" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231080140" title="null > sc-without-visu.png"></p> <p> What you see here is the usual textual representation of a Map. But, what if we enabled custom visualization of certain results? The Swing console allows you to do just that. First of all, you have to ensure that the visualization option is ticked: View -> Visualize Script Results — for the record, all settings of the Groovy Console are stored and remembered thanks to the Preference API. There are a few result visualizations built-in: if the script returns a java.awt.Image, a javax.swing.Icon, or a java.awt.Component with no parent, the object is displayed instead of its toString() representation. Otherwise, everything else is still just represented as text. Now, create the following Groovy script in ~/.groovy/OutputTransforms.groovy:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import javax.swing.* transforms << { result -> if (result instanceof Map) { def table = new JTable( result.collect{ k, v -> [k, v?.inspect()] as Object[] } as Object[][], ['Key', 'Value'] as Object[]) table.preferredViewportSize = table.preferredSize return new JScrollPane(table) } } </pre></td></tr></table> <p>The Groovy Swing console will execute that script on startup, injecting a transforms list in the binding of the script, so that you can add your own script results representations. In our case, we transform the Map into a nice-looking Swing JTable. And we're now able to visualize maps in a friendly and attractive fashion, as the screenshot below shows:</p> <p><img class="confluence-embedded-image" src="/download/attachments/231080140/sc-with-visu.png?version=1&modificationDate=1369383397941" data-image-src="/download/attachments/231080140/sc-with-visu.png?version=1&modificationDate=1369383397941" data-linked-resource-id="231375671" data-linked-resource-type="attachment" data-linked-resource-default-alias="sc-with-visu.png" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231080140" title="null > sc-with-visu.png"><br /> <br class="atl-forced-newline" /></p> <h1>AST browser</h1> <p>Groovy Console can visualize the AST (Abstract Syntax Tree) representing the currently edited script, as shown by the screenshot below. This is particularly handy when you want to develop <a class="confluence-link" href="/display/GROOVY/Compile-time+Metaprogramming+-+AST+Transformations" data-linked-resource-id="114786379" data-linked-resource-type="page" data-linked-resource-default-alias="Compile-time Metaprogramming - AST Transformations" data-base-url="http://docs.codehaus.org">AST transformations</a>.</p> <p><img class="confluence-embedded-image" src="/download/attachments/231080140/astbrowser.png?version=1&modificationDate=1369383397925" data-image-src="/download/attachments/231080140/astbrowser.png?version=1&modificationDate=1369383397925" data-linked-resource-id="231375669" data-linked-resource-type="attachment" data-linked-resource-default-alias="astbrowser.png" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231080140" title="null > astbrowser.png"></p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced