Changeset 2031 in josm


Ignore:
Timestamp:
2009-09-03T01:00:52+02:00 (15 years ago)
Author:
Gubaer
Message:

applied #2905: patch by xeen: Add option to disable the new "Move elements" dialog)

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/UploadAction.java

    r2025 r2031  
    487487
    488488            while(true) {
    489                 int result = new ExtendedDialog(Main.parent,
     489                ExtendedDialog dialog = new ExtendedDialog(
     490                        Main.parent,
    490491                        tr("Upload these changes?"),
    491                         p,
    492                         new String[] {tr("Upload Changes"), tr("Cancel")},
    493                         new String[] {"upload.png", "cancel.png"}).getValue();
     492                        new String[] {tr("Upload Changes"), tr("Cancel")}
     493                );
     494                dialog.setButtonIcons(new String[] {"upload.png", "cancel.png"});
     495                dialog.setContent(p);
     496                dialog.showDialog();
     497                int result = dialog.getValue();
     498
    494499
    495500                // cancel pressed
  • trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java

    r2017 r2031  
    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();
  • trunk/src/org/openstreetmap/josm/gui/ExtendedDialog.java

    r2025 r2031  
    11package org.openstreetmap.josm.gui;
     2
     3import static org.openstreetmap.josm.tools.I18n.tr;
    24
    35import java.awt.Component;
     
    1214import javax.swing.Action;
    1315import javax.swing.JButton;
     16import javax.swing.JCheckBox;
    1417import javax.swing.JComponent;
    1518import javax.swing.JDialog;
     
    2023import javax.swing.KeyStroke;
    2124
     25import org.openstreetmap.josm.Main;
    2226import org.openstreetmap.josm.tools.GBC;
    2327import org.openstreetmap.josm.tools.ImageProvider;
     
    2630public class ExtendedDialog extends JDialog {
    2731    private int result = 0;
     32    public static final int DialogNotShown = -99;
     33    public static final int DialogClosedOtherwise = 0;
     34    private boolean toggleable = false;
     35    private String togglePref = "";
     36    private String toggleCheckboxText = tr("Do not show again");
     37    private JCheckBox toggleCheckbox = null;
    2838    private Component parent;
     39    private Component content;
    2940    private final String[] bTexts;
     41    private String[] bIcons;
    3042
    3143    // For easy access when inherited
    3244    protected Object contentConstraints = GBC.eol().anchor(GBC.CENTER).fill(GBC.HORIZONTAL).insets(5,10,5,0);
    3345    protected ArrayList<JButton> buttons = new ArrayList<JButton>();
     46
     47    /**
     48     * This method sets up the most basic options for the dialog. Add all more
     49     * advanced features with dedicated methods.
     50     * Possible features:
     51     * <ul>
     52     *   <li><code>setButtonIcons</code></li>
     53     *   <li><code>setContent</code></li>
     54     *   <li><code>toggleEnable</code></li>
     55     *   <li><code>toggleDisable</code></li>
     56     *   <li><code>setToggleCheckboxText</code></li>
     57     * </ul>
     58     *
     59     * When done, call <code>showDialog</code> to display it. You can receive
     60     * the user's choice using <code>getValue</code>. Have a look at this function
     61     * for possible return values.
     62     *
     63     * @param parent       The parent element that will be used for position and maximum size
     64     * @param title        The text that will be shown in the window titlebar
     65     * @param buttonTexts  String Array of the text that will appear on the buttons. The first button is the default one.
     66     */
     67    public ExtendedDialog(Component parent, String title, String[] buttonTexts) {
     68        super(JOptionPane.getFrameForComponent(parent), title, true);
     69        this.parent = parent;
     70        bTexts = buttonTexts;
     71    }
     72
     73    /**
     74     * Same as above but lets you define if the dialog should be modal.
     75     */
     76    public ExtendedDialog(Component parent, String title, String[] buttonTexts,
     77            boolean modal) {
     78        super(JOptionPane.getFrameForComponent(parent), title, modal);
     79        this.parent = parent;
     80        bTexts = buttonTexts;
     81    }
    3482
    3583    /**
     
    4189     * @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.
    4290     */
    43     public ExtendedDialog(Component parent, String title, Component content, String[] buttonTexts, String[] buttonIcons) {
     91    @Deprecated public ExtendedDialog(Component parent, String title, Component content,
     92            String[] buttonTexts, String[] buttonIcons) {
    4493        super(JOptionPane.getFrameForComponent(parent), title, true /* modal */);
    4594        this.parent = parent;
    4695        bTexts = buttonTexts;
    47         setupDialog(content, buttonIcons);
     96        this.content = content;
     97        this.bIcons = buttonIcons;
     98        setupDialog();
    4899        setVisible(true);
    49100    }
    50101
    51     public ExtendedDialog(Component parent, String title, Component content, String[] buttonTexts) {
     102    @Deprecated public ExtendedDialog(Component parent, String title, Component content,
     103            String[] buttonTexts) {
    52104        this(parent, title, content, buttonTexts, null);
    53105    }
     
    56108     * Sets up the dialog and displays the given message in a breakable label
    57109     */
    58     public ExtendedDialog(Component parent, String title, String message, String[] buttonTexts, String[] buttonIcons) {
    59         super(JOptionPane.getFrameForComponent(parent), title, true);
    60 
    61         JMultilineLabel lbl = new JMultilineLabel(message);
    62         // Make it not wider than 2/3 of the screen
    63         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    64         lbl.setMaxWidth(Math.round(screenSize.width*2/3));
    65 
    66         this.parent = parent;
    67         bTexts = buttonTexts;
    68         setupDialog(lbl, buttonIcons);
     110    @Deprecated public ExtendedDialog(Component parent, String title, String message,
     111            String[] buttonTexts, String[] buttonIcons) {
     112        this(parent, title, string2label(message), buttonTexts, buttonIcons);
     113    }
     114
     115    @Deprecated public ExtendedDialog(Component parent, String title, String message,
     116            String[] buttonTexts) {
     117        this(parent, title, message, buttonTexts, null);
     118    }
     119
     120    /**
     121     * Allows decorating the buttons with icons. Expects an String[] with paths
     122     * to images relative to JOSM/images.
     123     * @param buttonIcons
     124     */
     125    public void setButtonIcons(String[] buttonIcons) {
     126        this.bIcons = buttonIcons;
     127    }
     128
     129    /**
     130     * Sets the content that will be displayed in the message dialog.
     131     *
     132     * Note that depending on your other settings more UI elements may appear.
     133     * The content is played on top of the other elements though.
     134     *
     135     * @param content Any element that can be displayed in the message dialog
     136     */
     137    public void setContent(Component content) {
     138        this.content = content;
     139    }
     140
     141    /**
     142     * Sets the message that will be displayed. The String will be automatically
     143     * wrapped if it is too long.
     144     *
     145     * Note that depending on your other settings more UI elements may appear.
     146     * The content is played on top of the other elements though.
     147     *
     148     * @param message The text that should be shown to the user
     149     */
     150    public void setContent(String message) {
     151        setContent(string2label(message));
     152    }
     153
     154
     155
     156    /**
     157     * Show the dialog to the user. Call this after you have set all options
     158     * for the dialog. You can retrieve the result using <code>getValue</code>
     159     */
     160    public void showDialog() {
     161        // Check if the user has set the dialog to not be shown again
     162        if(toggleCheckState(togglePref)) {
     163            result = ExtendedDialog.DialogNotShown;
     164            return;
     165        }
     166
     167        setupDialog();
    69168        setVisible(true);
    70     }
    71 
    72     public ExtendedDialog(Component parent, String title, String message, String[] buttonTexts) {
    73         this(parent, title, message, buttonTexts, null);
    74     }
    75 
    76     /**
    77      * Constructor that doesn't make the dialog visible immediately. Intended for when inheriting.
    78      */
    79     public ExtendedDialog(Component parent, String title, String[] buttonTexts, boolean modal) {
    80         super(JOptionPane.getFrameForComponent(parent), title, modal);
    81         this.parent = parent;
    82         bTexts = buttonTexts;
    83     }
    84 
    85     protected void setupDialog(Component content, String[] buttonIcons) {
     169        toggleSaveState();
     170    }
     171
     172    /**
     173     * @return int * The selected button. The count starts with 1.
     174     *             * A return value of ExtendedDialog.DialogClosedOtherwise means the dialog has been closed otherwise.
     175     *             * A return value of ExtendedDialog.DialogNotShown means the
     176     *               dialog has been toggled off in the past
     177     */
     178    public int getValue() {
     179        return result;
     180    }
     181
     182    @Deprecated protected void setupDialog(Component content, String[] buttonIcons) {
     183        this.setContent(content);
     184        this.setButtonIcons(buttonIcons);
     185        this.setupDialog();
     186    }
     187
     188    protected void setupDialog() {
    86189        setupEscListener();
    87190
     
    97200
    98201            button = new JButton(action);
    99             if(buttonIcons != null && buttonIcons[i] != null) {
    100                 button.setIcon(ImageProvider.get(buttonIcons[i]));
     202            if(bIcons != null && bIcons[i] != null) {
     203                button.setIcon(ImageProvider.get(bIcons[i]));
    101204            }
    102205
     
    110213        JPanel cp = new JPanel(new GridBagLayout());
    111214        cp.add(content, contentConstraints);
     215
     216        if(toggleable) {
     217            toggleCheckbox = new JCheckBox(toggleCheckboxText);
     218            boolean showDialog = Main.pref.getBoolean("message."+ togglePref, true);
     219            toggleCheckbox.setSelected(!showDialog);
     220            cp.add(toggleCheckbox, GBC.eol().anchor(GBC.LINE_START).insets(5,5,5,5));
     221        }
     222
    112223        cp.add(buttonsPanel, GBC.eol().anchor(GBC.CENTER).insets(5,5,5,5));
    113224
     
    139250        setSize(d);
    140251        setLocationRelativeTo(parent);
    141     }
    142 
    143     /**
    144      * @return int The selected button. The count starts with 1.
    145      *             A return value of 0 means the dialog has been closed otherwise.
    146      */
    147     public int getValue() {
    148         return result;
    149252    }
    150253
     
    189292                // We need to set it to zero again, in case the dialog has been re-used
    190293                // and the result differs from its default value
    191                 result = 0;
     294                result = ExtendedDialog.DialogClosedOtherwise;
    192295                setVisible(false);
    193296            }
     
    206309        }
    207310    }
     311
     312    /**
     313     * Calling this will offer the user a "Do not show again" checkbox for the
     314     * dialog. Default is to not offer the choice; the dialog will be shown
     315     * every time. If the dialog is not shown due to the previous choice of the
     316     * user, the result <code>ExtendedDialog.DialogNotShown</code> is returned
     317     * @param togglePref  The preference to save the checkbox state to
     318     */
     319    public void toggleEnable(String togglePref) {
     320        this.toggleable = true;
     321        this.togglePref = togglePref;
     322    }
     323
     324    /**
     325     * Call this if you "accidentally" called toggleEnable. This doesn't need
     326     * to be called for every dialog, as it's the default anyway.
     327     */
     328    public void toggleDisable() {
     329        this.toggleable = false;
     330    }
     331
     332    /**
     333     * Overwrites the default "Don't show again" text of the toggle checkbox
     334     * if you want to give more information. Only has an effect if
     335     * <code>toggleEnable</code> is set.
     336     * @param text
     337     */
     338    public void setToggleCheckboxText(String text) {
     339        this.toggleCheckboxText = text;
     340    }
     341
     342    /**
     343     * This function returns true if the dialog has been set to "do not show again"
     344     * @return true if dialog should not be shown again
     345     */
     346    private boolean toggleCheckState(String togglePref) {
     347        toggleable = togglePref != null && !togglePref.equals("");
     348
     349        // No identifier given, so return false (= show the dialog)
     350        if(!toggleable)
     351            return false;
     352
     353        this.togglePref = togglePref;
     354        // The pref is true, if the dialog should be shown.
     355        return !(Main.pref.getBoolean("message."+ togglePref, true));
     356    }
     357
     358    /**
     359     * This function checks the state of the "Do not show again" checkbox and
     360     * writes the corresponding pref
     361     */
     362    private void toggleSaveState() {
     363        if(!toggleable || toggleCheckbox == null)
     364            return;
     365        Main.pref.put("message."+ togglePref, !toggleCheckbox.isSelected());
     366    }
     367
     368    /**
     369     * Convenience function that converts a given string into a JMultilineLabel
     370     * @param msg
     371     * @return JMultilineLabel
     372     */
     373    private static JMultilineLabel string2label(String msg) {
     374        JMultilineLabel lbl = new JMultilineLabel(msg);
     375        // Make it not wider than 2/3 of the screen
     376        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
     377        lbl.setMaxWidth(Math.round(screenSize.width*2/3));
     378        return lbl;
     379    }
    208380}
Note: See TracChangeset for help on using the changeset viewer.