Java/SWT JFace Eclipse/JFace Dialog

Материал из Java эксперт
Перейти к: навигация, поиск

JFace"s ErrorDialog class

   <source lang="java">

import org.eclipse.core.runtime.*; import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.events.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; /**

* This class demonstrates JFace"s ErrorDialog class
*/

public class ShowError extends ApplicationWindow {

 /**
  * ShowError constructor
  */
 public ShowError() {
   super(null);
 }
 /**
  * Runs the application
  */
 public void run() {
   // Don"t return from open() until window closes
   setBlockOnOpen(true);
   // Open the main window
   open();
   // Dispose the display
   Display.getCurrent().dispose();
 }
 /**
  * Configures the shell
  * 
  * @param shell the shell
  */
 protected void configureShell(Shell shell) {
   super.configureShell(shell);
   // Set the title bar text and the size
   shell.setText("Show Error");
   shell.setSize(400, 400);
 }
 /**
  * Creates the main window"s contents
  * 
  * @param parent the main window
  * @return Control
  */
 protected Control createContents(Composite parent) {
   Composite composite = new Composite(parent, SWT.NONE);
   composite.setLayout(new GridLayout(1, false));
   // Create a big text box to accept error text
   final Text text = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
   text.setLayoutData(new GridData(GridData.FILL_BOTH));
   // Create the button to launch the error dialog
   Button show = new Button(composite, SWT.PUSH);
   show.setText("Show Error");
   show.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent event) {
       // Create the required Status object
       Status status = new Status(IStatus.ERROR, "My Plug-in ID", 0,
           "Status Error Message", null);
       // Display the dialog
       ErrorDialog.openError(Display.getCurrent().getActiveShell(),
           "JFace Error", text.getText(), status);
     }
   });
   return composite;
 }
 /**
  * The application entry point
  * 
  * @param args the command line arguments
  */
 public static void main(String[] args) {
   new ShowError().run();
 }

}


      </source>
   
  
 
  



JFace"s IconAndMessageDialog

   <source lang="java">

import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.*; import org.eclipse.swt.events.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; /**

* This class demonstrates JFace"s IconAndMessageDialog class
*/

public class DumbUser extends ApplicationWindow {

 /**
  * DumbUser constructor
  */
 public DumbUser() {
   super(null);
 }
 /**
  * Runs the application
  */
 public void run() {
   // Don"t return from open() until window closes
   setBlockOnOpen(true);
   // Open the main window
   open();
   // Dispose the display
   Display.getCurrent().dispose();
 }
 /**
  * Creates the main window"s contents
  * 
  * @param parent the main window
  * @return Control
  */
 protected Control createContents(Composite parent) {
   Composite composite = new Composite(parent, SWT.NONE);
   composite.setLayout(new GridLayout(1, true));
   // Create the button
   Button show = new Button(composite, SWT.NONE);
   show.setText("Show");
   final Shell shell = parent.getShell();
   // Display the dialog
   show.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent event) {
       // Create and show the dialog
       DumbMessageDialog dlg = new DumbMessageDialog(shell);
       dlg.open();
     }
   });
   parent.pack();
   return composite;
 }
 /**
  * The application entry point
  * 
  * @param args the command line arguments
  */
 public static void main(String[] args) {
   new DumbUser().run();
 }

} /**

* This class demonstrates the IconAndMessageDialog class
*/

class DumbMessageDialog extends IconAndMessageDialog {

 public static final int I_DUNNO_ID = IDialogConstants.CLIENT_ID;
 public static final String I_DUNNO_LABEL = "I Dunno";
 // The image
 private Image image;
 // The label for the "hidden" message
 private Label label;
 /**
  * DumbMessageDialog constructor
  * 
  * @param parent the parent shell
  */
 public DumbMessageDialog(Shell parent) {
   super(parent);
   // Create the image
   try {
     image = new Image(parent.getDisplay(), new FileInputStream("jexp.gif"));
   } catch (FileNotFoundException e) {}
   // Set the default message
   message = "Are you sure you want to do something that dumb?";
 }
 /**
  * Sets the message
  * 
  * @param message the message
  */
 public void setMessage(String message) {
   this.message = message;
 }
 /**
  * Closes the dialog
  * 
  * @return boolean
  */
 public boolean close() {
   if (image != null) image.dispose();
   return super.close();
 }
 /**
  * Creates the dialog area
  * 
  * @param parent the parent composite
  * @return Control
  */
 protected Control createDialogArea(Composite parent) {
   createMessageArea(parent);
   // Create a composite to hold the label
   Composite composite = new Composite(parent, SWT.NONE);
   GridData data = new GridData(GridData.FILL_BOTH);
   data.horizontalSpan = 2;
   composite.setLayoutData(data);
   composite.setLayout(new FillLayout());
   // Create the label for the "hidden" message
   label = new Label(composite, SWT.LEFT);
   return composite;
 }
 /**
  * Creates the buttons
  * 
  * @param parent the parent composite
  */
 protected void createButtonsForButtonBar(Composite parent) {
   createButton(parent, IDialogConstants.YES_ID, IDialogConstants.YES_LABEL,
       true);
   createButton(parent, IDialogConstants.NO_ID, IDialogConstants.NO_LABEL, false);
   createButton(parent, I_DUNNO_ID, I_DUNNO_LABEL, false);
 }
 /**
  * Handles a button press
  * 
  * @param buttonId the ID of the pressed button
  */
 protected void buttonPressed(int buttonId) {
   // If they press I Dunno, close the dialog
   if (buttonId == I_DUNNO_ID) {
     setReturnCode(buttonId);
     close();
   } else {
     // Otherwise, have some fun
     label.setText("Yeah, right. You know nothing.");
   }
 }
 /**
  * Gets the image to use
  */
 protected Image getImage() {
   return image;
 }

}

      </source>
   
  
 
  



