TitleAreaDialog: MailDialog : Dialog « SWT JFace Eclipse « Java






TitleAreaDialog: MailDialog


import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class MailDialog extends TitleAreaDialog {
  // IDs for MailDialog buttons
  // We use large integers because we don't want
  // to conflict with system constants
  public static final int OPEN = 9999;

  public static final int DELETE = 9998;

  // List widget
  List list;

  // Initial content of the list
  String[] items;

  // Selected items
  String[] itemsToOpen;

  /**
   * Constructor for MailDialog.
   * 
   * @param shell -
   *            Containing shell
   * @param items -
   *            Mail messages passed to the dialog
   */
  public MailDialog(Shell shell, String[] items) {
    super(shell);
    this.items = items;
  }

  /**
   * @see org.eclipse.jface.window.Window#create() We complete the dialog with
   *      a title and a message
   */
  public void create() {
    super.create();
    setTitle("Mail");
    setMessage("You have mail! \n It could be vital for this evening...");
  }

  /**
   * @see org.eclipse.jface.dialogs.Dialog#
   *      createDialogArea(org.eclipse.swt.widgets.Composite) Here we fill the
   *      center area of the dialog
   */
  protected Control createDialogArea(Composite parent) {
    // Create new composite as container
    final Composite area = new Composite(parent, SWT.NULL);
    // We use a grid layout and set the size of the margins
    final GridLayout gridLayout = new GridLayout();
    gridLayout.marginWidth = 15;
    gridLayout.marginHeight = 10;
    area.setLayout(gridLayout);
    // Now we create the list widget
    list = new List(area, SWT.BORDER | SWT.MULTI);
    // We define a minimum width for the list
    final GridData gridData = new GridData();
    gridData.widthHint = 200;
    list.setLayoutData(gridData);
    // We add a SelectionListener
    list.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        // When the selection changes, we re-validate the list
        validate();
      }
    });
    // We add the initial mail messages to the list
    for (int i = 0; i < items.length; i++) {
      list.add(items[i]);
    }
    return area;
  }

  private void validate() {
    // We select the number of selected list entries
    boolean selected = (list.getSelectionCount() > 0);
    // We enable/disable the Open and Delete buttons
    getButton(OPEN).setEnabled(selected);
    getButton(DELETE).setEnabled(selected);
    if (!selected)
      // If nothing was selected, we set an error message
      setErrorMessage("Select at least one entry!");
    else
      // Otherwise we set the error message to null
      // to show the intial content of the message area
      setErrorMessage(null);
  }

  /**
   * @see org.eclipse.jface.dialogs.Dialog#
   *      createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) We
   *      replace the OK and Cancel buttons by our own creations We use the
   *      method createButton() (from Dialog), to create the new buttons
   */
  protected void createButtonsForButtonBar(Composite parent) {
    // Create Open button
    Button openButton = createButton(parent, OPEN, "Open", true);
    // Initially deactivate it
    openButton.setEnabled(false);
    // Add a SelectionListener
    openButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        // Retrieve selected entries from list
        itemsToOpen = list.getSelection();
        // Set return code
        setReturnCode(OPEN);
        // Close dialog
        close();
      }
    });
    // Create Delete button
    Button deleteButton = createButton(parent, DELETE, "Delete", false);
    deleteButton.setEnabled(false);
    // Add a SelectionListener
    deleteButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        // Get the indices of the selected entries
        int selectedItems[] = list.getSelectionIndices();
        // Remove all these entries
        list.remove(selectedItems);
        // Now re-validate the list because it has changed
        validate();
      }
    });
    // Create Cancel button
    Button cancelButton = createButton(parent, CANCEL, "Cancel", false);
    // Add a SelectionListener
    cancelButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        setReturnCode(CANCEL);
        close();
      }
    });
  }

  /**
   * Method getItemsToOpen.
   * 
   * @return String[] - the selected items
   */
  public String[] getItemsToOpen() {
    return itemsToOpen;
  }
}

           
       








Related examples in the same category

1.Dialog ShellDialog Shell
2.MessageBox ExampleMessageBox Example
3.Number Input DialogNumber Input Dialog
4.Dialog ExamplesDialog Examples
5.Demonstrates FileDialogDemonstrates FileDialog
6.A facade for the save FileDialog
7.How to create your own dialog classesHow to create your own dialog classes
8.Demonstrates the FontDialog classDemonstrates the FontDialog class
9.Demonstrates the ColorDialog classDemonstrates the ColorDialog class
10.Demonstrates the DirectoryDialog classDemonstrates the DirectoryDialog class
11.Demonstrates the custom InputDialog classDemonstrates the custom InputDialog class
12.Demonstrates the MessageBox classDemonstrates the MessageBox class
13.Dialog Example
14.Shell Dialog ExampleShell Dialog Example
15.Color Dialog ExampleColor Dialog Example
16.File Dialog ExampleFile Dialog Example
17.Font Dialog ExampleFont Dialog Example
18.Yes No Icon MessageBoxYes No Icon MessageBox
19.Print Dialog ExamplePrint Dialog Example
20.SWT Dialog ClassSWT Dialog Class
21.Prevent escape from closing a SWT dialogPrevent escape from closing a SWT dialog
22.Create a dialog shell (prompt for a value)Create a dialog shell (prompt for a value)
23.Create a SWT dialog shellCreate a SWT dialog shell