1 11 package org.eclipse.core.runtime; 12 13 42 public class SubProgressMonitor extends ProgressMonitorWrapper { 43 44 50 public static final int SUPPRESS_SUBTASK_LABEL = 1 << 1; 51 57 public static final int PREPEND_MAIN_LABEL_TO_SUBTASK = 1 << 2; 58 59 private int parentTicks = 0; 60 private double sentToParent = 0.0; 61 private double scale = 0.0; 62 private int nestedBeginTasks = 0; 63 private boolean usedUp = false; 64 private boolean hasSubTask = false; 65 private int style; 66 private String mainTaskLabel; 67 68 77 public SubProgressMonitor(IProgressMonitor monitor, int ticks) { 78 this(monitor, ticks, 0); 79 } 80 81 97 public SubProgressMonitor(IProgressMonitor monitor, int ticks, int style) { 98 super(monitor); 99 this.parentTicks = (ticks > 0) ? ticks : 0; 100 this.style = style; 101 } 102 103 113 public void beginTask(String name, int totalWork) { 114 nestedBeginTasks++; 115 if (nestedBeginTasks > 1) { 117 return; 118 } 119 scale = totalWork <= 0 ? 0 : (double) parentTicks / (double) totalWork; 123 if ((style & PREPEND_MAIN_LABEL_TO_SUBTASK) != 0) { 124 mainTaskLabel = name; 125 } 126 } 127 128 131 public void done() { 132 if (nestedBeginTasks == 0 || --nestedBeginTasks > 0) 135 return; 136 double remaining = parentTicks - sentToParent; 138 if (remaining > 0) 139 super.internalWorked(remaining); 140 if (hasSubTask) 142 subTask(""); sentToParent = 0; 144 } 145 146 149 public void internalWorked(double work) { 150 if (usedUp || nestedBeginTasks != 1) { 151 return; 152 } 153 154 double realWork = (work > 0.0d) ? scale * work : 0.0d; 155 super.internalWorked(realWork); 156 sentToParent += realWork; 157 if (sentToParent >= parentTicks) { 158 usedUp = true; 159 } 160 } 161 162 165 public void subTask(String name) { 166 if ((style & SUPPRESS_SUBTASK_LABEL) != 0) { 167 return; 168 } 169 hasSubTask = true; 170 String label = name; 171 if ((style & PREPEND_MAIN_LABEL_TO_SUBTASK) != 0 && mainTaskLabel != null && mainTaskLabel.length() > 0) { 172 label = mainTaskLabel + ' ' + label; 173 } 174 super.subTask(label); 175 } 176 177 180 public void worked(int work) { 181 internalWorked(work); 182 } 183 } 184 | Popular Tags |