JFace"s InputDialog

   <source lang="java">

import org.eclipse.jface.dialogs.InputDialog; import org.eclipse.jface.dialogs.IInputValidator; import org.eclipse.jface.window.*; import org.eclipse.swt.SWT; import org.eclipse.swt.events.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; /**

* This class demonstrates JFace"s InputDialog class
*/

public class GetInput extends ApplicationWindow {

 /**
  * GetInput constructor
  */
 public GetInput() {
   super(null);
 }
 /**
  * Runs the application
  */
 public void run() {
   // Don"t return from open() until window closes
   setBlockOnOpen(true);
   // Open the main window
   open();
   // Dispose the display
   Display.getCurrent().dispose();
 }
 /**
  * Configures the shell
  * 
  * @param shell the shell
  */
 protected void configureShell(Shell shell) {
   super.configureShell(shell);
   // Set the title bar text
   shell.setText("Get Input");
 }
 /**
  * Creates the main window"s contents
  * 
  * @param parent the main window
  * @return Control
  */
 protected Control createContents(Composite parent) {
   Composite composite = new Composite(parent, SWT.NONE);
   composite.setLayout(new GridLayout(1, false));
   // Create a label to display what the user typed in
   final Label label = new Label(composite, SWT.NONE);
   label.setText("This will display the user input from InputDialog");
   // Create the button to launch the error dialog
   Button show = new Button(composite, SWT.PUSH);
   show.setText("Get Input");
   show.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent event) {
       InputDialog dlg = new InputDialog(Display.getCurrent().getActiveShell(),
           "", "Enter 5-8 characters", label.getText(), new LengthValidator());
       if (dlg.open() == Window.OK) {
         // User clicked OK; update the label with the input
         label.setText(dlg.getValue());
       }
     }
   });
   parent.pack();
   return composite;
 }
 /**
  * The application entry point
  * 
  * @param args the command line arguments
  */
 public static void main(String[] args) {
   new GetInput().run();
 }

} /**

* This class validates a String. It makes sure that the String is between 5 and 8
* characters
*/

class LengthValidator implements IInputValidator {

 /**
  * Validates the String. Returns null for no error, or an error message
  * 
  * @param newText the String to validate
  * @return String
  */
 public String isValid(String newText) {
   int len = newText.length();
   // Determine if input is too short or too long
   if (len < 5) return "Too short";
   if (len > 8) return "Too long";
   // Input must be OK
   return null;
 }

}

      </source>
   
  
 
  



JFace"s MessageDialog

   <source lang="java">

import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.events.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; /**

* This class demonstrates JFace"s MessageDialog class
*/

public class SendMessage extends ApplicationWindow {

