jdk/src/java.desktop/share/classes/javax/swing/DefaultBoundedRangeModel.java
author aghaisas
Mon, 10 Jul 2017 14:55:29 +0530
changeset 47151 362dcbee0613
parent 25859 3317bb8137f4
permissions -rw-r--r--
6919529: NPE from MultiUIDefaults.getUIError Reviewed-by: aghaisas, psadhukhan, serb Contributed-by: shashidhara.veerabhadraiah@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
25201
4adc75e0c4e5 8046485: Add missing @since tag under javax.swing.*
henryjen
parents: 22574
diff changeset
    47
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    49
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class DefaultBoundedRangeModel implements BoundedRangeModel, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Only one <code>ChangeEvent</code> is needed per model instance since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * event's only (read-only) state is the source property.  The source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * of events generated here is always "this".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected transient ChangeEvent changeEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** The listeners waiting for model changes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected EventListenerList listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private int extent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private int min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private int max = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean isAdjusting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Initializes all of the properties with default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Those values are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <li><code>value</code> = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <li><code>extent</code> = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <li><code>minimum</code> = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <li><code>maximum</code> = 100
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <li><code>adjusting</code> = false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public DefaultBoundedRangeModel() {
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Initializes value, extent, minimum and maximum. Adjusting is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Throws an <code>IllegalArgumentException</code> if the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * constraints aren't satisfied:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * min &lt;= value &lt;= value+extent &lt;= max
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * </pre>
25760
7955db32d6b0 8046597: fix doclint issues in swing classes, part 4 of 4
ssides
parents: 25201
diff changeset
    91
     *
7955db32d6b0 8046597: fix doclint issues in swing classes, part 4 of 4
ssides
parents: 25201
diff changeset
    92
     * @param value  an int giving the current value
7955db32d6b0 8046597: fix doclint issues in swing classes, part 4 of 4
ssides
parents: 25201
diff changeset
    93
     * @param extent the length of the inner range that begins at the model's value
7955db32d6b0 8046597: fix doclint issues in swing classes, part 4 of 4
ssides
parents: 25201
diff changeset
    94
     * @param min    an int giving the minimum value
7955db32d6b0 8046597: fix doclint issues in swing classes, part 4 of 4
ssides
parents: 25201
diff changeset
    95
     * @param max    an int giving the maximum value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public DefaultBoundedRangeModel(int value, int extent, int min, int max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if ((max >= min) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            (value >= min) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            ((value + extent) >= value) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            ((value + extent) <= max)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            this.extent = extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            this.min = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            this.max = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new IllegalArgumentException("invalid range properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Returns the model's current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @return the model's current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @see BoundedRangeModel#getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
      return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Returns the model's extent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return the model's extent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @see #setExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @see BoundedRangeModel#getExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public int getExtent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
      return extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Returns the model's minimum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @return the model's minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @see BoundedRangeModel#getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public int getMinimum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
      return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns the model's maximum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return  the model's maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see BoundedRangeModel#getMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public int getMaximum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Sets the current value of the model. For a slider, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * determines where the knob appears. Ensures that the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * value, <I>n</I> falls within the model's constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see BoundedRangeModel#setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public void setValue(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        n = Math.min(n, Integer.MAX_VALUE - extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int newValue = Math.max(n, min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (newValue + extent > max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            newValue = max - extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        setRangeProperties(newValue, extent, min, max, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Sets the extent to <I>n</I> after ensuring that <I>n</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * is greater than or equal to zero and falls within the model's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @see BoundedRangeModel#setExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public void setExtent(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int newExtent = Math.max(0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if(value + newExtent > max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            newExtent = max - value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        setRangeProperties(value, newExtent, min, max, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Sets the minimum to <I>n</I> after ensuring that <I>n</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * that the other three properties obey the model's constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @see #getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @see BoundedRangeModel#setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public void setMinimum(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        int newMax = Math.max(n, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int newValue = Math.max(n, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int newExtent = Math.min(newMax - newValue, extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        setRangeProperties(newValue, newExtent, n, newMax, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Sets the maximum to <I>n</I> after ensuring that <I>n</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * that the other three properties obey the model's constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @see BoundedRangeModel#setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public void setMaximum(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int newMin = Math.min(n, min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int newExtent = Math.min(n - newMin, extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int newValue = Math.min(n - newExtent, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        setRangeProperties(newValue, newExtent, newMin, n, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Sets the <code>valueIsAdjusting</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @see #getValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @see BoundedRangeModel#setValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public void setValueIsAdjusting(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        setRangeProperties(value, extent, min, max, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns true if the value is in the process of changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * as a result of actions being taken by the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return the value of the <code>valueIsAdjusting</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @see BoundedRangeModel#getValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public boolean getValueIsAdjusting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return isAdjusting;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Sets all of the <code>BoundedRangeModel</code> properties after forcing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * the arguments to obey the usual constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * At most, one <code>ChangeEvent</code> is generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @see BoundedRangeModel#setRangeProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @see #setExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @see #setValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public void setRangeProperties(int newValue, int newExtent, int newMin, int newMax, boolean adjusting)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (newMin > newMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            newMin = newMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (newValue > newMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            newMax = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (newValue < newMin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            newMin = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        /* Convert the addends to long so that extent can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         * Integer.MAX_VALUE without rolling over the sum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         * A JCK test covers this, see bug 4097718.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (((long)newExtent + (long)newValue) > newMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            newExtent = newMax - newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (newExtent < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            newExtent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        boolean isChange =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            (newValue != value) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            (newExtent != extent) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            (newMin != min) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            (newMax != max) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            (adjusting != isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (isChange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            value = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            extent = newExtent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            min = newMin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            max = newMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            isAdjusting = adjusting;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Adds a <code>ChangeListener</code>.  The change listeners are run each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * time any one of the Bounded Range model properties changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param l the ChangeListener to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @see #removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @see BoundedRangeModel#addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public void addChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        listenerList.add(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Removes a <code>ChangeListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @param l the <code>ChangeListener</code> to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @see BoundedRangeModel#removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public void removeChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        listenerList.remove(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Returns an array of all the change listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * registered on this <code>DefaultBoundedRangeModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return all of this model's <code>ChangeListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *         or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *         array if no change listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @see #removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public ChangeListener[] getChangeListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   353
        return listenerList.getListeners(ChangeListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Runs each <code>ChangeListener</code>'s <code>stateChanged</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @see #setRangeProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    protected void fireStateChanged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        for (int i = listeners.length - 2; i >= 0; i -=2 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (listeners[i] == ChangeListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                if (changeEvent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    changeEvent = new ChangeEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Returns a string that displays all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <code>BoundedRangeModel</code> properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public String toString()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        String modelString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            "value=" + getValue() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            "extent=" + getExtent() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            "min=" + getMinimum() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            "max=" + getMaximum() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            "adj=" + getValueIsAdjusting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return getClass().getName() + "[" + modelString + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Returns an array of all the objects currently registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * upon this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * are registered using the <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * For example, you can query a <code>DefaultBoundedRangeModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * instance <code>m</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * for its change listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * If no such listeners exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
25760
7955db32d6b0 8046597: fix doclint issues in swing classes, part 4 of 4
ssides
parents: 25201
diff changeset
   411
     * @param <T> the type of {@code EventListener} class being requested
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @param listenerType  the type of listeners requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *          this parameter should specify an interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *          that descends from <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *          <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *          on this model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @exception ClassCastException if <code>listenerType</code> doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *          specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @see #getChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return listenerList.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
}