Ignore:
Timestamp:
2012-03-25T22:47:16+02:00 (12 years ago)
Author:
Don-vip
Message:

Enhancements in plugin dependencies system (view "requires" in plugin prefs + auto selection of required plugins + alert when unselecting a plugin still required)

Location:
trunk/src/org/openstreetmap/josm/gui/preferences/plugin
Files:
2 edited

Legend:

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

    r5120 r5121  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    5 
     5import static org.openstreetmap.josm.tools.I18n.trn;
     6
     7import java.awt.Component;
    68import java.awt.GridBagConstraints;
    79import java.awt.GridBagLayout;
     
    1012import java.awt.event.ActionEvent;
    1113import java.awt.event.ActionListener;
     14import java.util.HashSet;
    1215import java.util.List;
     16import java.util.Set;
    1317
    1418import javax.swing.JCheckBox;
    1519import javax.swing.JLabel;
     20import javax.swing.JOptionPane;
    1621import javax.swing.SwingConstants;
    1722import javax.swing.SwingUtilities;
     
    2227import org.openstreetmap.josm.gui.widgets.HtmlPanel;
    2328import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
     29import org.openstreetmap.josm.plugins.PluginHandler;
    2430import org.openstreetmap.josm.plugins.PluginInformation;
    2531import org.openstreetmap.josm.tools.OpenBrowser;
     
    8288        add(hint, gbc);
    8389    }
     90   
     91    /**
     92     * A plugin checkbox.
     93     *
     94     */
     95    private class JPluginCheckBox extends JCheckBox {
     96        public final PluginInformation pi;
     97        public JPluginCheckBox(final PluginInformation pi, boolean selected) {
     98            this.pi = pi;
     99            setSelected(selected);
     100            setToolTipText(formatCheckboxTooltipText(pi));
     101            addActionListener(new PluginCbActionListener(this));
     102        }
     103    }
     104   
     105    /**
     106     * Listener called when the user selects/unselects a plugin checkbox.
     107     *
     108     */
     109    private class PluginCbActionListener implements ActionListener {
     110        private final JPluginCheckBox cb;
     111        public PluginCbActionListener(JPluginCheckBox cb) {
     112            this.cb = cb;
     113        }
     114        public void actionPerformed(ActionEvent e) {
     115            // Select/unselect corresponding plugin in the model
     116            model.setPluginSelected(cb.pi.getName(), cb.isSelected());
     117            // Does the newly selected plugin require other plugins ?
     118            if (cb.isSelected() && cb.pi.requires != null) {
     119                // Select required plugins
     120                for (String s : cb.pi.requires.split(";")) {
     121                    model.setPluginSelected(s.trim(), true);
     122                }
     123                // Alert user if plugin requirements are not met
     124                PluginHandler.checkRequiredPluginsPreconditions(PluginListPanel.this, model.getAvailablePlugins(), cb.pi);
     125            }
     126            // If the plugin has been unselected, was it required by other plugins still selected ?
     127            else if (!cb.isSelected()) {
     128                Set<String> otherPlugins = new HashSet<String>();
     129                for (PluginInformation pi : model.getAvailablePlugins()) {
     130                    if (!pi.equals(cb.pi) && pi.requires != null && model.isSelectedPlugin(pi.getName())) {
     131                        for (String s : pi.requires.split(";")) {
     132                            if (s.trim().equals(cb.pi.getName())) {
     133                                otherPlugins.add(pi.getName());
     134                                break;
     135                            }
     136                        }
     137                    }
     138                }
     139                if (!otherPlugins.isEmpty()) {
     140                    alertPluginStillRequired(PluginListPanel.this, cb.pi.getName(), otherPlugins);
     141                }
     142            }
     143        }
     144    };
     145   
     146
     147    /**
     148     * Alerts the user if an unselected plugin is still required by another plugins
     149     *
     150     * @param parent The parent Component used to display error popup
     151     * @param plugin the plugin
     152     * @param otherPlugins the other plugins
     153     */
     154    private static void alertPluginStillRequired(Component parent, String plugin, Set<String> otherPlugins) {
     155        StringBuilder sb = new StringBuilder();
     156        sb.append("<html>");
     157        sb.append(trn("Plugin {0} is still required by this plugin:",
     158                "Plugin {0} is still required by these {1} plugins:",
     159                otherPlugins.size(),
     160                plugin,
     161                otherPlugins.size()
     162        ));
     163        sb.append("<ul>");
     164        for (String p: otherPlugins) {
     165            sb.append("<li>").append(p).append("</li>");
     166        }
     167        sb.append("</ul>").append("</html>");
     168        JOptionPane.showMessageDialog(
     169                parent,
     170                sb.toString(),
     171                tr("Warning"),
     172                JOptionPane.WARNING_MESSAGE
     173        );
     174    }
    84175
    85176    public void refreshView() {
     
    105196            String localversion = formatPluginLocalVersion(model.getPluginInformation(pi.getName()));
    106197
    107             final JCheckBox cbPlugin = new JCheckBox();
    108             cbPlugin.setSelected(selected);
    109             cbPlugin.setToolTipText(formatCheckboxTooltipText(pi));
    110             cbPlugin.addActionListener(new ActionListener(){
    111                 public void actionPerformed(ActionEvent e) {
    112                     model.setPluginSelected(pi.getName(), cbPlugin.isSelected());
    113                 }
    114             });
     198            JPluginCheckBox cbPlugin = new JPluginCheckBox(pi, selected);
     199            String pluginText = tr("{0}: Version {1} (local: {2})", pi.getName(), remoteversion, localversion);
     200            if (pi.requires != null && !pi.requires.isEmpty()) {
     201                pluginText += tr(" (requires: {0})", pi.requires);
     202            }
    115203            JLabel lblPlugin = new JLabel(
    116                     tr("{0}: Version {1} (local: {2})", pi.getName(), remoteversion, localversion),
     204                    pluginText,
    117205                    pi.getScaledIcon(),
    118206                    SwingConstants.LEFT);
  • trunk/src/org/openstreetmap/josm/gui/preferences/plugin/PluginPreferencesModel.java

    r4191 r5121  
    294294        return ret;
    295295    }
     296   
     297    /**
     298     * Replies the set of all available plugins.
     299     *
     300     * @return the set of all available plugins
     301     */
     302    public List<PluginInformation> getAvailablePlugins() {
     303        return new LinkedList<PluginInformation>(availablePlugins);
     304    }
    296305
    297306    /**
Note: See TracChangeset for help on using the changeset viewer.