1 11 package org.eclipse.ui.internal.misc; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.lang.reflect.Method ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.MultiStatus; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.swt.widgets.Shell; 25 import org.eclipse.ui.internal.WorkbenchMessages; 26 import org.eclipse.ui.internal.WorkbenchPlugin; 27 import org.eclipse.ui.statushandlers.StatusManager; 28 29 35 public class StatusUtil { 36 39 protected static List flatten(IStatus aStatus) { 40 List result = new ArrayList (); 41 42 if (aStatus.isMultiStatus()) { 43 IStatus[] children = aStatus.getChildren(); 44 for (int i = 0; i < children.length; i++) { 45 IStatus currentChild = children[i]; 46 if (currentChild.isMultiStatus()) { 47 Iterator childStatiiEnum = flatten(currentChild).iterator(); 48 while (childStatiiEnum.hasNext()) { 49 result.add(childStatiiEnum.next()); 50 } 51 } else { 52 result.add(currentChild); 53 } 54 } 55 } else { 56 result.add(aStatus); 57 } 58 59 return result; 60 } 61 62 67 protected static IStatus newStatus(IStatus[] stati, String message, 68 Throwable exception) { 69 70 Assert.isTrue(message != null); 71 Assert.isTrue(message.trim().length() != 0); 72 73 return new MultiStatus(WorkbenchPlugin.PI_WORKBENCH, IStatus.ERROR, 74 stati, message, exception); 75 } 76 77 public static IStatus newStatus(String pluginId, Throwable exception) { 78 return newStatus(pluginId, getLocalizedMessage(exception), exception); 79 } 80 81 88 public static String getLocalizedMessage(Throwable exception) { 89 String message = exception.getLocalizedMessage(); 90 91 if (message != null) { 92 return message; 93 } 94 95 if (exception instanceof CoreException) { 98 CoreException ce = (CoreException)exception; 99 return ce.getStatus().getMessage(); 100 } 101 102 return WorkbenchMessages.StatusUtil_errorOccurred; 103 } 104 105 112 public static IStatus newStatus(IStatus originalStatus, String newMessage) { 113 return new Status(originalStatus.getSeverity(), 114 originalStatus.getPlugin(), originalStatus.getCode(), newMessage, originalStatus.getException()); 115 } 116 117 public static IStatus newStatus(String pluginId, String message, Throwable exception) { 118 return new Status(IStatus.ERROR, pluginId, IStatus.OK, 119 message, getCause(exception)); 120 } 121 122 public static Throwable getCause(Throwable exception) { 123 Throwable cause = null; 126 if (exception != null) { 127 if (exception instanceof CoreException) { 128 CoreException ce = (CoreException)exception; 133 cause = ce.getStatus().getException(); 134 } else { 135 try { 137 Method causeMethod = exception.getClass().getMethod("getCause", new Class [0]); Object o = causeMethod.invoke(exception, new Object [0]); 139 if (o instanceof Throwable ) { 140 cause = (Throwable ) o; 141 } 142 } 143 catch (NoSuchMethodException e) { 144 } catch (IllegalArgumentException e) { 146 } catch (IllegalAccessException e) { 148 } catch (InvocationTargetException e) { 150 } 152 } 153 154 if (cause == null) { 155 cause = exception; 156 } 157 } 158 159 return cause; 160 } 161 162 167 public static IStatus newStatus(int severity, String message, 168 Throwable exception) { 169 170 String statusMessage = message; 171 if (message == null || message.trim().length() == 0) { 172 if (exception.getMessage() == null) { 173 statusMessage = exception.toString(); 174 } else { 175 statusMessage = exception.getMessage(); 176 } 177 } 178 179 return new Status(severity, WorkbenchPlugin.PI_WORKBENCH, severity, 180 statusMessage, getCause(exception)); 181 } 182 183 188 public static IStatus newStatus(List children, String message, 189 Throwable exception) { 190 191 List flatStatusCollection = new ArrayList (); 192 Iterator iter = children.iterator(); 193 while (iter.hasNext()) { 194 IStatus currentStatus = (IStatus) iter.next(); 195 Iterator childrenIter = flatten(currentStatus).iterator(); 196 while (childrenIter.hasNext()) { 197 flatStatusCollection.add(childrenIter.next()); 198 } 199 } 200 201 IStatus[] stati = new IStatus[flatStatusCollection.size()]; 202 flatStatusCollection.toArray(stati); 203 return newStatus(stati, message, exception); 204 } 205 206 211 public static void handleStatus(IStatus status, int hint, Shell shell) { 212 StatusManager.getManager().handle(status, hint); 213 } 214 215 220 public static void handleStatus(Throwable e, int hint) { 221 StatusManager.getManager().handle( 222 newStatus(WorkbenchPlugin.PI_WORKBENCH, e), hint); 223 } 224 225 230 public static void handleStatus(String message, Throwable e, int hint) { 231 StatusManager.getManager().handle( 232 newStatus(WorkbenchPlugin.PI_WORKBENCH, message, e), hint); 233 } 234 235 240 public static void handleStatus(String message, Throwable e, int hint, 241 Shell shell) { 242 StatusManager.getManager().handle( 243 newStatus(WorkbenchPlugin.PI_WORKBENCH, message, e), hint); 244 } 245 246 251 public static void handleStatus(IStatus status, String message, int hint) { 252 StatusManager.getManager().handle(newStatus(status, message), hint); 253 } 254 255 260 public static void handleStatus(IStatus status, String message, int hint, 261 Shell shell) { 262 StatusManager.getManager().handle(newStatus(status, message), hint); 263 } 264 265 270 public static void handleStatus(String message, int hint) { 271 handleStatus(message, null, hint); 272 } 273 274 279 public static void handleStatus(String message, int hint, Shell shell) { 280 handleStatus(message, null, hint); 281 } 282 } 283 | Popular Tags |