1 13 package org.eclipse.core.runtime; 14 15 218 public final class SubMonitor implements IProgressMonitorWithBlocking { 219 220 225 private static final int MINIMUM_RESOLUTION = 1000; 226 227 231 private static final class RootInfo { 232 private final IProgressMonitor root; 233 234 238 private String taskName = null; 239 240 244 private String subTask = null; 245 246 252 public RootInfo(IProgressMonitor root) { 253 this.root = root; 254 } 255 256 public boolean isCanceled() { 257 return root.isCanceled(); 258 } 259 260 public void setCanceled(boolean value) { 261 root.setCanceled(value); 262 } 263 264 public void setTaskName(String taskName) { 265 if (eq(taskName, this.taskName)) { 266 return; 267 } 268 this.taskName = taskName; 269 root.setTaskName(taskName); 270 } 271 272 public void subTask(String name) { 273 if (eq(subTask, name)) { 274 return; 275 } 276 277 this.subTask = name; 278 root.subTask(name); 279 } 280 281 public void worked(int i) { 282 root.worked(i); 283 } 284 285 public void clearBlocked() { 286 if (root instanceof IProgressMonitorWithBlocking) 287 ((IProgressMonitorWithBlocking) root).clearBlocked(); 288 } 289 290 public void setBlocked(IStatus reason) { 291 if (root instanceof IProgressMonitorWithBlocking) 292 ((IProgressMonitorWithBlocking) root).setBlocked(reason); 293 } 294 295 } 296 297 301 private int totalParent; 302 303 306 private int usedForParent = 0; 307 308 311 private double usedForChildren = 0.0; 312 313 317 private int totalForChildren; 318 319 324 private IProgressMonitor lastSubMonitor = null; 325 326 329 private final RootInfo root; 330 331 334 private final int flags; 335 336 342 public static final int SUPPRESS_SUBTASK = 0x0001; 343 344 352 public static final int SUPPRESS_BEGINTASK = 0x0002; 353 354 360 public static final int SUPPRESS_SETTASKNAME = 0x0004; 361 362 366 public static final int SUPPRESS_ALL_LABELS = SUPPRESS_SETTASKNAME | SUPPRESS_BEGINTASK | SUPPRESS_SUBTASK; 367 368 373 public static final int SUPPRESS_NONE = 0; 374 375 383 private SubMonitor(RootInfo rootInfo, int totalWork, int availableToChildren, int flags) { 384 root = rootInfo; 385 totalParent = (totalWork > 0) ? totalWork : 0; 386 this.totalForChildren = availableToChildren; 387 this.flags = flags; 388 } 389 390 402 public static SubMonitor convert(IProgressMonitor monitor) { 403 return convert(monitor, "", 0); } 405 406 420 public static SubMonitor convert(IProgressMonitor monitor, int work) { 421 return convert(monitor, "", work); } 423 424 439 public static SubMonitor convert(IProgressMonitor monitor, String taskName, int work) { 440 if (monitor == null) 441 monitor = new NullProgressMonitor(); 442 443 if (monitor instanceof SubMonitor) { 445 monitor.beginTask(taskName, work); 446 return (SubMonitor) monitor; 447 } 448 449 monitor.beginTask(taskName, MINIMUM_RESOLUTION); 450 return new SubMonitor(new RootInfo(monitor), MINIMUM_RESOLUTION, work, SUPPRESS_NONE); 451 } 452 453 467 public SubMonitor setWorkRemaining(int workRemaining) { 468 workRemaining = Math.max(0, workRemaining); 470 471 if (totalForChildren > 0 && totalParent > usedForParent) { 473 double remainForParent = totalParent * (1.0d - (usedForChildren / totalForChildren)); 475 usedForChildren = (workRemaining * (1.0d - remainForParent / (totalParent - usedForParent))); 476 } else 477 usedForChildren = 0.0d; 478 479 totalParent = totalParent - usedForParent; 480 usedForParent = 0; 481 totalForChildren = workRemaining; 482 return this; 483 } 484 485 492 private int consume(double ticks) { 493 if (totalParent == 0 || totalForChildren == 0) return 0; 495 496 usedForChildren += ticks; 497 498 if (usedForChildren > totalForChildren) 499 usedForChildren = totalForChildren; 500 else if (usedForChildren < 0.0) 501 usedForChildren = 0.0; 502 503 int parentPosition = (int) (totalParent * usedForChildren / totalForChildren); 504 int delta = parentPosition - usedForParent; 505 506 usedForParent = parentPosition; 507 return delta; 508 } 509 510 513 public boolean isCanceled() { 514 return root.isCanceled(); 515 } 516 517 520 public void setTaskName(String name) { 521 if ((flags & SUPPRESS_SETTASKNAME) == 0) 522 root.setTaskName(name); 523 } 524 525 539 public void beginTask(String name, int totalWork) { 540 if ((flags & SUPPRESS_BEGINTASK) == 0 && name != null) 541 root.setTaskName(name); 542 setWorkRemaining(totalWork); 543 } 544 545 548 public void done() { 549 cleanupActiveChild(); 550 int delta = totalParent - usedForParent; 551 if (delta > 0) 552 root.worked(delta); 553 554 totalParent = 0; 555 usedForParent = 0; 556 totalForChildren = 0; 557 usedForChildren = 0.0d; 558 } 559 560 563 public void internalWorked(double work) { 564 int delta = consume((work > 0.0d) ? work : 0.0d); 565 if (delta != 0) 566 root.worked(delta); 567 } 568 569 572 public void subTask(String name) { 573 if ((flags & SUPPRESS_SUBTASK) == 0) 574 root.subTask(name); 575 } 576 577 580 public void worked(int work) { 581 internalWorked(work); 582 } 583 584 587 public void setCanceled(boolean b) { 588 root.setCanceled(b); 589 } 590 591 651 public SubMonitor newChild(int totalWork) { 652 return newChild(totalWork, SUPPRESS_BEGINTASK); 653 } 654 655 715 public SubMonitor newChild(int totalWork, int suppressFlags) { 716 double totalWorkDouble = (totalWork > 0) ? totalWork : 0.0d; 717 totalWorkDouble = Math.min(totalWorkDouble, totalForChildren - usedForChildren); 718 cleanupActiveChild(); 719 720 int childFlags = SUPPRESS_NONE; 726 727 if ((flags & SUPPRESS_SETTASKNAME) != 0) { 728 childFlags |= SUPPRESS_SETTASKNAME | SUPPRESS_BEGINTASK; 732 } 733 734 if ((flags & SUPPRESS_SUBTASK) != 0) { 735 childFlags |= SUPPRESS_SUBTASK; 737 } 738 739 childFlags |= suppressFlags; 742 743 SubMonitor result = new SubMonitor(root, consume(totalWorkDouble), 0, childFlags); 744 lastSubMonitor = result; 745 return result; 746 } 747 748 private void cleanupActiveChild() { 749 if (lastSubMonitor == null) 750 return; 751 752 IProgressMonitor child = lastSubMonitor; 753 lastSubMonitor = null; 754 child.done(); 755 } 756 757 760 public void clearBlocked() { 761 root.clearBlocked(); 762 } 763 764 767 public void setBlocked(IStatus reason) { 768 root.setBlocked(reason); 769 } 770 771 protected static boolean eq(Object o1, Object o2) { 772 if (o1 == null) 773 return (o2 == null); 774 if (o2 == null) 775 return false; 776 return o1.equals(o2); 777 } 778 } 779 | Popular Tags |