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

Last change on this file since 6540 was 6318, checked in by jmt12, 21 years ago

Changed JButtons for GLIButtons, which know whether they should paint their background depending on what platform they are run on, and finished keyboard shortcuts

  • Property svn:keywords set to Author Date Id Revision
File size: 10.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 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.gui;
38
39import java.awt.*;
40import java.awt.event.*;
41import javax.swing.*;
42import javax.swing.border.*;
43import org.greenstone.gatherer.Dictionary;
44import org.greenstone.gatherer.Gatherer;
45import org.greenstone.gatherer.collection.Job;
46import org.greenstone.gatherer.gui.GLIButton;
47import org.greenstone.gatherer.util.Utility;
48
49public class GProgressBar
50 extends JPanel
51 implements ActionListener {
52
53 private boolean simple = false;
54
55 private Dimension bar_size = new Dimension(520, 15);
56
57 private int current_action;
58 private int err_count;
59 private int file_count;
60 private int total_count;
61 private int warning_count;
62
63 private JLabel current_status;
64 private JLabel main_status;
65 private JLabel results_status;
66 private JLabel status;
67
68 private JPanel center_pane;
69 private JPanel inner_pane;
70
71 private JProgressBar progress;
72
73 private long file_size;
74 private long total_size;
75
76 private String current_url;
77 private String initial_url;
78
79 private Job owner;
80
81 public JButton action;
82 public JButton cancel;
83
84 public GProgressBar(Job owner, String initial_url, boolean simple) {
85 this.owner = owner;
86 this.current_url = null;
87 this.err_count = 0;
88 this.initial_url = initial_url;
89 this.file_count = 0;
90 this.file_size = 0;
91 this.simple = simple;
92 this.total_count = 0;
93 this.total_size = 0;
94 this.warning_count = 0;
95
96 this.setLayout(new BorderLayout());
97 this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
98
99 this.current_action = Job.STOPPED;
100
101 inner_pane = new JPanel(new BorderLayout());
102 inner_pane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
103
104 action = new GLIButton(Utility.getImage("vcrplay.gif"));
105 action.addActionListener(this);
106 action.addActionListener(owner);
107 action.setMnemonic(KeyEvent.VK_ENTER);
108 Dictionary.setTooltip(action, "Mirroring.Job.Start_Tooltip");
109
110 center_pane = new JPanel(new GridLayout(3,1));
111
112 main_status = new JLabel();
113
114 current_status = new JLabel();
115
116 results_status = new JLabel();
117
118 progress = new JProgressBar();
119 progress.setStringPainted(true);
120 progress.setMinimum(0);
121 progress.setMaximum(0);
122 progress.setEnabled(false);
123 Dictionary.setText(progress, "Mirroring.Job.Waiting");
124 inner_pane.add(progress, BorderLayout.CENTER);
125
126 center_pane.add(main_status);
127 center_pane.add(current_status);
128 center_pane.add(results_status);
129
130 cancel = new GLIButton(Utility.getImage("vcrstop.gif"));
131 cancel.addActionListener(owner);
132 cancel.addActionListener(this);
133 cancel.setMnemonic(KeyEvent.VK_BACK_SPACE);
134 Dictionary.setTooltip(cancel, "Mirroring.Job.Stop_Tooltip");
135
136 inner_pane.add(center_pane, BorderLayout.NORTH);
137
138 this.add(action, BorderLayout.WEST);
139 this.add(inner_pane, BorderLayout.CENTER);
140 this.add(cancel, BorderLayout.EAST);
141
142 // Make the labels, etc update.
143 refresh();
144 }
145
146 public void actionPerformed(ActionEvent event) {
147 if(event.getSource() == action) {
148 if (current_action == Job.RUNNING) {
149 current_action = Job.STOPPED;
150 action.setIcon(Utility.getImage("vcrplay.gif"));
151 Dictionary.setTooltip(action, "Mirroring.Job.Start_Tooltip");
152 cancel.setEnabled(true);
153 }
154 else {
155 current_action = Job.RUNNING;
156 action.setIcon(Utility.getImage("vcrpause.gif"));
157 Dictionary.setTooltip(action, "Mirroring.Job.Pause_Tooltip");
158 cancel.setEnabled(false);
159 }
160 }
161 }
162
163 /** This method is called when a new download is begun. The
164 * details of the download are updated and a new JProgressBar
165 * assigned to track the download.
166 * @param url The url String of the file that is being downloaded.
167 * @param size The size of the file as an Int.
168 */
169 public void addDownload(String url) {
170 current_url = url;
171 file_size = 0;
172 refresh();
173 }
174
175 /** When the download of the current url is completed, this method
176 * is called to enlighten the GProgressBar of this fact.
177 */
178 public void downloadComplete() {
179 current_url = null;
180 file_count++;
181 if(total_count < (file_count + err_count + warning_count)) {
182 total_count = (file_count + err_count + warning_count);
183 }
184 progress.setValue(progress.getMaximum());
185 Dictionary.setText(progress, "Mirroring.Job.Download_Complete");
186 refresh();
187 }
188
189 public void downloadFailed() {
190 err_count++;
191 if(total_count < (file_count + err_count + warning_count)) {
192 total_count = (file_count + err_count + warning_count);
193 }
194 refresh();
195 }
196
197 public void downloadWarning() {
198 warning_count++;
199 if(total_count < (file_count + err_count + warning_count)) {
200 total_count = (file_count + err_count + warning_count);
201 }
202 refresh();
203 }
204
205 public Dimension getPreferredSize() {
206 return Utility.PROGRESS_BAR_SIZE;
207 }
208
209 /** When a link to be downloaded is located, the increaseTotalCount
210 * method is called. In this way the total count shows the most
211 * accurate remaining number of files to be downloaded.
212 */
213 public void increaseFileCount() {
214 total_count++;
215 refresh();
216 }
217
218 /** When a mirroring task is first initiated this function is called
219 * to set initial values for the variables if necessary and to
220 * fiddle visual components such as the tool tip etc.
221 * @param reset A Boolean specifying whether the variables should be
222 * reset to zero.
223 */
224 public void mirrorBegun(boolean reset, boolean simple) {
225 if(reset) {
226 this.file_count = 0;
227 this.file_size = 0;
228 this.total_count = 0;
229 this.total_size = 0;
230 this.err_count = 0;
231 this.warning_count = 0;
232 }
233 current_action = Job.RUNNING;
234 action.setIcon(Utility.getImage("vcrpause.gif"));
235 Dictionary.setTooltip(cancel, "Mirroring.Job.Pause_Tooltip");
236 // If this is a simple download, then pause is not available (nor is prematurely stopping!)
237 action.setEnabled(!simple);
238 cancel.setEnabled(false);
239 if(simple) {
240 progress.setIndeterminate(true);
241 }
242 }
243
244 /** Once a mirroring task is complete, is the Job returns from the
245 * native call but the status is still running, then this method
246 * is called to once again tinker with the pritty visual
247 * components.
248 */
249 public void mirrorComplete() {
250 current_action = Job.COMPLETE;
251 current_url = null;
252 if(simple) {
253 progress.setIndeterminate(false);
254 }
255 progress.setValue(progress.getMaximum());
256 Dictionary.setText(progress, "Mirroring.Job.Mirror_Complete");
257
258 action.setIcon(Utility.getImage("vcrfastforward.gif"));
259 Dictionary.setTooltip(action, "Mirroring.Job.Restart_Tooltip");
260 action.setEnabled(true);
261 cancel.setEnabled(true);
262 this.updateUI();
263 }
264
265 /** When called this method updates the GProgressBar to reflect
266 * the ammount of the current file downloaded.
267 */
268 public void updateProgress(long current, long expected) {
269 file_size = file_size + current;
270 if(!progress.isIndeterminate()) {
271 // If current is zero, then this is the 'precall' before the
272 // downloading actually starts.
273 if(current == 0) {
274 // Remove the old progress bar, then deallocate it.
275 inner_pane.remove(progress);
276 progress = null;
277 if(expected == 0) {
278 // We don't have a content length. This bar will go from 0 to 100 only!
279 progress = new JProgressBar(0, 100);
280 }
281 else {
282 // Assign a new progress bar of length expected content length.
283 progress = new JProgressBar(0, (new Long(expected)).intValue());
284 }
285 progress.setEnabled(true);
286 // Add the new progress bar.
287 inner_pane.add(progress, BorderLayout.CENTER);
288 inner_pane.updateUI();
289 }
290 // Otherwise if expected is not zero move the progress bar and
291 // update percent complete.
292 else if (expected != 0) {
293 progress.setValue((new Long(file_size)).intValue());
294 int p_c = (new Double(progress.getPercentComplete() * 100)).intValue();
295 progress.setString(p_c + "%");
296 progress.setStringPainted(true);
297 }
298 // Finally, in the case we have no content length, we'll instead
299 // write the current number of bytes downloaded again.
300 else {
301 progress.setString(file_size + " b");
302 progress.setStringPainted(true);
303 }
304 }
305 refresh();
306 }
307
308 /** Causes the two labels associated with this GProgressBar object to
309 * update, thus reflecting the progression of the download. This
310 * method is called by any of the other public setter methods in this
311 * class.
312 */
313 private void refresh() {
314 // Refresh the contents of main label.
315 String args1[] = new String[1];
316 args1[0] = initial_url.toString();
317 Dictionary.setText(main_status, "Mirroring.Job.Mirroring", args1);
318
319 if (current_url != null) {
320 // Refresh the current label contents.
321 String args2[] = new String[1];
322 args2[0] = current_url;
323 Dictionary.setText(current_status, "Mirroring.Job.Downloading", args2);
324 }
325 else if (current_action == Job.STOPPED || current_action == Job.PAUSED) {
326 Dictionary.setText(current_status, "Mirroring.Job.Waiting_User");
327 }
328 else {
329 Dictionary.setText(current_status, "Mirroring.Job.Mirroring_Complete");
330 }
331
332 // Refresh the contents of results label
333 String args3[] = new String[4];
334 args3[0] = file_count + "";
335 args3[1] = total_count + "";
336 args3[2] = warning_count + "";
337 args3[3] = err_count + "";
338 Dictionary.setText(results_status, "Mirroring.Job.Status", args3);
339
340 this.updateUI();
341 }
342}
Note: See TracBrowser for help on using the repository browser.