source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/gui/GProgressBar.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

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