1 11 package org.eclipse.ui.internal.progress; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import java.util.Collection ; 16 import java.util.Collections ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Set ; 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 40 public class ErrorNotificationManager { 41 42 private static final String ERROR_JOB = "errorstate.gif"; 44 static final String ERROR_JOB_KEY = "ERROR_JOB"; 46 private Collection errors = Collections.synchronizedSet(new HashSet ()); 47 48 private JobErrorDialog dialog; 49 50 53 public ErrorNotificationManager() { 54 } 56 57 62 void setUpImages(URL iconsRoot) throws MalformedURLException { 63 JFaceResources.getImageRegistry().put(ERROR_JOB_KEY, 64 ImageDescriptor.createFromURL(new URL (iconsRoot, ERROR_JOB))); 65 } 66 67 72 void addError(IStatus status, Job job) { 73 74 final Throwable exception = status.getException(); 76 if (exception != null && exception instanceof OutOfMemoryError ) { 77 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable () { 78 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 97 private void showError(final ErrorInfo errorInfo) { 98 99 if (!PlatformUI.isWorkbenchRunning()) { 100 WorkbenchPlugin.log(errorInfo.getJob().getName(), errorInfo.getErrorStatus()); 102 return; 103 } 104 105 WorkbenchJob job = new WorkbenchJob(ProgressMessages.ErrorNotificationManager_OpenErrorDialogJob) { 108 public IStatus runInUIThread(IProgressMonitor monitor) { 109 110 errors.add(errorInfo); 112 if (dialog != null) { 113 dialog.refresh(); 114 } else if (Platform.isRunning()) { 115 Object noPromptProperty = errorInfo.getJob().getProperty(IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY); 117 boolean prompt= true; 118 if (noPromptProperty instanceof Boolean ) { 119 prompt = !((Boolean )noPromptProperty).booleanValue(); 120 } 121 122 if (prompt) { 123 return openErrorDialog(null , null , errorInfo); 124 } 125 } 126 return Status.OK_STATUS; 127 } 128 }; 129 job.setSystem(true); 130 job.schedule(); 131 } 132 133 137 Collection getErrors() { 138 return errors; 139 } 140 141 149 private IStatus openErrorDialog(String title, String msg, final ErrorInfo errorInfo) { 150 IWorkbench workbench = PlatformUI.getWorkbench(); 151 152 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 168 void removeErrors(Collection errorsToRemove) { 169 errors.removeAll(errorsToRemove); 170 removeFromFinishedJobs(errorsToRemove); 171 } 172 173 177 private void removeFromFinishedJobs(Collection errorsToRemove) { 178 Iterator errorIterator = errorsToRemove.iterator(); 179 Set errorStatuses = new HashSet (); 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 200 private void clearAllErrors() { 201 removeFromFinishedJobs(errors); 202 errors.clear(); 203 } 204 205 215 public boolean showErrorFor(Job job, String title, String msg) { 216 if (dialog != null) { 217 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 236 private ErrorInfo getMostRecentError() { 237 ErrorInfo mostRecentInfo = null; 238 for (Iterator 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 250 private ErrorInfo getErrorInfo(Job job) { 251 for (Iterator 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 264 public boolean hasErrors() { 265 return !errors.isEmpty(); 266 } 267 268 272 public void dialogClosed() { 273 dialog = null; 274 clearAllErrors(); 275 } 276 } 277 | Popular Tags |