Dialogs are almost always used in every application. Dialogs are used to either inform user or to take user input. In Eclipse dialogs can be broadly classified into two categories i.e. SWT dialogs and JFace Dialogs. We will discuss how to create dialogs in eclipse pluginsstarting with Introduction to SWT Dialogs.
SWT Dialogs
Figure 6-1
Dialog class is an abstract class from which you can derive concrete native dialogs. The SWT already contains some concrete subclasses of dialog, such as shown in table below:
Subclass | Description |
ColorDialog | Dialog for selecting a color |
DirectoryDialog | Dialog for selecting a directory in host file system |
FileDialog | Dialog for selecting a file in host file system. Supported styles are SWT.OPEN and SWT.SAVE |
FontDialog | Dialog for selecting text font |
MessageBox | Dialog for displaying a message. Various style parameters govern which buttons will be displayed to the user For ex: SWT.OK, SWT.CLOSE etc. Icon to be displayed with the message can be governed by styles such as SWT.ICON_ERROR, SWT.ICON_QUESTION etc. |
PrintDialog | Dialog for selecting printer and for printer settings |
JFace Dialogs
The package org.eclipse.jface.dialogs provides classes that implement standard Dialog. All these classes are subclasses of abstract class Dialog which itself is a subclass of abstract class Window. Following table describes some of the most commonly used subclasses of Jface Dialog.
Subclass | Description |
IconAndMessageDialog | superclass of dialogs with an icon and a message. |
SelectionDialog | superclass for displaying a selection |
StatusDialog | superclass for dialogs with status bar |
TitleAreaDialog | dialog having a title area. |
ErrorDialog | A dialog to display errors |
MessageDialog | dialog class for showing messages to the user. |
ListDialog | A dialog which prompts for one element out of a list. |
ProgressMonitorDialog | A modal dialog to display progress of a operation. |
WizardDialog | A dialog to show a wizard to the end user. |
InputDialog | A simple input dialog for getting an input from the user. |
import org.eclipse.core.runtime.IStatus; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.InputDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.ui.IViewActionDelegate; import org.eclipse.ui.IViewPart; public class ResourceManagerViewActionDelegate implements IViewActionDelegate { private IViewPart view; public void init(IViewPart view) { this.view = view; // cache the view part, this will be used in run action // to fetch the parent shell for dialog } public void run(IAction action) { InputDialog dialog = new InputDialog(view.getSite().getShell(),"Lets try!", "Please enter your name","",null); // new input dialog if( dialog.open()== IStatus.OK){ // open dialog and wait for return status code. // If user clicks ok display message box String value = dialog.getValue(); // fetch the value entered by the user. MessageBox box = new MessageBox(view.getSite().getShell(),SWT.ICON_INFORMATION); box.setMessage("Hey there! You entered : " + value); box.open(); }else{ MessageBox box = new MessageBox(view.getSite().getShell(),SWT.ICON_INFORMATION); box.setMessage("Bye!"); box.open(); } } public void selectionChanged(IAction action, ISelection selection) {} }