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

Last change on this file since 6590 was 6590, checked in by jmt12, 20 years ago

Started effecting the changes requested by Ian and David. So far I've removed the complex arguments, and have ensured that the path is correct for downloads without page requisites. I've also tried to get the workspace tree to update properly, and it is much closer than before but it is now temporarily displaying the same node twice.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.4 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.Utility;
48
49public class GProgressBar
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 private JLabel status;
67
68 private JPanel center_pane;
69 private JPanel inner_pane;
70
71 private JProgressBar progress;
72
73 private long file_size;
74 private long total_size;
75
76 private String current_url;
77 private String initial_url;
78
79 private Job owner;
80
81 public JButton action;
82 public JButton cancel;
83
84 public GProgressBar(Job 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 = Job.STOPPED;
100
101 inner_pane = new JPanel(new BorderLayout());
102 inner_pane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
103
104 action = new GLIButton(Utility.getImage("vcrplay.gif"));
105 action.addActionListener(this);
106 action.addActionListener(owner);
107 action.setMnemonic(KeyEvent.VK_ENTER);
108 Dictionary.setTooltip(action, "Mirroring.Job.Start_Tooltip");
109
110 center_pane = new JPanel(new GridLayout(3,1));
111
112 main_status = new JLabel();
113
114 current_status = new JLabel();
115
116 results_status = new JLabel();
117
118 progress = new JProgressBar();
119 progress.setStringPainted(true);
120 progress.setMinimum(0);
121 progress.setMaximum(0);
122 progress.setEnabled(false);
123 Dictionary.setText(progress, "Mirroring.Job.Waiting");
124 inner_pane.add(progress, BorderLayout.CENTER);
125
126 center_pane.add(main_status);
127 center_pane.add(current_status);
128 center_pane.add(results_status);
129
130 cancel = new GLIButton(Utility.getImage("vcrstop.gif"));
131 cancel.addActionListener(owner);
132 cancel.addActionListener(this);
133 cancel.setMnemonic(KeyEvent.VK_BACK_SPACE);
134 Dictionary.setTooltip(cancel, "Mirroring.Job.Stop_Tooltip");
135
136 inner_pane.add(center_pane, BorderLayout.NORTH);
137
138 this.add(action, BorderLayout.WEST);
139 this.add(inner_pane, BorderLayout.CENTER);
140 this.add(cancel, BorderLayout.EAST);
141
142 // Make the labels, etc update.
143 refresh();
144 }
145
146 public void actionPerformed(ActionEvent event) {
147 if(event.getSource() == action) {
148 if (current_action == Job.RUNNING) {
149 current_action = Job.STOPPED;
150 action.setIcon(Utility.getImage("vcrplay.gif"));
151 Dictionary.setTooltip(action, "Mirroring.Job.Start_Tooltip");
152 cancel.setEnabled(true);
153 }
154 else {
155 current_action = Job.RUNNING;
156 action.setIcon(Utility.getImage("vcrpause.gif"));
157 Dictionary.setTooltip(action, "Mirroring.Job.Pause_Tooltip");
158 cancel.setEnabled(false);
159 }
160 }
161 }
162
163 /** This method is called when a new download is begun. The
164 * details of the download are updated and a new JProgressBar
165 * assigned to track the download.
166 * @param url The url String of the file that is being downloaded.
167 * @param size The size of the file as an Int.
168 */
169 public void addDownload(String url) {
170 current_url = url;
171 file_size = 0;
172 refresh();
173 }
174
175 /** When the download of the current url is completed, this method
176 * is called to enlighten the GProgressBar of this fact.
177 */
178 public void downloadComplete() {
179 current_url = null;
180 file_count++;
181 if(total_count < (file_count + err_count + warning_count)) {
182 total_count = (file_count + err_count + warning_count);
183 }
184 progress.setValue(progress.getMaximum());
185 Dictionary.setText(progress, "Mirroring.Job.Download_Complete");
186 refresh();
187 }
188
189 public void downloadFailed() {
190 err_count++;
191 if(total_count < (file_count + err_count + warning_count)) {
192 total_count = (file_count + err_count + warning_count);
193 }
194 refresh();
195 }
196
197 public void downloadWarning() {
198 warning_count++;
199 if(total_count < (file_count + err_count + warning_count)) {
200 total_count = (file_count + err_count + warning_count);
201 }
202 refresh();
203 }
204
205 public Dimension getPreferredSize() {
206 return Utility.PROGRESS_BAR_SIZE;
207 }
208
209 /** When a link to be downloaded is located, the increaseTotalCount
210 * method is called. In this way the total count shows the most
211 * accurate remaining number of files to be downloaded.
212 */
213 public void increaseFileCount() {
214 total_count++;
215 refresh();
216 }
217
218 /** When a mirroring task is first initiated this function is called
219 * to set initial values for the variables if necessary and to
220 * fiddle visual components such as the tool tip etc.
221 * @param reset A Boolean specifying whether the variables should be
222 * reset to zero.
223 */
224 public void mirrorBegun(boolean reset, boolean simple) {
225 if(reset) {
226 this.file_count = 0;
227 this.file_size = 0;
228 this.total_count = 0;
229 this.total_size = 0;
230 this.err_count = 0;
231 this.warning_count = 0;
232 }
233 current_action = Job.RUNNING;
234 action.setIcon(Utility.getImage("vcrpause.gif"));
235 Dictionary.setTooltip(cancel, "Mirroring.Job.Pause_Tooltip");
236 // If this is a simple download, then pause is not available (nor is prematurely stopping!)
237 action.setEnabled(!simple);
238 cancel.setEnabled(false);
239 if(simple) {
240 progress.setIndeterminate(true);
241 }
242 }
243
244 /** Once a mirroring task is complete, is the Job returns from the
245 * native call but the status is still running, then this method
246 * is called to once again tinker with the pritty visual
247 * components.
248 */
249 public void mirrorComplete() {
250 current_action = Job.COMPLETE;
251 current_url = null;
252 if(simple) {
253 progress.setIndeterminate(false);
254 }
255 progress.setValue(progress.getMaximum());
256 /** @todo - put into dictionary */
257 progress.setString("Mirroring.Job.Mirror_Complete");
258 //Dictionary.setText(progress, "Mirroring.Job.Mirror_Complete");
259
260 action.setIcon(Utility.getImage("vcrfastforward.gif"));
261 Dictionary.setTooltip(action, "Mirroring.Job.Restart_Tooltip");
262 action.setEnabled(true);
263 cancel.setEnabled(true);
264 this.updateUI();
265 }
266
267 /** When called this method updates the GProgressBar to reflect
268 * the ammount of the current file downloaded.
269 */
270 public void updateProgress(long current, long expected) {
271 file_size = file_size + current;
272 if(!progress.isIndeterminate()) {
273 // If current is zero, then this is the 'precall' before the
274 // downloading actually starts.
275 if(current == 0) {
276 // Remove the old progress bar, then deallocate it.
277 inner_pane.remove(progress);
278 progress = null;
279 if(expected == 0) {
280 // We don't have a content length. This bar will go from 0 to 100 only!
281 progress = new JProgressBar(0, 100);
282 }
283 else {
284 // Assign a new progress bar of length expected content length.
285 progress = new JProgressBar(0, (new Long(expected)).intValue());
286 }
287 progress.setEnabled(true);
288 // Add the new progress bar.
289 inner_pane.add(progress, BorderLayout.CENTER);
290 inner_pane.updateUI();
291 }
292 // Otherwise if expected is not zero move the progress bar and
293 // update percent complete.
294 else if (expected != 0) {
295 progress.setValue((new Long(file_size)).intValue());
296 int p_c = (new Double(progress.getPercentComplete() * 100)).intValue();
297 progress.setString(p_c + "%");
298 progress.setStringPainted(true);
299 }
300 // Finally, in the case we have no content length, we'll instead
301 // write the current number of bytes downloaded again.
302 else {
303 progress.setString(file_size + " b");
304 progress.setStringPainted(true);
305 }
306 }
307 refresh();
308 }
309
310 /** Causes the two labels associated with this GProgressBar object to
311 * update, thus reflecting the progression of the download. This
312 * method is called by any of the other public setter methods in this
313 * class.
314 */
315 private void refresh() {
316 // Refresh the contents of main label.
317 String args1[] = new String[1];
318 args1[0] = initial_url.toString();
319 Dictionary.setText(main_status, "Mirroring.Job.Mirroring", args1);
320
321 if (current_url != null) {
322 // Refresh the current label contents.
323 String args2[] = new String[1];
324 args2[0] = current_url;
325 Dictionary.setText(current_status, "Mirroring.Job.Downloading", args2);
326 }
327 else if (current_action == Job.STOPPED || current_action == Job.PAUSED) {
328 Dictionary.setText(current_status, "Mirroring.Job.Waiting_User");
329 }
330 else {
331 Dictionary.setText(current_status, "Mirroring.Job.Mirroring_Complete");
332 }
333
334 // Refresh the contents of results label
335 String args3[] = new String[4];
336 args3[0] = file_count + "";
337 args3[1] = total_count + "";
338 args3[2] = warning_count + "";
339 args3[3] = err_count + "";
340 Dictionary.setText(results_status, "Mirroring.Job.Status", args3);
341
342 this.updateUI();
343 }
344}
Note: See TracBrowser for help on using the repository browser.