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

Last change on this file since 8003 was 8003, checked in by mdewsnip, 20 years ago

Tightened up some public functions to be private.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
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 */
27package org.greenstone.gatherer.gui;
28
29import java.math.BigInteger;
30import javax.swing.JProgressBar;
31import 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 */
37final 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 SwingUtilities.invokeLater(safe_set_string);
187 }
188
189 /** Increase the value of the progress bar.
190 * @param ammount The ammount to increase as an <i>int</i>.
191 * Original code: Tad Frysinger (TeamB)
192 */
193 private void safeSetValue(final int ammount) {
194 // Update progress bar,
195 if(safe_set_value == null) {
196 safe_set_value = new SafeSetValue(ammount);
197 }
198 else {
199 safe_set_value.setValue(ammount);
200 }
201 SwingUtilities.invokeLater(safe_set_value);
202 }
203
204 private class SafeSetIndeterminate
205 implements Runnable {
206 private boolean value;
207 public SafeSetIndeterminate(boolean value) {
208 this.value = value;
209 }
210 public void run() {
211 unsafeSetIndeterminate(value);
212 }
213 synchronized public void setState(boolean value) {
214 this.value = value;
215 }
216 }
217
218 private class SafeSetString
219 implements Runnable {
220 private String label = null;
221 public SafeSetString(String label) {
222 this.label = label;
223 }
224 public void run() {
225 unsafeSetString(label);
226 }
227 synchronized public void setLabel(String label) {
228 this.label = label;
229 }
230 }
231
232 private class SafeSetValue
233 implements Runnable {
234 private int value = -1;
235 public SafeSetValue(int value) {
236 this.value = value;
237 }
238 public void run() {
239 unsafeSetValue(value);
240 }
241 synchronized public void setValue(int value) {
242 this.value = value;
243 }
244 }
245}
Note: See TracBrowser for help on using the repository browser.