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>To show how to use the the JFace builder I converted a couple of applications from the standard SWT example set:</p> <ul> <li>TextEditor: A simple application showing how to use menubar, toolbar and a StyledText.</li> <li>AddressBook: A more complete application with a simple addressbook. Shows how to use toolbars, tables, sorting, Search dialogs, editing, copy/paste</li> </ul> <p>I have on purpose changed the application code as little as possible (only to make work in Groovy), and with the main focus to change the creation of the GUI to show how to use the JFace builder. But it also shows how much easier it is to use the JFace builder.</p> <p>Here is the extract from the groovy code (61 lines):</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> mainapp = jface.applicationWindow( title:'Groovy Text Editor', location:[100,100], size:[500, 300] ) { gridLayout(numColumns:1) menuManager( 'File' ) { action ( 'Exit', closure:{ mainapp.close() } ) } menuManager( 'Edit' ) { action ( 'Cut', accelerator: 'Ctrl+X', closure:{ handleCutCopy(); text.cut() } ) action ( 'Copy', accelerator: 'Ctrl+C',closure:{ handleCutCopy(); text.copy() } ) action ( 'Paste', accelerator: 'Ctrl+P',closure:{ text.paste() } ) separator() action ( 'Set Font', closure:{ setFont() } ) } toolBar( style:'none' ) { boldButton = toolItem(style:'check', toolTipText:'Bold') { image( src:'bold.png' ) onEvent('Selection') {setStyle(it.widget)} } italicButton = toolItem(style:'check', toolTipText:'Italic') { image( src:'italic.png' ) onEvent('Selection') {setStyle(it.widget)} } underlineButton = toolItem(style:'check', toolTipText:'Underline') { image( src:'underline.png' ) onEvent('Selection'){setStyle(it.widget)} } strikeoutButton = toolItem(style:'check', toolTipText:'Strikeout') { image( src:'strikeout.png' ) onEvent('Selection') {setStyle(it.widget)} } toolItem(style:'separator') toolItem(style:'push', toolTipText:'Red text') { image( src:'red.png' ) onEvent('Selection') {fgColor(RED)} } toolItem(style:'push', toolTipText:'Blue text') { image( src:'blue.png' ) onEvent('Selection'){fgColor(BLUE)} } toolItem(style:'push', toolTipText:'Green text') { image( src:'green.png' ) onEvent('Selection'){fgColor(GREEN)} } toolItem(style:'separator') toolItem(style:'push', toolTipText:'Clear formatting') { image( src:'erase.png' ) onEvent('Selection') {clear()} } } text = styledText ( style: 'Border, Multi, V_Scroll, H_Scroll') { gridData(horizontalAlignment: GridData.FILL, verticalAlignment: GridData.FILL, grabExcessHorizontalSpace: true, grabExcessVerticalSpace: true ) } text.addExtendedModifyListener(this) } mainapp.open() </pre></td></tr></table> <p>And here is the extract from the java code (175 lines)</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Menu createEditMenu() { Menu bar = shell.getMenuBar (); Menu menu = new Menu (bar); MenuItem item = new MenuItem (menu, SWT.PUSH); item.setText (resources.getString("Cut_menuitem")); item.setAccelerator(SWT.MOD1 + 'X'); item.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { handleCutCopy(); text.cut(); } }); item = new MenuItem (menu, SWT.PUSH); item.setText (resources.getString("Copy_menuitem")); item.setAccelerator(SWT.MOD1 + 'C'); item.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { handleCutCopy(); text.copy(); } }); item = new MenuItem (menu, SWT.PUSH); item.setText (resources.getString("Paste_menuitem")); item.setAccelerator(SWT.MOD1 + 'V'); item.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { text.paste(); } }); new MenuItem (menu, SWT.SEPARATOR); item = new MenuItem (menu, SWT.PUSH); item.setText (resources.getString("Font_menuitem")); item.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { setFont(); } }); return menu; } Menu createFileMenu() { Menu bar = shell.getMenuBar (); Menu menu = new Menu (bar); MenuItem item = new MenuItem (menu, SWT.PUSH); item.setText (resources.getString("Exit_menuitem")); item.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { shell.close (); } }); return menu; } void createMenuBar () { Menu bar = new Menu (shell, SWT.BAR); shell.setMenuBar (bar); MenuItem fileItem = new MenuItem (bar, SWT.CASCADE); fileItem.setText (resources.getString("File_menuitem")); fileItem.setMenu (createFileMenu ()); MenuItem editItem = new MenuItem (bar, SWT.CASCADE); editItem.setText (resources.getString("Edit_menuitem")); editItem.setMenu (createEditMenu ()); } void createShell (Display display) { shell = new Shell (display); shell.setText (resources.getString("Window_title")); images.loadAll (display); GridLayout layout = new GridLayout(); layout.numColumns = 1; shell.setLayout(layout); shell.addDisposeListener (new DisposeListener () { public void widgetDisposed (DisposeEvent e) { if (font != null) font.dispose(); images.freeAll (); RED.dispose(); GREEN.dispose(); BLUE.dispose(); } }); } void createStyledText() { initializeColors(); text = new StyledText (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); GridData spec = new GridData(); spec.horizontalAlignment = GridData.FILL; spec.grabExcessHorizontalSpace = true; spec.verticalAlignment = GridData.FILL; spec.grabExcessVerticalSpace = true; text.setLayoutData(spec); text.addExtendedModifyListener(new ExtendedModifyListener() { public void modifyText(ExtendedModifyEvent e) { handleExtendedModify(e); } }); } void createToolBar() { toolBar = new ToolBar(shell, SWT.NONE); SelectionAdapter listener = new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { setStyle (event.widget); } }; boldButton = new ToolItem(toolBar, SWT.CHECK); boldButton.setImage(images.Bold); boldButton.setToolTipText(resources.getString("Bold")); boldButton.addSelectionListener(listener); italicButton = new ToolItem(toolBar, SWT.CHECK); italicButton.setImage(images.Italic); italicButton.setToolTipText(resources.getString("Italic")); italicButton.addSelectionListener(listener); underlineButton = new ToolItem(toolBar, SWT.CHECK); underlineButton.setImage(images.Underline); underlineButton.setToolTipText(resources.getString("Underline")); underlineButton.addSelectionListener(listener); strikeoutButton = new ToolItem(toolBar, SWT.CHECK); strikeoutButton.setImage(images.Strikeout); strikeoutButton.setToolTipText(resources.getString("Strikeout")); strikeoutButton.addSelectionListener(listener); ToolItem item = new ToolItem(toolBar, SWT.SEPARATOR); item = new ToolItem(toolBar, SWT.PUSH); item.setImage(images.Red); item.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { fgColor(RED); } }); item = new ToolItem(toolBar, SWT.PUSH); item.setImage(images.Green); item.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { fgColor(GREEN); } }); item = new ToolItem(toolBar, SWT.PUSH); item.setImage(images.Blue); item.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { fgColor(BLUE); } }); item = new ToolItem(toolBar, SWT.SEPARATOR); item = new ToolItem(toolBar, SWT.PUSH); item.setImage(images.Erase); item.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { clear(); } }); } public static void main (String [] args) { Display display = new Display (); TextEditor example = new TextEditor (); Shell shell = example.open (display); while (!shell.isDisposed ()) if (!display.readAndDispatch ()) display.sleep (); display.dispose (); } public Shell open (Display display) { createShell (display); createMenuBar (); createToolBar (); createStyledText (); shell.setSize(500, 300); shell.open (); return shell; } </pre></td></tr></table> <p>So basically the groovy code is about 1/3 of the java code and a lot more readable.</p> <p>For the complete code see the attachment or <a href="http://svn.codehaus.org/groovy/trunk/groovy/modules/groovy-swt/src/examples/">subversion</a>.</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