RCP Message Dialog

While developing applications, it’s common to have a common set of functionalities used across most applications.
Message dialogs are one of those examples. In Rich Client Platform (RCP) applications, it may take too much time finding which JFace dialog class suits better for a giving aim…

The following notes will help in identifying which dialog to use. All the methods needed are statically available in the org.eclipse.jface.dialogs.MessageDialog class:

  • MessageDialog.openConfirm, for a confirmation dialog with an Ok/Cancel button set.
  • MessageDialog.openError, for an error dialog with an Ok button.
  • MessageDialog.openInformation, for an information dialog with an Ok button.
  • MessageDialog.openQuestion, for a question dialog with and Yes/No button set.
  • MessageDialog.openWarning, for warning dialog with an Ok button.

The org.eclipse.jface.dialogs.MessageDialogWithToggle is similar,but allows the user to adjust a toggle setting, like Yes Always/Yes/No or Yes/No/Never.

One can use org.eclipse.jface.dialogs.DialogSettings for a dialog setting, supporting loading and saving of properties in an XML file.

One can use org.eclipse.jface.dialogs.ProgressMonitorDialog to display progress during a long running operation.

An, finally, one can design your own dialog windows.
To do it, one just has to extend the org.eclipse.jface.dialogs.IconAndMessageDialog class.

Note: in RCP, the shell can be obtained using
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();.

Selecting a value from a SWT CCombo on RCP

It looks like the CCombo custom SWT object lacks some selection functionality. When using a CCombo as a table cell editor on a Rich Client Platform (RCP) application, it is (almost) impossible to detect the user selection with both the keyboard and the mouse.

The selection listener does not work as expected. Documentation says:

  • the widgetSelected method is triggered whenever a selection occurs in the control, i.e. when the user browses the option list this event is triggered;
  • the widgetDefaultSelected method is triggered when default selection occurs in the control, i.e. when the user selects an option from the list this event is triggered.

One might think that all one has to do is to catch the widgetDefaultSelected event and one would know which option the user has selected from the list.
This is only true when the user performs the selection using the keyboard, i. e. after browsing through the options list the Enter/Return key is pressed.
If the user decides to use the mouse, the widgetDefaultSelected event is not triggered at all, but widgetSelected is.

One may though one could detect the user selection with the mouse listener. But it turns out there’s no (easy) way to detect if the user has performed a selection using the mouse…

Here’s a workaround for it. Since the widgetSelected is triggered by the mouse clicks, one may think in trying to use that event. Unfortunately the event has no real useful information, like if it was triggered by a right button mouse click. But fortunately, the CCombo object does have a couple of methods that allows to infer that a selection has been made. In particular, it has a method to check if the options list is visible or not. Since a selection with the mouse makes the options list disappear, one can use it.

Here’s a code snippet that does it:

// Selections events
combo.addSelectionListener(new SelectionAdapter() {
// Selection changed, check if the options are still visible
public void widgetSelected(SelectionEvent e) {
  // If list is not visible assume that the user has
  // performed selection with the mouse
  if (!combo.getListVisible()) {
      setEditionValue(combo);
  }
}

// Option selected
public void widgetDefaultSelected(SelectionEvent e) {
  // User performed a selection with the keyboard
  setEditionValue(combo);
}
});

This is not a perfect solution, it’s a workaround, but it’s a very helpful working solution.

SWT File Dialog

During the development of an Rich Client Platform (RCP) it is common to develop a file browse dialog, here’s how to show a file dialog in SWT:

import org.eclipse.swt.widgets.FileDialog;

FileDialog dialog = new FileDialog(this.getShell(), SWT.NULL);
dialog.setFilterExtensions(new String[] { "*.txt", "*.*" });
dialog.setFilterNames(new String[] { "Text files", "All files" });
String path = dialog.open();
if (path != null) {
   File file = new File(path);
   if (file.isFile()) {
     System.out.println(file.toString());
   }
}

The snippet above filters by text files (*.txt) and all files (*.*) and it is easily applied to the click event of any button or file menu option.