the corner office

a blog, by Colin Pretorius

# Eclipse RCP howtos

Contents:

How do radio buttons work?

Radio buttons on the web are straight-forward, they seem less so with Eclipse. Create a button of type SWT.RADIO. Each button needs to have a SelectionEvent associated with it, and when the event fires, the called SelectionAdapter can update some shared variable.

See:

  • University of Manitoba tutorial for examples.
  • Source code for RadioGroupFieldEditor - utility class which simplifies working with radio buttons. Designed for working with preferences, principle is useful for a generic radio button utility class.

How to refresh a table after a file or object has changed

Call TableViewer.refresh(). Generally speaking the view containing the table would have wrap the refresh method with a custom refresh method of its own - although a better approach is to decouple everything by registering listeners and notifiers.

See also:

How do I create a context menu?

How do I create a dialog?

Override org.eclipse.jface.dialogs.Dialog. The class looks a little overwhelming, but many of the protected methods don't need overriding. A dialog is structured as a window with a 'dialog area' and a 'button bar' at the bottom. If you're happy to work with that, then the primary methods that need overriding are:

  • createDialogArea() - for the contents of the dialog
  • createButtonsForButtonBar() - to add buttons
  • configureShell() - to set the window title.
  • okPressed(), cancelPressed(), buttonPressed(int buttonId) - to handle button clicks.

The default createDialogArea method sets up a Grid layout and does some useful housekeeping, so the API docs' recommended usage is:

protected Control createDialogArea(Composite parent) {
    Composite composite = (Composite) super.createDialogArea(parent);
    //add controls to composite as necessary
    return composite;
}

Dialog's default code for creating buttons is illustrative:

protected void createButtonsForButtonBar(Composite parent) {
    // create OK and Cancel buttons by default
    createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
    createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
}

Respond to button clicks by overriding one or all of okPressed(), cancelPressed(), or buttonPressed(int buttonId). The default behaviours for OK and cancel are to set the return code (a Window concept) and close the dialog. buttonPressed can handle buttons other than cancel and OK. The default implementation just palms off calls to okPressed and cancelPressed and ignores other buttons.

Finally, if you want to set the window title or icon, you have to override a Window method (this is taken from MessageDialog):

protected void configureShell(Shell shell) {
    super.configureShell(shell);
    if (title != null) {
        shell.setText(title);
    }
    if (titleImage != null) {
        shell.setImage(titleImage);
    }
}

The source code for Dialog is interesting and not at all hard to follow.

How do I create a multi-line text input, like a HTML textarea?

It seems it's not completely trivial. You create GridLayout, specify the dimensions of the layout, where the dimensions are calculated based on the current font. I think a utility routine might not be a bad thing.

See:

How do I set the width of a Text component?

Not straight-forward because the width (in pixels) needs to be calculated based on the Text control's font. Also, sometimes you set the size of the control and sometimes you have to provide a hint to the control's layout data. See [news.eclipse.platform.swt] How to set width of Combo or Text to match its TextLimit. Straightforward example calc in Snippet 55.

How do I prevent editors from being opened multiple times?

You should override equals() in your IEditorInput. For the simplest version of IWorkbenchPage.openEditor(), Eclipse decides whether an editor is already open by comparing the given IEditorInput to the inputs of already-instantiated editors using equals().

JavaDocs (3.3):

  • IEditorInput. Note the suggestion that the input be lightweight, and not hold a reference to model data. (Hold a primary key / identifier instead...?)
  • IWorkbenchPage.openEditor(). Note also the behaviour of the versions of this method that take the activate and matchFlags arguments.

Last modified: {2007.08.27 13.53}

Add a comment

Your name (mandatory):

Your email:
Your email address is not displayed
Your home page:

Comment (sorry, no HTML):

Remember details?
Yes No

meta

-home-
about
contact
disclaimer
articles
code
link blog

style: [?]
[plain.dark.blue]

Categories

java
linux
music
notes/domino
personal
politiek
studies
techie
thee_blog
world

RSS Feeds

rssfeed all posts
rssfeed all cmts
rssfeed tech posts
rssfeed tech cmts

Archives

2008.11
2008.10
2008.09
2008.08
2008.07
2008.06
2008.05
2008.04
2008.03
2008.02
2008.01
2007.12
2007.11
2007.10
2007.09
2007.08
2007.07
2007.06
2007.05
2007.04
2007.03
2007.02
2007.01
2006.12
2006.11
2006.10
2006.09
2006.08
2006.07
2006.06
2006.05
2006.04
2006.03
2006.02
2006.01
2005.12
2005.11
2005.10
2005.09
2005.08
2005.07
2005.06
2005.05
2005.04
2005.03
2005.02
2005.01
2004.12
2004.11
2004.10
2004.09
2004.08
2004.07
2004.06
2004.05
2004.04
2004.03
2004.02
2004.01
2003.12
2003.11
2003.10
2003.09
2003.08
2003.07
2003.06

© Colin Pretorius