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