source: trunk/gli/src/org/greenstone/gatherer/gui/GProgressBar.java@ 4428

Last change on this file since 4428 was 4367, checked in by mdewsnip, 21 years ago

Fixed tabbing.

  • Property svn:keywords set to Author Date Id Revision
File size: 12.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 */
37
38
39
40
41
42
43package org.greenstone.gatherer.gui;
44
45import java.awt.BorderLayout;
46import java.awt.Component;
47import java.awt.Dimension;
48import java.awt.Graphics2D;
49import java.awt.GridLayout;
50import java.awt.Image;
51import java.awt.event.ActionEvent;
52import java.awt.event.ActionListener;
53import java.awt.geom.AffineTransform;
54import java.awt.image.BufferedImage;
55import javax.swing.BorderFactory;
56import javax.swing.ImageIcon;
57import javax.swing.JButton;
58import javax.swing.JLabel;
59import javax.swing.JPanel;
60import javax.swing.JProgressBar;
61import javax.swing.border.BevelBorder;
62import org.greenstone.gatherer.Gatherer;
63import org.greenstone.gatherer.collection.Job;
64import org.greenstone.gatherer.util.Utility;
65
66public class GProgressBar
67 extends JPanel
68 implements ActionListener {
69
70 private boolean simple = false;
71
72 private Dimension bar_size = new Dimension(520, 15);
73
74 private Gatherer gatherer;
75
76 private ImageIcon busy_bar;
77 private ImageIcon ready_bar;
78
79 private int current_action;
80 private int err_count;
81 private int file_count;
82 private int total_count;
83 private int warning_count;
84
85 private JLabel current_status;
86 private JLabel main_status;
87 private JLabel results_status;
88 private JLabel status;
89
90 private JPanel center_pane;
91 private JPanel inner_pane;
92
93 private JProgressBar progress;
94
95 private long file_size;
96 private long total_size;
97
98 private String current_url;
99 private String initial_url;
100
101 private Job owner;
102
103 public JButton action;
104 public JButton cancel;
105
106 //public GProgressBar(Gatherer gatherer, WGet owner, String initial_url) {
107 // this.gatherer = gatherer;
108 public GProgressBar(Job owner, String initial_url, boolean simple) {
109 this.owner = owner;
110 this.current_url = null;
111 this.err_count = 0;
112 this.initial_url = initial_url;
113 this.file_count = 0;
114 this.file_size = 0;
115 this.simple = simple;
116 this.total_count = 0;
117 this.total_size = 0;
118 this.warning_count = 0;
119
120 this.setLayout(new BorderLayout());
121 this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
122 //this.setMinimumSize(Utility.PROGRESS_BAR_SIZE);
123 //this.setSize(Utility.PROGRESS_BAR_SIZE);
124 //this.setMaximumSize(Utility.PROGRESS_BAR_SIZE);
125 //this.setAlignmentY(Component.LEFT_ALIGNMENT);
126 //this.setHorizontalAlignment(JLabel.LEFT);
127
128 this.current_action = Job.STOPPED;
129
130 inner_pane = new JPanel(new BorderLayout());
131 inner_pane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
132
133 action = new JButton(Utility.getImage("vcrplay.gif"));
134 //action.setToolTipText(get("Action_Tip"));
135 action.setToolTipText("Start current download");
136 action.addActionListener(this);
137 action.addActionListener(owner);
138
139 center_pane = new JPanel(new GridLayout(3,1));
140
141 main_status = new JLabel();
142
143 current_status = new JLabel();
144
145 results_status = new JLabel();
146
147 progress = new JProgressBar();
148 progress.setString("Waiting to start");
149 progress.setStringPainted(true);
150 progress.setMinimum(0);
151 progress.setMaximum(0);
152 progress.setEnabled(false);
153 inner_pane.add(progress, BorderLayout.CENTER);
154
155 center_pane.add(main_status);
156 center_pane.add(current_status);
157 center_pane.add(results_status);
158
159 cancel = new JButton(Utility.getImage("vcrstop.gif"));
160 //cancel.setToolTipText(get("Cancel_Tip"));
161 cancel.setToolTipText("Cancel current download");
162 cancel.addActionListener(owner);
163 cancel.addActionListener(this);
164
165 inner_pane.add(center_pane, BorderLayout.NORTH);
166
167 this.add(action, BorderLayout.WEST);
168 this.add(inner_pane, BorderLayout.CENTER);
169 this.add(cancel, BorderLayout.EAST);
170
171 // Make the labels, etc update.
172 refresh();
173 }
174
175 public void actionPerformed(ActionEvent event) {
176 if(event.getSource() == action) {
177 if(current_action == Job.RUNNING) {
178 current_action = Job.STOPPED;
179 action.setIcon(Utility.getImage("vcrplay.gif"));
180 action.setToolTipText("Start current download");
181 cancel.setEnabled(true);
182 }
183 else {
184 current_action = Job.RUNNING;
185 action.setIcon(Utility.getImage("vcrpause.gif"));
186 action.setToolTipText("Pause current download");
187 cancel.setEnabled(false);
188 }
189 }
190 }
191
192 /** This method is called when a new download is begun. The
193 * details of the download are updated and a new JProgressBar
194 * assigned to track the download.
195 * @param url The url String of the file that is being downloaded.
196 * @param size The size of the file as an Int.
197 */
198 public void addDownload(String url) {
199 current_url = url;
200 file_size = 0;
201 refresh();
202 }
203
204 /** When the download of the current url is completed, this method
205 * is called to enlighten the GProgressBar of this fact.
206 */
207 public void downloadComplete() {
208 current_url = null;
209 file_count++;
210 if(total_count < (file_count + err_count + warning_count)) {
211 total_count = (file_count + err_count + warning_count);
212 }
213 progress.setValue(progress.getMaximum());
214 progress.setString("Download Complete.");
215 refresh();
216 }
217
218 public void downloadFailed() {
219 err_count++;
220 if(total_count < (file_count + err_count + warning_count)) {
221 total_count = (file_count + err_count + warning_count);
222 }
223 refresh();
224 }
225
226 public void downloadWarning() {
227 warning_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 Dimension getPerferredSize() {
235 return Utility.PROGRESS_BAR_SIZE;
236 }
237
238 /** When a link to be downloaded is located, the increaseTotalCount
239 * method is called. In this way the total count shows the most
240 * accurate remaining number of files to be downloaded.
241 */
242 public void increaseFileCount() {
243 total_count++;
244 refresh();
245 }
246
247 /** When a mirroring task is first initiated this function is called
248 * to set initial values for the variables if necessary and to
249 * fiddle visual components such as the tool tip etc.
250 * @param reset A Boolean specifying whether the variables should be
251 * reset to zero.
252 */
253 public void mirrorBegun(boolean reset) {
254 if(reset) {
255 this.file_count = 0;
256 this.file_size = 0;
257 this.total_count = 0;
258 this.total_size = 0;
259 this.err_count = 0;
260 this.warning_count = 0;
261 }
262 current_action = Job.RUNNING;
263 action.setIcon(Utility.getImage("vcrpause.gif"));
264 action.setToolTipText("Pause current download");
265 cancel.setEnabled(false);
266 if(simple) {
267 progress.setIndeterminate(true);
268 }
269 }
270
271 /** Once a mirroring task is complete, is the Job returns from the
272 * native call but the status is still running, then this method
273 * is called to once again tinker with the pritty visual
274 * components.
275 */
276 public void mirrorComplete() {
277 current_action = Job.COMPLETE;
278 current_url = null;
279 if(simple) {
280 progress.setIndeterminate(false);
281 }
282 progress.setValue(progress.getMaximum());
283 progress.setString("Mirroring Complete.");
284
285 action.setIcon(Utility.getImage("vcrfastforward.gif"));
286 action.setToolTipText("Restart current download");
287 cancel.setEnabled(true);
288 this.updateUI();
289 }
290
291 /** When called this method updates the GProgressBar to reflect
292 * the ammount of the current file downloaded.
293 */
294 public void updateProgress(long current, long expected) {
295 file_size = file_size + current;
296 if(!progress.isIndeterminate()) {
297 // If current is zero, then this is the 'precall' before the
298 // downloading actually starts.
299 if(current == 0) {
300 // Remove the old progress bar, then deallocate it.
301 inner_pane.remove(progress);
302 progress = null;
303 if(expected == 0) {
304 // We don't have a content length. This bar will go from
305 // 0 to 100 only!
306 progress = new JProgressBar(0, 100);
307 }
308 else {
309 // Assign a new progress bar of length expected content
310 // length.
311 progress =
312 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 =
324 (new Double(
325 progress.getPercentComplete() * 100)).intValue();
326 progress.setString(p_c + "%");
327 progress.setStringPainted(true);
328 }
329 // Finally, in the case we have no content length, we'll instead
330 // write the current number of bytes downloaded again.
331 else {
332 progress.setString( file_size + " bytes");
333 progress.setStringPainted(true);
334 }
335 }
336 refresh();
337 }
338
339 /** Retrieve a String from the dictionary using no arguments.
340 * @param key The String which acts as a key mapping for the
341 * ResourceBundle.
342 * @return A String which is the value for the given key.
343 */
344 private String get(String key) {
345 return get(key, null);
346 }
347
348 /** Retrieve a String from the dictionary using the given arguments.
349 * @param key The String which acts as a key mapping for the
350 * ResourceBundle.
351 * @param args A String array of arguments to be filled out inside the
352 * dictionary value.
353 * @return A String which is the value for the given key.
354 */
355 private String get(String key, String args[]) {
356 if(key.indexOf('.') == -1) {
357 return gatherer.dictionary.get("GProgressBar."+key, args);
358 }
359 return gatherer.dictionary.get(key, args);
360 }
361
362 /** The given parameter is taken as the number of bytes and is
363 * formatted into a strings such as:
364 * 45 bytes
365 * 1.21 Kbytes
366 * 2.12 Mbytes
367 * etc
368 * @param size The number to be formatted, in bytes, as a Long.
369 * @return A String which contains the size formatting into a
370 * nice little number - unit label.
371 */
372 private String formatMetric(long size) {
373 if(size > 1024) {
374 return (size / 1024) + " Kbytes";
375 }
376 return size + " bytes";
377 }
378
379 /** Causes the two labels associated with this GProgressBar object to
380 * update, thus reflecting the progression of the download. This
381 * method is called by any of the other public setter methods in this
382 * class.
383 */
384 private void refresh() {
385 // Refresh the contents of main label.
386 String args1[] = new String[1];
387 args1[0] = initial_url.toString();
388
389 //main_status.setText(get("Main_Status", args1));
390 main_status.setText("Mirroring " + args1[0] + ".");
391
392 if(current_url != null) {
393 // Refresh the current label contents.
394 String args2[] = new String[1];
395 args2[0] = current_url;
396
397 String cstext = "Downloading " + args2[0];
398 current_status.setText(Utility.trim(cstext, 50));
399 }
400 else if(current_action == Job.STOPPED
401 || current_action == Job.PAUSED) {
402 current_status.setText("Waiting for user action.");
403 }
404 else {
405 current_status.setText("Mirroring Complete.");
406 }
407
408 // Refresh the contents of results label
409 String args3[] = new String[4];
410 args3[0] = file_count + "";
411 args3[1] = total_count + "";
412 args3[2] = warning_count + "";
413 args3[3] = err_count + "";
414
415 results_status.setText("Downloaded " + args3[0] + " of " +
416 args3[1] + " files (" + args3[2] +
417 " warnings, " + args3[3] + " errors)");
418
419 this.updateUI();
420 }
421}
Note: See TracBrowser for help on using the repository browser.