 /**
  * SendMessage constructor
  */
 public SendMessage() {
   super(null);
 }
 /**
  * Runs the application
  */
 public void run() {
   // Don"t return from open() until window closes
   setBlockOnOpen(true);
   // Open the main window
   open();
   // Dispose the display
   Display.getCurrent().dispose();
 }
 /**
  * Configures the shell
  * 
  * @param shell the shell
  */
 protected void configureShell(Shell shell) {
   super.configureShell(shell);
   // Set the title bar text and the size
   shell.setText("Send Message");
   shell.setSize(500, 400);
 }
 /**
  * Creates the main window"s contents
  * 
  * @param parent the main window
  * @return Control
  */
 protected Control createContents(Composite parent) {
   Composite composite = new Composite(parent, SWT.NONE);
   composite.setLayout(new GridLayout(5, true));
   // Create a big text box for the message text
   final Text text = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
   GridData data = new GridData(GridData.FILL_BOTH);
   data.horizontalSpan = 5;
   text.setLayoutData(data);
   // Create the Confirm button
   Button confirm = new Button(composite, SWT.PUSH);
   confirm.setText("Confirm");
   confirm.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
   // Create the Error button
   Button error = new Button(composite, SWT.PUSH);
   error.setText("Error");
   error.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
   // Create the Information button
   Button information = new Button(composite, SWT.PUSH);
   information.setText("Information");
   information.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
   // Create the Question button
   Button question = new Button(composite, SWT.PUSH);
   question.setText("Question");
   question.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
   // Create the Warning button
   Button warning = new Button(composite, SWT.PUSH);
   warning.setText("Warning");
   warning.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
   // Create the label to display the return value
   final Label label = new Label(composite, SWT.NONE);
   data = new GridData(GridData.FILL_HORIZONTAL);
   data.horizontalSpan = 5;
   label.setLayoutData(data);
   // Save ourselves some typing
   final Shell shell = parent.getShell();
   // Display a Confirmation dialog
   confirm.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent event) {
       boolean b = MessageDialog.openConfirm(shell, "Confirm", text.getText());
       label.setText("Returned " + Boolean.toString(b));
     }
   });
   // Display an Error dialog
   error.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent event) {
       MessageDialog.openError(shell, "Error", text.getText());
       label.setText("Returned void");
     }
   });
   // Display an Information dialog
   information.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent event) {
       MessageDialog.openInformation(shell, "Information", text.getText());
       label.setText("Returned void");
     }
   });
   // Display a Question dialog
   question.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent event) {
       boolean b = MessageDialog.openQuestion(shell, "Question", text.getText());
       label.setText("Returned " + Boolean.toString(b));
     }
   });
   // Display a Warning dialog
   warning.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent event) {
       MessageDialog.openWarning(shell, "Warning", text.getText());
       label.setText("Returned void");
     }
   });
   return composite;
 }
 /**
  * The application entry point
  * 
  * @param args the command line arguments
  */
 public static void main(String[] args) {
   new SendMessage().run();
 }

}

      </source>
   
  
 
  



JFace"s ProgressMonitorDialog

   <source lang="java">

import java.lang.reflect.InvocationTargetException; import org.eclipse.jface.dialogs.*; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.events.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.operation.IRunnableWithProgress; /**

* This class demonstrates JFace"s ProgressMonitorDialog class
*/

public class ShowProgress extends ApplicationWindow {

 /**
  * ShowProgress constructor
  */
 public ShowProgress() {
   super(null);
 }
 /**
  * Runs the application
  */
 public void run() {
   // Don"t return from open() until window closes
   setBlockOnOpen(true);
   // Open the main window
   open();
   // Dispose the display
   Display.getCurrent().dispose();
 }
 /**
  * Configures the shell
  * 
  * @param shell the shell
  */
 protected void configureShell(Shell shell) {
   super.configureShell(shell);
   // Set the title bar text
   shell.setText("Show Progress");
 }
 /**
  * Creates the main window"s contents
  * 
  * @param parent the main window
  * @return Control
  */
 protected Control createContents(Composite parent) {
   Composite composite = new Composite(parent, SWT.NONE);
   composite.setLayout(new GridLayout(1, true));
   // Create the indeterminate checkbox
   final Button indeterminate = new Button(composite, SWT.CHECK);
   indeterminate.setText("Indeterminate");
   // Create the ShowProgress button
   Button showProgress = new Button(composite, SWT.NONE);
   showProgress.setText("Show Progress");
   final Shell shell = parent.getShell();
   // Display the ProgressMonitorDialog
   showProgress.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent event) {
       try {
         new ProgressMonitorDialog(shell).run(true, true,
             new LongRunningOperation(indeterminate.getSelection()));
       } catch (InvocationTargetException e) {
         MessageDialog.openError(shell, "Error", e.getMessage());
       } catch (InterruptedException e) {
         MessageDialog.openInformation(shell, "Cancelled", e.getMessage());
       }
     }
   });
   parent.pack();
   return composite;
 }
 /**
  * The application entry point
  * 
  * @param args the command line arguments
  */
 public static void main(String[] args) {
   new ShowProgress().run();
 }

}

/**

* This class represents a long running operation
*/

class LongRunningOperation implements IRunnableWithProgress {

