jdk/src/share/classes/javax/swing/text/TabSet.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2003 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
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  Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class TabSet implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /** TabStops this TabSet contains. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private TabStop[]              tabs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Since this class is immutable the hash code could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * calculated once. MAX_VALUE means that it was not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * yet. Hash code shouldn't has MAX_VALUE value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private int hashCode = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Creates and returns an instance of TabSet. The array of Tabs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * passed in must be sorted in ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public TabSet(TabStop[] tabs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // PENDING(sky): If this becomes a problem, make it sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if(tabs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            int          tabCount = tabs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            this.tabs = new TabStop[tabCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            System.arraycopy(tabs, 0, this.tabs, 0, tabCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            this.tabs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Returns the number of Tab instances the receiver contains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public int getTabCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return (tabs == null) ? 0 : tabs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Returns the TabStop at index <code>index</code>. This will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * IllegalArgumentException if <code>index</code> is outside the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * of tabs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public TabStop getTab(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int          numTabs = getTabCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if(index < 0 || index >= numTabs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new IllegalArgumentException(index +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                              " is outside the range of tabs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return tabs[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Returns the Tab instance after <code>location</code>. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * return null if there are no tabs after <code>location</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public TabStop getTabAfter(float location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int     index = getTabIndexAfter(location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return (index == -1) ? null : tabs[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @return the index of the TabStop <code>tab</code>, or -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <code>tab</code> is not contained in the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public int getTabIndex(TabStop tab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for(int counter = getTabCount() - 1; counter >= 0; counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // should this use .equals?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if(getTab(counter) == tab)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                return counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Returns the index of the Tab to be used after <code>location</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * This will return -1 if there are no tabs after <code>location</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public int getTabIndexAfter(float location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int     current, min, max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        max = getTabCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        while(min != max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            current = (max - min) / 2 + min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if(location > tabs[current].getPosition()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                if(min == current)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    min = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    min = current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                if(current == 0 || location > tabs[current - 1].getPosition())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    return current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                max = current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // no tabs after the passed in location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Indicates whether this <code>TabSet</code> is equal to another one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param o the <code>TabSet</code> instance which this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *  should be compared to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return <code>true</code> if <code>o</code> is the instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <code>TabSet</code>, has the same number of <code>TabStop</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * and they are all equal, <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (o == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (o instanceof TabSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            TabSet ts = (TabSet) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            int count = getTabCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (ts.getTabCount() != count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            for (int i=0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                TabStop ts1 = getTab(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                TabStop ts2 = ts.getTab(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                if ((ts1 == null && ts2 != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        (ts1 != null && !getTab(i).equals(ts.getTab(i)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Returns a hashcode for this set of TabStops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return  a hashcode value for this set of TabStops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (hashCode == Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            hashCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            int len = getTabCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                TabStop ts = getTab(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                hashCode ^= ts != null ? getTab(i).hashCode() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (hashCode == Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                hashCode -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return hashCode;
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
     * Returns the string representation of the set of tabs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int            tabCount = getTabCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        StringBuffer   buffer = new StringBuffer("[ ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        for(int counter = 0; counter < tabCount; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if(counter > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                buffer.append(" - ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            buffer.append(getTab(counter).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        buffer.append(" ]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}