001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.Component; 007import java.awt.GridBagLayout; 008import java.util.HashMap; 009import java.util.HashSet; 010import java.util.Map; 011import java.util.Set; 012 013import javax.swing.ButtonGroup; 014import javax.swing.JOptionPane; 015import javax.swing.JPanel; 016import javax.swing.JRadioButton; 017 018import org.openstreetmap.josm.gui.util.WindowOnTopListener; 019import org.openstreetmap.josm.gui.widgets.JMultilineLabel; 020import org.openstreetmap.josm.spi.preferences.Config; 021import org.openstreetmap.josm.tools.GBC; 022import org.openstreetmap.josm.tools.Utils; 023 024/** 025 * ConditionalOptionPaneUtil provides static utility methods for displaying modal message dialogs 026 * which can be enabled/disabled by the user. 027 * <p> 028 * They wrap the methods provided by {@link JOptionPane}. Within JOSM you should use these 029 * methods rather than the bare methods from {@link JOptionPane} because the methods provided 030 * by ConditionalOptionPaneUtil ensure that a dialog window is always on top and isn't hidden by one of the 031 * JOSM windows for detached dialogs, relation editors, history browser and the like. 032 * 033 */ 034public final class ConditionalOptionPaneUtil { 035 public static final int DIALOG_DISABLED_OPTION = Integer.MIN_VALUE; 036 037 /** (preference key => return value) mappings valid for the current operation (no, those two maps cannot be combined) */ 038 private static final Map<String, Integer> sessionChoices = new HashMap<>(); 039 /** (preference key => return value) mappings valid for the current session */ 040 private static final Map<String, Integer> immediateChoices = new HashMap<>(); 041 /** a set indication that (preference key) is or may be stored for the currently active bulk operation */ 042 private static final Set<String> immediateActive = new HashSet<>(); 043 /** The prefix for config options */ 044 private static final String CONFIG_PREFIX = "message."; 045 046 /** 047 * this is a static utility class only 048 */ 049 private ConditionalOptionPaneUtil() { 050 // Hide default constructor for utility classes 051 } 052 053 /** 054 * Returns the preference value for the preference key "message." + <code>prefKey</code> + ".value". 055 * The default value if the preference key is missing is -1. 056 * 057 * @param prefKey the preference key 058 * @return the preference value for the preference key "message." + <code>prefKey</code> + ".value" 059 */ 060 public static int getDialogReturnValue(String prefKey) { 061 return Utils.firstNonNull(immediateChoices.get(prefKey), 062 sessionChoices.get(prefKey), 063 !Config.getPref().getBoolean(CONFIG_PREFIX + prefKey, true) 064 ? Config.getPref().getInt(CONFIG_PREFIX + prefKey + ".value", -1) 065 : -1 066 ); 067 } 068 069 /** 070 * Marks the beginning of a bulk operation in order to provide a "Do not show again (this operation)" option. 071 * @param prefKey the preference key 072 */ 073 public static void startBulkOperation(final String prefKey) { 074 immediateActive.add(prefKey); 075 } 076 077 /** 078 * Determines whether the key has been marked to be part of a bulk operation 079 * (in order to provide a "Do not show again (this operation)" option). 080 * @param prefKey the preference key 081 * @return {@code true} if the key has been marked to be part of a bulk operation 082 */ 083 public static boolean isInBulkOperation(final String prefKey) { 084 return immediateActive.contains(prefKey); 085 } 086 087 /** 088 * Marks the ending of a bulk operation. Removes the "Do not show again (this operation)" result value. 089 * @param prefKey the preference key 090 */ 091 public static void endBulkOperation(final String prefKey) { 092 immediateActive.remove(prefKey); 093 immediateChoices.remove(prefKey); 094 } 095 096 /** 097 * Displays an confirmation dialog with some option buttons given by <code>optionType</code>. 098 * It is always on top even if there are other open windows like detached dialogs, 099 * relation editors, history browsers and the like. 100 * <p> 101 * Set <code>optionType</code> to {@link JOptionPane#YES_NO_OPTION} for a dialog with a YES and 102 * a NO button. 103 * <p> 104 * Set <code>optionType</code> to {@link JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES, 105 * a NO and a CANCEL button 106 * <p> 107 * Returns one of the constants JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, 108 * JOptionPane.CANCEL_OPTION or JOptionPane.CLOSED_OPTION depending on the action chosen by 109 * the user. 110 * 111 * @param preferenceKey the preference key 112 * @param parent the parent component 113 * @param message the message 114 * @param title the title 115 * @param optionType the option type 116 * @param messageType the message type 117 * @param options a list of options 118 * @param defaultOption the default option; only meaningful if options is used; can be null 119 * 120 * @return the option selected by user. 121 * {@link JOptionPane#CLOSED_OPTION} if the dialog was closed. 122 */ 123 public static int showOptionDialog(String preferenceKey, Component parent, Object message, String title, int optionType, 124 int messageType, Object[] options, Object defaultOption) { 125 int ret = getDialogReturnValue(preferenceKey); 126 if (isYesOrNo(ret)) 127 return ret; 128 MessagePanel pnl = new MessagePanel(message, isInBulkOperation(preferenceKey)); 129 ret = JOptionPane.showOptionDialog(parent, pnl, title, optionType, messageType, null, options, defaultOption); 130 if (isYesOrNo(ret)) { 131 pnl.getNotShowAgain().store(preferenceKey, ret); 132 } 133 return ret; 134 } 135 136 /** 137 * Displays a confirmation dialog with some option buttons given by <code>optionType</code>. 138 * It is always on top even if there are other open windows like detached dialogs, 139 * relation editors, history browsers and the like. 140 * <p> 141 * Set <code>optionType</code> to {@link JOptionPane#YES_NO_OPTION} for a dialog with a YES and 142 * a NO button. 143 * <p> 144 * Set <code>optionType</code> to {@link JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES, 145 * a NO and a CANCEL button 146 * <p> 147 * Replies true, if the selected option is equal to <code>trueOption</code>, otherwise false. 148 * Replies true, if the dialog is not displayed because the respective preference option 149 * <code>preferenceKey</code> is set to false and the user has previously chosen 150 * <code>trueOption</code>. 151 * 152 * @param preferenceKey the preference key 153 * @param parent the parent component 154 * @param message the message 155 * @param title the title 156 * @param optionType the option type 157 * @param messageType the message type 158 * @param trueOption if this option is selected the method replies true 159 * 160 * 161 * @return true, if the selected option is equal to <code>trueOption</code>, otherwise false. 162 * 163 * @see JOptionPane#INFORMATION_MESSAGE 164 * @see JOptionPane#WARNING_MESSAGE 165 * @see JOptionPane#ERROR_MESSAGE 166 */ 167 public static boolean showConfirmationDialog(String preferenceKey, Component parent, Object message, String title, 168 int optionType, int messageType, int trueOption) { 169 int ret = getDialogReturnValue(preferenceKey); 170 if (isYesOrNo(ret)) 171 return ret == trueOption; 172 MessagePanel pnl = new MessagePanel(message, isInBulkOperation(preferenceKey)); 173 ret = JOptionPane.showConfirmDialog(parent, pnl, title, optionType, messageType); 174 if (isYesOrNo(ret)) { 175 pnl.getNotShowAgain().store(preferenceKey, ret); 176 } 177 return ret == trueOption; 178 } 179 180 private static boolean isYesOrNo(int returnCode) { 181 return (returnCode == JOptionPane.YES_OPTION) || (returnCode == JOptionPane.NO_OPTION); 182 } 183 184 /** 185 * Displays an message in modal dialog with an OK button. Makes sure the dialog 186 * is always on top even if there are other open windows like detached dialogs, 187 * relation editors, history browsers and the like. 188 * <p> 189 * If there is a preference with key <code>preferenceKey</code> and value <code>false</code> 190 * the dialog is not show. 191 * 192 * @param preferenceKey the preference key 193 * @param parent the parent component 194 * @param message the message 195 * @param title the title 196 * @param messageType the message type 197 * 198 * @see JOptionPane#INFORMATION_MESSAGE 199 * @see JOptionPane#WARNING_MESSAGE 200 * @see JOptionPane#ERROR_MESSAGE 201 */ 202 public static void showMessageDialog(String preferenceKey, Component parent, Object message, String title, int messageType) { 203 if (getDialogReturnValue(preferenceKey) == Integer.MAX_VALUE) 204 return; 205 MessagePanel pnl = new MessagePanel(message, isInBulkOperation(preferenceKey)); 206 JOptionPane.showMessageDialog(parent, pnl, title, messageType); 207 pnl.getNotShowAgain().store(preferenceKey, Integer.MAX_VALUE); 208 } 209 210 /** 211 * An enum designating how long to not show this message again, i.e., for how long to store 212 */ 213 enum NotShowAgain { 214 NO, OPERATION, SESSION, PERMANENT; 215 216 /** 217 * Stores the dialog result {@code value} at the corresponding place. 218 * @param prefKey the preference key 219 * @param value the dialog result 220 */ 221 void store(String prefKey, Integer value) { 222 switch (this) { 223 case NO: 224 break; 225 case OPERATION: 226 immediateChoices.put(prefKey, value); 227 break; 228 case SESSION: 229 sessionChoices.put(prefKey, value); 230 break; 231 case PERMANENT: 232 Config.getPref().putBoolean(CONFIG_PREFIX + prefKey, false); 233 Config.getPref().putInt(CONFIG_PREFIX + prefKey + ".value", value); 234 break; 235 } 236 } 237 238 String getLabel() { 239 switch (this) { 240 case NO: 241 return tr("Show this dialog again the next time"); 242 case OPERATION: 243 return tr("Do not show again (this operation)"); 244 case SESSION: 245 return tr("Do not show again (this session)"); 246 case PERMANENT: 247 return tr("Do not show again (remembers choice)"); 248 } 249 throw new IllegalStateException(); 250 } 251 } 252 253 /** 254 * This is a message panel used in dialogs which can be enabled/disabled with a preference setting. 255 * In addition to the normal message any {@link JOptionPane} would display it includes 256 * a checkbox for enabling/disabling this particular dialog. 257 * 258 */ 259 public static class MessagePanel extends JPanel { 260 private final JRadioButton cbShowPermanentDialog = new JRadioButton(NotShowAgain.PERMANENT.getLabel()); 261 private final JRadioButton cbShowSessionDialog = new JRadioButton(NotShowAgain.SESSION.getLabel()); 262 private final JRadioButton cbShowImmediateDialog = new JRadioButton(NotShowAgain.OPERATION.getLabel()); 263 private final JRadioButton cbStandard = new JRadioButton(NotShowAgain.NO.getLabel()); 264 265 /** 266 * Constructs a new panel. 267 * @param message the the message (null to add no message, Component instances are added directly, 268 * otherwise a JLabel with the string representation is added) 269 * @param displayImmediateOption whether to provide "Do not show again (this session)" 270 */ 271 MessagePanel(Object message, boolean displayImmediateOption) { 272 cbStandard.setSelected(true); 273 ButtonGroup group = new ButtonGroup(); 274 group.add(cbShowPermanentDialog); 275 group.add(cbShowSessionDialog); 276 group.add(cbShowImmediateDialog); 277 group.add(cbStandard); 278 279 setLayout(new GridBagLayout()); 280 if (message instanceof Component) { 281 add((Component) message, GBC.eop()); 282 } else if (message != null) { 283 add(new JMultilineLabel(message.toString()), GBC.eop()); 284 } 285 add(cbShowPermanentDialog, GBC.eol()); 286 add(cbShowSessionDialog, GBC.eol()); 287 if (displayImmediateOption) { 288 add(cbShowImmediateDialog, GBC.eol()); 289 } 290 add(cbStandard, GBC.eol()); 291 292 this.addAncestorListener(new WindowOnTopListener()); 293 } 294 295 NotShowAgain getNotShowAgain() { 296 if (cbStandard.isSelected()) { 297 return NotShowAgain.NO; 298 } 299 if (cbShowImmediateDialog.isSelected()) { 300 return NotShowAgain.OPERATION; 301 } 302 if (cbShowSessionDialog.isSelected()) { 303 return NotShowAgain.SESSION; 304 } 305 if (cbShowPermanentDialog.isSelected()) { 306 return NotShowAgain.PERMANENT; 307 } 308 return null; 309 } 310 } 311}