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