source: trunk/gli/src/org/greenstone/gatherer/gui/LongProgressBar.java@ 5350

Last change on this file since 5350 was 4675, checked in by jmt12, 21 years ago

Sunday's work

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