Changeset 6221 in josm for trunk


Ignore:
Timestamp:
2013-09-07T17:52:27+02:00 (11 years ago)
Author:
Don-vip
Message:

Make some defensive copies of user-supplied arrays, move QuadStateCheckBox to widgets package, javadoc

Location:
trunk/src/org/openstreetmap/josm
Files:
5 edited
1 moved

Legend:

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

    r6087 r6221  
    3737import org.openstreetmap.josm.tools.GBC;
    3838import org.openstreetmap.josm.tools.ImageProvider;
     39import org.openstreetmap.josm.tools.Utils;
    3940import org.openstreetmap.josm.tools.WindowGeometry;
    4041
     
    132133    /**
    133134     * Same as above but lets you define if the dialog should be modal.
    134      */
    135     public ExtendedDialog(Component parent, String title, String[] buttonTexts,
    136             boolean modal) {
     135     * @param parent The parent element that will be used for position and maximum size
     136     * @param title The text that will be shown in the window titlebar
     137     * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one.
     138     * @param modal Set it to {@code true} if you want the dialog to be modal
     139     */
     140    public ExtendedDialog(Component parent, String title, String[] buttonTexts, boolean modal) {
    137141        this(parent, title, buttonTexts, modal, true);
    138142    }
    139143
    140     public ExtendedDialog(Component parent, String title, String[] buttonTexts,
    141             boolean modal, boolean disposeOnClose) {
     144    public ExtendedDialog(Component parent, String title, String[] buttonTexts, boolean modal, boolean disposeOnClose) {
    142145        super(JOptionPane.getFrameForComponent(parent), title, modal ? ModalityType.DOCUMENT_MODAL : ModalityType.MODELESS);
    143146        this.parent = parent;
    144147        this.modal = modal;
    145         bTexts = buttonTexts;
     148        bTexts = Utils.copyArray(buttonTexts);
    146149        if (disposeOnClose) {
    147150            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
     
    152155    /**
    153156     * Allows decorating the buttons with icons.
    154      * @param buttonIcons
     157     * @param buttonIcons The button icons
     158     * @return {@code this}
    155159     */
    156160    public ExtendedDialog setButtonIcons(Icon[] buttonIcons) {
    157         this.bIcons = buttonIcons;
     161        this.bIcons = Utils.copyArray(buttonIcons);
    158162        return this;
    159163    }
     
    161165    /**
    162166     * Convenience method to provide image names instead of images.
     167     * @param buttonIcons The button icon names
     168     * @return {@code this}
    163169     */
    164170    public ExtendedDialog setButtonIcons(String[] buttonIcons) {
     
    175181     *
    176182     * @param toolTipTexts the tool tip texts. Ignored, if null.
     183     * @return {@code this}
    177184     */
    178185    public ExtendedDialog setToolTipTexts(String[] toolTipTexts) {
    179         this.bToolTipTexts = toolTipTexts;
     186        this.bToolTipTexts = Utils.copyArray(toolTipTexts);
    180187        return this;
    181188    }
     
    188195     *
    189196     * @param content Any element that can be displayed in the message dialog
     197     * @return {@code this}
    190198     */
    191199    public ExtendedDialog setContent(Component content) {
     
    201209     * @param content Any element that can be displayed in the message dialog
    202210     * @param placeContentInScrollPane if  true, places  the content in a JScrollPane
    203      *
     211     * @return {@code this}
    204212     */
    205213    public ExtendedDialog setContent(Component content, boolean placeContentInScrollPane) {
     
    217225     *
    218226     * @param message The text that should be shown to the user
     227     * @return {@code this}
    219228     */
    220229    public ExtendedDialog setContent(String message) {
     
    225234     * Decorate the dialog with an icon that is shown on the left part of
    226235     * the window area. (Similar to how it is done in {@link JOptionPane})
     236     * @param icon The icon to display
     237     * @return {@code this}
    227238     */
    228239    public ExtendedDialog setIcon(Icon icon) {
     
    233244    /**
    234245     * Convenience method to allow values that would be accepted by {@link JOptionPane} as messageType.
     246     * @param messageType The {@link JOptionPane} messageType
     247     * @return {@code this}
    235248     */
    236249    public ExtendedDialog setIcon(int messageType) {
     
    254267     * Show the dialog to the user. Call this after you have set all options
    255268     * for the dialog. You can retrieve the result using {@link #getValue()}.
     269     * @return {@code this}
    256270     */
    257271    public ExtendedDialog showDialog() {
     
    493507     *              existing preference is found (only takes effect if
    494508     *              <code>pref</code> is not null or empty
    495      *
     509     * @return {@code this}
    496510     */
    497511    public ExtendedDialog setRememberWindowGeometry(String pref, WindowGeometry wg) {
     
    507521     * Currently, this is not supported for non-modal dialogs.
    508522     * @param togglePref  The preference to save the checkbox state to
     523     * @return {@code this}
    509524     */
    510525    public ExtendedDialog toggleEnable(String togglePref) {
     
    520535     * Call this if you "accidentally" called toggleEnable. This doesn't need
    521536     * to be called for every dialog, as it's the default anyway.
     537     * @return {@code this}
    522538     */
    523539    public ExtendedDialog toggleDisable() {
     
    530546     * if you want to give more information. Only has an effect if
    531547     * <code>toggleEnable</code> is set.
    532      * @param text
     548     * @param text The toggle checkbox text
     549     * @return {@code this}
    533550     */
    534551    public ExtendedDialog setToggleCheckboxText(String text) {
     
    539556    /**
    540557     * Sets the button that will react to ENTER.
     558     * @param defaultButtonIdx The button index (starts to )
     559     * @return {@code this}
    541560     */
    542561    public ExtendedDialog setDefaultButton(int defaultButtonIdx) {
     
    549568     * If the user presses 'cancel' the toggle settings are ignored and not saved to the pref
    550569     * @param cancelButtonIdx index of the button that stands for cancel, accepts multiple values
     570     * @return {@code this}
    551571     */
    552572    public ExtendedDialog setCancelButton(Integer... cancelButtonIdx) {
     
    623643     * @param helpTopic the help topic
    624644     * @param showHelpButton true, if the dialog displays a help button
     645     * @return {@code this}
    625646     */
    626647    public ExtendedDialog configureContextsensitiveHelp(String helpTopic, boolean showHelpButton) {
  • trunk/src/org/openstreetmap/josm/gui/FileDrop.java

    r6104 r6221  
    2424import java.util.ArrayList;
    2525import java.util.Arrays;
    26 import java.util.EventObject;
    2726import java.util.List;
    2827import java.util.TooManyListenersException;
     
    474473
    475474    /**
    476      * This is the event that is passed to the
    477      * {@link FileDrop.Listener#filesDropped filesDropped(...)} method in
    478      * your {@link FileDrop.Listener} when files are dropped onto
    479      * a registered drop target.
    480      *
    481      * <p>I'm releasing this code into the Public Domain. Enjoy.</p>
    482      *
    483      * @author  Robert Harder
    484      * @author  rob@iharder.net
    485      * @version 1.2
    486      */
    487     public static class Event extends EventObject {
    488 
    489         private File[] files;
    490 
    491         /**
    492          * Constructs an {@link Event} with the array
    493          * of files that were dropped and the
    494          * {@link FileDrop} that initiated the event.
    495          *
    496          * @param files The array of files that were dropped
    497          * @param source The event source
    498          */
    499         public Event( File[] files, Object source ) {
    500             super( source );
    501             this.files = files;
    502         }   // end constructor
    503 
    504         /**
    505          * Returns an array of files that were dropped on a
    506          * registered drop target.
    507          *
    508          * @return array of files that were dropped
    509          */
    510         public File[] getFiles() {
    511             return files;
    512         }   // end getFiles
    513 
    514     }   // end inner class Event
    515 
    516     /* ********  I N N E R   C L A S S  ******** */
    517 
    518     /**
    519475     * At last an easy way to encapsulate your custom objects for dragging and dropping
    520476     * in your Java programs!
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r6143 r6221  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55import static org.openstreetmap.josm.tools.I18n.trn;
     6import gnu.getopt.Getopt;
     7import gnu.getopt.LongOpt;
    68
    79import java.awt.Image;
     
    2830import javax.swing.RepaintManager;
    2931import javax.swing.SwingUtilities;
    30 
    31 import gnu.getopt.Getopt;
    32 import gnu.getopt.LongOpt;
    3332
    3433import org.jdesktop.swinghelper.debug.CheckThreadViolationRepaintManager;
     
    301300        Main.platform.preStartupHook();
    302301
    303         Main.commandLineArgs = argArray;
     302        Main.commandLineArgs = Utils.copyArray(argArray);
    304303       
    305304        if (args.containsKey(Option.VERSION)) {
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetItems.java

    r6198 r6221  
    4848import org.openstreetmap.josm.data.osm.Tag;
    4949import org.openstreetmap.josm.data.preferences.BooleanProperty;
    50 import org.openstreetmap.josm.gui.QuadStateCheckBox;
    5150import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
    5251import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPritority;
     
    5453import org.openstreetmap.josm.gui.widgets.JosmComboBox;
    5554import org.openstreetmap.josm.gui.widgets.JosmTextField;
     55import org.openstreetmap.josm.gui.widgets.QuadStateCheckBox;
    5656import org.openstreetmap.josm.tools.GBC;
    5757import org.openstreetmap.josm.tools.ImageProvider;
  • trunk/src/org/openstreetmap/josm/gui/widgets/QuadStateCheckBox.java

    r6219 r6221  
    11// License: GPL. Copyright 2008 by Frederik Ramm and others
    2 package org.openstreetmap.josm.gui;
     2package org.openstreetmap.josm.gui.widgets;
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
     
    2121import javax.swing.plaf.ActionMapUIResource;
    2222
     23import org.openstreetmap.josm.tools.Utils;
     24
     25/**
     26 * A four-state checkbox. The states are enumerated in {@link State}.
     27 * @since 591
     28 */
    2329public class QuadStateCheckBox extends JCheckBox {
    2430
    25     public enum State { NOT_SELECTED, SELECTED, UNSET, PARTIAL }
     31    /**
     32     * The 4 possible states of this checkbox.
     33     */
     34    public enum State {
     35        /** Not selected: the property is explicitly switched off */
     36        NOT_SELECTED,
     37        /** Selected: the property is explicitly switched on */
     38        SELECTED,
     39        /** Unset: do not set this property on the selected objects */
     40        UNSET,
     41        /** Partial: different selected objects have different values, do not change */
     42        PARTIAL
     43    }
    2644
    2745    private final QuadStateDecorator model;
    2846    private State[] allowed;
    2947
     48    /**
     49     * Constructs a new {@code QuadStateCheckBox}.
     50     * @param text the text of the check box
     51     * @param icon the Icon image to display
     52     * @param initial The initial state
     53     * @param allowed The allowed states
     54     */
    3055    public QuadStateCheckBox(String text, Icon icon, State initial, State[] allowed) {
    3156        super(text, icon);
    32         this.allowed = allowed;
     57        this.allowed = Utils.copyArray(allowed);
    3358        // Add a listener for when the mouse is pressed
    3459        super.addMouseListener(new MouseAdapter() {
     
    5479        setState(initial);
    5580    }
     81   
     82    /**
     83     * Constructs a new {@code QuadStateCheckBox}.
     84     * @param text the text of the check box
     85     * @param initial The initial state
     86     * @param allowed The allowed states
     87     */
    5688    public QuadStateCheckBox(String text, State initial, State[] allowed) {
    5789        this(text, null, initial, allowed);
     
    6092    /** Do not let anyone add mouse listeners */
    6193    @Override public void addMouseListener(MouseListener l) { }
     94   
    6295    /**
    6396     * Set the new state.
    64      */
    65     public void setState(State state) { model.setState(state); }
    66     /** Return the current state, which is determined by the
    67      * selection status of the model. */
    68     public State getState() { return model.getState(); }
    69     @Override public void setSelected(boolean b) {
     97     * @param state The new state
     98     */
     99    public void setState(State state) {
     100        model.setState(state);
     101    }
     102   
     103    /**
     104     * Return the current state, which is determined by the selection status of the model.
     105     * @return The current state
     106     */
     107    public State getState() {
     108        return model.getState();
     109    }
     110   
     111    @Override
     112    public void setSelected(boolean b) {
    70113        if (b) {
    71114            setState(State.SELECTED);
     
    77120    private class QuadStateDecorator implements ButtonModel {
    78121        private final ButtonModel other;
     122       
    79123        private QuadStateDecorator(ButtonModel other) {
    80124            this.other = other;
    81125        }
     126       
    82127        private void setState(State state) {
    83128            if (state == State.NOT_SELECTED) {
     
    103148            }
    104149        }
     150       
    105151        /**
    106152         * The current state is embedded in the selection / armed
     
    139185        @Override public void setSelected(boolean b) { }
    140186        @Override public void setPressed(boolean b) { }
    141         /** We disable focusing on the component when it is not
    142          * enabled. */
     187        /** We disable focusing on the component when it is not enabled. */
    143188        @Override public void setEnabled(boolean b) {
    144189            setFocusable(b);
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r6175 r6221  
    3030import java.util.AbstractList;
    3131import java.util.ArrayList;
     32import java.util.Arrays;
    3233import java.util.Collection;
    3334import java.util.Iterator;
     
    241242    }
    242243
     244    /**
     245     * Copies the given array. Unlike {@link Arrays#copyOf}, this method is null-safe.
     246     * @param array The array to copy
     247     * @return A copy of the original array, or {@code null} if {@code array} is null
     248     * @since 6221
     249     */
     250    public static <T> T[] copyArray(T[] array) {
     251        if (array != null) {
     252            return Arrays.copyOf(array, array.length);
     253        }
     254        return null;
     255    }
     256   
    243257    /**
    244258     * Simple file copy function that will overwrite the target file.<br/>
Note: See TracChangeset for help on using the changeset viewer.