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