source: trunk/gli/src/org/greenstone/gatherer/gui/DownloadProgressBar.java@ 10396

Last change on this file since 10396 was 10008, checked in by mdewsnip, 19 years ago

Removed the Dimension objects from the Utility class, as part of tidying it up.

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