# Eclipse RCP howtos
Contents:
- How do radio buttons work?
- How to refresh a table after a file or object has changed
- How do I create a context menu?
- How do I create a dialog?
- How do I create a multi-line text input, like a HTML textarea?
- How do I set the width of a Text component?
- How do I prevent the same editor (object) from being opened multiple times?
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:
- [news.eclipse.platform.rcp] Re: how to update a view in real-time?
- FAQ How do I make a view respond to selection changes in another view?. Deals with the related issue of having one view update its contents in response to a selection in another view.
How do I create a context menu?
- SWT: Snippet 40. Pure hardcoding.
- JFace: Snippet 005. Again, pure hard coding.
- Submenus: [news.eclipse.platform] JFace, MenuManager, submenu in a popup-menu.
- In a RCP world:
- FAQ How to create a context menu & add actions to the same?. Outdated with 3.3.
-
Tonny Madsen: Working with the
menusextension point. Overview of how it now works in 3.3. - Eclipse wiki: Menu Contributions. Discussion of 3.3 menus.
-
Creating an Eclipse View. Outdated, but detailed discussion of how old-style menus work, and explains importance of
setRemoveAllWhenShown(true).
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 dialogcreateButtonsForButtonBar()- to add buttonsconfigureShell()- 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:
- [news.eclipse.platform.rcp] Re: Making a multi-line text control display multiple lines.
- [news.eclipse.platform.swt] Re: Multi Line Text or StyledText Widget
- [news.eclipse.platform.swt] Re: Multi-line Text with constant number of visible rows...
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
activateandmatchFlagsarguments.
Last modified: {2007.08.27 13.53}