source: trunk/gli/src/org/greenstone/gatherer/gui/GProgressBar.java@ 11073

Last change on this file since 11073 was 11073, checked in by mdewsnip, 18 years ago

Renamed LongProgressBar to GProgressBar, and tidied up a great deal. Removed the BigInteger sillyness and used longs instead, so should be slightly more efficient. This will now be used for all progress bars in the GLI, as it sets indeterminate/string/value safely (should avoid NPEs when setting indeterminate).

  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 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
29
30import javax.swing.*;
31
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 */
36public class GProgressBar
37 extends JProgressBar
38{
39 private long cur_value = 0L;
40 private long max_value = 0L;
41
42 /** The previous percentage value. */
43 private int previous = -1;
44
45
46 public GProgressBar()
47 {
48 super(0, 100);
49 }
50
51
52 public void addMaximum(long amount)
53 {
54 if (amount > 0) {
55 max_value = max_value + amount;
56 update();
57 }
58 }
59
60
61 public void addValue(long amount)
62 {
63 if (amount > 0) {
64 cur_value = cur_value + amount;
65 update();
66 }
67 }
68
69
70 public void clear()
71 {
72 previous = -1;
73 update();
74 }
75
76
77 public void reset()
78 {
79 cur_value = 0L;
80 max_value = 0L;
81 previous = -1;
82 update();
83 }
84
85
86 public void setIndeterminate(boolean indeterminate)
87 {
88 SwingUtilities.invokeLater(new SafeSetIndeterminateTask(indeterminate));
89 }
90
91
92 public void setString(String label)
93 {
94 SwingUtilities.invokeLater(new SafeSetStringTask(label));
95 }
96
97
98 public void setValue(int value)
99 {
100 cur_value = value;
101 update();
102 }
103
104
105 /** Only bother updating the progress bar if the percentage has changed. */
106 private void update()
107 {
108 int percentage = -1;
109 if (cur_value == 0L || max_value == 0L) {
110 percentage = 0;
111 }
112 else {
113 long per_value = (cur_value * 100) / max_value;
114 percentage = (int) per_value;
115 if (percentage > 100) {
116 percentage = 100;
117 }
118 }
119
120 if (percentage != previous) {
121 previous = percentage;
122 setString(percentage + "%");
123 SwingUtilities.invokeLater(new SafeSetValueTask(percentage));
124 }
125 }
126
127
128 /** Internal class to call JProgressBar.setIndeterminate safely. */
129 private class SafeSetIndeterminateTask
130 implements Runnable
131 {
132 private boolean indeterminate;
133
134 public SafeSetIndeterminateTask(boolean indeterminate)
135 {
136 this.indeterminate = indeterminate;
137 }
138
139 public void run()
140 {
141 unsafeSetIndeterminate(indeterminate);
142 }
143 }
144
145 /** This method is unsafe, -unless- it is called from the Event Queue (eg. from SafeSetIndeterminateTask). */
146 private void unsafeSetIndeterminate(boolean indeterminate)
147 {
148 super.setIndeterminate(indeterminate);
149 }
150
151
152 /** Internal class to call JProgressBar.setString safely. */
153 private class SafeSetStringTask
154 implements Runnable
155 {
156 private String label;
157
158 public SafeSetStringTask(String label)
159 {
160 this.label = label;
161 }
162
163 public void run()
164 {
165 unsafeSetString(label);
166 }
167 }
168
169 /** This method is unsafe, -unless- it is called from the Event Queue (eg. from SafeSetStringTask). */
170 private void unsafeSetString(String label)
171 {
172 super.setString(label);
173 }
174
175
176 /** Internal class to call JProgressBar.setValue safely. */
177 private class SafeSetValueTask
178 implements Runnable
179 {
180 private int value;
181
182 public SafeSetValueTask(int value)
183 {
184 this.value = value;
185 }
186
187 public void run()
188 {
189 unsafeSetValue(value);
190 }
191 }
192
193 /** This method is unsafe, -unless- it is called from the Event Queue (eg. from SafeSetValueTask). */
194 private void unsafeSetValue(int value)
195 {
196 super.setValue(value);
197 }
198}
Note: See TracBrowser for help on using the repository browser.