1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.plugin;
|
---|
3 |
|
---|
4 | import static java.awt.GridBagConstraints.HORIZONTAL;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
8 |
|
---|
9 | import java.awt.BorderLayout;
|
---|
10 | import java.awt.Component;
|
---|
11 | import java.awt.GridBagLayout;
|
---|
12 | import java.awt.GridLayout;
|
---|
13 | import java.awt.event.ActionEvent;
|
---|
14 | import java.awt.event.ComponentAdapter;
|
---|
15 | import java.awt.event.ComponentEvent;
|
---|
16 | import java.lang.reflect.InvocationTargetException;
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.Collections;
|
---|
20 | import java.util.LinkedList;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.Set;
|
---|
23 | import java.util.regex.Pattern;
|
---|
24 | import java.util.stream.Collectors;
|
---|
25 | import java.util.stream.IntStream;
|
---|
26 |
|
---|
27 | import javax.swing.AbstractAction;
|
---|
28 | import javax.swing.ButtonGroup;
|
---|
29 | import javax.swing.DefaultListModel;
|
---|
30 | import javax.swing.JButton;
|
---|
31 | import javax.swing.JCheckBox;
|
---|
32 | import javax.swing.JLabel;
|
---|
33 | import javax.swing.JList;
|
---|
34 | import javax.swing.JOptionPane;
|
---|
35 | import javax.swing.JPanel;
|
---|
36 | import javax.swing.JRadioButton;
|
---|
37 | import javax.swing.JScrollPane;
|
---|
38 | import javax.swing.JTabbedPane;
|
---|
39 | import javax.swing.JTextArea;
|
---|
40 | import javax.swing.SwingUtilities;
|
---|
41 | import javax.swing.UIManager;
|
---|
42 |
|
---|
43 | import org.openstreetmap.josm.actions.ExpertToggleAction;
|
---|
44 | import org.openstreetmap.josm.data.Preferences;
|
---|
45 | import org.openstreetmap.josm.data.Version;
|
---|
46 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
47 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
48 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
49 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
50 | import org.openstreetmap.josm.gui.preferences.ExtensibleTabPreferenceSetting;
|
---|
51 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
52 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
53 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
54 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
55 | import org.openstreetmap.josm.gui.widgets.FilterField;
|
---|
56 | import org.openstreetmap.josm.plugins.PluginDownloadTask;
|
---|
57 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
58 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
59 | import org.openstreetmap.josm.plugins.ReadLocalPluginInformationTask;
|
---|
60 | import org.openstreetmap.josm.plugins.ReadRemotePluginInformationTask;
|
---|
61 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
62 | import org.openstreetmap.josm.tools.GBC;
|
---|
63 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
64 | import org.openstreetmap.josm.tools.Logging;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Preference settings for plugins.
|
---|
68 | * @since 168
|
---|
69 | */
|
---|
70 | public final class PluginPreference extends ExtensibleTabPreferenceSetting {
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Factory used to create a new {@code PluginPreference}.
|
---|
74 | */
|
---|
75 | public static class Factory implements PreferenceSettingFactory {
|
---|
76 | @Override
|
---|
77 | public PreferenceSetting createPreferenceSetting() {
|
---|
78 | return new PluginPreference();
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | private PluginListPanel pnlPluginPreferences;
|
---|
83 | private PluginPreferencesModel model;
|
---|
84 | private JScrollPane spPluginPreferences;
|
---|
85 | private PluginUpdatePolicyPanel pnlPluginUpdatePolicy;
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * is set to true if this preference pane has been selected by the user
|
---|
89 | */
|
---|
90 | private boolean pluginPreferencesActivated;
|
---|
91 |
|
---|
92 | private PluginPreference() {
|
---|
93 | super(/* ICON(preferences/) */ "plugin", tr("Plugins"), tr("Configure available plugins."), false);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Returns the download summary string to be shown.
|
---|
98 | * @param task The plugin download task that has completed
|
---|
99 | * @return the download summary string to be shown. Contains summary of success/failed plugins.
|
---|
100 | */
|
---|
101 | public static String buildDownloadSummary(PluginDownloadTask task) {
|
---|
102 | Collection<PluginInformation> downloaded = task.getDownloadedPlugins();
|
---|
103 | Collection<PluginInformation> failed = task.getFailedPlugins();
|
---|
104 | Exception exception = task.getLastException();
|
---|
105 | StringBuilder sb = new StringBuilder();
|
---|
106 | if (!downloaded.isEmpty()) {
|
---|
107 | sb.append(trn(
|
---|
108 | "The following plugin has been downloaded <strong>successfully</strong>:",
|
---|
109 | "The following {0} plugins have been downloaded <strong>successfully</strong>:",
|
---|
110 | downloaded.size(),
|
---|
111 | downloaded.size()
|
---|
112 | ));
|
---|
113 | sb.append("<ul>");
|
---|
114 | for (PluginInformation pi: downloaded) {
|
---|
115 | sb.append("<li>").append(pi.name).append(" (").append(pi.version).append(")</li>");
|
---|
116 | }
|
---|
117 | sb.append("</ul>");
|
---|
118 | }
|
---|
119 | if (!failed.isEmpty()) {
|
---|
120 | sb.append(trn(
|
---|
121 | "Downloading the following plugin has <strong>failed</strong>:",
|
---|
122 | "Downloading the following {0} plugins has <strong>failed</strong>:",
|
---|
123 | failed.size(),
|
---|
124 | failed.size()
|
---|
125 | ));
|
---|
126 | sb.append("<ul>");
|
---|
127 | for (PluginInformation pi: failed) {
|
---|
128 | sb.append("<li>").append(pi.name).append("</li>");
|
---|
129 | }
|
---|
130 | sb.append("</ul>");
|
---|
131 | }
|
---|
132 | if (exception != null) {
|
---|
133 | // Same i18n string in ExceptionUtil.explainBadRequest()
|
---|
134 | sb.append(tr("<br>Error message(untranslated): {0}", exception.getMessage()));
|
---|
135 | }
|
---|
136 | return sb.toString();
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Notifies user about result of a finished plugin download task.
|
---|
141 | * @param parent The parent component
|
---|
142 | * @param task The finished plugin download task
|
---|
143 | * @param restartRequired true if a restart is required
|
---|
144 | * @since 6797
|
---|
145 | */
|
---|
146 | public static void notifyDownloadResults(final Component parent, PluginDownloadTask task, boolean restartRequired) {
|
---|
147 | final Collection<PluginInformation> failed = task.getFailedPlugins();
|
---|
148 | final StringBuilder sb = new StringBuilder();
|
---|
149 | sb.append("<html>")
|
---|
150 | .append(buildDownloadSummary(task));
|
---|
151 | if (restartRequired) {
|
---|
152 | sb.append(tr("Please restart JOSM to activate the downloaded plugins."));
|
---|
153 | }
|
---|
154 | sb.append("</html>");
|
---|
155 | GuiHelper.runInEDTAndWait(() -> HelpAwareOptionPane.showOptionDialog(
|
---|
156 | parent,
|
---|
157 | sb.toString(),
|
---|
158 | tr("Update plugins"),
|
---|
159 | !failed.isEmpty() ? JOptionPane.WARNING_MESSAGE : JOptionPane.INFORMATION_MESSAGE,
|
---|
160 | HelpUtil.ht("/Preferences/Plugins")
|
---|
161 | ));
|
---|
162 | }
|
---|
163 |
|
---|
164 | private JPanel buildSearchFieldPanel() {
|
---|
165 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
166 | pnl.add(GBC.glue(0, 0));
|
---|
167 |
|
---|
168 | ButtonGroup bg = new ButtonGroup();
|
---|
169 | JPanel radios = new JPanel();
|
---|
170 | addRadioButton(bg, radios, new JRadioButton(trc("plugins", "All"), true), PluginInstallation.ALL);
|
---|
171 | addRadioButton(bg, radios, new JRadioButton(trc("plugins", "Installed")), PluginInstallation.INSTALLED);
|
---|
172 | addRadioButton(bg, radios, new JRadioButton(trc("plugins", "Available")), PluginInstallation.AVAILABLE);
|
---|
173 | pnl.add(radios, GBC.eol().fill(HORIZONTAL));
|
---|
174 |
|
---|
175 | pnl.add(new FilterField().filter(expr -> {
|
---|
176 | model.filterDisplayedPlugins(expr);
|
---|
177 | pnlPluginPreferences.refreshView();
|
---|
178 | }), GBC.eol().insets(0, 0, 0, 5).fill(HORIZONTAL));
|
---|
179 | return pnl;
|
---|
180 | }
|
---|
181 |
|
---|
182 | private void addRadioButton(ButtonGroup bg, JPanel pnl, JRadioButton rb, PluginInstallation value) {
|
---|
183 | bg.add(rb);
|
---|
184 | pnl.add(rb, GBC.std());
|
---|
185 | rb.addActionListener(e -> {
|
---|
186 | model.filterDisplayedPlugins(value);
|
---|
187 | pnlPluginPreferences.refreshView();
|
---|
188 | });
|
---|
189 | }
|
---|
190 |
|
---|
191 | private static Component addButton(JPanel pnl, JButton button, String buttonName) {
|
---|
192 | button.setName(buttonName);
|
---|
193 | return pnl.add(button);
|
---|
194 | }
|
---|
195 |
|
---|
196 | private JPanel buildActionPanel() {
|
---|
197 | JPanel pnl = new JPanel(new GridLayout(1, 4));
|
---|
198 |
|
---|
199 | // assign some component names to these as we go to aid testing
|
---|
200 | addButton(pnl, new JButton(new DownloadAvailablePluginsAction()), "downloadListButton");
|
---|
201 | addButton(pnl, new JButton(new UpdateSelectedPluginsAction()), "updatePluginsButton");
|
---|
202 | ExpertToggleAction.addVisibilitySwitcher(addButton(pnl, new JButton(new SelectByListAction()), "loadFromListButton"));
|
---|
203 | ExpertToggleAction.addVisibilitySwitcher(addButton(pnl, new JButton(new ConfigureSitesAction()), "configureSitesButton"));
|
---|
204 | return pnl;
|
---|
205 | }
|
---|
206 |
|
---|
207 | private JPanel buildPluginListPanel() {
|
---|
208 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
209 | pnl.add(buildSearchFieldPanel(), BorderLayout.NORTH);
|
---|
210 | model = new PluginPreferencesModel();
|
---|
211 | pnlPluginPreferences = new PluginListPanel(model);
|
---|
212 | spPluginPreferences = GuiHelper.embedInVerticalScrollPane(pnlPluginPreferences);
|
---|
213 | spPluginPreferences.getVerticalScrollBar().addComponentListener(
|
---|
214 | new ComponentAdapter() {
|
---|
215 | @Override
|
---|
216 | public void componentShown(ComponentEvent e) {
|
---|
217 | spPluginPreferences.setBorder(UIManager.getBorder("ScrollPane.border"));
|
---|
218 | }
|
---|
219 |
|
---|
220 | @Override
|
---|
221 | public void componentHidden(ComponentEvent e) {
|
---|
222 | spPluginPreferences.setBorder(null);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | );
|
---|
226 |
|
---|
227 | pnl.add(spPluginPreferences, BorderLayout.CENTER);
|
---|
228 | pnl.add(buildActionPanel(), BorderLayout.SOUTH);
|
---|
229 | return pnl;
|
---|
230 | }
|
---|
231 |
|
---|
232 | @Override
|
---|
233 | public void addGui(final PreferenceTabbedPane gui) {
|
---|
234 | JTabbedPane pane = getTabPane();
|
---|
235 | pnlPluginUpdatePolicy = new PluginUpdatePolicyPanel();
|
---|
236 | pane.addTab(tr("Plugins"), buildPluginListPanel());
|
---|
237 | pane.addTab(tr("Plugin update policy"), pnlPluginUpdatePolicy);
|
---|
238 | super.addGui(gui);
|
---|
239 | readLocalPluginInformation();
|
---|
240 | pluginPreferencesActivated = true;
|
---|
241 | }
|
---|
242 |
|
---|
243 | private void configureSites() {
|
---|
244 | ButtonSpec[] options = {
|
---|
245 | new ButtonSpec(
|
---|
246 | tr("OK"),
|
---|
247 | new ImageProvider("ok"),
|
---|
248 | tr("Accept the new plugin sites and close the dialog"),
|
---|
249 | null /* no special help topic */
|
---|
250 | ),
|
---|
251 | new ButtonSpec(
|
---|
252 | tr("Cancel"),
|
---|
253 | new ImageProvider("cancel"),
|
---|
254 | tr("Close the dialog"),
|
---|
255 | null /* no special help topic */
|
---|
256 | )
|
---|
257 | };
|
---|
258 | PluginConfigurationSitesPanel pnl = new PluginConfigurationSitesPanel();
|
---|
259 |
|
---|
260 | int answer = HelpAwareOptionPane.showOptionDialog(
|
---|
261 | pnlPluginPreferences,
|
---|
262 | pnl,
|
---|
263 | tr("Configure Plugin Sites"),
|
---|
264 | JOptionPane.QUESTION_MESSAGE,
|
---|
265 | null,
|
---|
266 | options,
|
---|
267 | options[0],
|
---|
268 | null /* no help topic */
|
---|
269 | );
|
---|
270 | if (answer != 0 /* OK */)
|
---|
271 | return;
|
---|
272 | Preferences.main().setPluginSites(pnl.getUpdateSites());
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Replies the set of plugins waiting for update or download
|
---|
277 | *
|
---|
278 | * @return the set of plugins waiting for update or download
|
---|
279 | */
|
---|
280 | public Set<PluginInformation> getPluginsScheduledForUpdateOrDownload() {
|
---|
281 | return model != null ? model.getPluginsScheduledForUpdateOrDownload() : null;
|
---|
282 | }
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Replies the list of plugins which have been added by the user to the set of activated plugins
|
---|
286 | *
|
---|
287 | * @return the list of newly activated plugins
|
---|
288 | */
|
---|
289 | public List<PluginInformation> getNewlyActivatedPlugins() {
|
---|
290 | return model != null ? model.getNewlyActivatedPlugins() : null;
|
---|
291 | }
|
---|
292 |
|
---|
293 | @Override
|
---|
294 | public boolean ok() {
|
---|
295 | if (!pluginPreferencesActivated)
|
---|
296 | return false;
|
---|
297 | pnlPluginUpdatePolicy.rememberInPreferences();
|
---|
298 | if (model.isActivePluginsChanged()) {
|
---|
299 | List<String> l = new LinkedList<>(model.getSelectedPluginNames());
|
---|
300 | Collections.sort(l);
|
---|
301 | Config.getPref().putList("plugins", l);
|
---|
302 | List<PluginInformation> deactivatedPlugins = model.getNewlyDeactivatedPlugins();
|
---|
303 | if (!deactivatedPlugins.isEmpty()) {
|
---|
304 | boolean requiresRestart = PluginHandler.removePlugins(deactivatedPlugins);
|
---|
305 | if (requiresRestart)
|
---|
306 | return requiresRestart;
|
---|
307 | }
|
---|
308 | return model.getNewlyActivatedPlugins().stream().anyMatch(pi -> !pi.canloadatruntime);
|
---|
309 | }
|
---|
310 | return false;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Reads locally available information about plugins from the local file system.
|
---|
315 | * Scans cached plugin lists from plugin download sites and locally available
|
---|
316 | * plugin jar files.
|
---|
317 | *
|
---|
318 | */
|
---|
319 | public void readLocalPluginInformation() {
|
---|
320 | final ReadLocalPluginInformationTask task = new ReadLocalPluginInformationTask();
|
---|
321 | Runnable r = () -> {
|
---|
322 | if (!task.isCanceled()) {
|
---|
323 | SwingUtilities.invokeLater(() -> {
|
---|
324 | model.setAvailablePlugins(task.getAvailablePlugins());
|
---|
325 | pnlPluginPreferences.resetDisplayedComponents();
|
---|
326 | pnlPluginPreferences.refreshView();
|
---|
327 | });
|
---|
328 | }
|
---|
329 | };
|
---|
330 | MainApplication.worker.submit(task);
|
---|
331 | MainApplication.worker.submit(r);
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * The action for downloading the list of available plugins
|
---|
336 | */
|
---|
337 | class DownloadAvailablePluginsAction extends AbstractAction {
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * Constructs a new {@code DownloadAvailablePluginsAction}.
|
---|
341 | */
|
---|
342 | DownloadAvailablePluginsAction() {
|
---|
343 | putValue(NAME, tr("Download list"));
|
---|
344 | putValue(SHORT_DESCRIPTION, tr("Download the list of available plugins"));
|
---|
345 | new ImageProvider("download").getResource().attachImageIcon(this);
|
---|
346 | }
|
---|
347 |
|
---|
348 | @Override
|
---|
349 | public void actionPerformed(ActionEvent e) {
|
---|
350 | Collection<String> pluginSites = Preferences.main().getOnlinePluginSites();
|
---|
351 | if (pluginSites.isEmpty()) {
|
---|
352 | return;
|
---|
353 | }
|
---|
354 | final ReadRemotePluginInformationTask task = new ReadRemotePluginInformationTask(pluginSites);
|
---|
355 | Runnable continuation = () -> {
|
---|
356 | if (!task.isCanceled()) {
|
---|
357 | SwingUtilities.invokeLater(() -> {
|
---|
358 | model.updateAvailablePlugins(task.getAvailablePlugins());
|
---|
359 | pnlPluginPreferences.resetDisplayedComponents();
|
---|
360 | pnlPluginPreferences.refreshView();
|
---|
361 | Config.getPref().putInt("pluginmanager.version", Version.getInstance().getVersion()); // fix #7030
|
---|
362 | });
|
---|
363 | }
|
---|
364 | };
|
---|
365 | MainApplication.worker.submit(task);
|
---|
366 | MainApplication.worker.submit(continuation);
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * The action for updating the list of selected plugins
|
---|
372 | */
|
---|
373 | class UpdateSelectedPluginsAction extends AbstractAction {
|
---|
374 | UpdateSelectedPluginsAction() {
|
---|
375 | putValue(NAME, tr("Update plugins"));
|
---|
376 | putValue(SHORT_DESCRIPTION, tr("Update the selected plugins"));
|
---|
377 | new ImageProvider("dialogs", "refresh").getResource().attachImageIcon(this);
|
---|
378 | }
|
---|
379 |
|
---|
380 | protected void alertNothingToUpdate() {
|
---|
381 | try {
|
---|
382 | SwingUtilities.invokeAndWait(() -> HelpAwareOptionPane.showOptionDialog(
|
---|
383 | pnlPluginPreferences,
|
---|
384 | tr("All installed plugins are up to date. JOSM does not have to download newer versions."),
|
---|
385 | tr("Plugins up to date"),
|
---|
386 | JOptionPane.INFORMATION_MESSAGE,
|
---|
387 | null // FIXME: provide help context
|
---|
388 | ));
|
---|
389 | } catch (InterruptedException | InvocationTargetException e) {
|
---|
390 | Logging.error(e);
|
---|
391 | }
|
---|
392 | }
|
---|
393 |
|
---|
394 | @Override
|
---|
395 | public void actionPerformed(ActionEvent e) {
|
---|
396 | final List<PluginInformation> toUpdate = model.getSelectedPlugins();
|
---|
397 | // the async task for downloading plugins
|
---|
398 | final PluginDownloadTask pluginDownloadTask = new PluginDownloadTask(
|
---|
399 | pnlPluginPreferences,
|
---|
400 | toUpdate,
|
---|
401 | tr("Update plugins")
|
---|
402 | );
|
---|
403 | // the async task for downloading plugin information
|
---|
404 | final ReadRemotePluginInformationTask pluginInfoDownloadTask = new ReadRemotePluginInformationTask(
|
---|
405 | Preferences.main().getOnlinePluginSites());
|
---|
406 |
|
---|
407 | // to be run asynchronously after the plugin download
|
---|
408 | //
|
---|
409 | final Runnable pluginDownloadContinuation = () -> {
|
---|
410 | if (pluginDownloadTask.isCanceled())
|
---|
411 | return;
|
---|
412 | boolean restartRequired = pluginDownloadTask.getDownloadedPlugins().stream()
|
---|
413 | .anyMatch(pi -> !(model.getNewlyActivatedPlugins().contains(pi) && pi.canloadatruntime));
|
---|
414 | notifyDownloadResults(pnlPluginPreferences, pluginDownloadTask, restartRequired);
|
---|
415 | model.refreshLocalPluginVersion(pluginDownloadTask.getDownloadedPlugins());
|
---|
416 | model.clearPendingPlugins(pluginDownloadTask.getDownloadedPlugins());
|
---|
417 | GuiHelper.runInEDT(pnlPluginPreferences::refreshView);
|
---|
418 | };
|
---|
419 |
|
---|
420 | // to be run asynchronously after the plugin list download
|
---|
421 | //
|
---|
422 | final Runnable pluginInfoDownloadContinuation = () -> {
|
---|
423 | if (pluginInfoDownloadTask.isCanceled())
|
---|
424 | return;
|
---|
425 | model.updateAvailablePlugins(pluginInfoDownloadTask.getAvailablePlugins());
|
---|
426 | // select plugins which actually have to be updated
|
---|
427 | //
|
---|
428 | toUpdate.removeIf(pi -> !pi.isUpdateRequired());
|
---|
429 | if (toUpdate.isEmpty()) {
|
---|
430 | alertNothingToUpdate();
|
---|
431 | return;
|
---|
432 | }
|
---|
433 | pluginDownloadTask.setPluginsToDownload(toUpdate);
|
---|
434 | MainApplication.worker.submit(pluginDownloadTask);
|
---|
435 | MainApplication.worker.submit(pluginDownloadContinuation);
|
---|
436 | };
|
---|
437 |
|
---|
438 | MainApplication.worker.submit(pluginInfoDownloadTask);
|
---|
439 | MainApplication.worker.submit(pluginInfoDownloadContinuation);
|
---|
440 | }
|
---|
441 | }
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * The action for configuring the plugin download sites
|
---|
445 | *
|
---|
446 | */
|
---|
447 | class ConfigureSitesAction extends AbstractAction {
|
---|
448 | ConfigureSitesAction() {
|
---|
449 | putValue(NAME, tr("Configure sites..."));
|
---|
450 | putValue(SHORT_DESCRIPTION, tr("Configure the list of sites where plugins are downloaded from"));
|
---|
451 | new ImageProvider("preference").getResource().attachImageIcon(this);
|
---|
452 | }
|
---|
453 |
|
---|
454 | @Override
|
---|
455 | public void actionPerformed(ActionEvent e) {
|
---|
456 | configureSites();
|
---|
457 | }
|
---|
458 | }
|
---|
459 |
|
---|
460 | /**
|
---|
461 | * The action for selecting the plugins given by a text file compatible to JOSM bug report.
|
---|
462 | * @author Michael Zangl
|
---|
463 | */
|
---|
464 | class SelectByListAction extends AbstractAction {
|
---|
465 | SelectByListAction() {
|
---|
466 | putValue(NAME, tr("Load from list..."));
|
---|
467 | putValue(SHORT_DESCRIPTION, tr("Load plugins from a list of plugins"));
|
---|
468 | new ImageProvider("misc/statusreport").getResource().attachImageIcon(this);
|
---|
469 | }
|
---|
470 |
|
---|
471 | @Override
|
---|
472 | public void actionPerformed(ActionEvent e) {
|
---|
473 | JTextArea textField = new JTextArea(10, 0);
|
---|
474 | JCheckBox deleteNotInList = new JCheckBox(tr("Disable all other plugins"));
|
---|
475 |
|
---|
476 | JLabel helpLabel = new JLabel("<html>" + String.join("<br/>",
|
---|
477 | tr("Enter a list of plugins you want to download."),
|
---|
478 | tr("You should add one plugin id per line, version information is ignored."),
|
---|
479 | tr("You can copy+paste the list of a status report here.")) + "</html>");
|
---|
480 |
|
---|
481 | if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(GuiHelper.getFrameForComponent(getTabPane()),
|
---|
482 | new Object[] {helpLabel, new JScrollPane(textField), deleteNotInList},
|
---|
483 | tr("Load plugins from list"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) {
|
---|
484 | activatePlugins(textField, deleteNotInList.isSelected());
|
---|
485 | }
|
---|
486 | }
|
---|
487 |
|
---|
488 | private void activatePlugins(JTextArea textField, boolean deleteNotInList) {
|
---|
489 | String[] lines = textField.getText().split("\n", -1);
|
---|
490 | List<String> toActivate = new ArrayList<>();
|
---|
491 | List<String> notFound = new ArrayList<>();
|
---|
492 | // This pattern matches the default list format JOSM uses for bug reports.
|
---|
493 | // It removes a list item mark at the beginning of the line: +, -, *
|
---|
494 | // It removes the version number after the plugin, like: 123, (123), (v5.7alpha3), (1b3), (v1-SNAPSHOT-1)...
|
---|
495 | Pattern regex = Pattern.compile("^[-+\\*\\s]*|\\s[\\d\\s]*(\\([^\\(\\)\\[\\]]*\\))?[\\d\\s]*$");
|
---|
496 | for (String line : lines) {
|
---|
497 | String name = regex.matcher(line).replaceAll("");
|
---|
498 | if (name.isEmpty()) {
|
---|
499 | continue;
|
---|
500 | }
|
---|
501 | PluginInformation plugin = model.getPluginInformation(name);
|
---|
502 | if (plugin == null) {
|
---|
503 | notFound.add(name);
|
---|
504 | } else {
|
---|
505 | toActivate.add(name);
|
---|
506 | }
|
---|
507 | }
|
---|
508 |
|
---|
509 | if (notFound.isEmpty() || confirmIgnoreNotFound(notFound)) {
|
---|
510 | activatePlugins(toActivate, deleteNotInList);
|
---|
511 | }
|
---|
512 | }
|
---|
513 |
|
---|
514 | private void activatePlugins(List<String> toActivate, boolean deleteNotInList) {
|
---|
515 | if (deleteNotInList) {
|
---|
516 | for (String name : model.getSelectedPluginNames()) {
|
---|
517 | if (!toActivate.contains(name)) {
|
---|
518 | model.setPluginSelected(name, false);
|
---|
519 | }
|
---|
520 | }
|
---|
521 | }
|
---|
522 | for (String name : toActivate) {
|
---|
523 | model.setPluginSelected(name, true);
|
---|
524 | }
|
---|
525 | pnlPluginPreferences.refreshView();
|
---|
526 | }
|
---|
527 |
|
---|
528 | private boolean confirmIgnoreNotFound(List<String> notFound) {
|
---|
529 | String list = "<ul><li>" + String.join("</li><li>", notFound) + "</li></ul>";
|
---|
530 | String message = "<html>" + tr("The following plugins were not found. Continue anyway?") + list + "</html>";
|
---|
531 | return JOptionPane.showConfirmDialog(GuiHelper.getFrameForComponent(getTabPane()),
|
---|
532 | message) == JOptionPane.OK_OPTION;
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 | private static class PluginConfigurationSitesPanel extends JPanel {
|
---|
537 |
|
---|
538 | private final DefaultListModel<String> model = new DefaultListModel<>();
|
---|
539 |
|
---|
540 | PluginConfigurationSitesPanel() {
|
---|
541 | super(new GridBagLayout());
|
---|
542 | add(new JLabel(tr("Add JOSM Plugin description URL.")), GBC.eol());
|
---|
543 | for (String s : Preferences.main().getPluginSites()) {
|
---|
544 | model.addElement(s);
|
---|
545 | }
|
---|
546 | final JList<String> list = new JList<>(model);
|
---|
547 | add(new JScrollPane(list), GBC.std().fill());
|
---|
548 | JPanel buttons = new JPanel(new GridBagLayout());
|
---|
549 | buttons.add(new JButton(new AbstractAction(tr("Add")) {
|
---|
550 | @Override
|
---|
551 | public void actionPerformed(ActionEvent e) {
|
---|
552 | String s = JOptionPane.showInputDialog(
|
---|
553 | GuiHelper.getFrameForComponent(PluginConfigurationSitesPanel.this),
|
---|
554 | tr("Add JOSM Plugin description URL."),
|
---|
555 | tr("Enter URL"),
|
---|
556 | JOptionPane.QUESTION_MESSAGE
|
---|
557 | );
|
---|
558 | if (s != null && !s.isEmpty()) {
|
---|
559 | model.addElement(s);
|
---|
560 | }
|
---|
561 | }
|
---|
562 | }), GBC.eol().fill(HORIZONTAL));
|
---|
563 | buttons.add(new JButton(new AbstractAction(tr("Edit")) {
|
---|
564 | @Override
|
---|
565 | public void actionPerformed(ActionEvent e) {
|
---|
566 | if (list.getSelectedValue() == null) {
|
---|
567 | JOptionPane.showMessageDialog(
|
---|
568 | GuiHelper.getFrameForComponent(PluginConfigurationSitesPanel.this),
|
---|
569 | tr("Please select an entry."),
|
---|
570 | tr("Warning"),
|
---|
571 | JOptionPane.WARNING_MESSAGE
|
---|
572 | );
|
---|
573 | return;
|
---|
574 | }
|
---|
575 | String s = (String) JOptionPane.showInputDialog(
|
---|
576 | MainApplication.getMainFrame(),
|
---|
577 | tr("Edit JOSM Plugin description URL."),
|
---|
578 | tr("JOSM Plugin description URL"),
|
---|
579 | JOptionPane.QUESTION_MESSAGE,
|
---|
580 | null,
|
---|
581 | null,
|
---|
582 | list.getSelectedValue()
|
---|
583 | );
|
---|
584 | if (s != null && !s.isEmpty()) {
|
---|
585 | model.setElementAt(s, list.getSelectedIndex());
|
---|
586 | }
|
---|
587 | }
|
---|
588 | }), GBC.eol().fill(HORIZONTAL));
|
---|
589 | buttons.add(new JButton(new AbstractAction(tr("Delete")) {
|
---|
590 | @Override
|
---|
591 | public void actionPerformed(ActionEvent event) {
|
---|
592 | if (list.getSelectedValue() == null) {
|
---|
593 | JOptionPane.showMessageDialog(
|
---|
594 | GuiHelper.getFrameForComponent(PluginConfigurationSitesPanel.this),
|
---|
595 | tr("Please select an entry."),
|
---|
596 | tr("Warning"),
|
---|
597 | JOptionPane.WARNING_MESSAGE
|
---|
598 | );
|
---|
599 | return;
|
---|
600 | }
|
---|
601 | model.removeElement(list.getSelectedValue());
|
---|
602 | }
|
---|
603 | }), GBC.eol().fill(HORIZONTAL));
|
---|
604 | add(buttons, GBC.eol());
|
---|
605 | }
|
---|
606 |
|
---|
607 | protected List<String> getUpdateSites() {
|
---|
608 | if (model.getSize() == 0)
|
---|
609 | return Collections.emptyList();
|
---|
610 | return IntStream.range(0, model.getSize())
|
---|
611 | .mapToObj(model::get)
|
---|
612 | .collect(Collectors.toList());
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | @Override
|
---|
617 | public String getHelpContext() {
|
---|
618 | return HelpUtil.ht("/Preferences/Plugins");
|
---|
619 | }
|
---|
620 | }
|
---|