📄 dialog.java
字号:
package util;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class Dialog {
/**
* 返回错误的对话框
* @param title
* @param msg
*/
public static void openErrorDialog(String title,String msg){
openErrorDialog(title, msg, null, null);
}
public static void openErrorDialog(Shell shell, String title, String msg){
openErrorDialog(title, msg, null, null);
}
public static void openErrorDialog(String title, String msg, final Throwable ex, final Class exceptionSourceClass){
openErrorDialog(Display.getCurrent().getActiveShell(), title, msg, ex, exceptionSourceClass);
}
public static void openErrorDialog(Shell shell, String title, String msg, final Throwable ex, final Class exceptionSourceClass){
String showTitle = title;
if (title != null && title.trim().equalsIgnoreCase("error"))
showTitle = "错误";
MessageDialog dialog = new MessageDialog(shell,
showTitle, null, msg, MessageDialog.ERROR, new String[]{"发送错误",IDialogConstants.OK_LABEL}, 1){
protected void buttonPressed(int buttonId) {
if (buttonId == 0){
} else {
super.buttonPressed(buttonId);
}
}
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
}
};
dialog.open();
}
/**
* 返回错误的对话框
* @param msg
*/
public static void openErrorDialog(String msg){
MessageDialog.openError(Display.getCurrent().getActiveShell(),
"错误", msg);
}
/**
* 弹出验证错误信息的对话框
* @param msg
*/
public static void openValidateErrorDialog(String msg){
openErrorDialog(msg);
}
/**
* 弹出确认的对话框,点击ok,返回true,点击cancel返回false
* @param title
* @param msg
*/
public static boolean openConfirmDialog(String title,String msg){
return MessageDialog.openConfirm(Display.getCurrent().getActiveShell(),
title, msg);
}
/**
* 弹出确认的对话框,点击ok,返回true,点击cancel返回false
* @param msg
*/
public static boolean openConfirmDialog(String msg){
return MessageDialog.openConfirm(Display.getCurrent().getActiveShell(),
"确认", msg);
}
/**
* 弹出没有保存的提示对话框,点击ok,返回 SWT.YES,点击no,返回 SWT.NO, 点击cancel返回SWT.CANCEL
*/
public static int openUnSaveDialog() {
MessageDialog dialog = new MessageDialog(Display.getCurrent()
.getActiveShell(), "警告", null,
"是否执行保存!", MessageDialog.WARNING,
new String[] { "是",
"否", "取消"},
SWT.YES | SWT.NO | SWT.CANCEL);
dialog.open();
int result = dialog.getReturnCode();
if (result == 0)
return SWT.YES;
if (result == 1)
return SWT.NO;
else
return SWT.CANCEL;
}
public static boolean openDeleteDialog(){
return MessageDialog.openConfirm(Display.getCurrent().getActiveShell(),
"确认",
"删除");
}
/**
* 弹出警告的对话框
* @param title
* @param msg
*/
public static void openWarningDialog(String title,String msg){
MessageDialog.openWarning(Display.getCurrent().getActiveShell(),title,msg);
}
/**
* 弹出警告的对话框
* @param msg
*/
public static void openWarningDialog(String msg){
MessageDialog.openWarning(Display.getCurrent().getActiveShell(),"警告",msg);
}
/**
* 弹出没有警告的提示对话框,点击ok,返回true,点击cancel返回false
*/
public static int openWarning(String msg){
MessageBox mb = new MessageBox(Display.getCurrent().getActiveShell(),SWT.YES | SWT.NO | SWT.CANCEL);
mb.setText("警告");
mb.setMessage(msg);
return mb.open();
}
public static int openDeleteWarningDialog(String msg) {
MessageDialog dialog = new MessageDialog(Display.getCurrent()
.getActiveShell(), "警告", null,
msg, MessageDialog.WARNING,
new String[] { "确定", "取消" },
SWT.YES | SWT.CANCEL);
dialog.open();
int result = dialog.getReturnCode();
if (result == 0)
return SWT.YES;
else
return SWT.CANCEL;
}
/**
* 弹出消息的对话框
* @param title
* @param msg
*/
public static void openInformationDialog(String msg){
MessageDialog.openInformation(Display.getCurrent().getActiveShell(),"消息",msg);
}
public static void openInformationDialog(Shell shell,String msg){
MessageDialog.openInformation(shell,"消息",msg);
}
/**
* 弹出问题的对话框
* @param title
* @param msg
*/
public static boolean openQuestionDialog(String title,String msg){
return MessageDialog.openQuestion(Display.getCurrent().getActiveShell(),title,msg);
}
public static boolean openQuestionDialog(String msg){
return MessageDialog.openQuestion(Display.getCurrent().getActiveShell(),"询问",msg);
}
/**
* 弹出对话框
* Shell shell : shell
* String title : dialog's title
* String msg : dialog's message
* int style : the style of the dialog
* return int : the result of user select about the dialog
*/
public static int openDlg(Shell shell, String title, String msg, int style) {
MessageBox mb = new MessageBox(shell, style);
mb.setText(title);
mb.setMessage(msg);
return mb.open();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -