1 11 package org.eclipse.ui.internal.statushandlers; 12 13 import java.util.Collection ; 14 import java.util.Collections ; 15 import java.util.Date ; 16 import java.util.HashSet ; 17 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.jobs.Job; 20 import org.eclipse.jface.dialogs.ErrorDialog; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.swt.SWTException; 23 import org.eclipse.swt.events.DisposeListener; 24 import org.eclipse.ui.PlatformUI; 26 import org.eclipse.ui.internal.WorkbenchPlugin; 27 import org.eclipse.ui.internal.progress.ProgressManagerUtil; 28 import org.eclipse.ui.internal.progress.ProgressMessages; 29 import org.eclipse.ui.progress.IProgressConstants; 30 import org.eclipse.ui.statushandlers.StatusAdapter; 31 32 import com.ibm.icu.text.DateFormat; 33 34 38 public class StatusNotificationManager { 39 40 private Collection errors = Collections.synchronizedSet(new HashSet ()); 41 42 private StatusDialog dialog; 43 44 private static StatusNotificationManager sharedInstance; 45 46 private DisposeListener disposeListener = new DisposeListener() { 47 52 public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) { 53 dialog = null; 54 errors.clear(); 55 } 56 }; 57 58 63 public static StatusNotificationManager getInstance() { 64 if (sharedInstance == null) { 65 sharedInstance = new StatusNotificationManager(); 66 } 67 return sharedInstance; 68 } 69 70 73 private StatusNotificationManager() { 74 } 75 76 82 public void addError(StatusAdapter statusAdapter, final boolean modal) { 83 84 if (ErrorDialog.AUTOMATED_MODE == true) { 85 return; 86 } 87 88 final StatusInfo statusInfo = new StatusInfo(statusAdapter); 89 90 if (!PlatformUI.isWorkbenchRunning()) { 91 WorkbenchPlugin.log(statusInfo.getStatus().getStatus()); 93 return; 94 } 95 96 if (dialog == null || dialog.getShell().isDisposed()) { 99 100 errors.add(statusInfo); 101 Object noPromptProperty = statusInfo.getStatus().getProperty( 103 IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY); 104 105 boolean prompt = true; 106 if (noPromptProperty instanceof Boolean ) { 107 prompt = !((Boolean ) noPromptProperty).booleanValue(); 108 } 109 110 if (prompt && !PlatformUI.getWorkbench().isClosing()) { 111 try{ 112 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable () { 113 public void run() { 114 115 117 if(PlatformUI.getWorkbench().isClosing()) 118 return; 119 120 if (dialog == null) { 121 dialog = new StatusDialog(ProgressManagerUtil 122 .getDefaultParent(), statusInfo, 123 IStatus.INFO | IStatus.WARNING 124 | IStatus.ERROR, modal); 125 dialog.open(); 126 dialog.getShell().addDisposeListener( 127 disposeListener); 128 } 129 } 130 }); 131 } 132 catch(SWTException exception){ 133 } 136 137 } 138 } else { 139 140 if (statusInfo.getStatus().getProperty( 141 IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY) != null) { 142 statusInfo.getStatus().setProperty( 143 IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY, 144 Boolean.FALSE); 145 } 146 147 if (!PlatformUI.getWorkbench().isClosing()) { 148 try{ 149 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable () { 150 public void run() { 151 if(PlatformUI.getWorkbench().isClosing()) 152 return; 153 openStatusDialog(modal,statusInfo); 154 } 155 }); 156 } 157 catch (SWTException exception){ 158 } 161 } 162 } 163 } 164 165 170 Collection getErrors() { 171 return errors; 172 } 173 174 178 void openStatusDialog(final boolean modal, final StatusInfo statusInfo) { 179 errors.add(statusInfo); 180 if (modal && !dialog.isModal()) { 181 dialog.getShell().removeDisposeListener(disposeListener); 182 dialog.close(); 183 dialog = new StatusDialog(ProgressManagerUtil.getDefaultParent(), 184 statusInfo, IStatus.INFO | IStatus.WARNING | IStatus.ERROR, 185 modal); 186 187 dialog.open(); 188 dialog.getShell().addDisposeListener(disposeListener); 189 } else { 190 dialog.refresh(); 191 } 192 } 193 194 198 protected static class StatusInfo implements Comparable { 199 200 public boolean equals(Object obj) { 201 if (obj instanceof StatusInfo) { 202 return statusAdapter.equals(((StatusInfo) obj).getStatus()); 203 } 204 return super.equals(obj); 205 } 206 207 public int hashCode() { 208 return statusAdapter.hashCode(); 209 } 210 211 private final StatusAdapter statusAdapter; 212 213 220 public StatusInfo(StatusAdapter statusAdapter) { 221 this.statusAdapter = statusAdapter; 222 223 Object timestampProperty = statusAdapter 224 .getProperty(StatusAdapter.TIMESTAMP_PROPERTY); 225 226 if (timestampProperty == null 227 || !(timestampProperty instanceof Long )) { 228 statusAdapter.setProperty(StatusAdapter.TIMESTAMP_PROPERTY, 229 new Long (System.currentTimeMillis())); 230 } 231 } 232 233 String getDisplayString() { 234 String text = statusAdapter.getStatus().getMessage(); 235 236 Job job = (Job) (statusAdapter.getAdapter(Job.class)); 237 if (job != null) { 238 text = job.getName(); 239 } 240 241 return NLS.bind(ProgressMessages.JobInfo_Error, 242 (new Object [] { 243 text, 244 DateFormat.getDateTimeInstance(DateFormat.LONG, 245 DateFormat.LONG).format( 246 new Date (getTimestamp())) })); 247 } 248 249 254 public long getTimestamp() { 255 return ((Long ) statusAdapter 256 .getProperty(StatusAdapter.TIMESTAMP_PROPERTY)).longValue(); 257 } 258 259 264 public int compareTo(Object arg0) { 265 if (arg0 instanceof StatusInfo) { 266 long otherTimestamp = ((StatusInfo) arg0).getTimestamp(); 268 if (getTimestamp() < otherTimestamp) { 269 return -1; 270 } else if (getTimestamp() > otherTimestamp) { 271 return 1; 272 } else { 273 return getDisplayString().compareTo( 274 ((StatusInfo) arg0).getDisplayString()); 275 } 276 } 277 return 0; 278 } 279 280 283 public StatusAdapter getStatus() { 284 return statusAdapter; 285 } 286 } 287 } 288 | Popular Tags |