Dialog Examples : Dialog « SWT JFace Eclipse « Java






Dialog Examples

Dialog Examples

/*******************************************************************************
 * All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
 * 
 * Created on 2004-5-13 22:13:55 by JACK $Id$
 *  
 ******************************************************************************/



import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
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.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class DialogExamples extends ApplicationWindow {

  /**
   * @param parentShell
   */
  public DialogExamples(Shell parentShell) {
    super(parentShell);
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.Composite)
   */
  protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NULL);

    composite.setLayout(new GridLayout());

    /* ------ MessageDialog ------------- */
    // openQuestion
    final Button buttonOpenMessage = new Button(composite, SWT.PUSH);
    buttonOpenMessage.setText("Demo: MessageDialog.openQuestion");
    buttonOpenMessage.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        
        boolean answer =
          MessageDialog.openQuestion(
            getShell(),
            "A Simple Question",
            "Is SWT/JFace your favorite Java UI framework?");
        System.out.println("Your answer is " + (answer ? "YES" : "NO"));
      }
    });

    final Button buttonMessageDialog = new Button(composite, SWT.PUSH);
    buttonMessageDialog.setText("Demo: new MessageDialog");
    buttonMessageDialog.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        MessageDialog dialog =
          new MessageDialog(
            getShell(),
            "Select your favorite Java UI framework",
            null,
            "Which one of the following is your favorite Java UI framework?",
            MessageDialog.QUESTION,
            new String[] { "AWT", "Swing", "SWT/JFace" },
            2);
        int answer = dialog.open();
        
        switch (answer) {
          case -1: // if the user closes the dialog without clicking any button.
            System.out.println("No selection");
            break;
            
          case 0 :
            System.out.println("Your selection is: AWT");
            break;
          case 1 :
            System.out.println("Your selection is: Swing");
            break;
          case 2 :
            System.out.println("Your selection is: SWT/JFace");
            break;
          
        }
      }
    });
    
    /* ------ InputDialog ------------- */
    final Button buttonInputDialog = new Button(composite, SWT.PUSH);
    buttonInputDialog.setText("Demo: InputDialog");
    buttonInputDialog.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        IInputValidator validator = new IInputValidator() {
          public String isValid(String newText) {
            if(newText.equalsIgnoreCase("SWT/JFace") ||
                newText.equalsIgnoreCase("AWT") ||
                newText.equalsIgnoreCase("Swing"))
              return null;
            else
              return "The allowed values are: SWT/JFace, AWT, Swing";
          }
        };
        InputDialog dialog = new InputDialog(getShell(), "Question", "What's your favorite Java UI framework?", "SWT/JFace", validator);
        if(dialog.open() == Window.OK) {
          System.out.println("Your favorite Java UI framework is: " + dialog.getValue());
        }else{
          System.out.println("Action cancelled");
        }
      }
    });
    
    /* ------ ProgressMonitorDialog ------------- */
    final Button buttonProgressDialog = new Button(composite, SWT.PUSH);
    buttonProgressDialog.setText("Demo: ProgressMonitorDialog");
    buttonProgressDialog.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        IRunnableWithProgress runnableWithProgress = new IRunnableWithProgress() {
          public void run(IProgressMonitor monitor)
            throws InvocationTargetException, InterruptedException {
            monitor.beginTask("Number counting", 10);
            for(int i=0; i<10; i++) {
              if(monitor.isCanceled()) {
                monitor.done();
                return;
              }
                
              System.out.println("Count number: " + i);
              monitor.worked(1);
              Thread.sleep(500); // 0.5s.
            }
            monitor.done();
          }
        };
        
        ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
        try {
          dialog.run(true, true, runnableWithProgress);
        } catch (InvocationTargetException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        
        
      }
    });    
    return super.createContents(parent);
  }

  public static void main(String[] args) {
    ApplicationWindow window = new DialogExamples(null);
    window.setBlockOnOpen(true);
    window.open();
  }
}


           
       








Related examples in the same category

1.TitleAreaDialog: MailDialog
2.Dialog ShellDialog Shell
3.MessageBox ExampleMessageBox Example
4.Number Input DialogNumber Input Dialog
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