Ticket #2905: ExtendedDialog-dontShowAgain.patch
File ExtendedDialog-dontShowAgain.patch, 14.6 KB (added by , 16 years ago) |
---|
-
src/org/openstreetmap/josm/actions/mapmode/SelectAction.java
464 464 } 465 465 if(s.size() > max) 466 466 { 467 if(1 != new ExtendedDialog(Main.parent, tr("Move elements"), 468 tr("You did move more than {0} elements. " 469 + "Moving a large number of elements is often an error.\n" 470 + "Really move them?", max), 471 new String[] {tr("Move them"), tr("Undo move")}, 472 new String[] {"reorder.png", "cancel.png"}).getValue()) 467 ExtendedDialog ed = new ExtendedDialog( 468 Main.parent, 469 tr("Move elements"), 470 new String[] {tr("Move them"), tr("Undo move")}); 471 ed.setButtonIcons(new String[] {"reorder.png", "cancel.png"}); 472 ed.setContent(tr("You moved more than {0} elements. " 473 + "Moving a large number of elements is often an error.\n" 474 + "Really move them?", max)); 475 ed.toggleEnable("movedManyElements"); 476 ed.setToggleCheckboxText(tr("Always move and don't show dialog again")); 477 ed.showDialog(); 478 479 if(ed.getValue() != 1 && ed.getValue() != ExtendedDialog.DialogNotShown) 473 480 { 474 481 Main.main.undoRedo.undo(); 475 482 } -
src/org/openstreetmap/josm/gui/ExtendedDialog.java
1 1 package org.openstreetmap.josm.gui; 2 2 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 3 5 import java.awt.Component; 4 6 import java.awt.Dimension; 5 7 import java.awt.GridBagLayout; … … 10 12 import javax.swing.AbstractAction; 11 13 import javax.swing.Action; 12 14 import javax.swing.JButton; 15 import javax.swing.JCheckBox; 13 16 import javax.swing.JComponent; 14 17 import javax.swing.JDialog; 15 18 import javax.swing.JOptionPane; … … 18 21 import javax.swing.JScrollPane; 19 22 import javax.swing.KeyStroke; 20 23 24 import org.openstreetmap.josm.Main; 21 25 import org.openstreetmap.josm.tools.GBC; 22 26 import org.openstreetmap.josm.tools.ImageProvider; 23 27 24 28 25 29 public class ExtendedDialog extends JDialog { 26 30 private int result = 0; 31 public static final int DialogNotShown = -99; 32 public static final int DialogClosedOtherwise = 0; 33 private boolean toggleable = false; 34 private String togglePref = ""; 35 private String toggleCheckboxText = tr("Do not show again"); 36 private JCheckBox toggleCheckbox = null; 27 37 private Component parent; 38 private Component content; 28 39 private final String[] bTexts; 40 private String[] bIcons; 29 41 30 42 // For easy access when inherited 31 43 protected Object contentConstraints = GBC.eol().anchor(GBC.CENTER).fill(GBC.HORIZONTAL).insets(5,10,5,0); 32 44 protected ArrayList<JButton> buttons = new ArrayList<JButton>(); 33 45 34 46 /** 47 * This method sets up the most basic options for the dialog. Add all more 48 * advanced features with dedicated methods. 49 * Possible features: 50 * <ul> 51 * <li><code>setButtonIcons</code></li> 52 * <li><code>setContent</code></li> 53 * <li><code>toggleEnable</code></li> 54 * <li><code>toggleDisable</code></li> 55 * <li><code>setToggleCheckboxText</code></li> 56 * </ul> 57 * 58 * When done, call <code>showDialog</code> to display it. You can receive 59 * the user's choice using <code>getValue</code>. Have a look at this function 60 * for possible return values. 61 * 62 * @param parent The parent element that will be used for position and maximum size 63 * @param title The text that will be shown in the window titlebar 64 * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one. 65 */ 66 public ExtendedDialog(Component parent, String title, String[] buttonTexts) { 67 super(JOptionPane.getFrameForComponent(parent), title, true); 68 this.parent = parent; 69 bTexts = buttonTexts; 70 } 71 72 /** 73 * Same as above but lets you define if the dialog should be modal. 74 */ 75 public ExtendedDialog(Component parent, String title, String[] buttonTexts, 76 boolean modal) { 77 super(JOptionPane.getFrameForComponent(parent), title, modal); 78 this.parent = parent; 79 bTexts = buttonTexts; 80 } 81 82 /** 35 83 * Sets up the dialog. The first button is always the default. 36 84 * @param parent The parent element that will be used for position and maximum size 37 85 * @param title The text that will be shown in the window titlebar … … 39 87 * @param buttonTexts The labels that will be displayed on the buttons 40 88 * @param buttonIcons The path to the icons that will be displayed on the buttons. Path is relative to JOSM's image directory. File extensions need to be included. If a button should not have an icon pass null. 41 89 */ 42 public ExtendedDialog(Component parent, String title, Component content, String[] buttonTexts, String[] buttonIcons) { 90 @Deprecated public ExtendedDialog(Component parent, String title, Component content, 91 String[] buttonTexts, String[] buttonIcons) { 43 92 super(JOptionPane.getFrameForComponent(parent), title, true /* modal */); 44 93 this.parent = parent; 45 94 bTexts = buttonTexts; 46 setupDialog(content, buttonIcons); 95 this.content = content; 96 this.bIcons = buttonIcons; 97 setupDialog(); 47 98 setVisible(true); 48 99 } 49 100 50 public ExtendedDialog(Component parent, String title, Component content, String[] buttonTexts) { 101 @Deprecated public ExtendedDialog(Component parent, String title, Component content, 102 String[] buttonTexts) { 51 103 this(parent, title, content, buttonTexts, null); 52 104 } 53 105 54 106 /** 55 107 * Sets up the dialog and displays the given message in a breakable label 56 108 */ 57 public ExtendedDialog(Component parent, String title, String message, String[] buttonTexts, String[] buttonIcons) { 58 super(JOptionPane.getFrameForComponent(parent), title, true); 109 @Deprecated public ExtendedDialog(Component parent, String title, String message, 110 String[] buttonTexts, String[] buttonIcons) { 111 this(parent, title, string2label(message), buttonTexts, buttonIcons); 112 } 59 113 60 JMultilineLabel lbl = new JMultilineLabel(message);61 // Make it not wider than 2/3 of the screen62 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();63 lbl.setMaxWidth(Math.round(screenSize.width*2/3));114 @Deprecated public ExtendedDialog(Component parent, String title, String message, 115 String[] buttonTexts) { 116 this(parent, title, message, buttonTexts, null); 117 } 64 118 65 this.parent = parent; 66 bTexts = buttonTexts; 67 setupDialog(lbl, buttonIcons); 68 setVisible(true); 119 /** 120 * Allows decorating the buttons with icons. Expects an String[] with paths 121 * to images relative to JOSM/images. 122 * @param buttonIcons 123 */ 124 public void setButtonIcons(String[] buttonIcons) { 125 this.bIcons = buttonIcons; 69 126 } 70 127 71 public ExtendedDialog(Component parent, String title, String message, String[] buttonTexts) { 72 this(parent, title, message, buttonTexts, null); 128 /** 129 * Sets the content that will be displayed in the message dialog. 130 * 131 * Note that depending on your other settings more UI elements may appear. 132 * The content is played on top of the other elements though. 133 * 134 * @param content Any element that can be displayed in the message dialog 135 */ 136 public void setContent(Component content) { 137 this.content = content; 73 138 } 74 139 75 140 /** 76 * Constructor that doesn't make the dialog visible immediately. Intended for when inheriting. 141 * Sets the message that will be displayed. The String will be automatically 142 * wrapped if it is too long. 143 * 144 * Note that depending on your other settings more UI elements may appear. 145 * The content is played on top of the other elements though. 146 * 147 * @param message The text that should be shown to the user 77 148 */ 78 public ExtendedDialog(Component parent, String title, String[] buttonTexts, boolean modal) { 79 super(JOptionPane.getFrameForComponent(parent), title, modal); 80 this.parent = parent; 81 bTexts = buttonTexts; 149 public void setContent(String message) { 150 setContent(string2label(message)); 151 } 152 153 154 155 /** 156 * Show the dialog to the user. Call this after you have set all options 157 * for the dialog. You can retrieve the result using <code>getValue</code> 158 */ 159 public void showDialog() { 160 // Check if the user has set the dialog to not be shown again 161 if(toggleCheckState(togglePref)) { 162 result = ExtendedDialog.DialogNotShown; 163 return; 164 } 165 166 setupDialog(); 167 setVisible(true); 168 toggleSaveState(); 169 } 170 171 /** 172 * @return int * The selected button. The count starts with 1. 173 * * A return value of ExtendedDialog.DialogClosedOtherwise means the dialog has been closed otherwise. 174 * * A return value of ExtendedDialog.DialogNotShown means the 175 * dialog has been toggled off in the past 176 */ 177 public int getValue() { 178 return result; 82 179 } 83 180 84 protected void setupDialog(Component content, String[] buttonIcons) { 181 @Deprecated protected void setupDialog(Component content, String[] buttonIcons) { 182 this.setContent(content); 183 this.setButtonIcons(buttonIcons); 184 this.setupDialog(); 185 } 186 187 protected void setupDialog() { 85 188 setupEscListener(); 86 189 87 190 JButton button; … … 95 198 }; 96 199 97 200 button = new JButton(action); 98 if(b uttonIcons != null && buttonIcons[i] != null) {99 button.setIcon(ImageProvider.get(b uttonIcons[i]));201 if(bIcons != null && bIcons[i] != null) { 202 button.setIcon(ImageProvider.get(bIcons[i])); 100 203 } 101 204 102 205 if(i == 0) { … … 108 211 109 212 JPanel cp = new JPanel(new GridBagLayout()); 110 213 cp.add(content, contentConstraints); 214 215 if(toggleable) { 216 toggleCheckbox = new JCheckBox(toggleCheckboxText); 217 boolean showDialog = Main.pref.getBoolean("message."+ togglePref, true); 218 toggleCheckbox.setSelected(!showDialog); 219 cp.add(toggleCheckbox, GBC.eol().anchor(GBC.LINE_START).insets(5,5,5,5)); 220 } 221 111 222 cp.add(buttonsPanel, GBC.eol().anchor(GBC.CENTER).insets(5,5,5,5)); 112 223 113 224 JScrollPane pane = new JScrollPane(cp); … … 140 251 } 141 252 142 253 /** 143 * @return int The selected button. The count starts with 1.144 * A return value of 0 means the dialog has been closed otherwise.145 */146 public int getValue() {147 return result;148 }149 150 /**151 254 * This gets performed whenever a button is clicked or activated 152 255 * @param evt the button event 153 256 */ … … 187 290 // 0 means that the dialog has been closed otherwise. 188 291 // We need to set it to zero again, in case the dialog has been re-used 189 292 // and the result differs from its default value 190 result = 0;293 result = ExtendedDialog.DialogClosedOtherwise; 191 294 setVisible(false); 192 295 } 193 296 }; … … 204 307 toFront(); 205 308 } 206 309 } 310 311 /** 312 * Calling this will offer the user a "Do not show again" checkbox for the 313 * dialog. Default is to not offer the choice; the dialog will be shown 314 * every time. If the dialog is not shown due to the previous choice of the 315 * user, the result <code>ExtendedDialog.DialogNotShown</code> is returned 316 * @param togglePref The preference to save the checkbox state to 317 */ 318 public void toggleEnable(String togglePref) { 319 this.toggleable = true; 320 this.togglePref = togglePref; 321 } 322 323 /** 324 * Call this if you "accidentally" called toggleEnable. This doesn't need 325 * to be called for every dialog, as it's the default anyway. 326 */ 327 public void toggleDisable() { 328 this.toggleable = false; 329 } 330 331 /** 332 * Overwrites the default "Don't show again" text of the toggle checkbox 333 * if you want to give more information. Only has an effect if 334 * <code>toggleEnable</code> is set. 335 * @param text 336 */ 337 public void setToggleCheckboxText(String text) { 338 this.toggleCheckboxText = text; 339 } 340 341 /** 342 * This function returns true if the dialog has been set to "do not show again" 343 * @return true if dialog should not be shown again 344 */ 345 private boolean toggleCheckState(String togglePref) { 346 toggleable = togglePref != null && !togglePref.equals(""); 347 348 // No identifier given, so return false (= show the dialog) 349 if(!toggleable) 350 return false; 351 352 this.togglePref = togglePref; 353 // The pref is true, if the dialog should be shown. 354 return !(Main.pref.getBoolean("message."+ togglePref, true)); 355 } 356 357 /** 358 * This function checks the state of the "Do not show again" checkbox and 359 * writes the corresponding pref 360 */ 361 private void toggleSaveState() { 362 if(!toggleable || toggleCheckbox == null) 363 return; 364 Main.pref.put("message."+ togglePref, !toggleCheckbox.isSelected()); 365 } 366 367 /** 368 * Convenience function that converts a given string into a JMultilineLabel 369 * @param msg 370 * @return JMultilineLabel 371 */ 372 private static JMultilineLabel string2label(String msg) { 373 JMultilineLabel lbl = new JMultilineLabel(msg); 374 // Make it not wider than 2/3 of the screen 375 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 376 lbl.setMaxWidth(Math.round(screenSize.width*2/3)); 377 return lbl; 378 } 207 379 } 380 No newline at end of file