KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > progress > ErrorNotificationManager


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.progress;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.core.runtime.jobs.Job;
26 import org.eclipse.jface.resource.ImageDescriptor;
27 import org.eclipse.jface.resource.JFaceResources;
28 import org.eclipse.ui.IWorkbench;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.internal.ExceptionHandler;
31 import org.eclipse.ui.internal.Workbench;
32 import org.eclipse.ui.internal.WorkbenchPlugin;
33 import org.eclipse.ui.progress.IProgressConstants;
34 import org.eclipse.ui.progress.WorkbenchJob;
35
36 /**
37  * The ErrorNotificationManager is the class that manages the display of
38  * error information.
39  */

40 public class ErrorNotificationManager {
41
42     private static final String JavaDoc ERROR_JOB = "errorstate.gif"; //$NON-NLS-1$
43

44     static final String JavaDoc ERROR_JOB_KEY = "ERROR_JOB"; //$NON-NLS-1$
45

46     private Collection JavaDoc errors = Collections.synchronizedSet(new HashSet JavaDoc());
47
48     private JobErrorDialog dialog;
49
50     /**
51      * Create a new instance of the receiver.
52      */

53     public ErrorNotificationManager() {
54         //No special initialization
55
}
56
57     /**
58      * Set up any images the error management needs.
59      * @param iconsRoot
60      * @throws MalformedURLException
61      */

62     void setUpImages(URL JavaDoc iconsRoot) throws MalformedURLException JavaDoc {
63         JFaceResources.getImageRegistry().put(ERROR_JOB_KEY,
64                 ImageDescriptor.createFromURL(new URL JavaDoc(iconsRoot, ERROR_JOB)));
65     }
66
67     /**
68      * Add a new error to the list for the supplied job.
69      * @param status
70      * @param job
71      */

72     void addError(IStatus status, Job job) {
73
74         //Handle out of memory errors via the workbench
75
final Throwable JavaDoc exception = status.getException();
76         if (exception != null && exception instanceof OutOfMemoryError JavaDoc) {
77             PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable JavaDoc() {
78                 /* (non-Javadoc)
79                  * @see java.lang.Runnable#run()
80                  */

81                 public void run() {
82                     ExceptionHandler.getInstance().handleException(exception);
83                 }
84             });
85
86             return;
87         }
88         ErrorInfo errorInfo = new ErrorInfo(status, job);
89         showError(errorInfo);
90     }
91
92     /**
93      * Show the error in the error dialog. This is done from the UI thread to
94      * ensure that no errors are dropped.
95      * @param errorInfo the error to be displayed
96      */

97     private void showError(final ErrorInfo errorInfo) {
98         
99         if (!PlatformUI.isWorkbenchRunning()) {
100             //We are shutdown so just log
101
WorkbenchPlugin.log(errorInfo.getJob().getName(), errorInfo.getErrorStatus());
102             return;
103         }
104
105         // We must open or update the error dialog in the UI thread to ensure that
106
// errors are not dropped
107
WorkbenchJob job = new WorkbenchJob(ProgressMessages.ErrorNotificationManager_OpenErrorDialogJob) {
108             public IStatus runInUIThread(IProgressMonitor monitor) {
109                          
110                 // Add the error in the UI thread to ensure thread safety in the dialog
111
errors.add(errorInfo);
112                 if (dialog != null) {
113                     dialog.refresh();
114                 } else if (Platform.isRunning()) {
115                     // Delay prompting if the job property is set
116
Object JavaDoc noPromptProperty = errorInfo.getJob().getProperty(IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY);
117                     boolean prompt= true;
118                     if (noPromptProperty instanceof Boolean JavaDoc) {
119                         prompt = !((Boolean JavaDoc)noPromptProperty).booleanValue();
120                     }
121                     
122                     if (prompt) {
123                         return openErrorDialog(null /* use default title */, null /* use default message */, errorInfo);
124                     }
125                 }
126                 return Status.OK_STATUS;
127             }
128         };
129         job.setSystem(true);
130         job.schedule();
131     }
132
133     /**
134      * Get the currently registered errors in the receiver.
135      * @return Collection of ErrorInfo
136      */

137     Collection JavaDoc getErrors() {
138         return errors;
139     }
140
141     /**
142      * The job caleed jobName has just failed with status status.
143      * Open the error dialog if possible - otherwise log the error.
144      * @param title the title of the dialog or <code>null</code>
145      * @param msg the message for the dialog oe <code>null</code>
146      * @param errorInfo The info the dialog is being opened for.
147      * @return IStatus
148      */

