jdk/src/share/classes/javax/swing/text/TabSet.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 22574 7f8ce0c8c20a
permissions -rw-r--r--
8041917: unexcepted behavior of LineBorder while using Boolean variable true Reviewed-by: alexsch, serb
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) 1998, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A TabSet is comprised of many TabStops. It offers methods for locating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * closest TabStop to a given position and finding all the potential TabStops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * It is also immutable.
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: 7668
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  Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    46
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class TabSet implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /** TabStops this TabSet contains. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private TabStop[]              tabs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Since this class is immutable the hash code could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * calculated once. MAX_VALUE means that it was not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * yet. Hash code shouldn't has MAX_VALUE value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int hashCode = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Creates and returns an instance of TabSet. The array of Tabs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * passed in must be sorted in ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public TabSet(TabStop[] tabs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        // PENDING(sky): If this becomes a problem, make it sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if(tabs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            int          tabCount = tabs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            this.tabs = new TabStop[tabCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            System.arraycopy(tabs, 0, this.tabs, 0, tabCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            this.tabs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns the number of Tab instances the receiver contains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public int getTabCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return (tabs == null) ? 0 : tabs.length;
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
     * Returns the TabStop at index <code>index</code>. This will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * IllegalArgumentException if <code>index</code> is outside the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * of tabs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public TabStop getTab(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        int          numTabs = getTabCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if(index < 0 || index >= numTabs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new IllegalArgumentException(index +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                              " is outside the range of tabs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return tabs[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Returns the Tab instance after <code>location</code>. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * return null if there are no tabs after <code>location</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public TabStop getTabAfter(float location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        int     index = getTabIndexAfter(location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return (index == -1) ? null : tabs[index];
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
     * @return the index of the TabStop <code>tab</code>, or -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <code>tab</code> is not contained in the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public int getTabIndex(TabStop tab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for(int counter = getTabCount() - 1; counter >= 0; counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            // should this use .equals?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if(getTab(counter) == tab)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                return counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return -1;
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
     * Returns the index of the Tab to be used after <code>location</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * This will return -1 if there are no tabs after <code>location</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public int getTabIndexAfter(float location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int     current, min, max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        max = getTabCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        while(min != max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            current = (max - min) / 2 + min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if(location > tabs[current].getPosition()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                if(min == current)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    min = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    min = current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                if(current == 0 || location > tabs[current - 1].getPosition())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                max = current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // no tabs after the passed in location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Indicates whether this <code>TabSet</code> is equal to another one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param o the <code>TabSet</code> instance which this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *  should be compared to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @return <code>true</code> if <code>o</code> is the instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <code>TabSet</code>, has the same number of <code>TabStop</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * and they are all equal, <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (o == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (o instanceof TabSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            TabSet ts = (TabSet) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            int count = getTabCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if (ts.getTabCount() != count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            for (int i=0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                TabStop ts1 = getTab(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                TabStop ts2 = ts.getTab(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if ((ts1 == null && ts2 != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        (ts1 != null && !getTab(i).equals(ts.getTab(i)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Returns a hashcode for this set of TabStops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @return  a hashcode value for this set of TabStops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (hashCode == Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            hashCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            int len = getTabCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                TabStop ts = getTab(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                hashCode ^= ts != null ? getTab(i).hashCode() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (hashCode == Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                hashCode -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Returns the string representation of the set of tabs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int            tabCount = getTabCount();
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   203
        StringBuilder buffer = new StringBuilder("[ ");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        for(int counter = 0; counter < tabCount; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if(counter > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                buffer.append(" - ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            buffer.append(getTab(counter).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        buffer.append(" ]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
}