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