149     private IStatus openErrorDialog(String JavaDoc title, String JavaDoc msg, final ErrorInfo errorInfo) {
150         IWorkbench workbench = PlatformUI.getWorkbench();
151
152         //Abort on shutdown
153
if (workbench instanceof Workbench
154                 && ((Workbench) workbench).isClosing()) {
155             return Status.CANCEL_STATUS;
156         }
157         dialog = new JobErrorDialog(ProgressManagerUtil.getNonModalShell(), title, msg, errorInfo, IStatus.OK
158                 | IStatus.INFO | IStatus.WARNING | IStatus.ERROR);
159         
160         dialog.open();
161         return Status.OK_STATUS;
162     }
163
164     /**
165      * Remove all of the errors supplied from the list of errors.
166      * @param errorsToRemove Collection of ErrorInfo
167      */

168     void removeErrors(Collection JavaDoc errorsToRemove) {
169         errors.removeAll(errorsToRemove);
170         removeFromFinishedJobs(errorsToRemove);
171     }
172
173     /**
174      * Remove all of the errors from the finished jobs
175      * @param errorsToRemove The ErrorInfos that will be deleted.
176      */

177     private void removeFromFinishedJobs(Collection JavaDoc errorsToRemove) {
178         Iterator JavaDoc errorIterator = errorsToRemove.iterator();
179         Set JavaDoc errorStatuses = new HashSet JavaDoc();
180         while(errorIterator.hasNext()){
181             ErrorInfo next = (ErrorInfo) errorIterator.next();
182             errorStatuses.add(next.getErrorStatus());
183         }
184         
185         JobTreeElement[] infos = FinishedJobs.getInstance().getJobInfos();
186         for (int i = 0; i < infos.length; i++) {
187             if(infos[i].isJobInfo()){
188                 JobInfo info = (JobInfo) infos[i];
189                 if(errorStatuses.contains(info.getJob().getResult())) {
190                     FinishedJobs.getInstance().remove(info);
191                 }
192             }
193         }
194         
195     }
196
197     /**
198      * Clear all of the errors held onto by the receiver.
199      */

200     private void clearAllErrors() {
201         removeFromFinishedJobs(errors);
202         errors.clear();
203     }
204
205     /**
206      * Display the error for the given job and any other errors
207      * that have been accumulated. This method must be invoked
208      * from the UI thread.
209      * @param job the job whose error should be displayed
210      * @param title The title for the dialog
211      * @param msg The message for the dialog.
212      * @return <code>true</code> if the info for the job was found and the error
213      * displayed and <code>false</code> otherwise.
214      */

215     public boolean showErrorFor(Job job, String JavaDoc title, String JavaDoc msg) {
216         if (dialog != null) {
217             // The dialog is already open so the error is being displayed
218
return true;
219         }
220         ErrorInfo info = getErrorInfo(job);
221         if (job == null) {
222             info = getMostRecentError();
223         } else {
224             info = getErrorInfo(job);
225         }
226         if (info != null) {
227             openErrorDialog(title, msg, info);
228             return true;
229         }
230         return false;
231     }
232
233     /*
234      * Return the most recent error.
235      */

236     private ErrorInfo getMostRecentError() {
237         ErrorInfo mostRecentInfo = null;
238         for (Iterator JavaDoc iter = errors.iterator(); iter.hasNext();) {
239             ErrorInfo info = (ErrorInfo) iter.next();
240             if (mostRecentInfo == null || info.getTimestamp() > mostRecentInfo.getTimestamp()) {
241                 mostRecentInfo = info;
242             }
243         }
244         return mostRecentInfo;
245     }
246
247     /*
248      * Return the error info for the given job
249      */

250     private ErrorInfo getErrorInfo(Job job) {
251         for (Iterator JavaDoc iter = errors.iterator(); iter.hasNext();) {
252             ErrorInfo info = (ErrorInfo) iter.next();
253             if (info.getJob() == job) {
254                 return info;
255             }
256         }
257         return null;
258     }
259
260     /**
261      * Return whether the manager has errors to report.
262      * @return whether the manager has errors to report
263      */

264     public boolean hasErrors() {
265         return !errors.isEmpty();
266     }
267
268     /**
269      * The error dialog has been closed. Clear the list of errors and
270      * the stored dialog.
271      */

272     public void dialogClosed() {
273         dialog = null;
274         clearAllErrors();
275     }
276 }
277
Popular Tags