jdk/src/share/classes/javax/swing/DefaultBoundedRangeModel.java
author yan
Mon, 19 May 2014 10:43:09 +0400
changeset 24500 399b77799d66
parent 22574 7f8ce0c8c20a
child 25201 4adc75e0c4e5
permissions -rw-r--r--
8040893: Add block tags for @return and @param to swing border classes Reviewed-by: malenkov, alexsch Contributed-by: Steven Sides <steve.sides@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.EventListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A generic implementation of BoundedRangeModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 5506
diff changeset
    40
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author David Kloba
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see BoundedRangeModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    48
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class DefaultBoundedRangeModel implements BoundedRangeModel, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Only one <code>ChangeEvent</code> is needed per model instance since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * event's only (read-only) state is the source property.  The source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * of events generated here is always "this".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected transient ChangeEvent changeEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** The listeners waiting for model changes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected EventListenerList listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private int extent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private int min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private int max = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private boolean isAdjusting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Initializes all of the properties with default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Those values are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <li><code>value</code> = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <li><code>extent</code> = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <li><code>minimum</code> = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <li><code>maximum</code> = 100
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <li><code>adjusting</code> = false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public DefaultBoundedRangeModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Initializes value, extent, minimum and maximum. Adjusting is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Throws an <code>IllegalArgumentException</code> if the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * constraints aren't satisfied:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * min &lt;= value &lt;= value+extent &lt;= max
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public DefaultBoundedRangeModel(int value, int extent, int min, int max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if ((max >= min) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            (value >= min) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            ((value + extent) >= value) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            ((value + extent) <= max)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            this.extent = extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            this.min = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            this.max = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new IllegalArgumentException("invalid range properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Returns the model's current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @return the model's current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see BoundedRangeModel#getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
      return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Returns the model's extent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @return the model's extent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @see #setExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see BoundedRangeModel#getExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public int getExtent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
      return extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Returns the model's minimum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return the model's minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @see BoundedRangeModel#getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public int getMinimum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
      return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Returns the model's maximum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return  the model's maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see BoundedRangeModel#getMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public int getMaximum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Sets the current value of the model. For a slider, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * determines where the knob appears. Ensures that the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * value, <I>n</I> falls within the model's constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @see BoundedRangeModel#setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void setValue(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        n = Math.min(n, Integer.MAX_VALUE - extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int newValue = Math.max(n, min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (newValue + extent > max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            newValue = max - extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        setRangeProperties(newValue, extent, min, max, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Sets the extent to <I>n</I> after ensuring that <I>n</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * is greater than or equal to zero and falls within the model's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @see BoundedRangeModel#setExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public void setExtent(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int newExtent = Math.max(0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if(value + newExtent > max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            newExtent = max - value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        setRangeProperties(value, newExtent, min, max, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Sets the minimum to <I>n</I> after ensuring that <I>n</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * that the other three properties obey the model's constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see #getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @see BoundedRangeModel#setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public void setMinimum(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int newMax = Math.max(n, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int newValue = Math.max(n, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        int newExtent = Math.min(newMax - newValue, extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        setRangeProperties(newValue, newExtent, n, newMax, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Sets the maximum to <I>n</I> after ensuring that <I>n</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * that the other three properties obey the model's constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @see BoundedRangeModel#setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public void setMaximum(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int newMin = Math.min(n, min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int newExtent = Math.min(n - newMin, extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        int newValue = Math.min(n - newExtent, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        setRangeProperties(newValue, newExtent, newMin, n, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Sets the <code>valueIsAdjusting</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @see #getValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @see BoundedRangeModel#setValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public void setValueIsAdjusting(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        setRangeProperties(value, extent, min, max, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Returns true if the value is in the process of changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * as a result of actions being taken by the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @return the value of the <code>valueIsAdjusting</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @see BoundedRangeModel#getValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public boolean getValueIsAdjusting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return isAdjusting;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Sets all of the <code>BoundedRangeModel</code> properties after forcing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * the arguments to obey the usual constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * At most, one <code>ChangeEvent</code> is generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @see BoundedRangeModel#setRangeProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @see #setExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @see #setValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public void setRangeProperties(int newValue, int newExtent, int newMin, int newMax, boolean adjusting)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (newMin > newMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            newMin = newMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (newValue > newMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            newMax = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (newValue < newMin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            newMin = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        /* Convert the addends to long so that extent can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         * Integer.MAX_VALUE without rolling over the sum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         * A JCK test covers this, see bug 4097718.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (((long)newExtent + (long)newValue) > newMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            newExtent = newMax - newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (newExtent < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            newExtent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        boolean isChange =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            (newValue != value) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            (newExtent != extent) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            (newMin != min) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            (newMax != max) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            (adjusting != isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (isChange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            value = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            extent = newExtent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            min = newMin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            max = newMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            isAdjusting = adjusting;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Adds a <code>ChangeListener</code>.  The change listeners are run each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * time any one of the Bounded Range model properties changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param l the ChangeListener to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @see #removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @see BoundedRangeModel#addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public void addChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        listenerList.add(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Removes a <code>ChangeListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param l the <code>ChangeListener</code> to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @see BoundedRangeModel#removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public void removeChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        listenerList.remove(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Returns an array of all the change listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * registered on this <code>DefaultBoundedRangeModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @return all of this model's <code>ChangeListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *         or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *         array if no change listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @see #removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public ChangeListener[] getChangeListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   347
        return listenerList.getListeners(ChangeListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Runs each <code>ChangeListener</code>'s <code>stateChanged</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @see #setRangeProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    protected void fireStateChanged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        for (int i = listeners.length - 2; i >= 0; i -=2 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (listeners[i] == ChangeListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                if (changeEvent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    changeEvent = new ChangeEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Returns a string that displays all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <code>BoundedRangeModel</code> properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public String toString()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        String modelString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            "value=" + getValue() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            "extent=" + getExtent() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            "min=" + getMinimum() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            "max=" + getMaximum() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            "adj=" + getValueIsAdjusting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return getClass().getName() + "[" + modelString + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Returns an array of all the objects currently registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * upon this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * are registered using the <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * For example, you can query a <code>DefaultBoundedRangeModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * instance <code>m</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * for its change listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * If no such listeners exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param listenerType  the type of listeners requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *          this parameter should specify an interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *          that descends from <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *          <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *          on this model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @exception ClassCastException if <code>listenerType</code> doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *          specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @see #getChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        return listenerList.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
}