jdk/src/share/classes/javax/swing/DefaultBoundedRangeModel.java
author ant
Thu, 17 May 2012 21:48:57 +0400
changeset 12661 6cf8b7116579
parent 5506 202f599c92aa
child 20458 f2423fb3fd19
permissions -rw-r--r--
7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application. Summary: forward port from 7u4 Reviewed-by: art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 1997, 2008, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class DefaultBoundedRangeModel implements BoundedRangeModel, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Only one <code>ChangeEvent</code> is needed per model instance since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * event's only (read-only) state is the source property.  The source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * of events generated here is always "this".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected transient ChangeEvent changeEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** The listeners waiting for model changes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected EventListenerList listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private int extent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private int min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private int max = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private boolean isAdjusting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Initializes all of the properties with default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Those values are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <li><code>value</code> = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <li><code>extent</code> = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <li><code>minimum</code> = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <li><code>maximum</code> = 100
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <li><code>adjusting</code> = false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public DefaultBoundedRangeModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
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
     * Initializes value, extent, minimum and maximum. Adjusting is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Throws an <code>IllegalArgumentException</code> if the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * constraints aren't satisfied:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * min &lt;= value &lt;= value+extent &lt;= max
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public DefaultBoundedRangeModel(int value, int extent, int min, int max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if ((max >= min) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            (value >= min) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            ((value + extent) >= value) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            ((value + extent) <= max)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            this.extent = extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            this.min = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            this.max = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new IllegalArgumentException("invalid range properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
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
     * Returns the model's current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @return the model's current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see BoundedRangeModel#getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public int getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
      return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
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
     * Returns the model's extent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @return the model's extent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @see #setExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @see BoundedRangeModel#getExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public int getExtent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
      return extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
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
     * Returns the model's minimum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return the model's minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @see BoundedRangeModel#getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public int getMinimum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
      return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
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
     * Returns the model's maximum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @return  the model's maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @see BoundedRangeModel#getMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public int getMaximum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
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
     * Sets the current value of the model. For a slider, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * determines where the knob appears. Ensures that the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * value, <I>n</I> falls within the model's constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @see BoundedRangeModel#setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public void setValue(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        n = Math.min(n, Integer.MAX_VALUE - extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        int newValue = Math.max(n, min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (newValue + extent > max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            newValue = max - extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        setRangeProperties(newValue, extent, min, max, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
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
     * Sets the extent to <I>n</I> after ensuring that <I>n</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * is greater than or equal to zero and falls within the model's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @see BoundedRangeModel#setExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public void setExtent(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        int newExtent = Math.max(0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if(value + newExtent > max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            newExtent = max - value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        setRangeProperties(value, newExtent, min, max, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
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
     * Sets the minimum to <I>n</I> after ensuring that <I>n</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * that the other three properties obey the model's constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @see #getMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see BoundedRangeModel#setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public void setMinimum(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int newMax = Math.max(n, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int newValue = Math.max(n, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int newExtent = Math.min(newMax - newValue, extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        setRangeProperties(newValue, newExtent, n, newMax, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
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
     * Sets the maximum to <I>n</I> after ensuring that <I>n</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * that the other three properties obey the model's constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @see BoundedRangeModel#setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public void setMaximum(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int newMin = Math.min(n, min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int newExtent = Math.min(n - newMin, extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int newValue = Math.min(n - newExtent, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        setRangeProperties(newValue, newExtent, newMin, n, isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
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
     * Sets the <code>valueIsAdjusting</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @see #getValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @see BoundedRangeModel#setValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public void setValueIsAdjusting(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        setRangeProperties(value, extent, min, max, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
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
     * Returns true if the value is in the process of changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * as a result of actions being taken by the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @return the value of the <code>valueIsAdjusting</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @see BoundedRangeModel#getValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public boolean getValueIsAdjusting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return isAdjusting;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
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
     * Sets all of the <code>BoundedRangeModel</code> properties after forcing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * the arguments to obey the usual constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *     minimum &lt;= value &lt;= value+extent &lt;= maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * At most, one <code>ChangeEvent</code> is generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @see BoundedRangeModel#setRangeProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @see #setExtent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @see #setMinimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @see #setMaximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @see #setValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public void setRangeProperties(int newValue, int newExtent, int newMin, int newMax, boolean adjusting)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (newMin > newMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            newMin = newMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (newValue > newMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            newMax = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (newValue < newMin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            newMin = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        /* Convert the addends to long so that extent can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * Integer.MAX_VALUE without rolling over the sum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         * A JCK test covers this, see bug 4097718.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (((long)newExtent + (long)newValue) > newMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            newExtent = newMax - newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (newExtent < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            newExtent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        boolean isChange =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            (newValue != value) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            (newExtent != extent) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            (newMin != min) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            (newMax != max) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            (adjusting != isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (isChange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            value = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            extent = newExtent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            min = newMin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            max = newMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            isAdjusting = adjusting;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            fireStateChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
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
     * Adds a <code>ChangeListener</code>.  The change listeners are run each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * time any one of the Bounded Range model properties changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param l the ChangeListener to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @see #removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @see BoundedRangeModel#addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public void addChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        listenerList.add(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
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
     * Removes a <code>ChangeListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @param l the <code>ChangeListener</code> to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @see BoundedRangeModel#removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public void removeChangeListener(ChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        listenerList.remove(ChangeListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
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
     * Returns an array of all the change listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * registered on this <code>DefaultBoundedRangeModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return all of this model's <code>ChangeListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *         or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *         array if no change listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @see #addChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @see #removeChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public ChangeListener[] getChangeListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   346
        return listenerList.getListeners(ChangeListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
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
     * Runs each <code>ChangeListener</code>'s <code>stateChanged</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see #setRangeProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    protected void fireStateChanged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        for (int i = listeners.length - 2; i >= 0; i -=2 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (listeners[i] == ChangeListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                if (changeEvent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    changeEvent = new ChangeEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
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
     * Returns a string that displays all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <code>BoundedRangeModel</code> properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public String toString()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        String modelString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            "value=" + getValue() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            "extent=" + getExtent() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            "min=" + getMinimum() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            "max=" + getMaximum() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            "adj=" + getValueIsAdjusting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return getClass().getName() + "[" + modelString + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Returns an array of all the objects currently registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * upon this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * are registered using the <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * For example, you can query a <code>DefaultBoundedRangeModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * instance <code>m</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * for its change listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * If no such listeners exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @param listenerType  the type of listeners requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *          this parameter should specify an interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *          that descends from <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *          <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *          on this model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @exception ClassCastException if <code>listenerType</code> doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *          specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @see #getChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return listenerList.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
}