Ticket #2905: ExtendedDialog-dontShowAgain.patch

File ExtendedDialog-dontShowAgain.patch, 14.6 KB (added by xeen, 16 years ago)
  • src/org/openstreetmap/josm/actions/mapmode/SelectAction.java

     
    464464                    }
    465465                    if(s.size() > max)
    466466                    {
    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)
    473480                        {
    474481                            Main.main.undoRedo.undo();
    475482                        }
  • src/org/openstreetmap/josm/gui/ExtendedDialog.java

     
    11package org.openstreetmap.josm.gui;
    22
     3import static org.openstreetmap.josm.tools.I18n.tr;
     4
    35import java.awt.Component;
    46import java.awt.Dimension;
    57import java.awt.GridBagLayout;
     
    1012import javax.swing.AbstractAction;
    1113import javax.swing.Action;
    1214import javax.swing.JButton;
     15import javax.swing.JCheckBox;
    1316import javax.swing.JComponent;
    1417import javax.swing.JDialog;
    1518import javax.swing.JOptionPane;
     
    1821import javax.swing.JScrollPane;
    1922import javax.swing.KeyStroke;
    2023
     24import org.openstreetmap.josm.Main;
    2125import org.openstreetmap.josm.tools.GBC;
    2226import org.openstreetmap.josm.tools.ImageProvider;
    2327
    2428
    2529public class ExtendedDialog extends JDialog {
    2630    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;
    2737    private Component parent;
     38    private Component content;
    2839    private final String[] bTexts;
     40    private String[] bIcons;
    2941
    3042    // For easy access when inherited
    3143    protected Object contentConstraints = GBC.eol().anchor(GBC.CENTER).fill(GBC.HORIZONTAL).insets(5,10,5,0);
    3244    protected ArrayList<JButton> buttons = new ArrayList<JButton>();
    3345
    3446    /**
     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    /**
    3583     * Sets up the dialog. The first button is always the default.
    3684     * @param parent The parent element that will be used for position and maximum size
    3785     * @param title The text that will be shown in the window titlebar
     
    3987     * @param buttonTexts The labels that will be displayed on the buttons
    4088     * @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.
    4189     */
    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) {
    4392        super(JOptionPane.getFrameForComponent(parent), title, true /* modal */);
    4493        this.parent = parent;
    4594        bTexts = buttonTexts;
    46         setupDialog(content, buttonIcons);
     95        this.content = content;
     96        this.bIcons = buttonIcons;
     97        setupDialog();
    4798        setVisible(true);
    4899    }
    49100
    50     public ExtendedDialog(Component parent, String title, Component content, String[] buttonTexts) {
     101    @Deprecated public ExtendedDialog(Component parent, String title, Component content,
     102            String[] buttonTexts) {
    51103        this(parent, title, content, buttonTexts, null);
    52104    }
    53105
    54106    /**
    55107     * Sets up the dialog and displays the given message in a breakable label
    56108     */
    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    }
    59113
    60         JMultilineLabel lbl = new JMultilineLabel(message);
    61         // Make it not wider than 2/3 of the screen
    62         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    }
    64118
    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;
    69126    }
    70127
    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;
    73138    }
    74139
    75140    /**
    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
    77148     */
    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;
    82179    }
    83180
    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() {
    85188        setupEscListener();
    86189
    87190        JButton button;
     
    95198            };
    96199
    97200            button = new JButton(action);
    98             if(buttonIcons != null && buttonIcons[i] != null) {
    99                 button.setIcon(ImageProvider.get(buttonIcons[i]));
     201            if(bIcons != null && bIcons[i] != null) {
     202                button.setIcon(ImageProvider.get(bIcons[i]));
    100203            }
    101204
    102205            if(i == 0) {
     
    108211
    109212        JPanel cp = new JPanel(new GridBagLayout());
    110213        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
    111222        cp.add(buttonsPanel, GBC.eol().anchor(GBC.CENTER).insets(5,5,5,5));
    112223
    113224        JScrollPane pane = new JScrollPane(cp);
     
    140251    }
    141252
    142253    /**
    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     /**
    151254     * This gets performed whenever a button is clicked or activated
    152255     * @param evt the button event
    153256     */
     
    187290                // 0 means that the dialog has been closed otherwise.
    188291                // We need to set it to zero again, in case the dialog has been re-used
    189292                // and the result differs from its default value
    190                 result = 0;
     293                result = ExtendedDialog.DialogClosedOtherwise;
    191294                setVisible(false);
    192295            }
    193296        };
     
    204307            toFront();
    205308        }
    206309    }
     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    }
    207379}
     380 No newline at end of file