KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > util > DialogUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.intro.impl.util;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.jface.dialogs.ErrorDialog;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.IWorkbenchWindow;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.internal.intro.impl.Messages;
23
24 /**
25  * Utiliy class for Pop-Up dialogs. This is an NL enabled utility class (ie: you
26  * do not have to NL enable the message before calling the methods here if you
27  * are calling one of the methids that takes an id as a parameter).
28  */

29
30 public class DialogUtil {
31
32     /**
33      * Displays core error dialog with a message from the Core error status
34      * object, and a user message. <br>
35      * The user message is logged using Log.logError().
36      */

37     public static void displayCoreErrorDialog(Shell parent, String JavaDoc msg,
38             CoreException coreEx) {
39
40         if (msg == null)
41             msg = coreEx.getMessage();
42         String JavaDoc title = Messages.MessageDialog_errorTitle;
43         if (parent == null)
44             parent = getActiveShell();
45         IStatus status = coreEx.getStatus();
46         ErrorDialog.openError(parent, title, msg, status);
47         Log.error(msg, coreEx);
48     }
49
50     /**
51      * Displays error dialog with the given message. <br>
52      */

53     public static void displayErrorMessage(Shell parent, String JavaDoc msg,
54             Throwable JavaDoc ex) {
55         String JavaDoc title = Messages.MessageDialog_errorTitle;
56         if (parent == null)
57             parent = getActiveShell();
58         MessageDialog.openError(parent, title, msg);
59         Log.error(msg, ex);
60     }
61
62
63     /**
64      * Displays error dialog with a message. <br>
65      * The user message is formatted with the passed variables. Also logs the
66      * error using Log.logError().
67      */

68     public static void displayErrorMessage(Shell parent, String JavaDoc msg,
69             Object JavaDoc[] variables, Throwable JavaDoc ex) {
70         if (msg == null)
71             return;
72         if (variables != null)
73             msg = NLS.bind(msg, variables);
74         displayErrorMessage(parent, msg, ex);
75     }
76
77     /**
78      * Displays warning dialog with a given message. <br>
79      * also logs the info using Log.logWarning(). msg error message to display
80      * and log.
81      */

82     public static void displayWarningMessage(Shell parent, String JavaDoc msg) {
83         String JavaDoc title = Messages.MessageDialog_warningTitle;
84         if (parent == null)
85             parent = getActiveShell();
86         MessageDialog.openWarning(parent, title, msg);
87         Log.warning(msg);
88     }
89
90     /**
91      * Displays warning dialog with a message. <br>
92      * also logs the info using Log.logWarning().
93      */

94     public static void displayWarningMessage(Shell parent, String JavaDoc msg,
95             Object JavaDoc[] variables) {
96         if (msg == null)
97             return;
98         if (variables != null)
99             msg = NLS.bind(msg, variables);
100         displayWarningMessage(parent, msg);
101     }
102
103     /**
104      * Displays info dialog with a message. Also logs the info using
105      * Log.logInfo().
106      */

107     public static void displayInfoMessage(Shell parent, String JavaDoc msg) {
108         String JavaDoc title = Messages.MessageDialog_infoTitle;
109         if (parent == null)
110             parent = getActiveShell();
111         MessageDialog.openInformation(parent, title, msg);
112         Log.info(msg);
113
114     }
115
116     /**
117      * Displays info dialog with a message. Also logs the info using
118      * Log.logInfo().
119      */

120     public static void displayInfoMessage(Shell parent, String JavaDoc msg,
121             Object JavaDoc[] variables) {
122         if (msg == null)
123             return;
124         if (variables != null)
125             msg = NLS.bind(msg, variables);
126         displayInfoMessage(parent, msg);
127     }
128
129     public static IWorkbenchWindow getActiveWorkbenchWindow() {
130         return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
131     }
132
133     /**
134      * Utility method to best find the active shell.
135      */

136     public static Shell getActiveShell() {
137         Display display = getCurrentDisplay();
138         Shell activeShell = display.getActiveShell();
139         if (activeShell == null)
140             return getActiveWorkbenchWindow().getShell();
141         return activeShell;
142     }
143
144     /**
145      * Utility method to best find the active Display.
146      */

147     public static Display getCurrentDisplay() {
148         Display display = Display.getCurrent();
149         if (display != null)
150             return display;
151         return Display.getDefault();
152     }
153
154 }
155
Popular Tags