Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1d97b41167abcfd507bc0dd2efbd2504af47a383 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2006 IBM Corporation and others.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
// IBM Corporation - initial implementation
//------------------------------------------------------------------------------
package org.eclipse.epf.common.ui.util;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

/**
 * Helper class to display a message box.
 * 
 * @author Phong Nguyen Le
 * @author Kelvin Low
 * @since 1.0
 */
public final class MsgBox {
	/**
	 * Use this method to show error message if you don't want long path to be
	 * cut off.
	 * 
	 * @param msg
	 */
	public static final void nativeShowError(Shell shell, String msg) {
		if (shell == null) {
			shell = getDefaultShell();
			if (shell == null) {
				return;
			}
		}
		MessageBox msgBox = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
		msgBox.setMessage(msg);
		msgBox.setText(shell.getText());
		msgBox.open();
	}

	public static final void showError(String msg) {
		showError(getDefaultShell(), msg);
	}

	public static final void showError(Shell shell, String msg) {
		WrappedMessageDialog.openError(shell, shell == null ? null : shell
				.getText(), msg);
	}

	public static final void showWarning(String msg) {
		showWarning(getDefaultShell(), msg);
	}

	public static final void showWarning(Shell shell, String msg) {
		WrappedMessageDialog.openWarning(shell, shell == null ? null : shell
				.getText(), msg);
	}

	public static final Shell getDefaultShell() {
		try {
			Display d = Display.getCurrent();
			if (d == null) {
				d = Display.getDefault();
			}

			Shell s = null;
			if (d != null) {
				s = d.getActiveShell();
			}

			return s;
		} catch (RuntimeException e) {
			return null;
		}
	}

	public static final Display getDisplay() {
		try {
			Display d = Display.getCurrent();
			if (d == null) {
				d = Display.getDefault();
			}

			return d;
		} catch (RuntimeException e) {
			return null;
		}
	}

	public static final int prompt(String msg) {
		return prompt(getDefaultShell(), msg);
	}

	public static final int prompt(Shell shell, String msg) {
		return prompt(shell, null, msg, SWT.YES | SWT.NO | SWT.CANCEL);
	}

	public static final int prompt(String msg, int buttons) {
		return prompt(getDefaultShell(), null, msg, buttons);
	}

	public static final int prompt(String title, String msg, int buttons) {
		return prompt(getDefaultShell(), title, msg, buttons);
	}

	public static final int prompt(Shell shell, String msg, int buttons) {
		return prompt(shell, null, msg, buttons);
	}

	public static final int prompt(Shell shell, String title, String msg,
			int buttons) {
		MessageBox msgBox = new MessageBox(shell, buttons | SWT.ICON_QUESTION);
		msgBox.setText(title != null && title.length() > 0 ? title : shell
				.getText());
		msgBox.setMessage(msg);
		return msgBox.open();
	}

}

Back to the top