 // The total sleep time
 private static final int TOTAL_TIME = 10000;
 // The increment sleep time
 private static final int INCREMENT = 500;
 private boolean indeterminate;
 /**
  * LongRunningOperation constructor
  * 
  * @param indeterminate whether the animation is unknown
  */
 public LongRunningOperation(boolean indeterminate) {
   this.indeterminate = indeterminate;
 }
 /**
  * Runs the long running operation
  * 
  * @param monitor the progress monitor
  */
 public void run(IProgressMonitor monitor) throws InvocationTargetException,
     InterruptedException {
   monitor.beginTask("Running long running operation",
       indeterminate ? IProgressMonitor.UNKNOWN : TOTAL_TIME);
   for (int total = 0; total < TOTAL_TIME && !monitor.isCanceled(); total += INCREMENT) {
     Thread.sleep(INCREMENT);
     monitor.worked(INCREMENT);
     if (total == TOTAL_TIME / 2) monitor.subTask("Doing second half");
   }
   monitor.done();
   if (monitor.isCanceled())
       throw new InterruptedException("The long running operation was cancelled");
 }

}

      </source>
   
  
 
  



JFace"s TitleAreaDialog

   <source lang="java">

import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.jface.dialogs.*; import org.eclipse.swt.SWT; import org.eclipse.swt.events.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; import java.io.*; /**

* This class demonstrates JFace"s TitleAreaDialog class
*/

public class ShowMyTitleAreaDialog extends ApplicationWindow {

 /**
  * ShowCustomDialog constructor
  */
 public ShowMyTitleAreaDialog() {
   super(null);
 }
 /**
  * Runs the application
  */
 public void run() {
   // Don"t return from open() until window closes
   setBlockOnOpen(true);
   // Open the main window
   open();
   // Dispose the display
   Display.getCurrent().dispose();
 }
 /**
  * Creates the main window"s contents
  * 
  * @param parent the main window
  * @return Control
  */
 protected Control createContents(Composite parent) {
   Composite composite = new Composite(parent, SWT.NONE);
   composite.setLayout(new GridLayout(1, true));
   // Create the button
   Button show = new Button(composite, SWT.NONE);
   show.setText("Show");
   final Shell shell = parent.getShell();
   // Display the TitleAreaDialog
   show.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent event) {
       // Create and show the dialog
       MyTitleAreaDialog dlg = new MyTitleAreaDialog(shell);
       dlg.open();
     }
   });
   parent.pack();
   return composite;
 }
 /**
  * The application entry point
  * 
  * @param args the command line arguments
  */
 public static void main(String[] args) {
   new ShowMyTitleAreaDialog().run();
 }

}

/**

* This class shows an about box, based on TitleAreaDialog
*/

class MyTitleAreaDialog extends TitleAreaDialog {

 // The image to display
 private Image image;
 /**
  * MyTitleAreaDialog constructor
  * 
  * @param shell the parent shell
  */
 public MyTitleAreaDialog(Shell shell) {
   super(shell);
   // Create the image
   try {
     image = new Image(null, new FileInputStream("jexp.gif"));
   } catch (FileNotFoundException e) {
     // Ignore
   }
 }
 /**
  * Closes the dialog box Override so we can dispose the image we created
  */
 public boolean close() {
   if (image != null) image.dispose();
   return super.close();
 }
 /**
  * Creates the dialog"s contents
  * 
  * @param parent the parent composite
  * @return Control
  */
 protected Control createContents(Composite parent) {
   Control contents = super.createContents(parent);
   // Set the title
   setTitle("About This Application");
   // Set the message
   setMessage("This is a JFace dialog", IMessageProvider.INFORMATION);
   // Set the image
   if (image != null) setTitleImage(image);
   return contents;
 }
 /**
  * Creates the gray area
  * 
  * @param parent the parent composite
  * @return Control
  */
 protected Control createDialogArea(Composite parent) {
   Composite composite = (Composite) super.createDialogArea(parent);
   // Create a table
   Table table = new Table(composite, SWT.FULL_SELECTION | SWT.BORDER);
   table.setLayoutData(new GridData(GridData.FILL_BOTH));
   // Create two columns and show
   TableColumn one = new TableColumn(table, SWT.LEFT);
   one.setText("Real Name");
   TableColumn two = new TableColumn(table, SWT.LEFT);
   two.setText("Preferred Name");
   table.setHeaderVisible(true);
   // Add some data
   TableItem item = new TableItem(table, SWT.NONE);
   item.setText(0, "Robert Harris");
   item.setText(1, "Bobby");
   item = new TableItem(table, SWT.NONE);
   item.setText(0, "Robert Warner");
   item.setText(1, "Rob");
   item = new TableItem(table, SWT.NONE);
   item.setText(0, "Gabor Liptak");
   item.setText(1, "Gabor");
   one.pack();
   two.pack();
   return composite;
 }
 /**
  * Creates the buttons for the button bar
  * 
  * @param parent the parent composite
  */
 protected void createButtonsForButtonBar(Composite parent) {
   createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
 }

}

      </source>