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