Changeset 10811 in josm


Ignore:
Timestamp:
2016-08-15T18:14:44+02:00 (8 years ago)
Author:
Don-vip
Message:

fix #13010 - Load plugins from list (modified patch by michael2402) - gsoc-core

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/plugin/PluginPreference.java

    r10763 r10811  
    1717import java.lang.reflect.InvocationTargetException;
    1818import java.util.ArrayList;
     19import java.util.Arrays;
    1920import java.util.Collection;
    2021import java.util.Collections;
     
    2324import java.util.List;
    2425import java.util.Set;
     26import java.util.regex.Pattern;
    2527
    2628import javax.swing.AbstractAction;
     
    2830import javax.swing.DefaultListModel;
    2931import javax.swing.JButton;
     32import javax.swing.JCheckBox;
    3033import javax.swing.JLabel;
    3134import javax.swing.JList;
     
    3437import javax.swing.JScrollPane;
    3538import javax.swing.JTabbedPane;
     39import javax.swing.JTextArea;
    3640import javax.swing.SwingUtilities;
    3741import javax.swing.UIManager;
     
    4044
    4145import org.openstreetmap.josm.Main;
     46import org.openstreetmap.josm.actions.ExpertToggleAction;
    4247import org.openstreetmap.josm.data.Version;
    4348import org.openstreetmap.josm.gui.HelpAwareOptionPane;
     
    5863import org.openstreetmap.josm.tools.GBC;
    5964import org.openstreetmap.josm.tools.ImageProvider;
     65import org.openstreetmap.josm.tools.Utils;
    6066
    6167/**
     
    182188
    183189    private JPanel buildActionPanel() {
    184         JPanel pnl = new JPanel(new GridLayout(1, 3));
     190        JPanel pnl = new JPanel(new GridLayout(1, 4));
    185191
    186192        pnl.add(new JButton(new DownloadAvailablePluginsAction()));
    187193        pnl.add(new JButton(new UpdateSelectedPluginsAction()));
    188         pnl.add(new JButton(new ConfigureSitesAction()));
     194        ExpertToggleAction.addVisibilitySwitcher(pnl.add(new JButton(new SelectByListAction())));
     195        ExpertToggleAction.addVisibilitySwitcher(pnl.add(new JButton(new ConfigureSitesAction())));
    189196        return pnl;
    190197    }
     
    462469
    463470    /**
     471     * The action for selecting the plugins given by a text file compatible to JOSM bug report.
     472     * @author Michael Zangl
     473     */
     474    class SelectByListAction extends AbstractAction {
     475        SelectByListAction() {
     476            putValue(NAME, tr("Load from list..."));
     477            putValue(SHORT_DESCRIPTION, tr("Load plugins from a list of plugins"));
     478        }
     479
     480        @Override
     481        public void actionPerformed(ActionEvent e) {
     482            JTextArea textField = new JTextArea(10, 0);
     483            JCheckBox deleteNotInList = new JCheckBox(tr("Disable all other plugins"));
     484
     485            JLabel helpLabel = new JLabel("<html>" + Utils.join("<br/>", Arrays.asList(
     486                    tr("Enter a list of plugins you want to download."),
     487                    tr("You should add one plugin id per line, version information is ignored."),
     488                    tr("You can copy+paste the list of a status report here."))) + "</html>");
     489
     490            if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(GuiHelper.getFrameForComponent(getTabPane()),
     491                    new Object[] {helpLabel, new JScrollPane(textField), deleteNotInList},
     492                    tr("Load plugins from list"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) {
     493                activatePlugins(textField, deleteNotInList.isSelected());
     494            }
     495        }
     496
     497        private void activatePlugins(JTextArea textField, boolean deleteNotInList) {
     498            String[] lines = textField.getText().split("\n");
     499            List<String> toActivate = new ArrayList<>();
     500            List<String> notFound = new ArrayList<>();
     501            Pattern regex = Pattern.compile("^[-+\\s]*|\\s[\\(\\)\\d\\s]*");
     502            for (String line : lines) {
     503                String name = regex.matcher(line).replaceAll("");
     504                if (name.isEmpty()) {
     505                    continue;
     506                }
     507                PluginInformation plugin = model.getPluginInformation(name);
     508                if (plugin == null) {
     509                    notFound.add(name);
     510                } else {
     511                    toActivate.add(name);
     512                }
     513            }
     514
     515            if (notFound.isEmpty() || confirmIgnoreNotFound(notFound)) {
     516                activatePlugins(toActivate, deleteNotInList);
     517            }
     518        }
     519
     520        private void activatePlugins(List<String> toActivate, boolean deleteNotInList) {
     521            if (deleteNotInList) {
     522                for (String name : model.getSelectedPluginNames()) {
     523                    if (!toActivate.contains(name)) {
     524                        model.setPluginSelected(name, false);
     525                    }
     526                }
     527            }
     528            for (String name : toActivate) {
     529                model.setPluginSelected(name, true);
     530            }
     531            pnlPluginPreferences.refreshView();
     532        }
     533
     534        private boolean confirmIgnoreNotFound(List<String> notFound) {
     535            String list = "<ul><li>" + Utils.join("</li><li>", notFound) + "</li></ul>";
     536            String message = "<html>" + tr("The following plugins were not found. Continue anyway?") + list + "</html>";
     537            return JOptionPane.showConfirmDialog(GuiHelper.getFrameForComponent(getTabPane()),
     538                    message) == JOptionPane.OK_OPTION;
     539        }
     540    }
     541
     542    /**
    464543     * Applies the current filter condition in the filter text field to the model.
    465544     */
Note: See TracChangeset for help on using the changeset viewer.