1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer 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.gatherer.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 refreshString() {
|
---|
88 | safeSetString(previous + "%");
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void reset() {
|
---|
92 | cur_value = BigInteger.ZERO;
|
---|
93 | max_value = BigInteger.ZERO;
|
---|
94 | previous = -1;
|
---|
95 | update();
|
---|
96 | }
|
---|
97 |
|
---|
98 | public void setIndeterminate(boolean state) {
|
---|
99 | if(safe_set_indeterminate == null) {
|
---|
100 | safe_set_indeterminate = new SafeSetIndeterminate(state);
|
---|
101 | }
|
---|
102 | else {
|
---|
103 | safe_set_indeterminate.setState(state);
|
---|
104 | }
|
---|
105 | SwingUtilities.invokeLater(safe_set_indeterminate);
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void setMaximum(int size) {
|
---|
109 | ///ystem.err.println("Add maximum " + size);
|
---|
110 | if(size > 0) {
|
---|
111 | max_value = new BigInteger((new Integer(size)).toString());
|
---|
112 | update();
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void setMaximum(long size) {
|
---|
117 | ///ystem.err.println("Add maximum " + size);
|
---|
118 | if(size > 0) {
|
---|
119 | max_value = new BigInteger((new Long(size)).toString());
|
---|
120 | update();
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void setString(String label) {
|
---|
125 | safeSetString(label);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public void setValue(int size) {
|
---|
129 | ///ystem.err.println("Set value " + size);
|
---|
130 | cur_value = new BigInteger((new Integer(size)).toString());
|
---|
131 | update();
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void setValue(long size) {
|
---|
135 | ///ystem.err.println("Set value " + size);
|
---|
136 | cur_value = new BigInteger((new Long(size)).toString());
|
---|
137 | update();
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** This method is an unsafe way to set the progress bar to be indeterminate, -unless- it is called from the Event Queue. */
|
---|
141 | public void unsafeSetIndeterminate(boolean state) {
|
---|
142 | super.setIndeterminate(state);
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void unsafeSetString(String label) {
|
---|
146 | super.setString(label);
|
---|
147 | }
|
---|
148 |
|
---|
149 | public void unsafeSetValue(int value) {
|
---|
150 | ///ystem.err.println("Set value: " + value);
|
---|
151 | super.setValue(value);
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void update() {
|
---|
155 | int percentage = -1;
|
---|
156 | if(cur_value.equals(BigInteger.ZERO) || max_value.equals(BigInteger.ZERO)) {
|
---|
157 | percentage = 0;
|
---|
158 | }
|
---|
159 | else {
|
---|
160 | BigInteger tmp_value = cur_value.multiply(new BigInteger("100"));
|
---|
161 | BigInteger per_value = tmp_value.divide(max_value);
|
---|
162 | percentage = per_value.intValue();
|
---|
163 | if(percentage > 100) {
|
---|
164 | percentage = 100;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | if(percentage != previous) {
|
---|
168 | ///ystem.err.println("Progress bar status: value = " + getValue());
|
---|
169 | previous = percentage;
|
---|
170 | safeSetValue(percentage);
|
---|
171 | safeSetString(percentage + "%");
|
---|
172 | }
|
---|
173 | }
|
---|
174 | /** Change the string displayed on the progress bar.
|
---|
175 | * @param label The name progress bar label as a <strong>String</strong>.
|
---|
176 | * Original code: Tad Frysinger (TeamB)
|
---|
177 | */
|
---|
178 | private void safeSetString(String label) {
|
---|
179 | if(safe_set_string == null) {
|
---|
180 | safe_set_string = new SafeSetString(label);
|
---|
181 | }
|
---|
182 | else {
|
---|
183 | safe_set_string.setLabel(label);
|
---|
184 | }
|
---|
185 | SwingUtilities.invokeLater(safe_set_string);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /** Increase the value of the progress bar.
|
---|
189 | * @param ammount The ammount to increase as an <i>int</i>.
|
---|
190 | * Original code: Tad Frysinger (TeamB)
|
---|
191 | */
|
---|
192 | private void safeSetValue(final int ammount) {
|
---|
193 | // Update progress bar,
|
---|
194 | if(safe_set_value == null) {
|
---|
195 | safe_set_value = new SafeSetValue(ammount);
|
---|
196 | }
|
---|
197 | else {
|
---|
198 | safe_set_value.setValue(ammount);
|
---|
199 | }
|
---|
200 | SwingUtilities.invokeLater(safe_set_value);
|
---|
201 | }
|
---|
202 |
|
---|
203 | private class SafeSetIndeterminate
|
---|
204 | implements Runnable {
|
---|
205 | private boolean value;
|
---|
206 | public SafeSetIndeterminate(boolean value) {
|
---|
207 | this.value = value;
|
---|
208 | }
|
---|
209 | public void run() {
|
---|
210 | unsafeSetIndeterminate(value);
|
---|
211 | }
|
---|
212 | synchronized public void setState(boolean value) {
|
---|
213 | this.value = value;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | private class SafeSetString
|
---|
218 | implements Runnable {
|
---|
219 | private String label = null;
|
---|
220 | public SafeSetString(String label) {
|
---|
221 | this.label = label;
|
---|
222 | }
|
---|
223 | public void run() {
|
---|
224 | unsafeSetString(label);
|
---|
225 | }
|
---|
226 | synchronized public void setLabel(String label) {
|
---|
227 | this.label = label;
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | private class SafeSetValue
|
---|
232 | implements Runnable {
|
---|
233 | private int value = -1;
|
---|
234 | public SafeSetValue(int value) {
|
---|
235 | this.value = value;
|
---|
236 | }
|
---|
237 | public void run() {
|
---|
238 | unsafeSetValue(value);
|
---|
239 | }
|
---|
240 | synchronized public void setValue(int value) {
|
---|
241 | this.value = value;
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|