[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;
|
---|
[648] | 13 | import net.oni2.aeinstaller.backend.packages.Package;
|
---|
| 14 | import net.oni2.aeinstaller.backend.packages.download.ModDownloader;
|
---|
| 15 | import net.oni2.aeinstaller.backend.packages.download.ModDownloaderListener;
|
---|
| 16 | import net.oni2.aeinstaller.backend.packages.download.ModDownloader.State;
|
---|
[605] | 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 |
|
---|
[646] | 33 | private JLabel lblNameVal;
|
---|
| 34 | private JLabel lblIsDep;
|
---|
[605] | 35 | private JLabel lblElapsedVal;
|
---|
| 36 | private JLabel lblRemainingVal;
|
---|
| 37 | private JLabel lblDownloadedVal;
|
---|
| 38 | private JLabel lblTotalVal;
|
---|
| 39 | private JLabel lblRateVal;
|
---|
| 40 | private JProgressBar progress;
|
---|
[646] | 41 |
|
---|
[638] | 42 | private JButton btnAbort;
|
---|
[605] | 43 |
|
---|
| 44 | private ModDownloader downloader;
|
---|
[648] | 45 | private TreeSet<Package> dependencies = new TreeSet<Package>();
|
---|
[605] | 46 |
|
---|
| 47 | /**
|
---|
| 48 | * @param mods
|
---|
| 49 | * Mods to download
|
---|
[646] | 50 | * @param dependencies
|
---|
| 51 | * List of mods that only are auto-resolved dependencies
|
---|
[605] | 52 | */
|
---|
[648] | 53 | public Downloader(TreeSet<Package> mods, TreeSet<Package> dependencies) {
|
---|
[605] | 54 | super();
|
---|
| 55 |
|
---|
[606] | 56 | setResizable(false);
|
---|
| 57 | setSize(500, (int) getSize().getHeight());
|
---|
[605] | 58 |
|
---|
[646] | 59 | if (dependencies != null)
|
---|
| 60 | this.dependencies = dependencies;
|
---|
| 61 |
|
---|
[605] | 62 | downloader = new ModDownloader(mods, this);
|
---|
| 63 | progress.setMaximum(downloader.getTotalSize());
|
---|
| 64 | progress.setStringPainted(true);
|
---|
| 65 | progress.setToolTipText(String.format("%d / %d files downloaded", 0,
|
---|
| 66 | mods.size()));
|
---|
| 67 |
|
---|
| 68 | lblDownloadedVal.setText(SizeFormatter.format(0, 3));
|
---|
| 69 | lblTotalVal.setText(SizeFormatter.format(downloader.getTotalSize(), 3));
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | private void close() {
|
---|
| 73 | if (!downloader.isFinished())
|
---|
| 74 | downloader.abort();
|
---|
| 75 | setVisible(false);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | @SuppressWarnings("unused")
|
---|
| 79 | private boolean confirm() {
|
---|
| 80 | int res = JOptionPane.showConfirmDialog(this,
|
---|
| 81 | bundle.getString("abort.text"),
|
---|
| 82 | bundle.getString("abort.title"), JOptionPane.YES_NO_OPTION,
|
---|
| 83 | JOptionPane.WARNING_MESSAGE);
|
---|
| 84 | return res == JOptionPane.YES_OPTION;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | private String formatTime(int sec) {
|
---|
| 88 | int min = sec / 60;
|
---|
| 89 | sec = sec % 60;
|
---|
| 90 | return String.format("%02d:%02d", min, sec);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | @Override
|
---|
[648] | 94 | public void updateStatus(ModDownloader source, Package currentDownload,
|
---|
[646] | 95 | State state, int filesDown, int filesTotal, int bytesDown,
|
---|
| 96 | int bytesTotal, int duration, int remaining, int speed) {
|
---|
[605] | 97 | if (state == ModDownloader.State.FINISHED) {
|
---|
| 98 | close();
|
---|
| 99 | } else {
|
---|
[638] | 100 | if (state == State.LAST_FILE_DOWNLOADED)
|
---|
| 101 | btnAbort.setEnabled(false);
|
---|
[646] | 102 |
|
---|
[605] | 103 | progress.setValue(bytesDown);
|
---|
| 104 | progress.setToolTipText(String.format("%d / %d files downloaded",
|
---|
| 105 | filesDown, filesTotal));
|
---|
| 106 |
|
---|
[646] | 107 | if (currentDownload != null) {
|
---|
| 108 | lblNameVal.setText(currentDownload.getName());
|
---|
| 109 | lblIsDep.setVisible(dependencies.contains(currentDownload));
|
---|
| 110 | } else {
|
---|
| 111 | lblNameVal.setText(bundle.getString("unpacking"));
|
---|
| 112 | lblIsDep.setVisible(false);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[605] | 115 | lblElapsedVal.setText(formatTime(duration));
|
---|
| 116 | lblRemainingVal.setText(formatTime(remaining));
|
---|
| 117 |
|
---|
| 118 | lblDownloadedVal.setText(SizeFormatter.format(bytesDown, 3));
|
---|
| 119 |
|
---|
| 120 | lblRateVal.setText(SizeFormatter.format(speed, 3) + "/s");
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /**
|
---|
| 125 | * @return were all downloads finished?
|
---|
| 126 | */
|
---|
| 127 | public boolean isFinished() {
|
---|
| 128 | return downloader.isFinished();
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | }
|
---|