1 11 package org.eclipse.jface.dialogs; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.custom.StackLayout; 15 import org.eclipse.swt.widgets.Composite; 16 import org.eclipse.swt.widgets.ProgressBar; 17 18 24 public class ProgressIndicator extends Composite { 25 private final static int PROGRESS_MAX = 1000; 27 private boolean animated = true; 29 30 private StackLayout layout; 31 32 private ProgressBar determinateProgressBar; 33 34 private ProgressBar indeterminateProgressBar; 35 36 private double totalWork; 37 38 private double sumWorked; 39 40 46 public ProgressIndicator(Composite parent) { 47 super(parent, SWT.NULL); 48 determinateProgressBar = new ProgressBar(this, SWT.HORIZONTAL); 49 indeterminateProgressBar = new ProgressBar(this, SWT.HORIZONTAL 50 | SWT.INDETERMINATE); 51 layout = new StackLayout(); 52 setLayout(layout); 53 } 54 55 58 public void beginAnimatedTask() { 59 done(); 60 layout.topControl = indeterminateProgressBar; 61 layout(); 62 animated = true; 63 } 64 65 71 public void beginTask(int max) { 72 done(); 73 this.totalWork = max; 74 this.sumWorked = 0; 75 determinateProgressBar.setMinimum(0); 76 determinateProgressBar.setMaximum(PROGRESS_MAX); 77 determinateProgressBar.setSelection(0); 78 layout.topControl = determinateProgressBar; 79 layout(); 80 animated = false; 81 } 82 83 86 public void done() { 87 if (!animated) { 88 determinateProgressBar.setMinimum(0); 89 determinateProgressBar.setMaximum(0); 90 determinateProgressBar.setSelection(0); 91 } 92 layout.topControl = null; 93 layout(); 94 } 95 96 99 public void sendRemainingWork() { 100 worked(totalWork - sumWorked); 101 } 102 103 107 public void worked(double work) { 108 if (work == 0 || animated) { 109 return; 110 } 111 sumWorked += work; 112 if (sumWorked > totalWork) { 113 sumWorked = totalWork; 114 } 115 if (sumWorked < 0) { 116 sumWorked = 0; 117 } 118 int value = (int) (sumWorked / totalWork * PROGRESS_MAX); 119 if (determinateProgressBar.getSelection() < value) { 120 determinateProgressBar.setSelection(value); 121 } 122 } 123 } 124 | Popular Tags |