KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > statushandlers > StatusManager


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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
12 package org.eclipse.ui.statushandlers;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.ILogListener;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.application.WorkbenchAdvisor;
24 import org.eclipse.ui.internal.WorkbenchErrorHandlerProxy;
25 import org.eclipse.ui.internal.WorkbenchPlugin;
26 import org.eclipse.ui.internal.misc.StatusUtil;
27 import org.eclipse.ui.internal.progress.FinishedJobs;
28 import org.eclipse.ui.internal.progress.StatusAdapterHelper;
29 import org.eclipse.ui.internal.statushandlers.StatusHandlerRegistry;
30 import org.eclipse.ui.progress.IProgressConstants;
31
32 /**
33  * <p>
34  * StatusManager is the entry point for all statuses to be reported in the
35  * user interface.
36  * </p>
37  *
38  * <p>
39  * Handlers shoudn't be used directly but through the StatusManager singleton
40  * which keeps the status handling policy and chooses handlers.
41  * <code>StatusManager.getManager().handle(IStatus)</code> and
42  * <code>handle(IStatus status, int style)</code> are the methods are the
43  * primary access points to the StatusManager.
44  * </p>
45  *
46  * <p>
47  * Acceptable styles (can be combined with logical OR)
48  * <ul>
49  * <li>NONE - a style indicating that the status should not be acted on. This
50  * is used by objects such as log listeners that do not want to report a status
51  * twice</li>
52  * <li>LOG - a style indicating that the status should be logged only</li>
53  * <li>SHOW - a style indicating that handlers should show a problem to an user
54  * without blocking the calling method while awaiting user response. This is
55  * generally done using a non modal {@link Dialog}</li>
56  * <li>BLOCK - a style indicating that the handling should block the calling
57  * method until the user has responded. This is generally done using a modal
58  * window such as a {@link Dialog}</li>
59  * </ul>
60  * </p>
61  *
62  * <p>
63  * Handlers are intended to be accessed via the status manager. The StatusManager chooses
64  * which handler should be used for a particular error. There are two ways for adding
65  * handlers to the handling flow. First using extension point
66  * <code>org.eclipse.ui.statusHandlers</code>, second by the workbench
67  * advisor and its method {@link WorkbenchAdvisor#getWorkbenchErrorHandler()}.
68  * If a handler is associated with a product, it is used instead of this defined
69  * in advisor.
70  * </p>
71  *
72  * @since 3.3
73  * @see AbstractStatusHandler
74  */

