Changeset 3403 in josm


Ignore:
Timestamp:
2010-08-02T09:47:56+02:00 (14 years ago)
Author:
bastiK
Message:

some extensions to exteded dialog; modified the warning dialog box for too short changeset comments (from [3399])

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

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

    r3385 r3403  
    55import java.awt.Component;
    66import java.awt.Dimension;
     7import java.awt.GridBagConstraints;
    78import java.awt.GridBagLayout;
     9import java.awt.Insets;
    810import java.awt.Toolkit;
    911import java.awt.event.ActionEvent;
    1012import java.util.ArrayList;
     13import java.util.Arrays;
     14import java.util.Collections;
     15import java.util.List;
    1116
    1217import javax.swing.AbstractAction;
    1318import javax.swing.Action;
     19import javax.swing.Icon;
    1420import javax.swing.JButton;
    1521import javax.swing.JCheckBox;
    1622import javax.swing.JComponent;
    1723import javax.swing.JDialog;
     24import javax.swing.JLabel;
    1825import javax.swing.JOptionPane;
    1926import javax.swing.JPanel;
     
    2128import javax.swing.JScrollPane;
    2229import javax.swing.KeyStroke;
     30import javax.swing.SwingUtilities;
     31import javax.swing.UIManager;
    2332
    2433import org.openstreetmap.josm.Main;
     
    2938import org.openstreetmap.josm.tools.WindowGeometry;
    3039
     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 */
    3155public class ExtendedDialog extends JDialog {
    3256    private final boolean disposeOnClose;
     
    4468    private final String[] bTexts;
    4569    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;
    4973    private JButton defaultButton = null;
     74    private Icon icon;
     75    private boolean modal;
    5076
    5177    /** true, if the dialog should include a help button */
     
    6187
    6288    // 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);
    6490    protected ArrayList<JButton> buttons = new ArrayList<JButton>();
    6591
     
    101127        super(JOptionPane.getFrameForComponent(parent), title, modal);
    102128        this.parent = parent;
     129        this.modal = modal;
    103130        bTexts = buttonTexts;
    104131        if (disposeOnClose) {
     
    109136
    110137    /**
    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.
    113139     * @param buttonIcons
    114140     */
     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     */
    115149    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        }
    117154        return this;
    118155    }
     
    171208
    172209    /**
     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    /**
    173239     * Show the dialog to the user. Call this after you have set all options
    174240     * for the dialog. You can retrieve the result using <code>getValue</code>
     
    176242    public ExtendedDialog showDialog() {
    177243        // Check if the user has set the dialog to not be shown again
    178         if(toggleCheckState(togglePref)) {
     244        if (toggleCheckState(togglePref)) {
    179245            result = toggleValue;
    180246            return this;
     
    185251            getRootPane().setDefaultButton(defaultButton);
    186252        }
     253        fixFocus();
    187254        setVisible(true);
    188255        toggleSaveState();
     
    227294            }
    228295            if(bIcons != null && bIcons[i] != null) {
    229                 button.setIcon(ImageProvider.get(bIcons[i]));
     296                button.setIcon(bIcons[i]);
    230297            }
    231298            if (bToolTipTexts != null && i < bToolTipTexts.length && bToolTipTexts[i] != null) {
     
    233300            }
    234301
    235             if(i == 0) {
    236                 rootPane.setDefaultButton(button);
    237             }
    238302            buttonsPanel.add(button, GBC.std().insets(2,2,2,2));
    239303            buttons.add(button);
     
    245309
    246310        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) {
    250336            toggleCheckbox = new JCheckBox(toggleCheckboxText);
    251337            boolean showDialog = Main.pref.getBoolean("message."+ togglePref, true);
    252338            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);
    257350        if (placeContentInScrollPane) {
    258351            JScrollPane pane = new JScrollPane(cp);
     
    326419
    327420        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
    328         .put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE");
     421            .put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE");
    329422        getRootPane().getActionMap().put("ESCAPE", actionListener);
    330423    }
     
    379472     * dialog. Default is to not offer the choice; the dialog will be shown
    380473     * every time.
     474     * Currently, this is not supported for non-modal dialogs.
    381475     * @param togglePref  The preference to save the checkbox state to
    382476     */
    383477    public ExtendedDialog toggleEnable(String togglePref) {
     478        if (!modal) {
     479            throw new IllegalArgumentException();
     480        }
    384481        this.toggleable = true;
    385482        this.togglePref = togglePref;
     
    418515     * Used in combination with toggle:
    419516     * 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        }
    425536    }
    426537
     
    446557     */
    447558    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)
    449563            return;
    450564        Main.pref.put("message."+ togglePref, !toggleCheckbox.isSelected());
  • trunk/src/org/openstreetmap/josm/gui/io/UploadDialog.java

    r3399 r3403  
    77import java.awt.Dimension;
    88import java.awt.FlowLayout;
     9import java.awt.Image;
    910import java.awt.event.ActionEvent;
    1011import java.awt.event.KeyEvent;
     
    1920import javax.swing.AbstractAction;
    2021import javax.swing.BorderFactory;
     22import javax.swing.Icon;
     23import javax.swing.ImageIcon;
    2124import javax.swing.InputMap;
    2225import javax.swing.JButton;
     
    3437import org.openstreetmap.josm.data.osm.Changeset;
    3538import org.openstreetmap.josm.data.osm.OsmPrimitive;
     39import org.openstreetmap.josm.gui.ExtendedDialog;
    3640import org.openstreetmap.josm.gui.HelpAwareOptionPane;
    3741import org.openstreetmap.josm.gui.SideButton;
    3842import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
    3943import org.openstreetmap.josm.gui.help.HelpUtil;
    40 import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
    4144import org.openstreetmap.josm.io.OsmApi;
    4245import org.openstreetmap.josm.tools.ImageProvider;
     
    364367         */
    365368        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 />" +
    386374                       "This is technically allowed, but please consider that many users who are<br />" +
    387375                       "watching changes in their area depend on meaningful changeset comments<br />" +
     
    389377                       "If you spend a minute now to explain your change, you will make life<br />" +
    390378                       "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;
    399396        }
    400397
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r3389 r3403  
    1010import java.awt.GridBagLayout;
    1111import java.awt.Image;
     12import java.awt.Insets;
    1213import java.awt.event.ActionEvent;
    1314import java.io.BufferedReader;
     
    934935                            new String[] { tr("Apply Preset"), tr("Cancel") },
    935936                            true);
    936                     contentConstraints = GBC.eol().fill().insets(5,10,5,0);
     937                    contentInsets = new Insets(10,5,0,5);
    937938                    setButtonIcons(new String[] {"ok.png", "cancel.png" });
    938939                    setContent(content);
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r3365 r3403  
    273273     */
    274274    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) {
    275279        GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
    276280        .getDefaultConfiguration();
    277281        int w = ground.getIconWidth();
    278282        int h = ground.getIconHeight();
    279         ImageIcon overlay = ImageProvider.get(overlayImage);
    280283        int wo = overlay.getIconWidth();
    281284        int ho = overlay.getIconHeight();
Note: See TracChangeset for help on using the changeset viewer.