Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

To show how to use the the JFace builder I converted a couple of applications from the standard SWT example set:

  • TextEditor: A simple application showing how to use menubar, toolbar and a StyledText.
  • AddressBook: A more complete application with a simple addressbook. Shows how to use toolbars, tables, sorting, Search dialogs, editing, copy/paste

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.

Here is the extract from the groovy code (63 lines):

         mainapp = jface.applicationWindow( title:"Groovy Text Editor", location:[100,100], size:[500, 300] ) {
             gridLayout(numColumns:1)
             menuManager( text:"File" ) {
                 action ( text:"Exit", closure:{ mainapp.close() } )
             }

             menuManager( text:"Edit" ) {
                 action ( text:"Cut", accelerator: SWT.MOD1 + (int) 'X', closure:{ handleCutCopy(); text.cut() } )
                 action ( text:"Copy", accelerator: SWT.MOD1 + (int) 'C',closure:{ handleCutCopy(); text.copy() } )
                 action ( text:"Paste", accelerator: SWT.MOD1 + (int) 'P',closure:{ text.paste() } )
                 separator()
                 action ( text:"Set Font", closure:{ setFont() } )
             }
             toolBar( style:"none" ) {
                boldButton = toolItem(style:"check", toolTipText:"Bold") {
                    image( src:"src/examples/groovy/swt/examples/TextEditor/bold.png" )
                    onEvent(type:"Selection", closure:{setStyle(it.widget)})
                }
                italicButton = toolItem(style:"check", toolTipText:"Italic") {
                    image( src:"src/examples/groovy/swt/examples/TextEditor/italic.png" )
                    onEvent(type:"Selection", closure:{setStyle(it.widget)})
                }
                underlineButton = toolItem(style:"check", toolTipText:"Underline") {
                    image( src:"src/examples/groovy/swt/examples/TextEditor/underline.png" )
                    onEvent(type:"Selection", closure:{setStyle(it.widget)})
                }
                strikeoutButton = toolItem(style:"check", toolTipText:"Strikeout") {
                    image( src:"src/examples/groovy/swt/examples/TextEditor/strikeout.png" )
                    onEvent(type:"Selection", closure:{setStyle(it.widget)})
                }
                toolItem(style:"separator")
                toolItem(style:"push", toolTipText:"Red text") {
                    image( src:"src/examples/groovy/swt/examples/TextEditor/red.png" )
                    onEvent(type:"Selection", closure:{fgColor(RED)})
                }
                toolItem(style:"push", toolTipText:"Blue text") {
                    image( src:"src/examples/groovy/swt/examples/TextEditor/blue.png" )
                    onEvent(type:"Selection", closure:{fgColor(BLUE)})
                }
                toolItem(style:"push", toolTipText:"Green text") {
                    image( src:"src/examples/groovy/swt/examples/TextEditor/green.png" )
                    onEvent(type:"Selection", closure:{fgColor(GREEN)})
                }
                toolItem(style:"separator")
                toolItem(style:"push", toolTipText:"Clear formatting") {
                    image( src:"src/examples/groovy/swt/examples/TextEditor/erase.png" )
                    onEvent(type:"Selection", closure:{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.menuBarManager.updateAll( true )
        mainapp.open()
    }

And here is the extract from the java code (175 lines)

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;
}

So basically the groovy code is about 1/3 of the java code and a lot more readable.

  • No labels