1 19 20 package org.netbeans.api.progress.aggregate; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import javax.swing.Action ; 26 import javax.swing.JComponent ; 27 import javax.swing.JLabel ; 28 import org.netbeans.api.progress.ProgressHandle; 29 import org.netbeans.api.progress.ProgressHandleFactory; 30 import org.openide.util.Cancellable; 31 32 42 public final class AggregateProgressHandle { 43 private ProgressMonitor monitor; 44 private ProgressHandle handle; 45 static final int WORKUNITS = 10000; 46 private boolean finished; 47 private Collection <ProgressContributor> contributors; 48 private int current; 49 50 51 AggregateProgressHandle(String displayName, ProgressContributor[] contribs, Cancellable cancellable, Action listAction, boolean systemtask) { 52 handle = ProgressHandleFactory.createHandle(displayName, cancellable, listAction); 53 finished = false; 54 contributors = new ArrayList <ProgressContributor>(); 55 if (contribs != null) { 56 for (int i = 0; i < contribs.length; i++) { 57 addContributor(contribs[i]); 58 } 59 } 60 } 61 62 63 67 public void start() { 68 start(-1); 69 } 70 71 76 public synchronized void start(long estimate) { 77 handle.start(WORKUNITS, estimate); 78 current = 0; 79 } 80 81 85 public synchronized void finish() { 86 if (finished) { 87 return; 88 } 89 finished = true; 90 handle.finish(); 91 } 92 93 102 public void setInitialDelay(int millis) { 103 handle.setInitialDelay(millis); 104 } 105 106 110 public synchronized void addContributor(ProgressContributor contributor) { 111 if (finished) { 112 return; 113 } 114 int length = contributors.size(); 116 int remainingUnits = 0; 117 double completedRatio = 0; 118 Iterator <ProgressContributor> it; 119 if (length > 0) { 120 it = contributors.iterator(); 121 while (it.hasNext()) { 122 ProgressContributor cont = it.next(); 123 remainingUnits = remainingUnits + cont.getRemainingParentWorkUnits(); 124 completedRatio = completedRatio + (1 - cont.getCompletedRatio()); 125 } 126 } else { 127 remainingUnits = WORKUNITS; 128 completedRatio = 0; 129 } 130 131 int currentShare = (int)(remainingUnits / (completedRatio + 1)); 133 it = contributors.iterator(); 136 while (it.hasNext()) { 137 ProgressContributor cont = it.next(); 138 int newshare = (int)((1 - cont.getCompletedRatio()) * currentShare); 139 remainingUnits = remainingUnits - newshare; 141 cont.setAvailableParentWorkUnits(newshare); 142 } 143 contributor.setAvailableParentWorkUnits(remainingUnits); 145 contributors.add(contributor); 146 contributor.setParent(this); 147 148 } 149 150 153 int getCurrentProgress() { 154 return current; 155 } 156 157 158 void processContributorStep(ProgressContributor contributor, String message, int delta) { 159 synchronized (this) { 160 if (finished) { 161 return; 162 } 163 current = current + delta; 164 handle.progress(message, current); 165 } 166 if (monitor != null) { 169 monitor.progressed(contributor); 170 } 171 172 } 173 174 void processContributorStart(ProgressContributor contributor, String message) { 175 synchronized (this) { 176 if (finished) { 177 return; 178 } 179 if (message != null) { 180 handle.progress(message); 181 } 182 } 183 if (monitor != null) { 186 monitor.started(contributor); 187 } 188 } 189 190 void processContributorFinish(ProgressContributor contributor) { 191 synchronized (this) { 192 if (finished) { 193 return; 194 } 195 contributors.remove(contributor); 196 if (contributors.size() == 0) { 197 finish(); 198 } 199 } 200 if (monitor != null) { 203 monitor.finished(contributor); 204 } 205 } 206 207 208 211 public void setMonitor(ProgressMonitor monitor) { 212 this.monitor = monitor; 213 } 214 215 220 public void setDisplayName(String newDisplayName) { 221 handle.setDisplayName(newDisplayName); 222 } 223 224 227 JComponent extractComponent() { 228 return ProgressHandleFactory.createProgressComponent(handle); 229 } 230 231 JLabel extractDetailLabel() { 232 return ProgressHandleFactory.createDetailLabelComponent(handle); 233 } 234 235 JLabel extractMainLabel() { 236 return ProgressHandleFactory.createMainLabelComponent(handle); 237 } 238 } 239 | Popular Tags |