|
@@ -23,39 +23,25 @@
|
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;
|
27
|
26
|
import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
|
28
|
|
-import com.dmdirc.addons.ui_swing.components.text.TextLabel;
|
29
|
27
|
import com.dmdirc.logger.ErrorLevel;
|
30
|
28
|
import com.dmdirc.logger.Logger;
|
31
|
29
|
import com.dmdirc.util.ConfigFile;
|
32
|
|
-import com.dmdirc.util.DownloadListener;
|
33
|
|
-import com.dmdirc.util.Downloader;
|
34
|
30
|
import com.dmdirc.util.InvalidConfigFileException;
|
35
|
31
|
|
36
|
32
|
import java.io.File;
|
37
|
33
|
import java.io.IOException;
|
38
|
|
-import java.util.ArrayList;
|
39
|
34
|
import java.util.Collection;
|
40
|
35
|
import java.util.Collections;
|
41
|
|
-import java.util.List;
|
42
|
36
|
import java.util.Map;
|
43
|
37
|
import java.util.concurrent.ExecutionException;
|
44
|
|
-import javax.swing.Box;
|
45
|
|
-
|
46
|
|
-import javax.swing.JPanel;
|
47
|
|
-import javax.swing.JProgressBar;
|
48
|
38
|
import javax.swing.JScrollPane;
|
49
|
|
-import javax.swing.text.StyleConstants;
|
50
|
|
-
|
51
|
|
-import net.miginfocom.swing.MigLayout;
|
52
|
39
|
|
53
|
40
|
/**
|
54
|
41
|
* Loads the addon data feed into the addon browser.
|
55
|
42
|
*/
|
56
|
43
|
public class DataLoaderWorker
|
57
|
|
- extends LoggingSwingWorker<Collection<AddonInfo>, Object>
|
58
|
|
- implements DownloadListener {
|
|
44
|
+ extends LoggingSwingWorker<Collection<Map<String, String>>, Object> {
|
59
|
45
|
|
60
|
46
|
/** List to load data into. */
|
61
|
47
|
private final AddonTable table;
|
|
@@ -63,24 +49,18 @@ public class DataLoaderWorker
|
63
|
49
|
private final BrowserWindow browserWindow;
|
64
|
50
|
/** Table's parent scrollpane. */
|
65
|
51
|
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;
|
70
|
52
|
|
71
|
53
|
/**
|
72
|
54
|
* Creates a new data loader worker.
|
73
|
55
|
*
|
74
|
56
|
* @param table Table to load data into
|
75
|
|
- * @param download Download new addons feed?
|
76
|
57
|
* @param browserWindow Browser window to pass to table objects
|
77
|
58
|
* @param scrollPane Table's parent scrollpane
|
78
|
59
|
*/
|
79
|
|
- public DataLoaderWorker(final AddonTable table, final boolean download,
|
|
60
|
+ public DataLoaderWorker(final AddonTable table,
|
80
|
61
|
final BrowserWindow browserWindow, final JScrollPane scrollPane) {
|
81
|
62
|
super();
|
82
|
63
|
|
83
|
|
- this.download = download;
|
84
|
64
|
this.table = table;
|
85
|
65
|
this.browserWindow = browserWindow;
|
86
|
66
|
this.scrollPane = scrollPane;
|
|
@@ -88,46 +68,17 @@ public class DataLoaderWorker
|
88
|
68
|
|
89
|
69
|
/** {@inheritDoc} */
|
90
|
70
|
@Override
|
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."));
|
|
71
|
+ protected Collection<Map<String, String>> doInBackground() {
|
116
|
72
|
final ConfigFile data = new ConfigFile(Main.getConfigDir()
|
117
|
73
|
+ File.separator + "addons.feed");
|
118
|
74
|
try {
|
119
|
75
|
data.read();
|
120
|
76
|
} catch (IOException ex) {
|
121
|
|
- return Collections.<AddonInfo>emptyList();
|
|
77
|
+ return Collections.<Map<String, String>>emptyList();
|
122
|
78
|
} catch (InvalidConfigFileException ex) {
|
123
|
|
- return Collections.<AddonInfo>emptyList();
|
|
79
|
+ return Collections.<Map<String, String>>emptyList();
|
124
|
80
|
}
|
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;
|
|
81
|
+ return data.getKeyDomains().values();
|
131
|
82
|
}
|
132
|
83
|
|
133
|
84
|
/** {@inheritDoc} */
|
|
@@ -136,15 +87,14 @@ public class DataLoaderWorker
|
136
|
87
|
if (isCancelled()) {
|
137
|
88
|
return;
|
138
|
89
|
}
|
139
|
|
-
|
140
|
|
- Collection<AddonInfo> data;
|
|
90
|
+ Collection<Map<String, String>> data;
|
141
|
91
|
try {
|
142
|
92
|
data = get();
|
143
|
93
|
} catch (InterruptedException ex) {
|
144
|
|
- data = Collections.<AddonInfo>emptyList();
|
|
94
|
+ data = Collections.<Map<String, String>>emptyList();
|
145
|
95
|
} catch (ExecutionException ex) {
|
146
|
96
|
Logger.appError(ErrorLevel.MEDIUM, ex.getMessage(), ex);
|
147
|
|
- data = Collections.<AddonInfo>emptyList();
|
|
97
|
+ data = Collections.<Map<String, String>>emptyList();
|
148
|
98
|
}
|
149
|
99
|
final int selectedRow;
|
150
|
100
|
if (table.getRowCount() > 0 && table.getSelectedRow() > 0) {
|
|
@@ -153,7 +103,8 @@ public class DataLoaderWorker
|
153
|
103
|
selectedRow = 0;
|
154
|
104
|
}
|
155
|
105
|
table.getModel().setRowCount(0);
|
156
|
|
- for (AddonInfo info : data) {
|
|
106
|
+ for (Map<String, String> entry : data) {
|
|
107
|
+ final AddonInfo info = new AddonInfo(entry);
|
157
|
108
|
table.getModel().addRow(new Object[]{
|
158
|
109
|
new AddonInfoLabel(info, browserWindow),
|
159
|
110
|
});
|
|
@@ -161,31 +112,4 @@ public class DataLoaderWorker
|
161
|
112
|
table.getSelectionModel().setSelectionInterval(selectedRow, selectedRow);
|
162
|
113
|
scrollPane.setViewportView(table);
|
163
|
114
|
}
|
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
|
|
-
|
191
|
115
|
}
|