1 11 package org.eclipse.ui.internal.progress; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.QualifiedName; 18 import org.eclipse.core.runtime.jobs.Job; 19 import org.eclipse.jface.viewers.Viewer; 20 import org.eclipse.jface.viewers.ViewerComparator; 21 import org.eclipse.jface.window.IShellProvider; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.graphics.GC; 24 import org.eclipse.swt.graphics.Point; 25 import org.eclipse.swt.graphics.Rectangle; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Shell; 28 import org.eclipse.ui.IWorkbench; 29 import org.eclipse.ui.IWorkbenchPage; 30 import org.eclipse.ui.IWorkbenchWindow; 31 import org.eclipse.ui.PartInitException; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.internal.RectangleAnimation; 34 import org.eclipse.ui.internal.WorkbenchPlugin; 35 import org.eclipse.ui.internal.WorkbenchWindow; 36 import org.eclipse.ui.internal.misc.StatusUtil; 37 import org.eclipse.ui.internal.util.BundleUtility; 38 import org.eclipse.ui.progress.IProgressConstants; 39 import org.eclipse.ui.views.IViewDescriptor; 40 41 45 46 public class ProgressManagerUtil { 47 51 public static long SHORT_OPERATION_TIME = 250; 52 53 static final QualifiedName KEEP_PROPERTY = IProgressConstants.KEEP_PROPERTY; 54 55 static final QualifiedName KEEPONE_PROPERTY = IProgressConstants.KEEPONE_PROPERTY; 56 57 static final Object [] EMPTY_OBJECT_ARRAY = new Object [0]; 58 59 static final QualifiedName INFRASTRUCTURE_PROPERTY = new QualifiedName(WorkbenchPlugin.PI_WORKBENCH,"INFRASTRUCTURE_PROPERTY"); 61 62 private static String ellipsis = ProgressMessages.ProgressFloatingWindow_EllipsisValue; 63 64 70 static IStatus exceptionStatus(Throwable exception) { 71 return StatusUtil.newStatus(IStatus.ERROR, 72 exception.getMessage() == null ? "" : exception.getMessage(), exception); 74 } 75 76 81 static void logException(Throwable exception) { 82 BundleUtility.log(PlatformUI.PLUGIN_ID, exception); 83 } 84 85 98 static ViewerComparator getProgressViewerComparator() { 99 return new ViewerComparator() { 100 106 public int compare(Viewer testViewer, Object e1, Object e2) { 107 return ((Comparable ) e1).compareTo(e2); 108 } 109 }; 110 } 111 112 117 static void openProgressView(WorkbenchWindow window) { 118 IWorkbenchPage page = window.getActivePage(); 119 if (page == null) { 120 return; 121 } 122 try { 123 IViewDescriptor reference = WorkbenchPlugin.getDefault() 124 .getViewRegistry() 125 .find(IProgressConstants.PROGRESS_VIEW_ID); 126 127 if (reference == null) { 128 return; 129 } 130 page.showView(IProgressConstants.PROGRESS_VIEW_ID); 131 } catch (PartInitException exception) { 132 logException(exception); 133 } 134 } 135 136 146 static String shortenText(String textValue, Control control) { 147 if (textValue == null) { 148 return null; 149 } 150 GC gc = new GC(control); 151 int maxWidth = control.getBounds().width - 5; 152 if (gc.textExtent(textValue).x < maxWidth) { 153 gc.dispose(); 154 return textValue; 155 } 156 int length = textValue.length(); 157 int ellipsisWidth = gc.textExtent(ellipsis).x; 158 int secondWord = findSecondWhitespace(textValue, gc, maxWidth); 160 int pivot = ((length - secondWord) / 2) + secondWord; 161 int start = pivot; 162 int end = pivot + 1; 163 while (start >= secondWord && end < length) { 164 String s1 = textValue.substring(0, start); 165 String s2 = textValue.substring(end, length); 166 int l1 = gc.textExtent(s1).x; 167 int l2 = gc.textExtent(s2).x; 168 if (l1 + ellipsisWidth + l2 < maxWidth) { 169 gc.dispose(); 170 return s1 + ellipsis + s2; 171 } 172 start--; 173 end++; 174 } 175 gc.dispose(); 176 return textValue; 177 } 178 179 190 private static int findSecondWhitespace(String textValue, GC gc, 191 int maxWidth) { 192 int firstCharacter = 0; 193 char[] chars = textValue.toCharArray(); 194 for (int i = 0; i < chars.length; i++) { 196 if (Character.isWhitespace(chars[i])) { 197 firstCharacter = i; 198 break; 199 } 200 } 201 if (firstCharacter == 0) { 203 return 0; 204 } 205 int secondCharacter = firstCharacter; 207 for (int i = firstCharacter; i < chars.length; i++) { 209 if (Character.isWhitespace(chars[i])) { 210 secondCharacter = i; 211 break; 212 } 213 } 214 if (gc.textExtent(textValue.substring(0, secondCharacter)).x > maxWidth) { 217 if (gc.textExtent(textValue.substring(0, firstCharacter)).x > maxWidth) { 218 return 0; 219 } 220 return firstCharacter; 221 } 222 return secondCharacter; 223 } 224 225 233 public static boolean rescheduleIfModalShellOpen(Job openJob) { 234 Shell modal = getModalShellExcluding(null); 235 if (modal == null) { 236 return false; 237 } 238 239 openJob.schedule(PlatformUI.getWorkbench().getProgressService() 241 .getLongOperationTime()); 242 return true; 243 } 244 245 257 public static boolean safeToOpen(ProgressMonitorJobsDialog dialog, 258 Shell excludedShell) { 259 Shell modal = getModalShellExcluding(excludedShell); 260 if (modal == null) { 261 return true; 262 } 263 264 dialog.watchTicks(); 265 return false; 266 } 267 268 277 public static Shell getModalShellExcluding(Shell shell) { 278 IWorkbench workbench = PlatformUI.getWorkbench(); 279 Shell[] shells = workbench.getDisplay().getShells(); 280 int modal = SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL 281 | SWT.PRIMARY_MODAL; 282 for (int i = 0; i < shells.length; i++) { 283 if (shells[i].equals(shell)) { 284 break; 285 } 286 if (shells[i].isVisible()) { 288 int style = shells[i].getStyle(); 289 if ((style & modal) != 0) { 290 return shells[i]; 291 } 292 } 293 } 294 return null; 295 } 296 297 305 public static Shell getDefaultParent() { 306 Shell modal = getModalShellExcluding(null); 307 if (modal != null) { 308 return modal; 309 } 310 311 return getNonModalShell(); 312 } 313 314 319 public static Shell getNonModalShell() { 320 IWorkbenchWindow window = PlatformUI.getWorkbench() 321 .getActiveWorkbenchWindow(); 322 if (window == null) { 323 IWorkbenchWindow[] windows = PlatformUI.getWorkbench() 324 .getWorkbenchWindows(); 325 if (windows.length > 0) 326 return windows[0].getShell(); 327 } else 328 return window.getShell(); 329 330 return null; 331 } 332 333 340 public static void animateDown(Rectangle startPosition) { 341 IWorkbenchWindow currentWindow = PlatformUI.getWorkbench() 342 .getActiveWorkbenchWindow(); 343 if (currentWindow == null) { 344 return; 345 } 346 WorkbenchWindow internalWindow = (WorkbenchWindow) currentWindow; 347 348 ProgressRegion progressRegion = internalWindow.getProgressRegion(); 349 if (progressRegion == null) { 350 return; 351 } 352 Rectangle endPosition = progressRegion.getControl().getBounds(); 353 354 Point windowLocation = internalWindow.getShell().getLocation(); 355 endPosition.x += windowLocation.x; 356 endPosition.y += windowLocation.y; 357 RectangleAnimation animation = new RectangleAnimation(internalWindow 358 .getShell(), startPosition, endPosition); 359 animation.schedule(); 360 } 361 362 369 public static void animateUp(Rectangle endPosition) { 370 IWorkbenchWindow currentWindow = PlatformUI.getWorkbench() 371 .getActiveWorkbenchWindow(); 372 if (currentWindow == null) { 373 return; 374 } 375 WorkbenchWindow internalWindow = (WorkbenchWindow) currentWindow; 376 Point windowLocation = internalWindow.getShell().getLocation(); 377 378 ProgressRegion progressRegion = internalWindow.getProgressRegion(); 379 if (progressRegion == null) { 380 return; 381 } 382 Rectangle startPosition = progressRegion.getControl().getBounds(); 383 startPosition.x += windowLocation.x; 384 startPosition.y += windowLocation.y; 385 386 RectangleAnimation animation = new RectangleAnimation(internalWindow 387 .getShell(), startPosition, endPosition); 388 animation.schedule(); 389 } 390 391 398 static IShellProvider getShellProvider() { 399 return new IShellProvider() { 400 401 406 public Shell getShell() { 407 return getDefaultParent(); 408 } 409 }; 410 } 411 412 416 public static URL getIconsRoot() { 417 return BundleUtility.find(PlatformUI.PLUGIN_ID, 418 ProgressManager.PROGRESS_FOLDER); 419 } 420 421 425 public static URL getProgressSpinnerLocation(){ 426 try { 427 return new URL (getIconsRoot(), "progress_spinner.gif"); } catch (MalformedURLException e) { 429 return null; 430 } 431 } 432 } 433 | Popular Tags |