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

Last change on this file since 9101 was 8994, checked in by mdewsnip, 19 years ago

Changed all "MetaEdit" to "Enrich".

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