75 public class StatusManager {
76     /**
77      * A style indicating that the status should not be acted on. This is used
78      * by objects such as log listeners that do not want to report a status
79      * twice.
80      */

81     public static final int NONE = 0;
82
83     /**
84      * A style indicating that the status should be logged only.
85      */

86     public static final int LOG = 0x01;
87
88     /**
89      * A style indicating that handlers should show a problem to an user without
90      * blocking the calling method while awaiting user response. This is
91      * generally done using a non modal {@link Dialog}.
92      */

93     public static final int SHOW = 0x02;
94
95     /**
96      * A style indicating that the handling should block the calling method
97      * until the user has responded. This is generally done using a modal window
98      * such as a {@link Dialog}.
99      */

100     public static final int BLOCK = 0x04;
101
102     private static StatusManager MANAGER;
103
104     private AbstractStatusHandler workbenchHandler;
105
106     private List JavaDoc loggedStatuses = new ArrayList JavaDoc();
107
108     /**
109      * Returns StatusManager singleton instance.
110      *
111      * @return the manager instance
112      */

113     public static StatusManager getManager() {
114         if (MANAGER == null) {
115             MANAGER = new StatusManager();
116         }
117         return MANAGER;
118     }
119
120     private StatusManager() {
121         Platform.addLogListener(new StatusManagerLogListener());
122     }
123
124     /**
125      * @return the workbench status handler
126      */

127     private AbstractStatusHandler getWorkbenchHandler() {
128         if (workbenchHandler == null) {
129             workbenchHandler = new WorkbenchErrorHandlerProxy();
130         }
131
132         return workbenchHandler;
133     }
134
135     /**
136      * Handles the given status adapter due to the style. Because the facility
137      * depends on Workbench, this method will log the status, if Workbench isn't
138      * initialized and the style isn't NONE. If Workbench isn't initialized and
139      * the style is NONE, the manager will do nothing.
140      *
141      * @param statusAdapter
142      * the status adapter. Both the status adapter and the wrapped
143      * status may not be <code>null</code>.
144      * @param style
145      * the style.Value can be combined with logical OR. One of
146      * {@link #NONE}, {@link #LOG}, {@link #SHOW} and {@link #BLOCK}.
147      *
148      */

149     public void handle(StatusAdapter statusAdapter, int style) {
150         try {
151             // The manager will only log the status, if Workbench isn't
152
// initialized and the style isn't NONE. If Workbench isn't
153
// initialized and the style is NONE, the manager will do nothing.
154
if (!PlatformUI.isWorkbenchRunning()) {
155                 if (style != StatusManager.NONE) {
156                     logError(statusAdapter.getStatus());
157                 }
158                 return;
159             }
160
161             // tries to handle the problem with default (product) handler
162
if (StatusHandlerRegistry.getDefault()
163                     .getDefaultHandlerDescriptor() != null) {
164                 try {
165                     StatusHandlerRegistry.getDefault()
166                             .getDefaultHandlerDescriptor().getStatusHandler()
167                             .handle(statusAdapter, style);
168                     // if statuses are shown, all finished jobs with error will
169
// be removed,
170
// we should remove it from the status manager, when error
171
// icon
172
// will be part of handlers not ProgressAnimationItem
173
if (((style & StatusManager.SHOW) == StatusManager.SHOW || (style & StatusManager.BLOCK) == StatusManager.BLOCK)
174                             && statusAdapter
175                                     .getProperty(IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY) != Boolean.TRUE) {
176                         FinishedJobs.getInstance().removeErrorJobs();
177                         StatusAdapterHelper.getInstance().clear();
178                     }
179                     return;
180                 } catch (CoreException ex) {
181                     logError("Errors during the default handler creating", ex); //$NON-NLS-1$
182
}
183             }
184
185             // delegates the problem to workbench handler
186
getWorkbenchHandler().handle(statusAdapter, style);
187
188             // if statuses are shown, all finished jobs with error will be
189
// removed,
190
// we should remove it from the status manager, when error icon
191
// will be part of handlers not ProgressAnimationItem
192
if (((style & StatusManager.SHOW) == StatusManager.SHOW || (style & StatusManager.BLOCK) == StatusManager.BLOCK)
193                     && statusAdapter
194                             .getProperty(IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY) != Boolean.TRUE) {
195                 FinishedJobs.getInstance().removeErrorJobs();
196             }
197         } catch (Throwable JavaDoc ex) {
198             // The used status handler failed
199
logError(statusAdapter.getStatus());
200             logError("Error occurred during status handling", ex); //$NON-NLS-1$
201
}
202     }
203
204     /**
205      * Handles the given status adapter. The log style is used when this method
206      * is called.
207      *
208      * @param statusAdapter
209      * the status adapter. Both the status adapter and the wrapped
210      * status may not be <code>null</code>.
211      */

212     public void handle(StatusAdapter statusAdapter) {
213         handle(statusAdapter, StatusManager.LOG);
214     }
215
216     /**
217      * Handles the given status due to the style. Because the facility depends
218      * on Workbench, this method will log the status, if Workbench isn't
219      * initialized and the style isn't NONE. If Workbench isn't initialized and
220      * the style is NONE, the manager will do nothing.
221      *
222      * @param status
223      * the status to handle. May not be <code>null</code>.
224      * @param style
225      * the style. Acceptable values are defined in
226      * {@link StatusManager} and can be combined with logical OR.
227      */

228     public void handle(IStatus status, int style) {
229         StatusAdapter statusAdapter = new StatusAdapter(status);
230         handle(statusAdapter, style);
231     }
232
233     /**
234      * Handles the given status. The log style is used when this method is
235      * called.
236      *
237      * @param status
238      * the status to handle. May not be <code>null</code>.
239      */

240     public void handle(IStatus status) {
241         handle(status, StatusManager.LOG);
242     }
243
244     /**
245      * This method informs the StatusManager that this IStatus is being handled
246      * by the handler and to ignore it when it shows up in our ILogListener.
247      *
248      * @param status
249      * already handled and logged status
250      */

251     public void addLoggedStatus(IStatus status) {
252         loggedStatuses.add(status);
253     }
254
255     private void logError(String JavaDoc message, Throwable JavaDoc ex) {
256         IStatus status = StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH,
257                 message, ex);
258         addLoggedStatus(status);
259         WorkbenchPlugin.log(status);
260     }
261
262     private void logError(IStatus status) {
263         addLoggedStatus(status);
264         WorkbenchPlugin.log(status);
265     }
266
267     /**
268      * This log listener handles statuses added to a plug-in's log. If our own
269      * WorkbenchErrorHandler inserts it into the log, then ignore it.
270      *
271      * @see #addLoggedStatus(IStatus)
272      * @since 3.3
273      */

274     private class StatusManagerLogListener implements ILogListener {
275
276         /*
277          * (non-Javadoc)
278          *
279          * @see org.eclipse.core.runtime.ILogListener#logging(org.eclipse.core.runtime.IStatus,
280          * java.lang.String)
281          */

282         public void logging(IStatus status, String JavaDoc plugin) {
283             if (!loggedStatuses.contains(status)) {
284                 handle(status, StatusManager.NONE);
285             } else {
286                 loggedStatuses.remove(status);
287             }
288         }
289     }
290 }
291
Popular Tags