[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;
|
---|
[840] | 17 | import net.oni2.resourcebundle.UTF8ResourceBundleLoader;
|
---|
[605] | 18 |
|
---|
| 19 | import org.javabuilders.BuildResult;
|
---|
| 20 | import org.javabuilders.swing.SwingJavaBuilder;
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * @author Christian Illy
|
---|
| 24 | */
|
---|
| 25 | public class Downloader extends JDialog implements ModDownloaderListener {
|
---|
| 26 | private static final long serialVersionUID = 9097967828001263776L;
|
---|
| 27 |
|
---|
[840] | 28 | private ResourceBundle bundle = UTF8ResourceBundleLoader
|
---|
[629] | 29 | .getBundle("net.oni2.aeinstaller.localization."
|
---|
| 30 | + getClass().getSimpleName());
|
---|
[605] | 31 | @SuppressWarnings("unused")
|
---|
| 32 | private BuildResult result = SwingJavaBuilder.build(this, bundle);
|
---|
| 33 |
|
---|
[646] | 34 | private JLabel lblNameVal;
|
---|
| 35 | private JLabel lblIsDep;
|
---|
[605] | 36 | private JLabel lblElapsedVal;
|
---|
| 37 | private JLabel lblRemainingVal;
|
---|
| 38 | private JLabel lblDownloadedVal;
|
---|
| 39 | private JLabel lblTotalVal;
|
---|
| 40 | private JLabel lblRateVal;
|
---|
| 41 | private JProgressBar progress;
|
---|
[646] | 42 |
|
---|
[638] | 43 | private JButton btnAbort;
|
---|
[605] | 44 |
|
---|
| 45 | private ModDownloader downloader;
|
---|
[648] | 46 | private TreeSet<Package> dependencies = new TreeSet<Package>();
|
---|
[605] | 47 |
|
---|
| 48 | /**
|
---|
| 49 | * @param mods
|
---|
| 50 | * Mods to download
|
---|
[646] | 51 | * @param dependencies
|
---|
| 52 | * List of mods that only are auto-resolved dependencies
|
---|
[773] | 53 | * @param isCoreDownload
|
---|
| 54 | * Downloading core packages - can not be aborted
|
---|
[605] | 55 | */
|
---|
[773] | 56 | public Downloader(TreeSet<Package> mods, TreeSet<Package> dependencies,
|
---|
| 57 | boolean isCoreDownload) {
|
---|
[605] | 58 | super();
|
---|
| 59 |
|
---|
[773] | 60 | if (isCoreDownload) {
|
---|
| 61 | setTitle(bundle.getString("frame.titleCore"));
|
---|
| 62 | btnAbort.setEnabled(false);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[606] | 65 | setResizable(false);
|
---|
| 66 | setSize(500, (int) getSize().getHeight());
|
---|
[605] | 67 |
|
---|
[646] | 68 | if (dependencies != null)
|
---|
| 69 | this.dependencies = dependencies;
|
---|
| 70 |
|
---|
[605] | 71 | downloader = new ModDownloader(mods, this);
|
---|
| 72 | progress.setMaximum(downloader.getTotalSize());
|
---|
| 73 | progress.setStringPainted(true);
|
---|
| 74 | progress.setToolTipText(String.format("%d / %d files downloaded", 0,
|
---|
| 75 | mods.size()));
|
---|
| 76 |
|
---|
| 77 | lblDownloadedVal.setText(SizeFormatter.format(0, 3));
|
---|
| 78 | lblTotalVal.setText(SizeFormatter.format(downloader.getTotalSize(), 3));
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | private void close() {
|
---|
| 82 | if (!downloader.isFinished())
|
---|
| 83 | downloader.abort();
|
---|
| 84 | setVisible(false);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | @SuppressWarnings("unused")
|
---|
| 88 | private boolean confirm() {
|
---|
[773] | 89 | if (btnAbort.isEnabled()) {
|
---|
| 90 | int res = JOptionPane.showConfirmDialog(this,
|
---|
| 91 | bundle.getString("abort.text"),
|
---|
| 92 | bundle.getString("abort.title"), JOptionPane.YES_NO_OPTION,
|
---|
| 93 | JOptionPane.WARNING_MESSAGE);
|
---|
| 94 | return res == JOptionPane.YES_OPTION;
|
---|
| 95 | } else
|
---|
| 96 | return false;
|
---|
[605] | 97 | }
|
---|
| 98 |
|
---|
| 99 | private String formatTime(int sec) {
|
---|
| 100 | int min = sec / 60;
|
---|
| 101 | sec = sec % 60;
|
---|
| 102 | return String.format("%02d:%02d", min, sec);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | @Override
|
---|
[648] | 106 | public void updateStatus(ModDownloader source, Package currentDownload,
|
---|
[646] | 107 | State state, int filesDown, int filesTotal, int bytesDown,
|
---|
| 108 | int bytesTotal, int duration, int remaining, int speed) {
|
---|
[605] | 109 | if (state == ModDownloader.State.FINISHED) {
|
---|
| 110 | close();
|
---|
| 111 | } else {
|
---|
[638] | 112 | if (state == State.LAST_FILE_DOWNLOADED)
|
---|
| 113 | btnAbort.setEnabled(false);
|
---|
[646] | 114 |
|
---|
[605] | 115 | progress.setValue(bytesDown);
|
---|
| 116 | progress.setToolTipText(String.format("%d / %d files downloaded",
|
---|
| 117 | filesDown, filesTotal));
|
---|
| 118 |
|
---|
[646] | 119 | if (currentDownload != null) {
|
---|
| 120 | lblNameVal.setText(currentDownload.getName());
|
---|
| 121 | lblIsDep.setVisible(dependencies.contains(currentDownload));
|
---|
| 122 | } else {
|
---|
| 123 | lblNameVal.setText(bundle.getString("unpacking"));
|
---|
| 124 | lblIsDep.setVisible(false);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[605] | 127 | lblElapsedVal.setText(formatTime(duration));
|
---|
| 128 | lblRemainingVal.setText(formatTime(remaining));
|
---|
| 129 |
|
---|
| 130 | lblDownloadedVal.setText(SizeFormatter.format(bytesDown, 3));
|
---|
| 131 |
|
---|
| 132 | lblRateVal.setText(SizeFormatter.format(speed, 3) + "/s");
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * @return were all downloads finished?
|
---|
| 138 | */
|
---|
| 139 | public boolean isFinished() {
|
---|
| 140 | return downloader.isFinished();
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | }
|
---|