|
@@ -23,25 +23,39 @@
|
23
|
23
|
package com.dmdirc.addons.ui_swing.components.addonbrowser;
|
24
|
24
|
|
25
|
25
|
import com.dmdirc.Main;
|
|
26
|
+import com.dmdirc.addons.ui_swing.UIUtilities;
|
26
|
27
|
import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
|
|
28
|
+import com.dmdirc.addons.ui_swing.components.text.TextLabel;
|
27
|
29
|
import com.dmdirc.logger.ErrorLevel;
|
28
|
30
|
import com.dmdirc.logger.Logger;
|
29
|
31
|
import com.dmdirc.util.ConfigFile;
|
|
32
|
+import com.dmdirc.util.DownloadListener;
|
|
33
|
+import com.dmdirc.util.Downloader;
|
30
|
34
|
import com.dmdirc.util.InvalidConfigFileException;
|
31
|
35
|
|
32
|
36
|
import java.io.File;
|
33
|
37
|
import java.io.IOException;
|
|
38
|
+import java.util.ArrayList;
|
34
|
39
|
import java.util.Collection;
|
35
|
40
|
import java.util.Collections;
|
|
41
|
+import java.util.List;
|
36
|
42
|
import java.util.Map;
|
37
|
43
|
import java.util.concurrent.ExecutionException;
|
|
44
|
+import javax.swing.Box;
|
|
45
|
+
|
|
46
|
+import javax.swing.JPanel;
|
|
47
|
+import javax.swing.JProgressBar;
|
38
|
48
|
import javax.swing.JScrollPane;
|
|
49
|
+import javax.swing.text.StyleConstants;
|
|
50
|
+
|
|
51
|
+import net.miginfocom.swing.MigLayout;
|
39
|
52
|
|
40
|
53
|
/**
|
41
|
54
|
* Loads the addon data feed into the addon browser.
|
42
|
55
|
*/
|
43
|
56
|
public class DataLoaderWorker
|
44
|
|
- extends LoggingSwingWorker<Collection<Map<String, String>>, Object> {
|
|
57
|
+ extends LoggingSwingWorker<Collection<AddonInfo>, Object>
|
|
58
|
+ implements DownloadListener {
|
45
|
59
|
|
46
|
60
|
/** List to load data into. */
|
47
|
61
|
private final AddonTable table;
|
|
@@ -49,18 +63,24 @@ public class DataLoaderWorker
|
49
|
63
|
private final BrowserWindow browserWindow;
|
50
|
64
|
/** Table's parent scrollpane. */
|
51
|
65
|
private final JScrollPane scrollPane;
|
|
66
|
+ /** Downloader progress bar. */
|
|
67
|
+ private final JProgressBar jpb = new JProgressBar(0, 100);
|
|
68
|
+ /** Refresh addons feed? */
|
|
69
|
+ private final boolean download;
|
52
|
70
|
|
53
|
71
|
/**
|
54
|
72
|
* Creates a new data loader worker.
|
55
|
73
|
*
|
56
|
74
|
* @param table Table to load data into
|
|
75
|
+ * @param download Download new addons feed?
|
57
|
76
|
* @param browserWindow Browser window to pass to table objects
|
58
|
77
|
* @param scrollPane Table's parent scrollpane
|
59
|
78
|
*/
|
60
|
|
- public DataLoaderWorker(final AddonTable table,
|
|
79
|
+ public DataLoaderWorker(final AddonTable table, final boolean download,
|
61
|
80
|
final BrowserWindow browserWindow, final JScrollPane scrollPane) {
|
62
|
81
|
super();
|
63
|
82
|
|
|
83
|
+ this.download = download;
|
64
|
84
|
this.table = table;
|
65
|
85
|
this.browserWindow = browserWindow;
|
66
|
86
|
this.scrollPane = scrollPane;
|
|
@@ -68,17 +88,46 @@ public class DataLoaderWorker
|
68
|
88
|
|
69
|
89
|
/** {@inheritDoc} */
|
70
|
90
|
@Override
|
71
|
|
- protected Collection<Map<String, String>> doInBackground() {
|
|
91
|
+ protected Collection<AddonInfo> doInBackground() {
|
|
92
|
+ final JPanel loadingPanel = new JPanel(
|
|
93
|
+ new MigLayout("fill, alignx 50%, aligny 50%"));
|
|
94
|
+ scrollPane.setViewportView(loadingPanel);
|
|
95
|
+ if (download) {
|
|
96
|
+ final TextLabel label = new TextLabel(
|
|
97
|
+ "Downloading addon info, please wait...");
|
|
98
|
+ label.setAlignment(StyleConstants.ALIGN_CENTER);
|
|
99
|
+ loadingPanel.add(Box.createVerticalGlue(), "growy, pushy, wrap");
|
|
100
|
+ loadingPanel.add(label, "growx, wrap");
|
|
101
|
+ loadingPanel.add(jpb, "growx, wrap");
|
|
102
|
+ loadingPanel.add(Box.createVerticalGlue(), "growy, pushy");
|
|
103
|
+ try {
|
|
104
|
+ Downloader.downloadPage("http://addons.dmdirc.com/feed",
|
|
105
|
+ Main.getConfigDir() + File.separator + "addons.feed",
|
|
106
|
+ this);
|
|
107
|
+ } catch (IOException ex) {
|
|
108
|
+ loadingPanel.removeAll();
|
|
109
|
+ loadingPanel.add(new TextLabel("Unable to download feeds."));
|
|
110
|
+ return Collections.<AddonInfo>emptyList();
|
|
111
|
+ }
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ loadingPanel.removeAll();
|
|
115
|
+ loadingPanel.add(new TextLabel("Loading addon info, please wait."));
|
72
|
116
|
final ConfigFile data = new ConfigFile(Main.getConfigDir()
|
73
|
117
|
+ File.separator + "addons.feed");
|
74
|
118
|
try {
|
75
|
119
|
data.read();
|
76
|
120
|
} catch (IOException ex) {
|
77
|
|
- return Collections.<Map<String, String>>emptyList();
|
|
121
|
+ return Collections.<AddonInfo>emptyList();
|
78
|
122
|
} catch (InvalidConfigFileException ex) {
|
79
|
|
- return Collections.<Map<String, String>>emptyList();
|
|
123
|
+ return Collections.<AddonInfo>emptyList();
|
80
|
124
|
}
|
81
|
|
- return data.getKeyDomains().values();
|
|
125
|
+
|
|
126
|
+ final List<AddonInfo> list = new ArrayList<AddonInfo>();
|
|
127
|
+ for (Map<String, String> entry : data.getKeyDomains().values()) {
|
|
128
|
+ list.add(new AddonInfo(entry));
|
|
129
|
+ }
|
|
130
|
+ return list;
|
82
|
131
|
}
|
83
|
132
|
|
84
|
133
|
/** {@inheritDoc} */
|
|
@@ -87,14 +136,15 @@ public class DataLoaderWorker
|
87
|
136
|
if (isCancelled()) {
|
88
|
137
|
return;
|
89
|
138
|
}
|
90
|
|
- Collection<Map<String, String>> data;
|
|
139
|
+
|
|
140
|
+ Collection<AddonInfo> data;
|
91
|
141
|
try {
|
92
|
142
|
data = get();
|
93
|
143
|
} catch (InterruptedException ex) {
|
94
|
|
- data = Collections.<Map<String, String>>emptyList();
|
|
144
|
+ data = Collections.<AddonInfo>emptyList();
|
95
|
145
|
} catch (ExecutionException ex) {
|
96
|
146
|
Logger.appError(ErrorLevel.MEDIUM, ex.getMessage(), ex);
|
97
|
|
- data = Collections.<Map<String, String>>emptyList();
|
|
147
|
+ data = Collections.<AddonInfo>emptyList();
|
98
|
148
|
}
|
99
|
149
|
final int selectedRow;
|
100
|
150
|
if (table.getRowCount() > 0 && table.getSelectedRow() > 0) {
|
|
@@ -103,8 +153,7 @@ public class DataLoaderWorker
|
103
|
153
|
selectedRow = 0;
|
104
|
154
|
}
|
105
|
155
|
table.getModel().setRowCount(0);
|
106
|
|
- for (Map<String, String> entry : data) {
|
107
|
|
- final AddonInfo info = new AddonInfo(entry);
|
|
156
|
+ for (AddonInfo info : data) {
|
108
|
157
|
table.getModel().addRow(new Object[]{
|
109
|
158
|
new AddonInfoLabel(info, browserWindow),
|
110
|
159
|
});
|
|
@@ -112,4 +161,31 @@ public class DataLoaderWorker
|
112
|
161
|
table.getSelectionModel().setSelectionInterval(selectedRow, selectedRow);
|
113
|
162
|
scrollPane.setViewportView(table);
|
114
|
163
|
}
|
|
164
|
+
|
|
165
|
+ /** {@inheritDoc} */
|
|
166
|
+ @Override
|
|
167
|
+ public void downloadProgress(final float percent) {
|
|
168
|
+ UIUtilities.invokeLater(new Runnable() {
|
|
169
|
+
|
|
170
|
+ /** {@inheritDoc} */
|
|
171
|
+ @Override
|
|
172
|
+ public void run() {
|
|
173
|
+ jpb.setValue((int) percent);
|
|
174
|
+ }
|
|
175
|
+ });
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ /** {@inheritDoc} */
|
|
179
|
+ @Override
|
|
180
|
+ public void setIndeterminate(final boolean indeterminate) {
|
|
181
|
+ UIUtilities.invokeLater(new Runnable() {
|
|
182
|
+
|
|
183
|
+ /** {@inheritDoc} */
|
|
184
|
+ @Override
|
|
185
|
+ public void run() {
|
|
186
|
+ jpb.setIndeterminate(indeterminate);
|
|
187
|
+ }
|
|
188
|
+ });
|
|
189
|
+ }
|
|
190
|
+
|
115
|
191
|
}
|