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 | import net.oni2.resourcebundle.UTF8ResourceBundleLoader;
|
---|
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 |
|
---|
28 | private ResourceBundle bundle = UTF8ResourceBundleLoader
|
---|
29 | .getBundle("net.oni2.aeinstaller.localization."
|
---|
30 | + getClass().getSimpleName());
|
---|
31 | @SuppressWarnings("unused")
|
---|
32 | private BuildResult result = SwingJavaBuilder.build(this, bundle);
|
---|
33 |
|
---|
34 | private JLabel lblNameVal;
|
---|
35 | private JLabel lblIsDep;
|
---|
36 | private JLabel lblElapsedVal;
|
---|
37 | private JLabel lblRemainingVal;
|
---|
38 | private JLabel lblDownloadedVal;
|
---|
39 | private JLabel lblTotalVal;
|
---|
40 | private JLabel lblRateVal;
|
---|
41 | private JProgressBar progress;
|
---|
42 |
|
---|
43 | private JButton btnAbort;
|
---|
44 |
|
---|
45 | private ModDownloader downloader;
|
---|
46 | private TreeSet<Package> dependencies = new TreeSet<Package>();
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * @param mods
|
---|
50 | * Mods to download
|
---|
51 | * @param dependencies
|
---|
52 | * List of mods that only are auto-resolved dependencies
|
---|
53 | * @param isCoreDownload
|
---|
54 | * Downloading core packages - can not be aborted
|
---|
55 | */
|
---|
56 | public Downloader(TreeSet<Package> mods, TreeSet<Package> dependencies,
|
---|
57 | boolean isCoreDownload) {
|
---|
58 | super();
|
---|
59 |
|
---|
60 | if (isCoreDownload) {
|
---|
61 | setTitle(bundle.getString("frame.titleCore"));
|
---|
62 | btnAbort.setEnabled(false);
|
---|
63 | }
|
---|
64 |
|
---|
65 | setResizable(false);
|
---|
66 | setSize(500, (int) getSize().getHeight());
|
---|
67 |
|
---|
68 | if (dependencies != null)
|
---|
69 | this.dependencies = dependencies;
|
---|
70 |
|
---|
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() {
|
---|
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;
|
---|
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
|
---|
106 | public void updateStatus(ModDownloader source, Package currentDownload,
|
---|
107 | State state, int filesDown, int filesTotal, int bytesDown,
|
---|
108 | int bytesTotal, int duration, int remaining, int speed) {
|
---|
109 | if (state == ModDownloader.State.FINISHED) {
|
---|
110 | close();
|
---|
111 | } else {
|
---|
112 | if (state == State.LAST_FILE_DOWNLOADED)
|
---|
113 | btnAbort.setEnabled(false);
|
---|
114 |
|
---|
115 | progress.setValue(bytesDown);
|
---|
116 | progress.setToolTipText(String.format("%d / %d files downloaded",
|
---|
117 | filesDown, filesTotal));
|
---|
118 |
|
---|
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 |
|
---|
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 | }
|
---|