Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

jFace: ErrorDialog

By using ErrorDialog class, we can show one (or) more errors to users. If the Error has more information to display, then a 'Details' button is added automatically. When user clicks on 'Details' button, then it displays the complete information to user.

ErrorDialog class provides following constructor to instantiate an object.

ErrorDialog(Shell parentShell, String dialogTitle, String Message, IStatus Status, int displayMask)

Following table summarizes the parameters.

Parameter
Description
parentShell
the shell under which to create this dialog

dialogTitle
the title to use for this dialog, or null to indicate that the default title should be used
message
the message to show in this dialog, or null to indicate that the error's message should be shown as the primary message
status
the error to show to the user
displayMask
the mask to use to filter the displaying of child items, as per IStatus.matches

Note
a.   ErrorDialog don’t has any visual representation, unless you call open() method on it.
b.   The error dialog will only be displayed if there is at least one child status matching the mask.

What is IStatus object?
IStatus object represents the outcome of an operation. IStatus object carries following information.

a.   plug-in identifier (required)
b.   severity (required)
c.    status code (required)
d.   message (required) - localized to current locale
e.   exception (optional) - for problems stemming from a failure at a lower level
IStatus class provides following constants to specify the type of message.

Constant
Description
static int CANCEL
Status represents a cancelation, bitmask value is 8.
static int ERROR
Status represents an error, bitmask value is 4.
static int INFO
Status represents information, bitmask value is 1.
static int OK
Status represents normal case, bitmask value is 0.
static int WARNING
Status represents warning, bitmask value is 2.

Note
Some status objects, known as multi-statuses, have other status objects as children. SWT provides MultiStatus class, it implements IStatus interface.
package test;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {
private static MultiStatus getMultiStatus(String msg, Throwable t) {

ListStatus> statuses = new ArrayListStatus>();
StackTraceElement[] stackTraces = t.getStackTrace();

for (StackTraceElement stackTrace : stackTraces) {
Status status = new Status(IStatus.ERROR, Test.class.getName(), stackTrace.toString());
statuses.add(status);
}

MultiStatus ms = new MultiStatus(Test.class.getName(), IStatus.ERROR, statuses.toArray(new Status[] {}),
t.toString(), t);
return ms;
}

public static void main(String[] args) {
System.out.println(System.getProperty("user.home"));

/* Instantiate Display object, it represents SWT session */
Display display = new Display();

/*
* Define Shell, it represent a window, You can add more than one shell
* to Display
*/
Shell shell = new Shell(display);

/* Open shell window */
shell.open();

try {
String s = null;
s.toCharArray();
} catch (Exception e) {
String message = e.getMessage();
MultiStatus multiStatus = getMultiStatus(message, e);

ErrorDialog dialog = new ErrorDialog(Display.getDefault().getActiveShell(), "Demo Error Dialog",
"NullPointerException", multiStatus, IStatus.ERROR);
dialog.open();
}

/*
* Run the event dispatching loop until an exit condition occurs, which
* is typically when the main shell window is closed by the user.
*/

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}

/* Dispose the display */
display.dispose();
}
}


Run above application, you can able to see following window.

ErrorDialog class provides following convenient methods to open an error dialog.

static int openError(Shell parent, String dialogTitle, String message, IStatus status)
Opens an error dialog to display the given error.

static int openError(Shell parentShell, String title, String message, IStatus status, int displayMask)
Opens an error dialog to display the given error.

Ex:
String message = e.getMessage();
MultiStatus multiStatus = getMultiStatus(message, e);

ErrorDialog.openError(Display.getDefault().getActiveShell(), "Demo Error Dialog", message, multiStatus);

Following is the complete working application.
package test;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {
private static MultiStatus getMultiStatus(String msg, Throwable t) {

ListStatus> statuses = new ArrayListStatus>();
StackTraceElement[] stackTraces = t.getStackTrace();

for (StackTraceElement stackTrace : stackTraces) {
Status status = new Status(IStatus.ERROR, Test.class.getName(), stackTrace.toString());
statuses.add(status);
}

MultiStatus ms = new MultiStatus(Test.class.getName(), IStatus.ERROR, statuses.toArray(new Status[] {}),
t.toString(), t);
return ms;
}

public static void main(String[] args) {
System.out.println(System.getProperty("user.home"));

/* Instantiate Display object, it represents SWT session */
Display display = new Display();

/*
* Define Shell, it represent a window, You can add more than one shell
* to Display
*/
Shell shell = new Shell(display);

/* Open shell window */
shell.open();

try {
String s = null;
s.toCharArray();
} catch (Exception e) {
String message = e.getMessage();
MultiStatus multiStatus = getMultiStatus(message, e);

ErrorDialog.openError(Display.getDefault().getActiveShell(), "Demo Error Dialog", message, multiStatus);
}

/*
* Run the event dispatching loop until an exit condition occurs, which
* is typically when the main shell window is closed by the user.
*/

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}

/* Dispose the display */
display.dispose();
}
}






Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

jFace: ErrorDialog

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×