[605] | 1 | package net.oni2.aeinstaller.gui.downloadwindow;
|
---|
| 2 |
|
---|
| 3 | import java.util.ResourceBundle;
|
---|
| 4 | import java.util.TreeSet;
|
---|
| 5 |
|
---|
[638] | 6 | import javax.swing.JButton;
|
---|
[605] | 7 | import javax.swing.JDialog;
|
---|
| 8 | import javax.swing.JLabel;
|
---|
| 9 | import javax.swing.JOptionPane;
|
---|
| 10 | import javax.swing.JProgressBar;
|
---|
| 11 |
|
---|
| 12 | import net.oni2.aeinstaller.backend.SizeFormatter;
|
---|
| 13 | import net.oni2.aeinstaller.backend.mods.Mod;
|
---|
| 14 | import net.oni2.aeinstaller.backend.mods.download.ModDownloader;
|
---|
| 15 | import net.oni2.aeinstaller.backend.mods.download.ModDownloader.State;
|
---|
| 16 | import net.oni2.aeinstaller.backend.mods.download.ModDownloaderListener;
|
---|
| 17 |
|
---|
| 18 | import org.javabuilders.BuildResult;
|
---|
| 19 | import org.javabuilders.swing.SwingJavaBuilder;
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
| 22 | * @author Christian Illy
|
---|
| 23 | */
|
---|
| 24 | public class Downloader extends JDialog implements ModDownloaderListener {
|
---|
| 25 | private static final long serialVersionUID = 9097967828001263776L;
|
---|
| 26 |
|
---|
[629] | 27 | private ResourceBundle bundle = ResourceBundle
|
---|
| 28 | .getBundle("net.oni2.aeinstaller.localization."
|
---|
| 29 | + getClass().getSimpleName());
|
---|
[605] | 30 | @SuppressWarnings("unused")
|
---|
| 31 | private BuildResult result = SwingJavaBuilder.build(this, bundle);
|
---|
| 32 |
|
---|
| 33 | private JLabel lblElapsedVal;
|
---|
| 34 | private JLabel lblRemainingVal;
|
---|
| 35 | private JLabel lblDownloadedVal;
|
---|
| 36 | private JLabel lblTotalVal;
|
---|
| 37 | private JLabel lblRateVal;
|
---|
| 38 | private JProgressBar progress;
|
---|
[638] | 39 |
|
---|
| 40 | private JButton btnAbort;
|
---|
[605] | 41 |
|
---|
| 42 | private ModDownloader downloader;
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * @param mods
|
---|
| 46 | * Mods to download
|
---|
| 47 | */
|
---|
| 48 | public Downloader(TreeSet<Mod> mods) {
|
---|
| 49 | super();
|
---|
| 50 |
|
---|
[606] | 51 | setResizable(false);
|
---|
| 52 | setSize(500, (int) getSize().getHeight());
|
---|
[605] | 53 |
|
---|
| 54 | downloader = new ModDownloader(mods, this);
|
---|
| 55 | progress.setMaximum(downloader.getTotalSize());
|
---|
| 56 | progress.setStringPainted(true);
|
---|
| 57 | progress.setToolTipText(String.format("%d / %d files downloaded", 0,
|
---|
| 58 | mods.size()));
|
---|
| 59 |
|
---|
| 60 | lblDownloadedVal.setText(SizeFormatter.format(0, 3));
|
---|
| 61 | lblTotalVal.setText(SizeFormatter.format(downloader.getTotalSize(), 3));
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | private void close() {
|
---|
| 65 | if (!downloader.isFinished())
|
---|
| 66 | downloader.abort();
|
---|
| 67 | setVisible(false);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | @SuppressWarnings("unused")
|
---|
| 71 | private boolean confirm() {
|
---|
| 72 | int res = JOptionPane.showConfirmDialog(this,
|
---|
| 73 | bundle.getString("abort.text"),
|
---|
| 74 | bundle.getString("abort.title"), JOptionPane.YES_NO_OPTION,
|
---|
| 75 | JOptionPane.WARNING_MESSAGE);
|
---|
| 76 | return res == JOptionPane.YES_OPTION;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | private String formatTime(int sec) {
|
---|
| 80 | int min = sec / 60;
|
---|
| 81 | sec = sec % 60;
|
---|
| 82 | return String.format("%02d:%02d", min, sec);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | @Override
|
---|
| 86 | public void updateStatus(ModDownloader source, State state, int filesDown,
|
---|
| 87 | int filesTotal, int bytesDown, int bytesTotal, int duration,
|
---|
| 88 | int remaining, int speed) {
|
---|
| 89 | if (state == ModDownloader.State.FINISHED) {
|
---|
| 90 | close();
|
---|
| 91 | } else {
|
---|
[638] | 92 | if (state == State.LAST_FILE_DOWNLOADED)
|
---|
| 93 | btnAbort.setEnabled(false);
|
---|
| 94 |
|
---|
[605] | 95 | progress.setValue(bytesDown);
|
---|
| 96 | progress.setToolTipText(String.format("%d / %d files downloaded",
|
---|
| 97 | filesDown, filesTotal));
|
---|
| 98 |
|
---|
| 99 | lblElapsedVal.setText(formatTime(duration));
|
---|
| 100 | lblRemainingVal.setText(formatTime(remaining));
|
---|
| 101 |
|
---|
| 102 | lblDownloadedVal.setText(SizeFormatter.format(bytesDown, 3));
|
---|
| 103 |
|
---|
| 104 | lblRateVal.setText(SizeFormatter.format(speed, 3) + "/s");
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * @return were all downloads finished?
|
---|
| 110 | */
|
---|
| 111 | public boolean isFinished() {
|
---|
| 112 | return downloader.isFinished();
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | }
|
---|