source: main/trunk/gli/src/org/greenstone/gatherer/download/DownloadProgressBar.java@ 22103

Last change on this file since 22103 was 22103, checked in by ak19, 14 years ago

The Download Progress Pane used to say Download Complete almost immediately upon starting a download and while this was still in progress. This was a problem found with OAI download, but is now also fixed with the Web download mode. Tested with both.

  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 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.download;
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;
45//import org.greenstone.gatherer.collection.DownloadJob;
46import org.greenstone.gatherer.util.AppendLineOnlyFileDocument;
47import org.greenstone.gatherer.util.Utility;
48import org.greenstone.gatherer.gui.*;
49
50public class DownloadProgressBar
51 extends JPanel
52 implements ActionListener {
53
54 private boolean simple = false;
55
56 private Dimension bar_size = new Dimension(520, 25);
57
58 private Dimension MINIMUM_BUTTON_SIZE = new Dimension(100, 25);
59 private Dimension PROGRESS_BAR_SIZE = new Dimension(580,75);
60
61 private int current_action;
62 private int err_count;
63 private int file_count;
64 private int total_count;
65 private int warning_count;
66
67 private JLabel current_status;
68 private JLabel main_status;
69 private JLabel results_status;
70
71 private JPanel center_pane;
72 private JPanel inner_pane;
73
74 private JProgressBar progress;
75
76 private long file_size;
77 private long total_size;
78
79 private String current_url;
80 private String initial_url;
81
82 private DownloadJob owner;
83
84 public JButton stop_start_button;
85 public JButton log_button;
86 public JButton close_button;
87
88 public DownloadProgressBar(DownloadJob owner, String initial_url, boolean simple) {
89 this.owner = owner;
90 this.current_url = null;
91 this.err_count = 0;
92 this.initial_url = initial_url;
93 this.file_count = 0;
94 this.file_size = 0;
95 this.simple = simple;
96 this.total_count = 0;
97 this.total_size = 0;
98 this.warning_count = 0;
99
100 this.setLayout(new BorderLayout());
101 this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
102
103 this.current_action = DownloadJob.STOPPED;
104
105 inner_pane = new JPanel(new BorderLayout());
106 inner_pane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
107
108 center_pane = new JPanel(new GridLayout(3,1));
109
110 main_status = new JLabel();
111
112 current_status = new JLabel();
113
114 results_status = new JLabel();
115
116 progress = new JProgressBar();
117 progress.setStringPainted(true);
118 progress.setMinimum(0);
119 progress.setMaximum(0);
120 progress.setEnabled(false);
121 progress.setString(Dictionary.get("Mirroring.DownloadJob.Waiting"));
122 inner_pane.add(progress, BorderLayout.CENTER);
123
124 center_pane.add(main_status);
125 center_pane.add(current_status);
126 center_pane.add(results_status);
127
128 JPanel button_pane = new JPanel();
129
130 stop_start_button = new GLIButton(Dictionary.get("Mirroring.DownloadJob.Pause"),Dictionary.get("Mirroring.DownloadJob.Pause_Tooltip"));
131 stop_start_button.addActionListener(this);
132 stop_start_button.addActionListener(owner);
133 stop_start_button.setMinimumSize(MINIMUM_BUTTON_SIZE);
134 stop_start_button.setMnemonic(KeyEvent.VK_P);
135 stop_start_button.setEnabled(true);
136
137
138 log_button = new GLIButton(Dictionary.get("Mirroring.DownloadJob.Log"),Dictionary.get("Mirroring.DownloadJob.Log_Tooltip"));
139 log_button.addActionListener(owner);
140 log_button.addActionListener(this);
141 log_button.setEnabled(false);
142 log_button.setMinimumSize(MINIMUM_BUTTON_SIZE);
143 log_button.setMnemonic(KeyEvent.VK_L);
144
145
146 close_button = new GLIButton(Dictionary.get("Mirroring.DownloadJob.Close"),Dictionary.get("Mirroring.DownloadJob.Close_Tooltip"));
147 close_button.addActionListener(owner);
148 close_button.addActionListener(this);
149 close_button.setMinimumSize(MINIMUM_BUTTON_SIZE);
150 close_button.setMnemonic(KeyEvent.VK_C);
151 close_button.setEnabled(true);
152
153
154 // Layout - or at least some of it
155
156 inner_pane.add(center_pane, BorderLayout.NORTH);
157
158 button_pane.setLayout(new GridLayout(3,1));
159 button_pane.add(stop_start_button);
160 button_pane.add(log_button);
161 button_pane.add(close_button);
162
163 this.add(inner_pane, BorderLayout.CENTER);
164 this.add(button_pane, BorderLayout.EAST);
165
166 // Make the labels, etc update.
167 refresh();
168 }
169
170 public void actionPerformed(ActionEvent event) {
171 Object source = event.getSource();
172 if(source == stop_start_button) {
173 // If we are running, stop.
174 if (current_action == DownloadJob.RUNNING) {
175 current_action = DownloadJob.STOPPED;
176 stop_start_button.setText(Dictionary.get("Mirroring.DownloadJob.Resume"));
177 stop_start_button.setToolTipText(Dictionary.get("Mirroring.DownloadJob.Resume_Tooltip"));
178
179 progress.setString(Dictionary.get("Mirroring.DownloadJob.Download_Stopped"));
180 progress.setIndeterminate(false);
181 } else {
182 current_action = DownloadJob.RUNNING;
183 // we are stopped, so restart
184 stop_start_button.setText(Dictionary.get("Mirroring.DownloadJob.Pause"));
185 stop_start_button.setToolTipText(Dictionary.get("Mirroring.DownloadJob.Pause_Tooltip"));
186 progress.setString(Dictionary.get("Mirroring.DownloadJob.Download_Progress"));
187 progress.setIndeterminate(true);
188 }
189 }
190 else if(source == close_button) {
191 // If we are running, stop.
192 if (current_action == DownloadJob.RUNNING) {
193 current_action = DownloadJob.STOPPED;
194 progress.setString(Dictionary.get("Mirroring.DownloadJob.Download_Stopped"));
195
196 progress.setIndeterminate(false);
197 }
198 }
199 else if(source == log_button) {
200
201 LogDialog dialog = new LogDialog(owner.getLogDocument());
202 dialog.setVisible(true);
203 dialog = null;
204 }
205
206 refresh();
207 }
208
209 /** This method is called when a new download is begun. The
210 * details of the download are updated and a new JProgressBar
211 * assigned to track the download.
212 * @param url The url String of the file that is being downloaded.
213 */
214 public void addDownload(String url) {
215 current_url = url;
216 file_size = 0;
217 refresh();
218 }
219
220 /** When the download of the current url is completed, this method
221 * is called to enlighten the DownloadProgressBar of this fact.
222 */
223 public void downloadComplete() {
224 current_url = null;
225 file_count++;
226 if(total_count < (file_count + err_count + warning_count)) {
227 total_count = (file_count + err_count + warning_count);
228 }
229 progress.setValue(progress.getMaximum());
230 //Dictionary.setText(progress, "Mirroring.DownloadJob.Download_Complete");
231 refresh();
232 }
233
234 public void downloadFailed() {
235 err_count++;
236 if(total_count < (file_count + err_count + warning_count)) {
237 total_count = (file_count + err_count + warning_count);
238 }
239 refresh();
240 }
241
242 public void downloadWarning() {
243 warning_count++;
244 if(total_count < (file_count + err_count + warning_count)) {
245 total_count = (file_count + err_count + warning_count);
246 }
247 refresh();
248 }
249
250 public void setTotalDownload(int total_download) {
251 total_count = total_download;
252 refresh();
253 }
254
255 public Dimension getPreferredSize() {
256 return PROGRESS_BAR_SIZE;
257 }
258
259 public void increaseFileCount() {
260 file_count++;
261 refresh();
262 }
263
264 public void increaseFileCount(int amount) {
265 file_count += amount;
266 refresh();
267 }
268
269 public void resetFileCount() {
270 file_count = 0;
271 refresh();
272 }
273
274 /** When a mirroring task is first initiated this function is called
275 * to set initial values for the variables if necessary and to
276 * fiddle visual components such as the tool tip etc.
277 * @param reset A Boolean specifying whether the variables should be
278 * reset to zero.
279 */
280 public void mirrorBegun(boolean reset, boolean simple) {
281 if(reset) {
282 this.file_count = 0;
283 this.file_size = 0;
284 this.total_count = 0;
285 this.total_size = 0;
286 this.err_count = 0;
287 this.warning_count = 0;
288 }
289 current_action = DownloadJob.RUNNING;
290 stop_start_button.setEnabled(true);
291 log_button.setEnabled(true);
292 if(simple) {
293 progress.setIndeterminate(true);
294 progress.setString(Dictionary.get("Mirroring.DownloadJob.Download_Progress"));
295 }
296 }
297
298 /** Once a mirroring task is complete, if the DownloadJob returns from the
299 * native call but the status is still running, then this method
300 * is called to once again tinker with the pretty visual
301 * components.
302 */
303 public void mirrorComplete() {
304 current_action = DownloadJob.COMPLETE;
305 current_url = null;
306 if(simple) {
307 progress.setIndeterminate(false);
308 }
309 progress.setValue(progress.getMaximum());
310 progress.setString(Dictionary.get("Mirroring.DownloadJob.Download_Complete"));
311 current_status.setText(Dictionary.get("Mirroring.DownloadJob.Download_Complete"));
312 // will display "Download Complete" once an OAI download session has finished running
313
314 stop_start_button.setEnabled(false);
315 this.updateUI();
316 }
317
318 /** When called this method updates the DownloadProgressBar to reflect
319 * the amount of the current file downloaded.
320 */
321 public void updateProgress(long current, long expected) {
322 file_size = file_size + current;
323 if(!progress.isIndeterminate()) {
324 // If current is zero, then this is the 'precall' before the
325 // downloading actually starts.
326 if(current == 0) {
327 // Remove the old progress bar, then deallocate it.
328 inner_pane.remove(progress);
329 progress = null;
330 if(expected == 0) {
331 // We don't have a content length. This bar will go from 0 to 100 only!
332 progress = new JProgressBar(0, 100);
333 }
334 else {
335 // Assign a new progress bar of length expected content length.
336 progress = new JProgressBar(0, (new Long(expected)).intValue());
337 }
338 progress.setEnabled(true);
339 // Add the new progress bar.
340 inner_pane.add(progress, BorderLayout.CENTER);
341 inner_pane.updateUI();
342 }
343 // Otherwise if expected is not zero move the progress bar and
344 // update percent complete.
345 else if (expected != 0) {
346 progress.setValue((new Long(file_size)).intValue());
347 int p_c = (new Double(progress.getPercentComplete() * 100)).intValue();
348 progress.setString(p_c + "%");
349 progress.setStringPainted(true);
350 }
351 // Finally, in the case we have no content length, we'll instead
352 // write the current number of bytes downloaded again.
353 else {
354 progress.setString(file_size + " b");
355 progress.setStringPainted(true);
356 }
357 }
358 refresh();
359 }
360
361 /** Causes the two labels associated with this DownloadProgressBar object to
362 * update, thus reflecting the progression of the download. This
363 * method is called by any of the other public setter methods in this
364 * class.
365 */
366 private void refresh() {
367 // Refresh the contents of main label.
368 String args1[] = new String[1];
369 args1[0] = initial_url.toString();
370 main_status.setText(Dictionary.get("Mirroring.DownloadJob.Downloading", args1));
371
372 if (current_url != null) {
373 // Refresh the current label contents.
374 String args2[] = new String[1];
375 args2[0] = current_url;
376 current_status.setText(Dictionary.get("Mirroring.DownloadJob.Downloading", args2));
377 }
378 else if (current_action == DownloadJob.STOPPED || current_action == DownloadJob.PAUSED) {
379 current_status.setText(Dictionary.get("Mirroring.DownloadJob.Waiting_User"));
380 }
381 else {
382 current_status.setText(Dictionary.get("Mirroring.DownloadJob.Download_Complete"));
383 }
384
385 // Refresh the contents of results label
386 String args3[] = new String[4];
387 args3[0] = file_count + "";
388 args3[1] = total_count + "";
389 args3[2] = warning_count + "";
390 args3[3] = err_count + "";
391 results_status.setText(Dictionary.get("Mirroring.DownloadJob.Status", args3));
392
393 this.updateUI();
394 }
395
396 static final private Dimension DIALOG_SIZE = new Dimension(640,480);
397
398 private class LogDialog
399 extends JDialog {
400
401 public LogDialog(AppendLineOnlyFileDocument document) {
402 super(Gatherer.g_man, Dictionary.get("Mirroring.DownloadJob.Log_Title"));
403 setSize(DIALOG_SIZE);
404 JPanel content_pane = (JPanel) getContentPane();
405 JTextArea text_area = new JTextArea(document);
406 JButton button = new GLIButton(Dictionary.get("General.Close"));
407 // Connection
408 button.addActionListener(new CloseActionListener());
409 // Layout
410 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
411 content_pane.setLayout(new BorderLayout());
412 content_pane.add(new JScrollPane(text_area), BorderLayout.CENTER);
413 content_pane.add(button, BorderLayout.SOUTH);
414 }
415
416 private class CloseActionListener
417 implements ActionListener {
418 public void actionPerformed(ActionEvent event) {
419 LogDialog.this.dispose();
420 }
421 }
422 }
423}
Note: See TracBrowser for help on using the repository browser.