Changeset 3403 in josm for trunk/src/org
- Timestamp:
- 2010-08-02T09:47:56+02:00 (14 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/ExtendedDialog.java
r3385 r3403 5 5 import java.awt.Component; 6 6 import java.awt.Dimension; 7 import java.awt.GridBagConstraints; 7 8 import java.awt.GridBagLayout; 9 import java.awt.Insets; 8 10 import java.awt.Toolkit; 9 11 import java.awt.event.ActionEvent; 10 12 import java.util.ArrayList; 13 import java.util.Arrays; 14 import java.util.Collections; 15 import java.util.List; 11 16 12 17 import javax.swing.AbstractAction; 13 18 import javax.swing.Action; 19 import javax.swing.Icon; 14 20 import javax.swing.JButton; 15 21 import javax.swing.JCheckBox; 16 22 import javax.swing.JComponent; 17 23 import javax.swing.JDialog; 24 import javax.swing.JLabel; 18 25 import javax.swing.JOptionPane; 19 26 import javax.swing.JPanel; … … 21 28 import javax.swing.JScrollPane; 22 29 import javax.swing.KeyStroke; 30 import javax.swing.SwingUtilities; 31 import javax.swing.UIManager; 23 32 24 33 import org.openstreetmap.josm.Main; … … 29 38 import org.openstreetmap.josm.tools.WindowGeometry; 30 39 40 /** 41 * General configurable dialog window. 42 * 43 * If dialog is modal, you can use getValue() to retrieve the 44 * button index. Note that the user can close the dialog 45 * by other means. This is usually equivalent to cancel action. 46 * 47 * For non-modal dialogs, buttonAction(int) can be overridden. 48 * 49 * There are various options, see below. 50 * 51 * Note: The button indices are counted from 1 and upwards. 52 * So for getValue(), setDefaultButton(int) and setCancelButton(int) the 53 * first button has index 1. 54 */ 31 55 public class ExtendedDialog extends JDialog { 32 56 private final boolean disposeOnClose; … … 44 68 private final String[] bTexts; 45 69 private String[] bToolTipTexts; 46 private String[] bIcons;47 private int cancelButtonIdx = -1;48 private int defaultButtonIdx = -1;70 private Icon[] bIcons; 71 private List<Integer> cancelButtonIdx = Collections.emptyList(); 72 private int defaultButtonIdx = 1; 49 73 private JButton defaultButton = null; 74 private Icon icon; 75 private boolean modal; 50 76 51 77 /** true, if the dialog should include a help button */ … … 61 87 62 88 // For easy access when inherited 63 protected Object contentConstraints = GBC.eol().anchor(GBC.CENTER).fill(GBC.BOTH).insets(5,10,5,0);89 protected Insets contentInsets = new Insets(10,5,0,5); 64 90 protected ArrayList<JButton> buttons = new ArrayList<JButton>(); 65 91 … … 101 127 super(JOptionPane.getFrameForComponent(parent), title, modal); 102 128 this.parent = parent; 129 this.modal = modal; 103 130 bTexts = buttonTexts; 104 131 if (disposeOnClose) { … … 109 136 110 137 /** 111 * Allows decorating the buttons with icons. Expects an String[] with paths 112 * to images relative to JOSM/images. 138 * Allows decorating the buttons with icons. 113 139 * @param buttonIcons 114 140 */ 141 public ExtendedDialog setButtonIcons(Icon[] buttonIcons) { 142 this.bIcons = buttonIcons; 143 return this; 144 } 145 146 /** 147 * Convenience method to provide image names instead of images. 148 */ 115 149 public ExtendedDialog setButtonIcons(String[] buttonIcons) { 116 this.bIcons = buttonIcons; 150 bIcons = new Icon[buttonIcons.length]; 151 for (int i=0; i<buttonIcons.length; ++i) { 152 bIcons[i] = ImageProvider.get(buttonIcons[i]); 153 } 117 154 return this; 118 155 } … … 171 208 172 209 /** 210 * Decorate the dialog with an icon that is shown on the left part of 211 * the window area. (Similar to how it is done in JOptionPane) 212 */ 213 public ExtendedDialog setIcon(Icon icon) { 214 this.icon = icon; 215 return this; 216 } 217 218 /** 219 * Convenience method to allow values that would be accepted by JOptionPane as messageType. 220 */ 221 public ExtendedDialog setIcon(int messageType) { 222 switch (messageType) { 223 case JOptionPane.ERROR_MESSAGE: 224 return setIcon(UIManager.getIcon("OptionPane.errorIcon")); 225 case JOptionPane.INFORMATION_MESSAGE: 226 return setIcon(UIManager.getIcon("OptionPane.informationIcon")); 227 case JOptionPane.WARNING_MESSAGE: 228 return setIcon(UIManager.getIcon("OptionPane.warningIcon")); 229 case JOptionPane.QUESTION_MESSAGE: 230 return setIcon(UIManager.getIcon("OptionPane.questionIcon")); 231 case JOptionPane.PLAIN_MESSAGE: 232 return setIcon(null); 233 default: 234 throw new IllegalArgumentException("Unknown message type!"); 235 } 236 } 237 238 /** 173 239 * Show the dialog to the user. Call this after you have set all options 174 240 * for the dialog. You can retrieve the result using <code>getValue</code> … … 176 242 public ExtendedDialog showDialog() { 177 243 // Check if the user has set the dialog to not be shown again 178 if (toggleCheckState(togglePref)) {244 if (toggleCheckState(togglePref)) { 179 245 result = toggleValue; 180 246 return this; … … 185 251 getRootPane().setDefaultButton(defaultButton); 186 252 } 253 fixFocus(); 187 254 setVisible(true); 188 255 toggleSaveState(); … … 227 294 } 228 295 if(bIcons != null && bIcons[i] != null) { 229 button.setIcon( ImageProvider.get(bIcons[i]));296 button.setIcon(bIcons[i]); 230 297 } 231 298 if (bToolTipTexts != null && i < bToolTipTexts.length && bToolTipTexts[i] != null) { … … 233 300 } 234 301 235 if(i == 0) {236 rootPane.setDefaultButton(button);237 }238 302 buttonsPanel.add(button, GBC.std().insets(2,2,2,2)); 239 303 buttons.add(button); … … 245 309 246 310 JPanel cp = new JPanel(new GridBagLayout()); 247 cp.add(content, contentConstraints); 248 249 if(toggleable) { 311 312 GridBagConstraints gc = new GridBagConstraints(); 313 gc.gridx = 0; 314 int y = 0; 315 gc.gridy = y++; 316 gc.weightx = 0.0; 317 gc.weighty = 0.0; 318 319 if (icon != null) { 320 JLabel iconLbl = new JLabel(icon); 321 gc.insets = new Insets(10,10,10,10); 322 gc.anchor = GridBagConstraints.NORTH; 323 cp.add(iconLbl, gc); 324 gc.anchor = GridBagConstraints.CENTER; 325 gc.gridx = 1; 326 } 327 328 gc.fill = GridBagConstraints.BOTH; 329 gc.insets = contentInsets; 330 cp.add(content, gc); 331 332 gc.fill = GridBagConstraints.NONE; 333 gc.gridwidth = GridBagConstraints.REMAINDER; 334 335 if (toggleable) { 250 336 toggleCheckbox = new JCheckBox(toggleCheckboxText); 251 337 boolean showDialog = Main.pref.getBoolean("message."+ togglePref, true); 252 338 toggleCheckbox.setSelected(!showDialog); 253 cp.add(toggleCheckbox, GBC.eol().anchor(GBC.LINE_START).insets(5,5,5,5)); 254 } 255 256 cp.add(buttonsPanel, GBC.eol().anchor(GBC.CENTER).insets(5,5,5,5)); 339 gc.gridx = icon != null ? 1 : 0; 340 gc.gridy = y++; 341 gc.anchor = GridBagConstraints.LINE_START; 342 gc.insets = new Insets(5,contentInsets.left,5,contentInsets.right); 343 cp.add(toggleCheckbox, gc); 344 } 345 346 gc.gridy = y++; 347 gc.anchor = GridBagConstraints.CENTER; 348 gc.insets = new Insets(5,5,5,5); 349 cp.add(buttonsPanel, gc); 257 350 if (placeContentInScrollPane) { 258 351 JScrollPane pane = new JScrollPane(cp); … … 326 419 327 420 getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) 328 .put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE");421 .put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE"); 329 422 getRootPane().getActionMap().put("ESCAPE", actionListener); 330 423 } … … 379 472 * dialog. Default is to not offer the choice; the dialog will be shown 380 473 * every time. 474 * Currently, this is not supported for non-modal dialogs. 381 475 * @param togglePref The preference to save the checkbox state to 382 476 */ 383 477 public ExtendedDialog toggleEnable(String togglePref) { 478 if (!modal) { 479 throw new IllegalArgumentException(); 480 } 384 481 this.toggleable = true; 385 482 this.togglePref = togglePref; … … 418 515 * Used in combination with toggle: 419 516 * If the user presses 'cancel' the toggle settings are ignored and not saved to the pref 420 * @param cancelButton index of the button that stands for cancel 421 */ 422 public ExtendedDialog setCancelButton(int cancelButtonIdx) { 423 this.cancelButtonIdx = cancelButtonIdx; 424 return this; 517 * @param cancelButton index of the button that stands for cancel, accepts 518 * multiple values 519 */ 520 public ExtendedDialog setCancelButton(Integer... cancelButtonIdx) { 521 this.cancelButtonIdx = Arrays.<Integer>asList(cancelButtonIdx); 522 return this; 523 } 524 525 /** 526 * Don't focus the "do not show this again" check box, but the default button. 527 */ 528 protected void fixFocus() { 529 if (toggleable && defaultButton != null) { 530 SwingUtilities.invokeLater(new Runnable() { 531 public void run() { 532 defaultButton.requestFocusInWindow(); 533 } 534 }); 535 } 425 536 } 426 537 … … 446 557 */ 447 558 private void toggleSaveState() { 448 if(!toggleable || toggleCheckbox == null || result == cancelButtonIdx || result == ExtendedDialog.DialogClosedOtherwise) 559 if (!toggleable || 560 toggleCheckbox == null || 561 cancelButtonIdx.contains(result) || 562 result == ExtendedDialog.DialogClosedOtherwise) 449 563 return; 450 564 Main.pref.put("message."+ togglePref, !toggleCheckbox.isSelected()); -
trunk/src/org/openstreetmap/josm/gui/io/UploadDialog.java
r3399 r3403 7 7 import java.awt.Dimension; 8 8 import java.awt.FlowLayout; 9 import java.awt.Image; 9 10 import java.awt.event.ActionEvent; 10 11 import java.awt.event.KeyEvent; … … 19 20 import javax.swing.AbstractAction; 20 21 import javax.swing.BorderFactory; 22 import javax.swing.Icon; 23 import javax.swing.ImageIcon; 21 24 import javax.swing.InputMap; 22 25 import javax.swing.JButton; … … 34 37 import org.openstreetmap.josm.data.osm.Changeset; 35 38 import org.openstreetmap.josm.data.osm.OsmPrimitive; 39 import org.openstreetmap.josm.gui.ExtendedDialog; 36 40 import org.openstreetmap.josm.gui.HelpAwareOptionPane; 37 41 import org.openstreetmap.josm.gui.SideButton; 38 42 import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction; 39 43 import org.openstreetmap.josm.gui.help.HelpUtil; 40 import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;41 44 import org.openstreetmap.josm.io.OsmApi; 42 45 import org.openstreetmap.josm.tools.ImageProvider; … … 364 367 */ 365 368 protected boolean warnUploadComment() { 366 367 ButtonSpec[] options = new ButtonSpec[] { 368 new ButtonSpec( 369 tr("Yes, revise"), 370 ImageProvider.get("ok"), 371 tr("Go back to the changeset comment and enter a better description"), 372 null 373 ), 374 new ButtonSpec( 375 tr("No, continue as is"), 376 ImageProvider.get("cancel"), 377 tr("Continue without improving the changeset comment"), 378 null 379 ) 380 }; 381 382 return 0 == HelpAwareOptionPane.showOptionDialog( 383 UploadDialog.this, 384 "<html>" + 385 tr("Your upload comment is empty, or very short.<br /><br />" + 369 ExtendedDialog dlg = new ExtendedDialog(UploadDialog.this, 370 tr("Please revise upload comment"), 371 new String[] {tr("Revise"), tr("Cancel"), tr("Continue as is")}); 372 dlg.setContent("<html>" + 373 tr("Your upload comment is <i>empty</i>, or <i>very short</i>.<br /><br />" + 386 374 "This is technically allowed, but please consider that many users who are<br />" + 387 375 "watching changes in their area depend on meaningful changeset comments<br />" + … … 389 377 "If you spend a minute now to explain your change, you will make life<br />" + 390 378 "easier for many other mappers.") + 391 "</html>", 392 tr("Please revise upload comment"), 393 JOptionPane.WARNING_MESSAGE, 394 null, 395 options, 396 options[0], 397 ht("/Dialog/UploadDialog#ReviseUploadComment") 398 ); 379 "</html>"); 380 dlg.setButtonIcons(new Icon[] { 381 ImageProvider.get("ok"), 382 ImageProvider.get("cancel"), 383 ImageProvider.overlay( 384 ImageProvider.get("upload"), 385 new ImageIcon(ImageProvider.get("warning-small").getImage().getScaledInstance(10 , 10, Image.SCALE_SMOOTH)), 386 ImageProvider.OverlayPosition.SOUTHEAST)}); 387 dlg.setToolTipTexts(new String[] { 388 tr("Return to the previous dialog to enter a more descriptive comment"), 389 tr("Cancel and return to the previous dialog"), 390 tr("Ignore this hint and upload anyway")}); 391 dlg.setIcon(JOptionPane.WARNING_MESSAGE); 392 dlg.toggleEnable("upload_comment_is_empty_or_very_short"); 393 dlg.setToggleCheckboxText(tr("Do not show this message again")); 394 dlg.setCancelButton(1, 2); 395 return dlg.showDialog().getValue() != 3; 399 396 } 400 397 -
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
r3389 r3403 10 10 import java.awt.GridBagLayout; 11 11 import java.awt.Image; 12 import java.awt.Insets; 12 13 import java.awt.event.ActionEvent; 13 14 import java.io.BufferedReader; … … 934 935 new String[] { tr("Apply Preset"), tr("Cancel") }, 935 936 true); 936 content Constraints = GBC.eol().fill().insets(5,10,5,0);937 contentInsets = new Insets(10,5,0,5); 937 938 setButtonIcons(new String[] {"ok.png", "cancel.png" }); 938 939 setContent(content); -
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r3365 r3403 273 273 */ 274 274 public static ImageIcon overlay(Icon ground, String overlayImage, OverlayPosition pos) { 275 return overlay(ground, ImageProvider.get(overlayImage), pos); 276 } 277 278 public static ImageIcon overlay(Icon ground, Icon overlay, OverlayPosition pos) { 275 279 GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() 276 280 .getDefaultConfiguration(); 277 281 int w = ground.getIconWidth(); 278 282 int h = ground.getIconHeight(); 279 ImageIcon overlay = ImageProvider.get(overlayImage);280 283 int wo = overlay.getIconWidth(); 281 284 int ho = overlay.getIconHeight();
Note:
See TracChangeset
for help on using the changeset viewer.