Ticket #13010: patch-plugins-import-list.patch

File patch-plugins-import-list.patch, 8.6 KB (added by michael2402, 8 years ago)
  • src/org/openstreetmap/josm/gui/preferences/plugin/PluginPreference.java

    diff --git a/src/org/openstreetmap/josm/gui/preferences/plugin/PluginPreference.java b/src/org/openstreetmap/josm/gui/preferences/plugin/PluginPreference.java
    index 41f0494..fda427c 100644
    a b import javax.swing.AbstractAction;  
    2727import javax.swing.BorderFactory;
    2828import javax.swing.DefaultListModel;
    2929import javax.swing.JButton;
     30import javax.swing.JCheckBox;
    3031import javax.swing.JLabel;
    3132import javax.swing.JList;
    3233import javax.swing.JOptionPane;
    3334import javax.swing.JPanel;
    3435import javax.swing.JScrollPane;
    3536import javax.swing.JTabbedPane;
     37import javax.swing.JTextArea;
    3638import javax.swing.SwingUtilities;
    3739import javax.swing.UIManager;
    3840import javax.swing.event.DocumentEvent;
    import org.openstreetmap.josm.plugins.ReadLocalPluginInformationTask;  
    5759import org.openstreetmap.josm.plugins.ReadRemotePluginInformationTask;
    5860import org.openstreetmap.josm.tools.GBC;
    5961import org.openstreetmap.josm.tools.ImageProvider;
     62import org.openstreetmap.josm.tools.Utils;
    6063
    6164/**
    6265 * Preference settings for plugins.
    public final class PluginPreference extends DefaultTabPreferenceSetting {  
    107110                    downloaded.size()
    108111                    ));
    109112            sb.append("<ul>");
    110             for (PluginInformation pi: downloaded) {
     113            for (PluginInformation pi : downloaded) {
    111114                sb.append("<li>").append(pi.name).append(" (").append(pi.version).append(")</li>");
    112115            }
    113116            sb.append("</ul>");
    public final class PluginPreference extends DefaultTabPreferenceSetting {  
    120123                    failed.size()
    121124                    ));
    122125            sb.append("<ul>");
    123             for (PluginInformation pi: failed) {
     126            for (PluginInformation pi : failed) {
    124127                sb.append("<li>").append(pi.name).append("</li>");
    125128            }
    126129            sb.append("</ul>");
    public final class PluginPreference extends DefaultTabPreferenceSetting {  
    142145    public static void notifyDownloadResults(final Component parent, PluginDownloadTask task, boolean restartRequired) {
    143146        final Collection<PluginInformation> failed = task.getFailedPlugins();
    144147        final StringBuilder sb = new StringBuilder();
    145         sb.append("<html>")
    146           .append(buildDownloadSummary(task));
     148        sb.append("<html>");
     149        sb.append(buildDownloadSummary(task));
    147150        if (restartRequired) {
    148151            sb.append(tr("Please restart JOSM to activate the downloaded plugins."));
    149152        }
    public final class PluginPreference extends DefaultTabPreferenceSetting {  
    157160                            sb.toString(),
    158161                            tr("Update plugins"),
    159162                            !failed.isEmpty() ? JOptionPane.WARNING_MESSAGE : JOptionPane.INFORMATION_MESSAGE,
    160                                     HelpUtil.ht("/Preferences/Plugins")
     163                            HelpUtil.ht("/Preferences/Plugins")
    161164                            );
    162165                }
    163166            });
    public final class PluginPreference extends DefaultTabPreferenceSetting {  
    191194        pnl.add(new JButton(new DownloadAvailablePluginsAction()));
    192195        pnl.add(new JButton(new UpdateSelectedPluginsAction()));
    193196        pnl.add(new JButton(new ConfigureSitesAction()));
     197        pnl.add(new JButton(new SelectByListAction()));
    194198        return pnl;
    195199    }
    196200
    public final class PluginPreference extends DefaultTabPreferenceSetting {  
    211215                    public void componentHidden(ComponentEvent e) {
    212216                        spPluginPreferences.setBorder(null);
    213217                    }
    214                 }
    215                 );
     218                });
    216219
    217220        pnl.add(spPluginPreferences, BorderLayout.CENTER);
    218221        pnl.add(buildActionPanel(), BorderLayout.SOUTH);
    public final class PluginPreference extends DefaultTabPreferenceSetting {  
    248251                        tr("Accept the new plugin sites and close the dialog"),
    249252                        null /* no special help topic */
    250253                        ),
    251                         new ButtonSpec(
    252                                 tr("Cancel"),
    253                                 ImageProvider.get("cancel"),
    254                                 tr("Close the dialog"),
    255                                 null /* no special help topic */
    256                                 )
     254                new ButtonSpec(
     255                        tr("Cancel"),
     256                        ImageProvider.get("cancel"),
     257                        tr("Close the dialog"),
     258                        null /* no special help topic */
     259                        )
    257260        };
    258261        PluginConfigurationSitesPanel pnl = new PluginConfigurationSitesPanel();
    259262
    public final class PluginPreference extends DefaultTabPreferenceSetting {  
    476479        }
    477480    }
    478481
    479 
    480482    /**
    481483     * The action for configuring the plugin download sites
    482484     *
    public final class PluginPreference extends DefaultTabPreferenceSetting {  
    495497    }
    496498
    497499    /**
     500     * The action for selecting the plugins given by a text file compatible to JOSM bug report.
     501     */
     502    class SelectByListAction extends AbstractAction {
     503        SelectByListAction() {
     504            putValue(NAME, tr("Load from list..."));
     505            putValue(SHORT_DESCRIPTION, tr("Load plugins from a list of plugins"));
     506            //TODO: Icon putValue(SMALL_ICON, ImageProvider.get("dialogs", ""));
     507        }
     508
     509        @Override
     510        public void actionPerformed(ActionEvent e) {
     511            JTextArea textField = new JTextArea(10, 0);
     512            JCheckBox deleteNotInList = new JCheckBox(tr("Disable all other plugins"));
     513
     514            JLabel helpLabel = new JLabel(tr("<html>Enter a list of plugins you want to download."
     515                    + "<br/>You should add one plugin id per line, version information is ignored."
     516                    + "<br/>You can copy+paste the list of a status report here.</html>"));
     517            int result = JOptionPane.showConfirmDialog(
     518                    GuiHelper.getFrameForComponent(getTabPane()),
     519                    new Object[] { helpLabel, new JScrollPane(textField), deleteNotInList },
     520                    tr("Load plugins from list"),
     521                    JOptionPane.OK_CANCEL_OPTION,
     522                    JOptionPane.PLAIN_MESSAGE);
     523
     524            if (result == JOptionPane.OK_OPTION) {
     525                activatePlugins(textField, deleteNotInList.isSelected());
     526            }
     527        }
     528
     529        private void activatePlugins(JTextArea textField, boolean deleteNotInList) {
     530            String[] lines = textField.getText().split("\n");
     531            ArrayList<String> toActivate = new ArrayList<>();
     532            ArrayList<String> notFound = new ArrayList<>();
     533            for (String line : lines) {
     534                String name = line.replaceAll("^[-\\s]*|\\s[\\(\\)\\d\\s]*", "");
     535                if (name.isEmpty()) {
     536                    continue;
     537                }
     538                PluginInformation plugin = model.getPluginInformation(name);
     539                if (plugin == null) {
     540                    notFound.add(name);
     541                } else {
     542                    toActivate.add(name);
     543                }
     544            }
     545
     546            if (notFound.isEmpty() || confirmIgnoreNotFound(notFound)) {
     547                activatePlugins(toActivate, deleteNotInList);
     548            }
     549        }
     550
     551        private void activatePlugins(ArrayList<String> toActivate, boolean deleteNotInList) {
     552            if (deleteNotInList) {
     553                for (String name : model.getSelectedPluginNames()) {
     554                    if (!toActivate.contains(name)) {
     555                        model.setPluginSelected(name, false);
     556                    }
     557                }
     558            }
     559            for (String name : toActivate) {
     560                model.setPluginSelected(name, true);
     561            }
     562            pnlPluginPreferences.refreshView();
     563        }
     564
     565        private boolean confirmIgnoreNotFound(ArrayList<String> notFound) {
     566            String list = "<ul><li>" + Utils.join("</li><li>", notFound) + "</li></ul>";
     567            String message = "<html>" + tr("The following plugins were not found. Continue anyway?") + list + "</html>";
     568            return JOptionPane.showConfirmDialog(GuiHelper.getFrameForComponent(getTabPane()), message) == JOptionPane.OK_OPTION;
     569        }
     570    }
     571
     572    /**
    498573     * Applies the current filter condition in the filter text field to the model.
    499574     */
    500575    class SearchFieldAdapter implements DocumentListener {