Changeset 6594 in josm


Ignore:
Timestamp:
2014-01-02T00:58:33+01:00 (10 years ago)
Author:
simon04
Message:

fix #8969 fix #9394 - add "Do not show again (this session)" choice, and in addition "Do not show again (this operation)" if a bulk operation has been marked in the code

The dialog displays 3 or 4 radio boxes with the labels:

  • "Show this dialog again the next time"
  • "Do not show again (this operation)"
  • "Do not show again (this session)"
  • "Do not show again (remembers choice)"
Location:
trunk/src/org/openstreetmap/josm/gui
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/ConditionalOptionPaneUtil.java

    r6380 r6594  
    77import java.awt.GridBagLayout;
    88import java.awt.HeadlessException;
    9 
    10 import javax.swing.JCheckBox;
     9import java.util.HashMap;
     10import java.util.HashSet;
     11import java.util.Map;
     12import java.util.Set;
     13
     14import javax.swing.ButtonGroup;
    1115import javax.swing.JLabel;
    1216import javax.swing.JOptionPane;
    1317import javax.swing.JPanel;
     18import javax.swing.JRadioButton;
    1419
    1520import org.openstreetmap.josm.Main;
    1621import org.openstreetmap.josm.tools.GBC;
     22import org.openstreetmap.josm.tools.Utils;
    1723
    1824/**
     
    2935    static public final int DIALOG_DISABLED_OPTION = Integer.MIN_VALUE;
    3036
     37    /** (preference key => return value) mappings valid for the current operation (no, those two maps cannot be combined) */
     38    protected static final Map<String, Integer> sessionChoices = new HashMap<String, Integer>();
     39    /** (preference key => return value) mappings valid for the current session */
     40    protected static final Map<String, Integer> immediateChoices = new HashMap<String, Integer>();
     41    /** a set indication that (preference key) is or may be stored for the currently active bulk operation */
     42    protected static final Set<String> immediateActive = new HashSet<String>();
     43
    3144    /**
    3245     * this is a static utility class only
    3346     */
    3447    private ConditionalOptionPaneUtil() {}
    35 
    36     /**
    37      * Replies the preference value for the preference key "message." + <code>prefKey</code>.
    38      * The default value if the preference key is missing is true.
    39      *
    40      * @param  prefKey the preference key
    41      * @return the preference value for the preference key "message." + <code>prefKey</code>
    42      */
    43     public static boolean getDialogShowingEnabled(String prefKey) {
    44         return Main.pref.getBoolean("message."+prefKey, true);
    45     }
    46 
    47     /**
    48      * sets the value for the preference key "message." + <code>prefKey</code>.
    49      *
    50      * @param prefKey the key
    51      * @param enabled the value
    52      */
    53     public static void setDialogShowingEnabled(String prefKey, boolean enabled) {
    54         Main.pref.put("message."+prefKey, enabled);
    55     }
    5648
    5749    /**
     
    6254     * @return the preference value for the preference key "message." + <code>prefKey</code> + ".value"
    6355     */
    64     public static Integer getDialogReturnValue(String prefKey) {
    65         return Main.pref.getInteger("message."+prefKey+".value", -1);
    66     }
    67 
    68     /**
    69      * sets the value for the preference key "message." + <code>prefKey</code> + ".value".
    70      *
    71      * @param prefKey the key
    72      * @param value the value
    73      */
    74     public static void setDialogReturnValue(String prefKey, Integer value) {
    75         Main.pref.putInteger("message."+prefKey+".value", value);
     56    public static int getDialogReturnValue(String prefKey) {
     57        return Utils.firstNonNull(
     58                immediateChoices.get(prefKey),
     59                sessionChoices.get(prefKey),
     60                !Main.pref.getBoolean("message." + prefKey, true) ? Main.pref.getInteger("message." + prefKey + ".value", -1) : -1
     61        );
     62    }
     63
     64    /**
     65     * Marks the beginning of a bulk operation in order to provide a "Do not show again (this operation)" option.
     66     * @param prefKey the preference key
     67     */
     68    public static void startBulkOperation(final String prefKey) {
     69        immediateActive.add(prefKey);
     70    }
     71
     72    /**
     73     * Marks the ending of a bulk operation. Removes the "Do not show again (this operation)" result value.
     74     * @param prefKey the preference key
     75     */
     76    public static void endBulkOperation(final String prefKey) {
     77        immediateActive.remove(prefKey);
     78        immediateChoices.remove(prefKey);
    7679    }
    7780
     
    104107    static public int showOptionDialog(String preferenceKey, Component parent, Object message, String title, int optionType, int messageType, Object [] options, Object defaultOption) throws HeadlessException {
    105108        int ret = getDialogReturnValue(preferenceKey);
    106         if (!getDialogShowingEnabled(preferenceKey) && ((ret == JOptionPane.YES_OPTION) || (ret == JOptionPane.NO_OPTION)))
     109        if (isYesOrNo(ret))
    107110            return ret;
    108         MessagePanel pnl = new MessagePanel(false, message);
     111        MessagePanel pnl = new MessagePanel(message, immediateActive.contains(preferenceKey));
    109112        ret = JOptionPane.showOptionDialog(parent, pnl, title, optionType, messageType, null, options, defaultOption);
    110 
    111         if (((ret == JOptionPane.YES_OPTION) || (ret == JOptionPane.NO_OPTION)) && !pnl.getDialogShowingEnabled()) {
    112             setDialogShowingEnabled(preferenceKey, false);
    113             setDialogReturnValue(preferenceKey, ret);
     113        if (isYesOrNo(ret)) {
     114            pnl.getNotShowAgain().store(preferenceKey, ret);
    114115        }
    115116        return ret;
     
    149150    static public boolean showConfirmationDialog(String preferenceKey, Component parent, Object message, String title, int optionType, int messageType, int trueOption) throws HeadlessException {
    150151        int ret = getDialogReturnValue(preferenceKey);
    151         if (!getDialogShowingEnabled(preferenceKey) && ((ret == JOptionPane.YES_OPTION) || (ret == JOptionPane.NO_OPTION)))
     152        if (isYesOrNo(ret))
    152153            return ret == trueOption;
    153         MessagePanel pnl = new MessagePanel(false, message);
     154        MessagePanel pnl = new MessagePanel(message, immediateActive.contains(preferenceKey));
    154155        ret = JOptionPane.showConfirmDialog(parent, pnl, title, optionType, messageType);
    155         if (((ret == JOptionPane.YES_OPTION) || (ret == JOptionPane.NO_OPTION)) && !pnl.getDialogShowingEnabled()) {
    156             setDialogShowingEnabled(preferenceKey, false);
    157             setDialogReturnValue(preferenceKey, ret);
     156        if ((isYesOrNo(ret))) {
     157            pnl.getNotShowAgain().store(preferenceKey, ret);
    158158        }
    159159        return ret == trueOption;
     160    }
     161
     162    private static boolean isYesOrNo(int returnCode) {
     163        return (returnCode == JOptionPane.YES_OPTION) || (returnCode == JOptionPane.NO_OPTION);
    160164    }
    161165
     
    179183     */
    180184    static public void showMessageDialog(String preferenceKey, Component parent, Object message, String title,int messageType) {
    181         if (!getDialogShowingEnabled(preferenceKey))
     185        if (getDialogReturnValue(preferenceKey) == Integer.MAX_VALUE)
    182186            return;
    183         MessagePanel pnl = new MessagePanel(false, message);
     187        MessagePanel pnl = new MessagePanel(message, immediateActive.contains(preferenceKey));
    184188        JOptionPane.showMessageDialog(parent, pnl, title, messageType);
    185         if(!pnl.getDialogShowingEnabled()) {
    186             setDialogShowingEnabled(preferenceKey, false);
     189        pnl.getNotShowAgain().store(preferenceKey, Integer.MAX_VALUE);
     190    }
     191
     192    /**
     193     * An enum designating how long to not show this message again, i.e., for how long to store
     194     */
     195    static enum NotShowAgain {
     196        NO, OPERATION, SESSION, PERMANENT;
     197
     198        /**
     199         * Stores the dialog result {@code value} at the corresponding place.
     200         * @param prefKey the preference key
     201         * @param value the dialog result
     202         */
     203        void store(String prefKey, Integer value) {
     204            switch (this) {
     205                case NO:
     206                    break;
     207                case OPERATION:
     208                    immediateChoices.put(prefKey, value);
     209                    break;
     210                case SESSION:
     211                    sessionChoices.put(prefKey, value);
     212                    break;
     213                case PERMANENT:
     214                    Main.pref.put("message." + prefKey, false);
     215                    Main.pref.putInteger("message." + prefKey + ".value", value);
     216                    break;
     217            }
     218        }
     219
     220        String getLabel() {
     221            switch (this) {
     222                case NO:
     223                    return tr("Show this dialog again the next time");
     224                case OPERATION:
     225                    return tr("Do not show again (this operation)");
     226                case SESSION:
     227                    return tr("Do not show again (this session)");
     228                case PERMANENT:
     229                    return tr("Do not show again (remembers choice)");
     230            }
     231            throw new IllegalStateException();
    187232        }
    188233    }
     
    195240     *
    196241     */
    197     private static class MessagePanel extends JPanel {
    198         JCheckBox cbShowDialog;
    199 
    200         public MessagePanel(boolean donotshow, Object message) {
    201             cbShowDialog = new JCheckBox(tr("Do not show again (remembers choice)"));
    202             cbShowDialog.setSelected(donotshow);
     242    static class MessagePanel extends JPanel {
     243        private final ButtonGroup group = new ButtonGroup();
     244        private final JRadioButton cbShowPermanentDialog = new JRadioButton(NotShowAgain.PERMANENT.getLabel());
     245        private final JRadioButton cbShowSessionDialog = new JRadioButton(NotShowAgain.SESSION.getLabel());
     246        private final JRadioButton cbShowImmediateDialog = new JRadioButton(NotShowAgain.OPERATION.getLabel());
     247        private final JRadioButton cbStandard = new JRadioButton(NotShowAgain.NO.getLabel());
     248
     249        /**
     250         * Constructs a new panel.
     251         * @param message the the message (null to add no message, Component instances are added directly, otherwise a JLabel with the string representation is added)
     252         * @param displayImmediateOption whether to provide "Do not show again (this session)"
     253         */
     254        public MessagePanel(Object message, boolean displayImmediateOption) {
     255            cbStandard.setSelected(true);
     256            group.add(cbShowPermanentDialog);
     257            group.add(cbShowSessionDialog);
     258            group.add(cbShowImmediateDialog);
     259            group.add(cbStandard);
     260
    203261            setLayout(new GridBagLayout());
    204 
    205262            if (message instanceof Component) {
    206                 add((Component)message, GBC.eop());
    207             } else {
    208                 add(new JLabel(message.toString()),GBC.eop());
    209             }
    210             add(cbShowDialog, GBC.eol());
    211         }
    212 
    213         public boolean getDialogShowingEnabled() {
    214             return !cbShowDialog.isSelected();
     263                add((Component) message, GBC.eop());
     264            } else if (message != null) {
     265                add(new JLabel(message.toString()), GBC.eop());
     266            }
     267            add(cbShowPermanentDialog, GBC.eol());
     268            add(cbShowSessionDialog, GBC.eol());
     269            if (displayImmediateOption) {
     270                add(cbShowImmediateDialog, GBC.eol());
     271            }
     272            add(cbStandard, GBC.eol());
     273        }
     274
     275        NotShowAgain getNotShowAgain() {
     276            return cbStandard.isSelected()
     277                    ? NotShowAgain.NO
     278                    : cbShowImmediateDialog.isSelected()
     279                    ? NotShowAgain.OPERATION
     280                    : cbShowSessionDialog.isSelected()
     281                    ? NotShowAgain.SESSION
     282                    : cbShowPermanentDialog.isSelected()
     283                    ? NotShowAgain.PERMANENT
     284                    : null;
    215285        }
    216286    }
  • trunk/src/org/openstreetmap/josm/gui/ExtendedDialog.java

    r6443 r6594  
    2020import javax.swing.Icon;
    2121import javax.swing.JButton;
    22 import javax.swing.JCheckBox;
    2322import javax.swing.JComponent;
    2423import javax.swing.JDialog;
     
    7978    private String togglePref = "";
    8079    private int toggleValue = -1;
    81     private String toggleCheckboxText = tr("Do not show again (remembers choice)");
    82     private JCheckBox toggleCheckbox = null;
     80    private ConditionalOptionPaneUtil.MessagePanel togglePanel;
    8381    private Component parent;
    8482    private Component content;
     
    372370
    373371        if (toggleable) {
    374             toggleCheckbox = new JCheckBox(toggleCheckboxText);
    375             boolean showDialog = Main.pref.getBoolean("message."+ togglePref, true);
    376             toggleCheckbox.setSelected(!showDialog);
     372            togglePanel = new ConditionalOptionPaneUtil.MessagePanel(null, false);
    377373            gc.gridx = icon != null ? 1 : 0;
    378374            gc.gridy = y++;
    379375            gc.anchor = GridBagConstraints.LINE_START;
    380376            gc.insets = new Insets(5,contentInsets.left,5,contentInsets.right);
    381             cp.add(toggleCheckbox, gc);
     377            cp.add(togglePanel, gc);
    382378        }
    383379
     
    546542
    547543    /**
    548      * Overwrites the default "Don't show again" text of the toggle checkbox
    549      * if you want to give more information. Only has an effect if
    550      * <code>toggleEnable</code> is set.
    551      * @param text The toggle checkbox text
    552      * @return {@code this}
    553      */
    554     public ExtendedDialog setToggleCheckboxText(String text) {
    555         this.toggleCheckboxText = text;
    556         return this;
    557     }
    558 
    559     /**
    560544     * Sets the button that will react to ENTER.
    561545     * @param defaultButtonIdx The button index (starts to )
     
    597581    public final boolean toggleCheckState() {
    598582        toggleable = togglePref != null && !togglePref.isEmpty();
    599 
    600         toggleValue = Main.pref.getInteger("message."+togglePref+".value", -1);
    601         // No identifier given, so return false (= show the dialog)
    602         if (!toggleable || toggleValue == -1)
    603             return false;
    604         // The pref is true, if the dialog should be shown.
    605         return !(Main.pref.getBoolean("message."+ togglePref, true));
     583        toggleValue = ConditionalOptionPaneUtil.getDialogReturnValue(togglePref);
     584        return toggleable && toggleValue != -1;
    606585    }
    607586
     
    612591    private void toggleSaveState() {
    613592        if (!toggleable ||
    614                 toggleCheckbox == null ||
     593                togglePanel == null ||
    615594                cancelButtonIdx.contains(result) ||
    616595                result == ExtendedDialog.DialogClosedOtherwise)
    617596            return;
    618         Main.pref.put("message."+ togglePref, !toggleCheckbox.isSelected());
    619         Main.pref.putInteger("message."+togglePref+".value", result);
     597        togglePanel.getNotShowAgain().store(togglePref, result);
    620598    }
    621599
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java

    r6361 r6594  
    768768                return primitives;
    769769            List<OsmPrimitive> ret = new ArrayList<OsmPrimitive>();
     770            ConditionalOptionPaneUtil.startBulkOperation("add_primitive_to_relation");
    770771            for (OsmPrimitive primitive : primitives) {
    771772                if (primitive instanceof Relation && getRelation() != null && getRelation().equals(primitive)) {
     
    782783                }
    783784            }
     785            ConditionalOptionPaneUtil.endBulkOperation("add_primitive_to_relation");
    784786            return ret;
    785787        }
  • trunk/src/org/openstreetmap/josm/gui/io/UploadDialog.java

    r6565 r6594  
    458458            dlg.setIcon(JOptionPane.WARNING_MESSAGE);
    459459            dlg.toggleEnable(togglePref);
    460             dlg.setToggleCheckboxText(tr("Do not show this message again"));
    461460            dlg.setCancelButton(1, 2);
    462461            return dlg.showDialog().getValue() != 3;
Note: See TracChangeset for help on using the changeset viewer.