Ticket #20537: 20537.2.patch

File 20537.2.patch, 7.0 KB (added by taylor.smock, 2 years ago)

Add popup menu to determine action, when multiple actions are possible. Also sets background color of component (foreground might work on non-mac systems). The state is not currently used to remove plugin jar files.

  • src/org/openstreetmap/josm/gui/preferences/plugin/PluginCheckBox.java

    diff --git a/src/org/openstreetmap/josm/gui/preferences/plugin/PluginCheckBox.java b/src/org/openstreetmap/josm/gui/preferences/plugin/PluginCheckBox.java
    index d021169a68..2918430a87 100644
    a b package org.openstreetmap.josm.gui.preferences.plugin;  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55import static org.openstreetmap.josm.tools.I18n.trn;
    66
     7import java.awt.Color;
    78import java.awt.Component;
    89import java.awt.event.ActionEvent;
    910import java.awt.event.ActionListener;
     11import java.util.Objects;
    1012import java.util.Set;
    1113import java.util.stream.Collectors;
    1214
     15import javax.swing.AbstractAction;
     16import javax.swing.Action;
    1317import javax.swing.JCheckBox;
     18import javax.swing.JMenuItem;
    1419import javax.swing.JOptionPane;
     20import javax.swing.JPopupMenu;
     21import javax.swing.event.PopupMenuEvent;
    1522
     23import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    1624import org.openstreetmap.josm.plugins.PluginHandler;
    1725import org.openstreetmap.josm.plugins.PluginInformation;
    1826import org.openstreetmap.josm.tools.Utils;
    import org.openstreetmap.josm.tools.Utils;  
    2230 * @since 10228
    2331 */
    2432public class PluginCheckBox extends JCheckBox implements ActionListener {
     33    private static final String PREFERENCE_PREFIX = "plugins.";
     34
     35    /* Background colors for checkboxes */
     36    private static final NamedColorProperty COLOR_INSTALLED = new NamedColorProperty(PREFERENCE_PREFIX + PluginInstallation.INSTALLED,
     37            Color.GREEN.darker().darker());
     38    private static final NamedColorProperty COLOR_REINSTALL = new NamedColorProperty(PREFERENCE_PREFIX + PluginInstallation.REINSTALL,
     39            Color.GREEN);
     40    private static final NamedColorProperty COLOR_UPGRADE = new NamedColorProperty(PREFERENCE_PREFIX + PluginInstallation.UPGRADE,
     41            Color.GREEN.brighter().brighter());
     42    private static final NamedColorProperty COLOR_REMOVE = new NamedColorProperty(PREFERENCE_PREFIX + PluginInstallation.REMOVE,
     43            Color.RED.darker().darker());
     44    private static final NamedColorProperty COLOR_COMPLETE_REMOVE = new NamedColorProperty(PREFERENCE_PREFIX + PluginInstallation.COMPLETE_REMOVE,
     45            Color.RED);
     46    private final Color originalColor;
     47
    2548    private final transient PluginInformation pi;
    2649    private final PluginListPanel panel;
    2750    private final transient PluginPreferencesModel ppModel;
     51    private final boolean wasInstalled;
     52    private PluginInstallation state;
    2853
    2954    PluginCheckBox(PluginInformation pi, boolean selected, PluginListPanel panel, PluginPreferencesModel ppModel) {
    3055        this.pi = pi;
    public class PluginCheckBox extends JCheckBox implements ActionListener {  
    3358        setSelected(selected);
    3459        setToolTipText(PluginListPanel.formatCheckboxTooltipText(pi));
    3560        addActionListener(this);
     61        wasInstalled = selected;
     62        this.originalColor = this.getBackground();
     63        if (wasInstalled) {
     64            this.setBackground(COLOR_INSTALLED.get());
     65            state = PluginInstallation.INSTALLED;
     66        } else {
     67            state = PluginInstallation.ALL;
     68        }
    3669    }
    3770
    3871    protected void selectRequiredPlugins(PluginInformation info) {
    public class PluginCheckBox extends JCheckBox implements ActionListener {  
    67100                alertPluginStillRequired(panel, pi.getName(), otherPlugins);
    68101            }
    69102        }
     103        setState();
    70104    }
    71105
     106    private void setState() {
     107        if (isSelected() && this.wasInstalled) {
     108            state = PluginInstallation.INSTALLED;
     109            updateBackgroundColor();
     110        } else if (isSelected()) {
     111            // Really should use a specific state for "to install"
     112            state = PluginInstallation.UPGRADE;
     113            updateBackgroundColor();
     114        } else if (!isSelected() && this.wasInstalled) {
     115            final JPopupMenu jPopupMenu = new JPopupMenu();
     116            Action action = new AbstractAction() {
     117                @Override
     118                public void actionPerformed(ActionEvent e) {
     119                    final String actionCommand = e.getActionCommand();
     120                    if (tr("Mark for Reinstallation").equals(actionCommand)) {
     121                        state = PluginInstallation.REINSTALL;
     122                    } else if (tr("Mark for Complete Removal").equals(actionCommand)) {
     123                        state = PluginInstallation.COMPLETE_REMOVE;
     124                    } else {
     125                        state = PluginInstallation.REMOVE;
     126                    }
     127                    jPopupMenu.setVisible(false);
     128                    updateBackgroundColor();
     129                }
     130            };
     131            jPopupMenu.add(new JMenuItem(action)).setText(tr("Mark for Reinstallation"));
     132            jPopupMenu.add(new JMenuItem(action)).setText(tr("Mark for Removal"));
     133            jPopupMenu.add(new JMenuItem(action)).setText(tr("Mark for Complete Removal"));
     134            jPopupMenu.show(this, 0, this.getHeight());
     135        } else {
     136            state = PluginInstallation.AVAILABLE;
     137            updateBackgroundColor();
     138        }
     139    }
     140
     141    private void updateBackgroundColor() {
     142        final Color color;
     143        switch (state) {
     144        case ALL:
     145        case AVAILABLE:
     146        case INSTALLED:
     147            // INSTALLED should use COLOR_INSTALLED, but it also has a checkbox
     148            color = this.originalColor;
     149            break;
     150        case REMOVE:
     151            color = COLOR_REMOVE.get();
     152            break;
     153        case COMPLETE_REMOVE:
     154            color = COLOR_COMPLETE_REMOVE.get();
     155            break;
     156        case REINSTALL:
     157            color = COLOR_REINSTALL.get();
     158            break;
     159        case UPGRADE:
     160            color = COLOR_UPGRADE.get();
     161            break;
     162        default:
     163            throw new IllegalStateException("Unknown plugin state");
     164        }
     165        this.setOpaque(!Objects.equals(this.originalColor, color));
     166        this.setBackground(color);
     167    }
     168
     169
    72170    /**
    73171     * Alerts the user if an unselected plugin is still required by another plugins
    74172     *
  • src/org/openstreetmap/josm/gui/preferences/plugin/PluginInstallation.java

    diff --git a/src/org/openstreetmap/josm/gui/preferences/plugin/PluginInstallation.java b/src/org/openstreetmap/josm/gui/preferences/plugin/PluginInstallation.java
    index ab42df7fb0..ca6e4f3b4d 100644
    a b public enum PluginInstallation {  
    1111    /** Plugins not loaded **/
    1212    AVAILABLE,
    1313    /** All plugins **/
    14     ALL
     14    ALL,
     15    /**
     16     * Plugins to reinstall -- this involves <i>deleting</i> the plugin file
     17     * @since xxx
     18     */
     19    REINSTALL,
     20    /**
     21     * Plugins to upgrade
     22     * @since xxx
     23     */
     24    UPGRADE,
     25    /**
     26     * Plugins to remove
     27     * @since xxx
     28     */
     29    REMOVE,
     30    /**
     31     * Plugins to completely remove -- this involves <i>deleting</i> the plugin file
     32     * @since xxx
     33     */
     34    COMPLETE_REMOVE
    1535}