<更新記録>
2007年 12月 28日
執筆

姉妹サイト検索 Web検索


ErrorDialog

ErrorDialog

エラーを表示する場合、MessageDialogを使用して表示してもいいのですが、 ErrorDialogクラスを使用すると、より詳細なエラーの表示が可能になります。

しかし、 ErrorDialogは、若干Eclipse IDEを意識した構造になっているため、 一般アプリケーション開発においては意味のないプロパティーをコンストラクタに指定しなければいけないなどの使い勝手の悪さがあります

ErrorDialogの使い方も、MessageDialogとほぼ同じです。 ErrorDialogオブジェクトを作成して、その後でopenメソッドを呼び出すだけです。 ただ、ErrorDialogでは、IStatusインターフェイスが関連してきます。

IStatus

IStatusインターフェイスは、エラー情報をオブジェクトにラップしてしまうためのAPIを提供 します。 エラーメッセージや、例外オブジェクトです。 さらにEclipse IDEで使用する際には、プラグインのIDや状態も格納します。

                 +---------+
                 | IStatus |
                 +---------+
                      ^ 
        +-------------+----------------+
        |             |                |
+-------------+    +--------+    +------------+
| MergeStatus |    | Status |    | TeamStatus |
+-------------+    +--------+    +------------+
                      ^
                      |
                +-------------+
                | MultiStatus |
                +-------------+

Statusクラスは、1つのエラー情報をラップします。

Status(
	int severity,
	String pluginId,
	int code,
	String message,
	Throwable exception)
	
Status(
	int severity,
	String pluinId,
	String message)

Status(
	int severity,
	String pluginId,
	String message,
	Throwable exception)
severity引数は、それがどのような情報を表すかを指定します。 IStatus.CANCEL、IStatus.Error、IStatus.INFO、IStatus.OK、IStatus.WARNINGの何れかが指定可能です。 pluginId引数は、Eclipse IDEで有意義になる引数です。そうでなければ適当にダミーを指定します。 code引数は、Eclipse IDEのプラグイン用の開発でないのならば、常にIStatus.OKを指定します。 message引数は、エラーのメッセージを指定します。 exception引数は、発生した例外を指定します。

MultiStatusクラスは、複数のエラー情報を1つのエラー情報オブジェクトにラップします。 このクラスを使って複数のエラーをラップすると、エラー情報の木構造が作成されます。

MultiStatus(
	String pluginId,
	int code,
	IStatus[] newChildren,
	String message,
	Throwable exception
)

MultiStatus(
	String pluginId,
	int code,
	String message,
	Throwable exception
)
pluginId引数は、Eclipse IDEのプラグイン開発でないならば、適当に文字列を指定します。 code引数は、Eclipse IDEのプラグイン開発でないならば、IStatus.OKを指定します。 newChildren引数は、まとめたい複数のStatusオブジェクトを指定します。 ここに指定したオブジェクトは、IStatus#getChildrenメソッドを通して取得されます。 message引数は、このエラー情報のメッセージを指定します。 exception引数は、例外を指定します。

ErrorDialogサンプル

サンプルでは、静的にIStatusオブジェクトを作成していますが、 実際のアプリケーションでは、各クラス等から発生した例外等をキャッチして動的にIStatusオブジェクトを作成します。

ErrorDialogDemonstrate.java
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.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

public class ErrorDialogDemonstrate extends ApplicationWindow {
	private ErrorDialogDemonstrate() {
		super(null);
	}
	
	protected Control createContents(Composite parent) {
		
		Button errDlgOpenner = new Button(parent, SWT.PUSH);
		errDlgOpenner.setText("open error dialog");
		errDlgOpenner.pack();
		
		errDlgOpenner.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				ErrorDialog errDlg = new ErrorDialog(
						getShell(),
						"エラー",
						"エラーが発生しました。",
						getIStatus(),
						IStatus.ERROR | IStatus.WARNING);
				errDlg.open();
			}
		});
		
		return parent;
	}
	
	private IStatus getIStatus() {
		String dummy = "dummy";
		
		IStatus warnStatus = new Status(
				IStatus.WARNING,
				dummy,
				"なにかがwarning");
		
		IStatus errorStatus = new Status(
				IStatus.ERROR,
				dummy,
				"なにかがerror");
		
		return new MultiStatus(
				dummy,
				IStatus.OK,
				new IStatus[] {warnStatus, errorStatus},
				"何かがおかしい",
				new Exception());
	}
	
	public static void main(String[] args) {
		Window w = new ErrorDialogDemonstrate();
		w.setBlockOnOpen(true);
		w.open();
		Display.getCurrent().dispose();
	}
}


Powered by VeryEasyCMS