KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > TabSet


1 /*
2  * @(#)TabSet.java 1.15 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.text;
9
10 import java.io.Serializable JavaDoc;
11
12 /**
13  * A TabSet is comprised of many TabStops. It offers methods for locating the
14  * closest TabStop to a given position and finding all the potential TabStops.
15  * It is also immutable.
16  * <p>
17  * <strong>Warning:</strong>
18  * Serialized objects of this class will not be compatible with
19  * future Swing releases. The current serialization support is
20  * appropriate for short term storage or RMI between applications running
21  * the same version of Swing. As of 1.4, support for long term storage
22  * of all JavaBeans<sup><font size="-2">TM</font></sup>
23  * has been added to the <code>java.beans</code> package.
24  * Please see {@link java.beans.XMLEncoder}.
25  *
26  * @author Scott Violet
27  * @version 1.15 12/19/03
28  */

29 public class TabSet implements Serializable JavaDoc
30 {
31     /** TabStops this TabSet contains. */
32     private TabStop JavaDoc[] tabs;
33     /**
34      * Since this class is immutable the hash code could be
35      * calculated once. MAX_VALUE means that it was not initialized
36      * yet. Hash code shouldn't has MAX_VALUE value.
37      */

38     private int hashCode = Integer.MAX_VALUE;
39
40     /**
41      * Creates and returns an instance of TabSet. The array of Tabs
42      * passed in must be sorted in ascending order.
43      */

44     public TabSet(TabStop JavaDoc[] tabs) {
45     // PENDING(sky): If this becomes a problem, make it sort.
46
if(tabs != null) {
47         int tabCount = tabs.length;
48
49         this.tabs = new TabStop JavaDoc[tabCount];
50         System.arraycopy(tabs, 0, this.tabs, 0, tabCount);
51     }
52     else
53         this.tabs = null;
54     }
55
56     /**
57      * Returns the number of Tab instances the receiver contains.
58      */

59     public int getTabCount() {
60     return (tabs == null) ? 0 : tabs.length;
61     }
62
63     /**
64      * Returns the TabStop at index <code>index</code>. This will throw an
65      * IllegalArgumentException if <code>index</code> is outside the range
66      * of tabs.
67      */

68     public TabStop JavaDoc getTab(int index) {
69     int numTabs = getTabCount();
70
71     if(index < 0 || index >= numTabs)
72         throw new IllegalArgumentException JavaDoc(index +
73                           " is outside the range of tabs");
74     return tabs[index];
75     }
76
77     /**
78      * Returns the Tab instance after <code>location</code>. This will
79      * return null if there are no tabs after <code>location</code>.
80      */

81     public TabStop JavaDoc getTabAfter(float location) {
82     int index = getTabIndexAfter(location);
83
84     return (index == -1) ? null : tabs[index];
85     }
86
87     /**
88      * @return the index of the TabStop <code>tab</code>, or -1 if
89      * <code>tab</code> is not contained in the receiver.
90      */

91     public int getTabIndex(TabStop JavaDoc tab) {
92     for(int counter = getTabCount() - 1; counter >= 0; counter--)
93         // should this use .equals?
94
if(getTab(counter) == tab)
95         return counter;
96     return -1;
97     }
98
99     /**
100      * Returns the index of the Tab to be used after <code>location</code>.
101      * This will return -1 if there are no tabs after <code>location</code>.
102      */

103     public int getTabIndexAfter(float location) {
104     int current, min, max;
105
106     min = 0;
107     max = getTabCount();
108     while(min != max) {
109         current = (max - min) / 2 + min;
110         if(location > tabs[current].getPosition()) {
111         if(min == current)
112             min = max;
113         else
114             min = current;
115         }
116         else {
117         if(current == 0 || location > tabs[current - 1].getPosition())
118             return current;
119         max = current;
120         }
121     }
122     // no tabs after the passed in location.
123
return -1;
124     }
125     
126     /**
127      * Indicates whether this <code>TabSet</code> is equal to another one.
128      * @param o the <code>TabSet</code> instance which this instance
129      * should be compared to.
130      * @return <code>true</code> if <code>o</code> is the instance of
131      * <code>TabSet</code>, has the same number of <code>TabStop</code>s
132      * and they are all equal, <code>false</code> otherwise.
133      *
134      * @since 1.5
135      */

136     public boolean equals(Object JavaDoc o) {
137         if (o == this) {
138             return true;
139         }
140         if (o instanceof TabSet JavaDoc) {
141             TabSet JavaDoc ts = (TabSet JavaDoc) o;
142             int count = getTabCount();
143             if (ts.getTabCount() != count) {
144                 return false;
145             }
146             for (int i=0; i < count; i++) {
147                 TabStop JavaDoc ts1 = getTab(i);
148                 TabStop JavaDoc ts2 = ts.getTab(i);
149                 if ((ts1 == null && ts2 != null) ||
150                         (ts1 != null && !getTab(i).equals(ts.getTab(i)))) {
151                     return false;
152                 }
153             }
154             return true;
155         }
156         return false;
157     }
158
159     /**
160      * Returns a hashcode for this set of TabStops.
161      * @return a hashcode value for this set of TabStops.
162      *
163      * @since 1.5
164      */

165     public int hashCode() {
166         if (hashCode == Integer.MAX_VALUE) {
167             hashCode = 0;
168             int len = getTabCount();
169             for (int i = 0; i < len; i++) {
170                 TabStop JavaDoc ts = getTab(i);
171                 hashCode ^= ts != null ? getTab(i).hashCode() : 0;
172             }
173             if (hashCode == Integer.MAX_VALUE) {
174                 hashCode -= 1;
175             }
176         }
177         return hashCode;
178     }
179
180     /**
181      * Returns the string representation of the set of tabs.
182      */

183     public String JavaDoc toString() {
184     int tabCount = getTabCount();
185     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("[ ");
186
187     for(int counter = 0; counter < tabCount; counter++) {
188         if(counter > 0)
189         buffer.append(" - ");
190         buffer.append(getTab(counter).toString());
191     }
192     buffer.append(" ]");
193     return buffer.toString();
194     }
195 }
196
Popular Tags