[10929] | 1 | /**
|
---|
| 2 | *#########################################################################
|
---|
| 3 | *
|
---|
| 4 | * A component of the GAT application, part of the Greenstone digital
|
---|
| 5 | * library suite from the New Zealand Digital Library Project at the
|
---|
| 6 | * University of Waikato, New Zealand.
|
---|
| 7 | *
|
---|
| 8 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 9 | *
|
---|
| 10 | * Copyright (C) New Zealand Digital Library Project
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or modify
|
---|
| 13 | * it under the terms of the GNU General Public License as published by
|
---|
| 14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 15 | * (at your option) any later version.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful,
|
---|
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 20 | * GNU General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, write to the Free Software
|
---|
| 24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 25 | *########################################################################
|
---|
| 26 | */
|
---|
| 27 | package org.greenstone.core.gui;
|
---|
| 28 |
|
---|
| 29 | import java.math.BigInteger;
|
---|
| 30 | import javax.swing.JProgressBar;
|
---|
| 31 | import javax.swing.SwingUtilities;
|
---|
| 32 |
|
---|
| 33 | /** A progress bar that doesn't pack a sad when you feed it longs (such as the sizes of files) which get mangled into negative numbers. I know how it feels, as I often have negative progress. Also tries to be efficent when updating the JProgressBar, so only updates when the value actually changes, which become necessary when you move a large number of files with millions of bytes (where upon shifting a file of a thousand bytes would be pretty insignificant, maybe not even a percent, but would fire several progress updates).
|
---|
| 34 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 35 | * @version 2.3c
|
---|
| 36 | */
|
---|
| 37 | final public class LongProgressBar
|
---|
| 38 | extends JProgressBar {
|
---|
| 39 | private BigInteger cur_value = null;
|
---|
| 40 | private BigInteger max_value = null;
|
---|
| 41 | private BigInteger min_value = null;
|
---|
| 42 | /** The previous percentage value. */
|
---|
| 43 | private int previous = -1;
|
---|
| 44 | private SafeSetIndeterminate safe_set_indeterminate = null;
|
---|
| 45 | private SafeSetString safe_set_string = null;
|
---|
| 46 | private SafeSetValue safe_set_value = null;
|
---|
| 47 |
|
---|
| 48 | public LongProgressBar() {
|
---|
| 49 | super(0, 100);
|
---|
| 50 | cur_value = BigInteger.ZERO;
|
---|
| 51 | min_value = BigInteger.ZERO;
|
---|
| 52 | max_value = BigInteger.ZERO;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public void addMaximum(int size) {
|
---|
| 56 | ///ystem.err.println("Add maximum " + size);
|
---|
| 57 | if(size > 0) {
|
---|
| 58 | max_value = max_value.add(new BigInteger((new Integer(size)).toString()));
|
---|
| 59 | update();
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public void addMaximum(long size) {
|
---|
| 64 | ///ystem.err.println("Add maximum " + size);
|
---|
| 65 | if(size > 0) {
|
---|
| 66 | max_value = max_value.add(new BigInteger((new Long(size)).toString()));
|
---|
| 67 | update();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public void addValue(int size) {
|
---|
| 72 | ///ystem.err.println("Add value " + size);
|
---|
| 73 | if(size > 0) {
|
---|
| 74 | cur_value = cur_value.add(new BigInteger((new Integer(size)).toString()));
|
---|
| 75 | update();
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public void addValue(long size) {
|
---|
| 80 | ///ystem.err.println("Add value " + size);
|
---|
| 81 | if(size > 0) {
|
---|
| 82 | cur_value = cur_value.add(new BigInteger((new Long(size)).toString()));
|
---|
| 83 | update();
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public void clear()
|
---|
| 88 | {
|
---|
| 89 | previous = -1;
|
---|
| 90 | update();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public void reset() {
|
---|
| 94 | cur_value = BigInteger.ZERO;
|
---|
| 95 | max_value = BigInteger.ZERO;
|
---|
| 96 | previous = -1;
|
---|
| 97 | update();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public void setIndeterminate(boolean state) {
|
---|
| 101 | if(safe_set_indeterminate == null) {
|
---|
| 102 | safe_set_indeterminate = new SafeSetIndeterminate(state);
|
---|
| 103 | }
|
---|
| 104 | else {
|
---|
| 105 | safe_set_indeterminate.setState(state);
|
---|
| 106 | }
|
---|
| 107 | SwingUtilities.invokeLater(safe_set_indeterminate);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | public void setMaximum(int size) {
|
---|
| 111 | ///ystem.err.println("Add maximum " + size);
|
---|
| 112 | if(size > 0) {
|
---|
| 113 | max_value = new BigInteger((new Integer(size)).toString());
|
---|
| 114 | update();
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | public void setMaximum(long size) {
|
---|
| 119 | ///ystem.err.println("Add maximum " + size);
|
---|
| 120 | if(size > 0) {
|
---|
| 121 | max_value = new BigInteger((new Long(size)).toString());
|
---|
| 122 | update();
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | public void setString(String label) {
|
---|
| 127 | safeSetString(label);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | public void setValue(int size) {
|
---|
| 131 | ///ystem.err.println("Set value " + size);
|
---|
| 132 | cur_value = new BigInteger((new Integer(size)).toString());
|
---|
| 133 | update();
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | public void setValue(long size) {
|
---|
| 137 | ///ystem.err.println("Set value " + size);
|
---|
| 138 | cur_value = new BigInteger((new Long(size)).toString());
|
---|
| 139 | update();
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /** This method is an unsafe way to set the progress bar to be indeterminate, -unless- it is called from the Event Queue. */
|
---|
| 143 | private void unsafeSetIndeterminate(boolean state) {
|
---|
| 144 | super.setIndeterminate(state);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | private void unsafeSetString(String label) {
|
---|
| 148 | super.setString(label);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | private void unsafeSetValue(int value) {
|
---|
| 152 | super.setValue(value);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | private void update() {
|
---|
| 156 | int percentage = -1;
|
---|
| 157 | if(cur_value.equals(BigInteger.ZERO) || max_value.equals(BigInteger.ZERO)) {
|
---|
| 158 | percentage = 0;
|
---|
| 159 | }
|
---|
| 160 | else {
|
---|
| 161 | BigInteger tmp_value = cur_value.multiply(new BigInteger("100"));
|
---|
| 162 | BigInteger per_value = tmp_value.divide(max_value);
|
---|
| 163 | percentage = per_value.intValue();
|
---|
| 164 | if(percentage > 100) {
|
---|
| 165 | percentage = 100;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | if(percentage != previous) {
|
---|
| 169 | ///ystem.err.println("Progress bar status: value = " + getValue());
|
---|
| 170 | previous = percentage;
|
---|
| 171 | safeSetValue(percentage);
|
---|
| 172 | safeSetString(percentage + "%");
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | /** Change the string displayed on the progress bar.
|
---|
| 176 | * @param label The name progress bar label as a <strong>String</strong>.
|
---|
| 177 | * Original code: Tad Frysinger (TeamB)
|
---|
| 178 | */
|
---|
| 179 | private void safeSetString(String label) {
|
---|
| 180 | if(safe_set_string == null) {
|
---|
| 181 | safe_set_string = new SafeSetString(label);
|
---|
| 182 | }
|
---|
| 183 | else {
|
---|
| 184 | safe_set_string.setLabel(label);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | SwingUtilities.invokeLater(safe_set_string);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /** Increase the value of the progress bar.
|
---|
| 191 | * @param ammount The ammount to increase as an <i>int</i>.
|
---|
| 192 | * Original code: Tad Frysinger (TeamB)
|
---|
| 193 | */
|
---|
| 194 | private void safeSetValue(final int ammount) {
|
---|
| 195 | // Update progress bar,
|
---|
| 196 | if(safe_set_value == null) {
|
---|
| 197 | safe_set_value = new SafeSetValue(ammount);
|
---|
| 198 | }
|
---|
| 199 | else {
|
---|
| 200 | safe_set_value.setValue(ammount);
|
---|
| 201 | }
|
---|
| 202 | SwingUtilities.invokeLater(safe_set_value);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | private class SafeSetIndeterminate
|
---|
| 206 | implements Runnable {
|
---|
| 207 | private boolean value;
|
---|
| 208 | public SafeSetIndeterminate(boolean value) {
|
---|
| 209 | this.value = value;
|
---|
| 210 | }
|
---|
| 211 | public void run() {
|
---|
| 212 | unsafeSetIndeterminate(value);
|
---|
| 213 | }
|
---|
| 214 | synchronized public void setState(boolean value) {
|
---|
| 215 | this.value = value;
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | private class SafeSetString
|
---|
| 220 | implements Runnable {
|
---|
| 221 | private String label = null;
|
---|
| 222 | public SafeSetString(String label) {
|
---|
| 223 | this.label = label;
|
---|
| 224 | }
|
---|
| 225 | public void run() {
|
---|
| 226 | unsafeSetString(label);
|
---|
| 227 | }
|
---|
| 228 | synchronized public void setLabel(String label) {
|
---|
| 229 | this.label = label;
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | private class SafeSetValue
|
---|
| 234 | implements Runnable {
|
---|
| 235 | private int value = -1;
|
---|
| 236 | public SafeSetValue(int value) {
|
---|
| 237 | this.value = value;
|
---|
| 238 | }
|
---|
| 239 | public void run() {
|
---|
| 240 | unsafeSetValue(value);
|
---|
| 241 | }
|
---|
| 242 | synchronized public void setValue(int value) {
|
---|
| 243 | this.value = value;
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 | }
|
---|