10.1. Overview
Eclipse supports several predefined dialogs. For example
-
org.eclipse.swt.widgets.FileDialog
-
org.eclipse.swt.widgets.DirectoryDialog
-
org.eclipse.swt.widgets.MessageDialog
-
org.eclipse.jface.dialogs.ErrorDialog
Eclipse supports also user defined dialogs. Usually the class TitleAreaDialog is then extended.
The following will describe how to use standard dialogs.
Create a new project "de.vogella.rcp.intro.dialogs.standard". Use the "Hello RCP" as a template.
Add one command "de.vogella.rcp.intro.dialogs.standard.openDialog" and create the default handler "de.vogella.rcp.intro.dialogs.standard.handler.OpenDialog" for the command. Add the command to the menu.
Define the following code for the default handler.
package de.vogella.rcp.intro.dialogs.standard.handler;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;
public class OpenDialog extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
Shell shell = HandlerUtil.getActiveWorkbenchWindow(event).getShell();
// File standard dialog
FileDialog fileDialog = new FileDialog(shell);
// Set the text
fileDialog.setText("Select File");
// Set filter on .txt files
fileDialog.setFilterExtensions(new String[] { "*.txt" });
// Put in a readable name for the filter
fileDialog.setFilterNames(new String[] { "Textfiles(*.txt)" });
// Open Dialog and save result of selection
String selected = fileDialog.open();
System.out.println(selected);
// Directly standard selection
DirectoryDialog dirDialog = new DirectoryDialog(shell);
dirDialog.setText("Select your home directory");
String selectedDir = dirDialog.open();
System.out.println(selectedDir);
// Select Font
FontDialog fontDialog = new FontDialog(shell);
fontDialog.setText("Select your favorite font");
FontData selectedFont = fontDialog.open();
System.out.println(selectedFont);
// Select Color
ColorDialog colorDialog = new ColorDialog(shell);
colorDialog.setText("Select your favorite color");
RGB selectedColor = colorDialog.open();
System.out.println(selectedColor);
// Now a few messages
MessageDialog.openConfirm(shell, "Confirm", "Please confirm");
MessageDialog.openError(shell, "Error", "Error occured");
MessageDialog.openInformation(shell, "Info", "Info for you");
MessageDialog.openQuestion(shell, "Question", "Really, really?");
MessageDialog.openWarning(shell, "Warning", "I warn you");
return null;
}
}
Run the application. If you select your command the dialogs will be called after each other.
Eclipse provides the ElementListSelectionDialog which allows to select elements from a list.
Create a new project "de.vogella.rcp.intro.dialogs.selection". Use the "Hello RCP" as a template.
Add one command "de.vogella.rcp.intro.dialogs.selection.selectionDialog" and create the default handler "de.vogella.rcp.intro.dialogs.selection.handler.SelectionDialog" for the command. Add the command to the menu.
Define the following code in your handler.
package de.vogella.rcp.intro.dialogs.selection.handler;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.handlers.HandlerUtil;
public class SelectionDialog extends AbstractHandler {
private Object[] result;
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
Shell shell = HandlerUtil.getActiveWorkbenchWindow(event).getShell();
ElementListSelectionDialog dialog = new ElementListSelectionDialog(
shell, new LabelProvider());
dialog.setElements(new String[] { "Linux", "Mac", "Windows" });
dialog.setTitle("Which operating system are you using");
dialog.open();
result = dialog.getResult();
for (Object s : result) {
System.out.println(s.toString());
}
return null;
}
}
Run the application. If you select your command then the dialog will be displayed.
You can also define your own dialogs. Create a new project "de.vogella.rcp.intro.dialogs.custom". Use the "Hello RCP" as a template.
Create the following class MyDialog.
package de.vogella.rcp.intro.dialogs.custom.dialogs;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MyDialog extends TitleAreaDialog {
private Text firstNameText;
private Text lastNameText;
private String firstName;
private String lastName;
public MyDialog(Shell parentShell) {
super(parentShell);
}
@Override
public void create() {
super.create();
// Set the title
setTitle("This is my first own dialog");
// Set the message
setMessage("This is a TitleAreaDialog", IMessageProvider.INFORMATION);
}
@Override
protected Control createDialogArea(Composite parent) {
GridLayout layout = new GridLayout();
layout.numColumns = 2;
// layout.horizontalAlignment = GridData.FILL;
parent.setLayout(layout);
// The text fields will grow with the size of the dialog
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
Label label1 = new Label(parent, SWT.NONE);
label1.setText("First Name");
firstNameText = new Text(parent, SWT.BORDER);
firstNameText.setLayoutData(gridData);
Label label2 = new Label(parent, SWT.NONE);
label2.setText("Last Name");
lastNameText = new Text(parent, SWT.BORDER);
lastNameText.setLayoutData(gridData);
return parent;
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 3;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = SWT.CENTER;
parent.setLayoutData(gridData);
// Create Add button
// Own method as we need to overview the SelectionAdapter
createOkButton(parent, OK, "Add", true);
// Add a SelectionListener
// 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();
}
});
}
protected Button createOkButton(Composite parent, int id, String label,
boolean defaultButton) {
// increment the number of columns in the button bar
((GridLayout) parent.getLayout()).numColumns++;
Button button = new Button(parent, SWT.PUSH);
button.setText(label);
button.setFont(JFaceResources.getDialogFont());
button.setData(new Integer(id));
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (isValidInput()) {
okPressed();
}
}
});
if (defaultButton) {
Shell shell = parent.getShell();
if (shell != null) {
shell.setDefaultButton(button);
}
}
setButtonLayoutData(button);
return button;
}
private boolean isValidInput() {
boolean valid = true;
if (firstNameText.getText().length() == 0) {
setErrorMessage("Please maintain the first name");
valid = false;
}
if (lastNameText.getText().length() == 0) {
setErrorMessage("Please maintain the last name");
valid = false;
}
return valid;
}
// We allow the user to resize this dialog
@Override
protected boolean isResizable() {
return true;
}
// We need to have the textFields into Strings because the UI gets disposed
// and the Text Fields are not accessible any more.
private void saveInput() {
firstName = firstNameText.getText();
lastName = lastNameText.getText();
}
@Override
protected void okPressed() {
saveInput();
super.okPressed();
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Add one command "de.vogella.rcp.intro.dialogs.custom.openMyDialog" and create the following handler "de.vogella.rcp.intro.dialogs.custom.handler.OpenMyDialog". Add the command to the menu.
package de.vogella.rcp.intro.dialogs.custom.handler;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.handlers.HandlerUtil;
import de.vogella.rcp.intro.dialogs.custom.dialogs.MyDialog;
public class OpenMyDialog extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
MyDialog dialog = new MyDialog(HandlerUtil.getActiveWorkbenchWindow(
event).getShell());
dialog.create();
if (dialog.open() == Window.OK) {
System.out.println(dialog.getFirstName());
System.out.println(dialog.getLastName());
}
return null;
}
}
Run the application. You should be able to open the dialog. To press Ok you need to maintain the first and last name. If you press "Ok" both fields should be written to the console.
0 comments:
Post a Comment