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

Last change on this file since 7183 was 6770, checked in by kjdon, 20 years ago

fixed all the javadoc errors. (hope I didn't commit anything I wasn't supposed to)

  • Property svn:keywords set to Author Date Id Revision
File size: 11.9 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.Job;
46import org.greenstone.gatherer.gui.GLIButton;
47import org.greenstone.gatherer.util.AppendLineOnlyFileDocument;
48import org.greenstone.gatherer.util.Utility;
49
50public class GProgressBar
51 extends JPanel
52 implements ActionListener {
53
54 static final private Dimension MINIMUM_BUTTON_SIZE = new Dimension(100, 25);
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 private JLabel status;
70
71 private JPanel center_pane;
72 private JPanel inner_pane;
73
74 private JProgressBar progress;
75
76 private long file_size;
77 private long total_size;
78
79 private String current_url;
80 private String initial_url;
81
82 private Job owner;
83
84 public JButton action;
85 public JButton log_button;
86 public JButton cancel;
87
88 public GProgressBar(Job owner, String initial_url, boolean simple) {
89 this.owner = owner;
90 this.current_url = null;
91 this.err_count = 0;
92 this.initial_url = initial_url;
93 this.file_count = 0;
94 this.file_size = 0;
95 this.simple = simple;
96 this.total_count = 0;
97 this.total_size = 0;
98 this.warning_count = 0;
99
100 this.setLayout(new BorderLayout());
101 this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
102
103 this.current_action = Job.STOPPED;
104
105 inner_pane = new JPanel(new BorderLayout());
106 inner_pane.setBorder(BorderFactory.createEmptyBorder(2,(MINIMUM_BUTTON_SIZE.width + 2),2,2));
107
108 center_pane = new JPanel(new GridLayout(3,1));
109
110 main_status = new JLabel();
111
112 current_status = new JLabel();
113
114 results_status = new JLabel();
115
116 progress = new JProgressBar();
117 progress.setStringPainted(true);
118 progress.setMinimum(0);
119 progress.setMaximum(0);
120 progress.setEnabled(false);
121 Dictionary.setText(progress, "Mirroring.Job.Waiting");
122 inner_pane.add(progress, BorderLayout.CENTER);
123
124 center_pane.add(main_status);
125 center_pane.add(current_status);
126 center_pane.add(results_status);
127
128 JPanel button_pane = new JPanel();
129
130 /** @todo - add to dictionary, and rewrite existing tooltip */
131 action = new GLIButton("Mirroring.Job.Retry");
132 action.addActionListener(this);
133 action.addActionListener(owner);
134 action.setMinimumSize(MINIMUM_BUTTON_SIZE);
135 action.setMnemonic(KeyEvent.VK_A);
136 Dictionary.setTooltip(action, "Mirroring.Job.Start_Tooltip");
137
138 /** @todo - add to dictionary, and rewrite existing tooltip */
139 log_button = new GLIButton("Mirroring.Job.Log");
140 log_button.addActionListener(owner);
141 log_button.addActionListener(this);
142 log_button.setEnabled(false);
143 log_button.setMinimumSize(MINIMUM_BUTTON_SIZE);
144 log_button.setMnemonic(KeyEvent.VK_L);
145 //Dictionary.setTooltip(log_button, "Mirroring.Job.Stop_Tooltip");
146
147 /** @todo - add to dictionary, and rewrite existing tooltip */
148 cancel = new GLIButton("Mirroring.Job.Cancel");
149 cancel.addActionListener(owner);
150 cancel.addActionListener(this);
151 cancel.setMinimumSize(MINIMUM_BUTTON_SIZE);
152 cancel.setMnemonic(KeyEvent.VK_C);
153 Dictionary.setTooltip(cancel, "Mirroring.Job.Stop_Tooltip");
154
155 // Layout - or at least some of it
156
157 inner_pane.add(center_pane, BorderLayout.NORTH);
158
159 button_pane.setLayout(new GridLayout(3,1));
160 button_pane.add(action);
161 button_pane.add(log_button);
162 button_pane.add(cancel);
163
164 this.add(inner_pane, BorderLayout.CENTER);
165 this.add(button_pane, BorderLayout.EAST);
166
167 // Make the labels, etc update.
168 refresh();
169 }
170
171 public void actionPerformed(ActionEvent event) {
172 Object source = event.getSource();
173 if(source == action) {
174 current_action = Job.RUNNING;
175 action.setEnabled(false);
176
177 }
178 else if(source == cancel) {
179 // If we are running, stop.
180 if (current_action == Job.RUNNING) {
181 current_action = Job.STOPPED;
182 action.setEnabled(true);
183 }
184 }
185 else if(source == log_button) {
186 LogDialog dialog = new LogDialog(owner.getLogDocument());
187 dialog.show();
188 dialog = null;
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 */
197 public void addDownload(String url) {
198 current_url = url;
199 file_size = 0;
200 refresh();
201 }
202
203 /** When the download of the current url is completed, this method
204 * is called to enlighten the GProgressBar of this fact.
205 */
206 public void downloadComplete() {
207 current_url = null;
208 file_count++;
209 if(total_count < (file_count + err_count + warning_count)) {
210 total_count = (file_count + err_count + warning_count);
211 }
212 progress.setValue(progress.getMaximum());
213 Dictionary.setText(progress, "Mirroring.Job.Download_Complete");
214 refresh();
215 }
216
217 public void downloadFailed() {
218 err_count++;
219 if(total_count < (file_count + err_count + warning_count)) {
220 total_count = (file_count + err_count + warning_count);
221 }
222 refresh();
223 }
224
225 public void downloadWarning() {
226 warning_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 Dimension getPreferredSize() {
234 return Utility.PROGRESS_BAR_SIZE;
235 }
236
237 /** When a link to be downloaded is located, the increaseTotalCount
238 * method is called. In this way the total count shows the most
239 * accurate remaining number of files to be downloaded.
240 */
241 public void increaseFileCount() {
242 total_count++;
243 refresh();
244 }
245
246 /** When a mirroring task is first initiated this function is called
247 * to set initial values for the variables if necessary and to
248 * fiddle visual components such as the tool tip etc.
249 * @param reset A Boolean specifying whether the variables should be
250 * reset to zero.
251 */
252 public void mirrorBegun(boolean reset, boolean simple) {
253 if(reset) {
254 this.file_count = 0;
255 this.file_size = 0;
256 this.total_count = 0;
257 this.total_size = 0;
258 this.err_count = 0;
259 this.warning_count = 0;
260 }
261 current_action = Job.RUNNING;
262 action.setEnabled(false);
263 log_button.setEnabled(true);
264 if(simple) {
265 progress.setIndeterminate(true);
266 }
267 }
268
269 /** Once a mirroring task is complete, is the Job returns from the
270 * native call but the status is still running, then this method
271 * is called to once again tinker with the pritty visual
272 * components.
273 */
274 public void mirrorComplete() {
275 current_action = Job.COMPLETE;
276 current_url = null;
277 if(simple) {
278 progress.setIndeterminate(false);
279 }
280 progress.setValue(progress.getMaximum());
281 /** @todo - put into dictionary */
282 progress.setString("Mirroring.Job.Mirror_Complete");
283 //Dictionary.setText(progress, "Mirroring.Job.Mirror_Complete");
284
285 action.setEnabled(false);
286 cancel.setEnabled(true);
287 this.updateUI();
288 }
289
290 /** When called this method updates the GProgressBar 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 GProgressBar 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 Dictionary.setText(main_status, "Mirroring.Job.Mirroring", 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 Dictionary.setText(current_status, "Mirroring.Job.Downloading", args2);
349 }
350 else if (current_action == Job.STOPPED || current_action == Job.PAUSED) {
351 Dictionary.setText(current_status, "Mirroring.Job.Waiting_User");
352 }
353 else {
354 Dictionary.setText(current_status, "Mirroring.Job.Mirroring_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 Dictionary.setText(results_status, "Mirroring.Job.Status", args3);
364
365 this.updateUI();
366 }
367
368 static final private Dimension DIALOG_SIZE = new Dimension(640,480);
369
370 private class LogDialog
371 extends JDialog {
372
373 public LogDialog(AppendLineOnlyFileDocument document) {
374 super(Gatherer.g_man, "Mirroring.Job.Download_Log_Dialog_Title");
375 setSize(DIALOG_SIZE);
376 JPanel content_pane = (JPanel) getContentPane();
377 JTextArea text_area = new JTextArea(document);
378 JButton button = new GLIButton(Dictionary.get("General.Close"));
379 // Connection
380 button.addActionListener(new CloseActionListener());
381 // Layout
382 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
383 content_pane.setLayout(new BorderLayout());
384 content_pane.add(new JScrollPane(text_area), BorderLayout.CENTER);
385 content_pane.add(button, BorderLayout.SOUTH);
386 }
387
388 private class CloseActionListener
389 implements ActionListener {
390 public void actionPerformed(ActionEvent event) {
391 LogDialog.this.dispose();
392 }
393 }
394 }
395}
Note: See TracBrowser for help on using the repository browser.