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