📄 progressdialog.java
字号:
/*
* Copyright 2007 JavaAtWork All rights reserved.
* Use is subject to license terms.
*/
package com.javaatwork.mydownloader.dialog;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import com.javaatwork.mydownloader.MyDownloader;
import com.javaatwork.mydownloader.listeners.DownloadManager;
import com.javaatwork.mydownloader.utils.ByteFormatter;
import com.javaatwork.mydownloader.utils.LocaleManager;
/**
* Shows the progress of the download.
*
* @author Johannes Postma
*/
public class ProgressDialog extends JDialog {
private static final long serialVersionUID = 8672661113098681632L;
private JLabel labelStatus = null;
private JLabel labelStat = new JLabel("");
private JLabel labelFile = null;
private JLabel labelFl = new JLabel("");
private JLabel labelFiles = null;
private JLabel labelProgress = null;
private JLabel labelBytes = null;
private JLabel labelBts = new JLabel("");
private JButton buttonCancel = null;
private JPanel commandPanel = new JPanel();
private JPanel commandHelpPanel = new JPanel();
private JPanel helpPanel = new JPanel();
private JPanel mainPanel = new JPanel();
private JProgressBar progressBar = new JProgressBar(0,100);
private JProgressBar progressBarTotal = new JProgressBar(0,100);
private Timer timer = null;
private DownloadManager downloadManager = null;
private LocaleManager localeManager = null;
/**
* Progress dialog for the download process.
*
* @param applet MyDownloader.
* @param downloadManager The download controller.
*/
public ProgressDialog(MyDownloader applet, DownloadManager downloadManager) {
super(applet.getFrame(), true);
setModal(true);
setTitle("Download Process");
this.localeManager = LocaleManager.getInstance();
this.downloadManager = downloadManager;
labelStatus = new JLabel(localeManager.getString("status") + " ");
labelFile = new JLabel(localeManager.getString("file") + " ");
labelFiles = new JLabel(localeManager.getString("files") + " ");
labelProgress = new JLabel(localeManager.getString("progress") + " ");
labelBytes = new JLabel(localeManager.getString("bytes_retrieved") + " ");
//initialises the helppanel
helpPanel.setLayout(new GridLayout(5,2,5,5));
helpPanel.add(labelStatus);
helpPanel.add(labelStat);
helpPanel.add(labelFile);
helpPanel.add(labelFl);
helpPanel.add(labelFiles);
helpPanel.add(progressBarTotal);
helpPanel.add(labelProgress);
helpPanel.add(progressBar);
helpPanel.add(labelBytes);
helpPanel.add(labelBts);
helpPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
buttonCancel = new JButton(localeManager.getString("cancel"));
commandHelpPanel.setLayout(new GridLayout(0,1,5,5));
commandHelpPanel.add(buttonCancel);
commandPanel.add(commandHelpPanel);
// initialises the mainpanel
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5,5,5,5), BorderFactory.createEtchedBorder()));
mainPanel.add(helpPanel, BorderLayout.CENTER);
buttonCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cancel();
}
});
this.getContentPane().add(mainPanel, BorderLayout.CENTER);
this.getContentPane().add(commandPanel, BorderLayout.SOUTH);
this.pack();
// sets the frame to the center
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation(
(screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
/**
* Shows the progressbar.
*/
public void start() {
progressBar.setStringPainted(true);
progressBarTotal.setStringPainted(true);
// Create a timer.
timer = new Timer(50, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (!downloadManager.isFinished()) {
labelStat.setText(downloadManager.getStatus());
labelFl.setText(downloadManager.getFileInProgress());
// if the size is unkown (contentlength is unkown)
// the progressbar must be in the indeterminate mode
if (downloadManager.getPercentCompleted() > 100) {
progressBar.setIndeterminate(true);
progressBar.setString("");
} else {
progressBar.setIndeterminate(false);
progressBar.setValue(downloadManager.getPercentCompleted());
progressBar.setString(downloadManager.getPercentCompleted() + "%");
}
progressBarTotal.setValue((int)((double)downloadManager.getFileInRow()/ (double)downloadManager.getNumberOfFiles() * 100));
progressBarTotal.setString(downloadManager.getFileInRow() + " " + localeManager.getString("of") +" " + downloadManager.getNumberOfFiles());
labelBts.setText(ByteFormatter.format(downloadManager.getTotalBytesDownloaded()));
} else {
close();
}
}
});
timer.start();
setVisible(true);
}
/**
* Cancels the download.
*/
private void cancel() {
downloadManager.cancel();
setVisible(false);
}
/**
* Closes the dialog.
*/
private void close() {
this.setVisible(false);
}
/**
* Shows a message.
*
* @param message The message.
*/
public void showMessage(String message) {
JOptionPane.showMessageDialog(this, message);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -