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