- Timestamp:
- 2014-01-02T00:58:33+01:00 (11 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/ConditionalOptionPaneUtil.java
r6380 r6594 7 7 import java.awt.GridBagLayout; 8 8 import java.awt.HeadlessException; 9 10 import javax.swing.JCheckBox; 9 import java.util.HashMap; 10 import java.util.HashSet; 11 import java.util.Map; 12 import java.util.Set; 13 14 import javax.swing.ButtonGroup; 11 15 import javax.swing.JLabel; 12 16 import javax.swing.JOptionPane; 13 17 import javax.swing.JPanel; 18 import javax.swing.JRadioButton; 14 19 15 20 import org.openstreetmap.josm.Main; 16 21 import org.openstreetmap.josm.tools.GBC; 22 import org.openstreetmap.josm.tools.Utils; 17 23 18 24 /** … … 29 35 static public final int DIALOG_DISABLED_OPTION = Integer.MIN_VALUE; 30 36 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 31 44 /** 32 45 * this is a static utility class only 33 46 */ 34 47 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 key41 * @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 key51 * @param enabled the value52 */53 public static void setDialogShowingEnabled(String prefKey, boolean enabled) {54 Main.pref.put("message."+prefKey, enabled);55 }56 48 57 49 /** … … 62 54 * @return the preference value for the preference key "message." + <code>prefKey</code> + ".value" 63 55 */ 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); 76 79 } 77 80 … … 104 107 static public int showOptionDialog(String preferenceKey, Component parent, Object message, String title, int optionType, int messageType, Object [] options, Object defaultOption) throws HeadlessException { 105 108 int ret = getDialogReturnValue(preferenceKey); 106 if ( !getDialogShowingEnabled(preferenceKey) && ((ret == JOptionPane.YES_OPTION) || (ret == JOptionPane.NO_OPTION)))109 if (isYesOrNo(ret)) 107 110 return ret; 108 MessagePanel pnl = new MessagePanel( false, message);111 MessagePanel pnl = new MessagePanel(message, immediateActive.contains(preferenceKey)); 109 112 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); 114 115 } 115 116 return ret; … … 149 150 static public boolean showConfirmationDialog(String preferenceKey, Component parent, Object message, String title, int optionType, int messageType, int trueOption) throws HeadlessException { 150 151 int ret = getDialogReturnValue(preferenceKey); 151 if ( !getDialogShowingEnabled(preferenceKey) && ((ret == JOptionPane.YES_OPTION) || (ret == JOptionPane.NO_OPTION)))152 if (isYesOrNo(ret)) 152 153 return ret == trueOption; 153 MessagePanel pnl = new MessagePanel( false, message);154 MessagePanel pnl = new MessagePanel(message, immediateActive.contains(preferenceKey)); 154 155 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); 158 158 } 159 159 return ret == trueOption; 160 } 161 162 private static boolean isYesOrNo(int returnCode) { 163 return (returnCode == JOptionPane.YES_OPTION) || (returnCode == JOptionPane.NO_OPTION); 160 164 } 161 165 … … 179 183 */ 180 184 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) 182 186 return; 183 MessagePanel pnl = new MessagePanel( false, message);187 MessagePanel pnl = new MessagePanel(message, immediateActive.contains(preferenceKey)); 184 188 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(); 187 232 } 188 233 } … … 195 240 * 196 241 */ 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 203 261 setLayout(new GridBagLayout()); 204 205 262 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; 215 285 } 216 286 } -
trunk/src/org/openstreetmap/josm/gui/ExtendedDialog.java
r6443 r6594 20 20 import javax.swing.Icon; 21 21 import javax.swing.JButton; 22 import javax.swing.JCheckBox;23 22 import javax.swing.JComponent; 24 23 import javax.swing.JDialog; … … 79 78 private String togglePref = ""; 80 79 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; 83 81 private Component parent; 84 82 private Component content; … … 372 370 373 371 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); 377 373 gc.gridx = icon != null ? 1 : 0; 378 374 gc.gridy = y++; 379 375 gc.anchor = GridBagConstraints.LINE_START; 380 376 gc.insets = new Insets(5,contentInsets.left,5,contentInsets.right); 381 cp.add(toggle Checkbox, gc);377 cp.add(togglePanel, gc); 382 378 } 383 379 … … 546 542 547 543 /** 548 * Overwrites the default "Don't show again" text of the toggle checkbox549 * if you want to give more information. Only has an effect if550 * <code>toggleEnable</code> is set.551 * @param text The toggle checkbox text552 * @return {@code this}553 */554 public ExtendedDialog setToggleCheckboxText(String text) {555 this.toggleCheckboxText = text;556 return this;557 }558 559 /**560 544 * Sets the button that will react to ENTER. 561 545 * @param defaultButtonIdx The button index (starts to ) … … 597 581 public final boolean toggleCheckState() { 598 582 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; 606 585 } 607 586 … … 612 591 private void toggleSaveState() { 613 592 if (!toggleable || 614 toggle Checkbox== null ||593 togglePanel == null || 615 594 cancelButtonIdx.contains(result) || 616 595 result == ExtendedDialog.DialogClosedOtherwise) 617 596 return; 618 Main.pref.put("message."+ togglePref, !toggleCheckbox.isSelected()); 619 Main.pref.putInteger("message."+togglePref+".value", result); 597 togglePanel.getNotShowAgain().store(togglePref, result); 620 598 } 621 599 -
trunk/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java
r6361 r6594 768 768 return primitives; 769 769 List<OsmPrimitive> ret = new ArrayList<OsmPrimitive>(); 770 ConditionalOptionPaneUtil.startBulkOperation("add_primitive_to_relation"); 770 771 for (OsmPrimitive primitive : primitives) { 771 772 if (primitive instanceof Relation && getRelation() != null && getRelation().equals(primitive)) { … … 782 783 } 783 784 } 785 ConditionalOptionPaneUtil.endBulkOperation("add_primitive_to_relation"); 784 786 return ret; 785 787 } -
trunk/src/org/openstreetmap/josm/gui/io/UploadDialog.java
r6565 r6594 458 458 dlg.setIcon(JOptionPane.WARNING_MESSAGE); 459 459 dlg.toggleEnable(togglePref); 460 dlg.setToggleCheckboxText(tr("Do not show this message again"));461 460 dlg.setCancelButton(1, 2); 462 461 return dlg.showDialog().getValue() != 3;
Note:
See TracChangeset
for help on using the changeset viewer.