## Application dialogs

When a standard dialog is too simple for your plug-in, you can build your own dialog using the Dialog class. Earlier, we saw how the readme tool contributed an "Open Readme Browser" action in an action set. This action set is shown in the workbench tool bar and Window->Readme File Editor menu.

Now we are ready to look at the implementation of this action in the readme tool's WindowActionDelegate.

       public void run(IAction action) {
          SectionsDialog dialog = new SectionsDialog(window.getShell(),
             ReadmeModelFactory.getInstance().getSections(selection));
          dialog.open();
       }

The window action delegate for the action set uses the current selection in the resource navigator view (the .readme file) to get a list of sections in the readme file. This list and the workbench window's shell are passed to the SectionsDialog.

When the user selects the action, the SectionsDialog is opened.

The SectionsDialog is implemented in the readme tool plug-in by subclassing the Dialog class in the org.eclipse.jface.dialogs package.

The Dialog class provides basic support for building a dialog shell window, creating the common dialog buttons, and launching the dialog. The subclasses are responsible for handling the content of the dialog itself:

- createDialogArea creates the SWT controls that represent the dialog contents. This is similar to creating the controls for a view or editor.

  The SectionsDialog creates an SWT list to display the list of sections. It uses a JFace viewer to populate the list. (We'll look at JFace viewers in Viewers.) Note that our dialog does not have to create any of the buttons for the dialog since this is done by our superclass.

         protected Control createDialogArea(Composite parent) {
            Composite composite = (Composite)super.createDialogArea(parent);
            List list = new List(composite, SWT.BORDER);
            ...
            ListViewer viewer = new ListViewer(list);
            ...
            return composite;
         }

- configureShell is overridden to set an appropriate title for the shell window.

         protected void configureShell(Shell newShell) {
            super.configureShell(newShell);
            newShell.setText(MessageUtil.getString("Readme Sections"));
            ...
         }

- okButtonPressed is overridden to perform whatever action is necessary when the user presses the OK button. (You can also override cancelButtonPressed or buttonPressed(int) depending on the design of your dialog.)

SectionsDialog does not implement an okButtonPressed method. It inherits the "do-nothing" implementation from Dialog. This is not typical. Your dialog usually performs some processing in response to one of the dialog buttons being pressed.

Dialogs can be as simple or as complicated as necessary. When you implement a dialog, most of your dialog code is concerned with creating the SWT controls that represent its content area and handling any events necessary while the dialog is up. Once a button is pressed by the user, the dialog can query the state of the various controls (or viewers) that make up the dialog to determine what to do.