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.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 |
|
---|
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<Mod> dependencies = new TreeSet<Mod>();
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @param mods
|
---|
49 | * Mods to download
|
---|
50 | * @param dependencies
|
---|
51 | * List of mods that only are auto-resolved dependencies
|
---|
52 | */
|
---|
53 | public Downloader(TreeSet<Mod> mods, TreeSet<Mod> dependencies) {
|
---|
54 | super();
|
---|
55 |
|
---|
56 | setResizable(false);
|
---|
57 | setSize(500, (int) getSize().getHeight());
|
---|
58 |
|
---|
59 | if (dependencies != null)
|
---|
60 | this.dependencies = dependencies;
|
---|
61 |
|
---|
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
|
---|
94 | public void updateStatus(ModDownloader source, Mod currentDownload,
|
---|
95 | State state, int filesDown, int filesTotal, int bytesDown,
|
---|
96 | int bytesTotal, int duration, int remaining, int speed) {
|
---|
97 | if (state == ModDownloader.State.FINISHED) {
|
---|
98 | close();
|
---|
99 | } else {
|
---|
100 | if (state == State.LAST_FILE_DOWNLOADED)
|
---|
101 | btnAbort.setEnabled(false);
|
---|
102 |
|
---|
103 | progress.setValue(bytesDown);
|
---|
104 | progress.setToolTipText(String.format("%d / %d files downloaded",
|
---|
105 | filesDown, filesTotal));
|
---|
106 |
|
---|
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 |
|
---|
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 | }
|
---|