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

File:
1 moved

Legend:

Unmodified
Added
Removed
  • 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);
Note: See TracChangeset for help on using the changeset viewer.