/**
*#########################################################################
*
* A component of the Gatherer application, part of the Greenstone digital
* library suite from the New Zealand Digital Library Project at the
* University of Waikato, New Zealand.
*
*
*
* Author: John Thompson, Greenstone Digital Library, University of Waikato
*
*
*
* Copyright (C) 1999 New Zealand Digital Library Project
*
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*########################################################################
*/
package org.greenstone.gatherer.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import org.greenstone.gatherer.Dictionary;
import org.greenstone.gatherer.Gatherer;
import org.greenstone.gatherer.collection.Job;
import org.greenstone.gatherer.util.Utility;
public class GProgressBar
extends JPanel
implements ActionListener {
private boolean simple = false;
private Dimension bar_size = new Dimension(520, 15);
private int current_action;
private int err_count;
private int file_count;
private int total_count;
private int warning_count;
private JLabel current_status;
private JLabel main_status;
private JLabel results_status;
private JLabel status;
private JPanel center_pane;
private JPanel inner_pane;
private JProgressBar progress;
private long file_size;
private long total_size;
private String current_url;
private String initial_url;
private Job owner;
public JButton action;
public JButton cancel;
public GProgressBar(Job owner, String initial_url, boolean simple) {
this.owner = owner;
this.current_url = null;
this.err_count = 0;
this.initial_url = initial_url;
this.file_count = 0;
this.file_size = 0;
this.simple = simple;
this.total_count = 0;
this.total_size = 0;
this.warning_count = 0;
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
this.current_action = Job.STOPPED;
inner_pane = new JPanel(new BorderLayout());
inner_pane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
action = new JButton(Utility.getImage("vcrplay.gif"));
action.addActionListener(this);
action.addActionListener(owner);
Dictionary.setTooltip(action, "Mirroring.Job.Start_Tooltip");
center_pane = new JPanel(new GridLayout(3,1));
main_status = new JLabel();
current_status = new JLabel();
results_status = new JLabel();
progress = new JProgressBar();
progress.setStringPainted(true);
progress.setMinimum(0);
progress.setMaximum(0);
progress.setEnabled(false);
Dictionary.setText(progress, "Mirroring.Job.Waiting");
inner_pane.add(progress, BorderLayout.CENTER);
center_pane.add(main_status);
center_pane.add(current_status);
center_pane.add(results_status);
cancel = new JButton(Utility.getImage("vcrstop.gif"));
cancel.addActionListener(owner);
cancel.addActionListener(this);
Dictionary.setTooltip(cancel, "Mirroring.Job.Stop_Tooltip");
inner_pane.add(center_pane, BorderLayout.NORTH);
this.add(action, BorderLayout.WEST);
this.add(inner_pane, BorderLayout.CENTER);
this.add(cancel, BorderLayout.EAST);
// Make the labels, etc update.
refresh();
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == action) {
if (current_action == Job.RUNNING) {
current_action = Job.STOPPED;
action.setIcon(Utility.getImage("vcrplay.gif"));
Dictionary.setTooltip(action, "Mirroring.Job.Start_Tooltip");
cancel.setEnabled(true);
}
else {
current_action = Job.RUNNING;
action.setIcon(Utility.getImage("vcrpause.gif"));
Dictionary.setTooltip(action, "Mirroring.Job.Pause_Tooltip");
cancel.setEnabled(false);
}
}
}
/** This method is called when a new download is begun. The
* details of the download are updated and a new JProgressBar
* assigned to track the download.
* @param url The url String of the file that is being downloaded.
* @param size The size of the file as an Int.
*/
public void addDownload(String url) {
current_url = url;
file_size = 0;
refresh();
}
/** When the download of the current url is completed, this method
* is called to enlighten the GProgressBar of this fact.
*/
public void downloadComplete() {
current_url = null;
file_count++;
if(total_count < (file_count + err_count + warning_count)) {
total_count = (file_count + err_count + warning_count);
}
progress.setValue(progress.getMaximum());
Dictionary.setText(progress, "Mirroring.Job.Download_Complete");
refresh();
}
public void downloadFailed() {
err_count++;
if(total_count < (file_count + err_count + warning_count)) {
total_count = (file_count + err_count + warning_count);
}
refresh();
}
public void downloadWarning() {
warning_count++;
if(total_count < (file_count + err_count + warning_count)) {
total_count = (file_count + err_count + warning_count);
}
refresh();
}
public Dimension getPreferredSize() {
return Utility.PROGRESS_BAR_SIZE;
}
/** When a link to be downloaded is located, the increaseTotalCount
* method is called. In this way the total count shows the most
* accurate remaining number of files to be downloaded.
*/
public void increaseFileCount() {
total_count++;
refresh();
}
/** When a mirroring task is first initiated this function is called
* to set initial values for the variables if necessary and to
* fiddle visual components such as the tool tip etc.
* @param reset A Boolean specifying whether the variables should be
* reset to zero.
*/
public void mirrorBegun(boolean reset, boolean simple) {
if(reset) {
this.file_count = 0;
this.file_size = 0;
this.total_count = 0;
this.total_size = 0;
this.err_count = 0;
this.warning_count = 0;
}
current_action = Job.RUNNING;
action.setIcon(Utility.getImage("vcrpause.gif"));
Dictionary.setTooltip(cancel, "Mirroring.Job.Pause_Tooltip");
// If this is a simple download, then pause is not available (nor is prematurely stopping!)
action.setEnabled(!simple);
cancel.setEnabled(false);
if(simple) {
progress.setIndeterminate(true);
}
}
/** Once a mirroring task is complete, is the Job returns from the
* native call but the status is still running, then this method
* is called to once again tinker with the pritty visual
* components.
*/
public void mirrorComplete() {
current_action = Job.COMPLETE;
current_url = null;
if(simple) {
progress.setIndeterminate(false);
}
progress.setValue(progress.getMaximum());
Dictionary.setText(progress, "Mirroring.Job.Mirror_Complete");
action.setIcon(Utility.getImage("vcrfastforward.gif"));
Dictionary.setText(action, "Mirroring.Job.Restart");
action.setEnabled(true);
cancel.setEnabled(true);
this.updateUI();
}
/** When called this method updates the GProgressBar to reflect
* the ammount of the current file downloaded.
*/
public void updateProgress(long current, long expected) {
file_size = file_size + current;
if(!progress.isIndeterminate()) {
// If current is zero, then this is the 'precall' before the
// downloading actually starts.
if(current == 0) {
// Remove the old progress bar, then deallocate it.
inner_pane.remove(progress);
progress = null;
if(expected == 0) {
// We don't have a content length. This bar will go from 0 to 100 only!
progress = new JProgressBar(0, 100);
}
else {
// Assign a new progress bar of length expected content length.
progress = new JProgressBar(0, (new Long(expected)).intValue());
}
progress.setEnabled(true);
// Add the new progress bar.
inner_pane.add(progress, BorderLayout.CENTER);
inner_pane.updateUI();
}
// Otherwise if expected is not zero move the progress bar and
// update percent complete.
else if (expected != 0) {
progress.setValue((new Long(file_size)).intValue());
int p_c = (new Double(progress.getPercentComplete() * 100)).intValue();
progress.setString(p_c + "%");
progress.setStringPainted(true);
}
// Finally, in the case we have no content length, we'll instead
// write the current number of bytes downloaded again.
else {
progress.setString(file_size + " b");
progress.setStringPainted(true);
}
}
refresh();
}
/** Causes the two labels associated with this GProgressBar object to
* update, thus reflecting the progression of the download. This
* method is called by any of the other public setter methods in this
* class.
*/
private void refresh() {
// Refresh the contents of main label.
String args1[] = new String[1];
args1[0] = initial_url.toString();
Dictionary.setText(main_status, "Mirroring.Job.Mirroring", args1);
if (current_url != null) {
// Refresh the current label contents.
String args2[] = new String[1];
args2[0] = current_url;
Dictionary.setText(current_status, "Mirroring.Job.Downloading", args2);
}
else if (current_action == Job.STOPPED || current_action == Job.PAUSED) {
Dictionary.setText(current_status, "Mirroring.Job.Waiting_User");
}
else {
Dictionary.setText(current_status, "Mirroring.Job.Mirroring_Complete");
}
// Refresh the contents of results label
String args3[] = new String[4];
args3[0] = file_count + "";
args3[1] = total_count + "";
args3[2] = warning_count + "";
args3[3] = err_count + "";
Dictionary.setText(results_status, "Mirroring.Job.Status", args3);
this.updateUI();
}
}