Jface第三天
主要内容:
ErrorDialog();
内容:
package com.cmo.mes.dialogutil;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class KErrorDialog extends ApplicationWindow {
final String dummyPlugin = "plugin id";
public KErrorDialog() {
super(null);
// TODO Auto-generated constructor stub
}
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new RowLayout(SWT.VERTICAL));
Button btn = new Button(composite, SWT.NONE);
btn.setText("打开一人错误");
btn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Status status = new Status(Status.ERROR, dummyPlugin, 1,
"未找到该类", new ClassNotFoundException());
ErrorDialog dlg = new ErrorDialog(Display.getCurrent()
.getActiveShell(), "提示错误", "装载类时出现错误", status,
Status.ERROR);
dlg.open();
}
});
return parent;
}
public static void main(String[] args)
{
KErrorDialog kerror=new KErrorDialog();
kerror.setBlockOnOpen(true);
kerror.open();
Display.getCurrent().dispose();
}
}
说明:
ErrorDialog需要用到IStatus类,该类是一个接口,
实现接口的类有Status和MultiStatus类,
1。Status可以保存一个错误的状态信息,
2。MultiStatus可以同时保存多个错误状态信息,
public Status(int severity,String pluginId,int code,String message,Throwable EXCEPTION)
servrity:错误的级别,有Status.INFO,IStatus.ERROR,IStatus.WARNING,IStatus.OK
Status.CANCEL等,
pluginId:主要用于eclipse平台中识别插件的唯一标识ID,如果不是在eclipse平台下使用,可以随意设置
字符,
code:表示eclipse的插件状态码,
message:出现该错误时,提示给用户的信息.
exception: 异常出现的类型
如:
Status status=new Status(IStatus.WARNING,"PLUGIN_ID",1,"未找到该 类",
new ClassNot Found Exception());
同时显示多个错误:
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new RowLayout(SWT.VERTICAL));
Button btn = new Button(composite, SWT.NONE);
btn.setText("打开一人错误");
btn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Status status = new Status(IStatus.ERROR, dummyPlugin, 1,
"未找到该类", new ClassNotFoundException());
IStatus[] statusesStatus = new IStatus[3];
statusesStatus[0] = new Status(IStatus.ERROR, dummyPlugin, 1,
"未找到该类", new ClassNotFoundException());
statusesStatus[1] = new Status(IStatus.ERROR, dummyPlugin, 1,
"未找到该类", new FileNotFoundException());
statusesStatus[2] = new Status(IStatus.ERROR, dummyPlugin, 1,
"未找到该类", new RuntimeException());
MultiStatus ms = new MultiStatus(dummyPlugin, IStatus.OK,
statusesStatus, "test", new Exception());
keimDialog.openDialog(status);
keimDialog.openMDialog(ms);
}
});
DialogLIB:
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;
public class keimDialog {
public static void openDialog(Status status) {
ErrorDialog dlg = new ErrorDialog(
Display.getCurrent().getActiveShell(),
"提示错误", "装载类时出现错误",status,
Status.ERROR);
dlg.open();
}
public static void openMDialog(MultiStatus status) {
ErrorDialog dlg = new ErrorDialog(
Display.getCurrent().getActiveShell(),
"提示错误", "装载类时出现错误",status,
MultiStatus.WARNING|MultiStatus.ERROR);
dlg.open();
}
}