21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.swing.text;
27
28 import java.io.Serializable;
29
30 /**
31 * A TabSet is comprised of many TabStops. It offers methods for locating the
32 * closest TabStop to a given position and finding all the potential TabStops.
33 * It is also immutable.
34 * <p>
35 * <strong>Warning:</strong>
36 * Serialized objects of this class will not be compatible with
37 * future Swing releases. The current serialization support is
38 * appropriate for short term storage or RMI between applications running
39 * the same version of Swing. As of 1.4, support for long term storage
40 * of all JavaBeans™
41 * has been added to the <code>java.beans</code> package.
42 * Please see {@link java.beans.XMLEncoder}.
43 *
44 * @author Scott Violet
45 */
46 @SuppressWarnings("serial") // Same-version serialization only
47 public class TabSet implements Serializable
48 {
49 /** TabStops this TabSet contains. */
50 private TabStop[] tabs;
51 /**
52 * Since this class is immutable the hash code could be
53 * calculated once. MAX_VALUE means that it was not initialized
54 * yet. Hash code shouldn't has MAX_VALUE value.
55 */
56 private int hashCode = Integer.MAX_VALUE;
57
58 /**
59 * Creates and returns an instance of TabSet. The array of Tabs
60 * passed in must be sorted in ascending order.
61 * @param tabs the TabStops to initialize the TabSet
64 // PENDING(sky): If this becomes a problem, make it sort.
65 if(tabs != null) {
66 int tabCount = tabs.length;
67
68 this.tabs = new TabStop[tabCount];
69 System.arraycopy(tabs, 0, this.tabs, 0, tabCount);
70 }
71 else
72 this.tabs = null;
73 }
74
75 /**
76 * Returns the number of Tab instances the receiver contains.
77 * @return the number of Tab instances the receiver contains
78 */
79 public int getTabCount() {
80 return (tabs == null) ? 0 : tabs.length;
81 }
82
83 /**
84 * Returns the TabStop at index <code>index</code>. This will throw an
85 * IllegalArgumentException if <code>index</code> is outside the range
86 * of tabs.
87 * @param index which TapStop to return
88 * @return the TabStop at index {@code index}
89 */
90 public TabStop getTab(int index) {
91 int numTabs = getTabCount();
92
93 if(index < 0 || index >= numTabs)
94 throw new IllegalArgumentException(index +
95 " is outside the range of tabs");
96 return tabs[index];
97 }
98
99 /**
100 * Returns the Tab instance after <code>location</code>. This will
101 * return null if there are no tabs after <code>location</code>.
102 * @param location location to find a Tab after
103 * @return the Tab instance after {@code location}
104 */
105 public TabStop getTabAfter(float location) {
106 int index = getTabIndexAfter(location);
107
108 return (index == -1) ? null : tabs[index];
109 }
110
111 /**
112 * Returns the index of the TabStop <code>tab</code>, or -1 if
113 * <code>tab</code> is not contained in the receiver.
114 * @param tab the TabStop to find
115 * @return the index of the TabStop <code>tab</code>, or -1 if
116 * <code>tab</code> is not contained in the receiver.
117 */
118 public int getTabIndex(TabStop tab) {
119 for(int counter = getTabCount() - 1; counter >= 0; counter--)
120 // should this use .equals?
121 if(getTab(counter) == tab)
122 return counter;
123 return -1;
124 }
125
126 /**
127 * Returns the index of the Tab to be used after <code>location</code>.
128 * This will return -1 if there are no tabs after <code>location</code>.
129 * @param location location to find a Tab after
130 * @return the index of the Tab to be used after <code>location</code>
131 */
132 public int getTabIndexAfter(float location) {
133 int current, min, max;
134
135 min = 0;
136 max = getTabCount();
137 while(min != max) {
138 current = (max - min) / 2 + min;
139 if(location > tabs[current].getPosition()) {
140 if(min == current)
141 min = max;
142 else
143 min = current;
144 }
145 else {
146 if(current == 0 || location > tabs[current - 1].getPosition())
147 return current;
148 max = current;
149 }
150 }
151 // no tabs after the passed in location.
152 return -1;
153 }
154
155 /**
156 * Indicates whether this <code>TabSet</code> is equal to another one.
157 * @param o the <code>TabSet</code> instance which this instance
158 * should be compared to.
159 * @return <code>true</code> if <code>o</code> is the instance of
160 * <code>TabSet</code>, has the same number of <code>TabStop</code>s
161 * and they are all equal, <code>false</code> otherwise.
162 *
163 * @since 1.5
164 */
165 public boolean equals(Object o) {
166 if (o == this) {
167 return true;
168 }
169 if (o instanceof TabSet) {
170 TabSet ts = (TabSet) o;
171 int count = getTabCount();
172 if (ts.getTabCount() != count) {
173 return false;
174 }
175 for (int i=0; i < count; i++) {
176 TabStop ts1 = getTab(i);
177 TabStop ts2 = ts.getTab(i);
178 if ((ts1 == null && ts2 != null) ||
179 (ts1 != null && !getTab(i).equals(ts.getTab(i)))) {
180 return false;
181 }
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.swing.text;
27
28 import java.io.Serializable;
29
30 /**
31 * A TabSet is comprised of many TabStops. It offers methods for locating the
32 * closest TabStop to a given position and finding all the potential TabStops.
33 * It is also immutable.
34 * <p>
35 * <strong>Warning:</strong>
36 * Serialized objects of this class will not be compatible with
37 * future Swing releases. The current serialization support is
38 * appropriate for short term storage or RMI between applications running
39 * the same version of Swing. As of 1.4, support for long term storage
40 * of all JavaBeans™
41 * has been added to the {@code java.beans} package.
42 * Please see {@link java.beans.XMLEncoder}.
43 *
44 * @author Scott Violet
45 */
46 @SuppressWarnings("serial") // Same-version serialization only
47 public class TabSet implements Serializable
48 {
49 /** TabStops this TabSet contains. */
50 private TabStop[] tabs;
51 /**
52 * Since this class is immutable the hash code could be
53 * calculated once. MAX_VALUE means that it was not initialized
54 * yet. Hash code shouldn't has MAX_VALUE value.
55 */
56 private int hashCode = Integer.MAX_VALUE;
57
58 /**
59 * Creates and returns an instance of TabSet. The array of Tabs
60 * passed in must be sorted in ascending order.
61 * @param tabs the TabStops to initialize the TabSet
64 // PENDING(sky): If this becomes a problem, make it sort.
65 if(tabs != null) {
66 int tabCount = tabs.length;
67
68 this.tabs = new TabStop[tabCount];
69 System.arraycopy(tabs, 0, this.tabs, 0, tabCount);
70 }
71 else
72 this.tabs = null;
73 }
74
75 /**
76 * Returns the number of Tab instances the receiver contains.
77 * @return the number of Tab instances the receiver contains
78 */
79 public int getTabCount() {
80 return (tabs == null) ? 0 : tabs.length;
81 }
82
83 /**
84 * Returns the TabStop at index {@code index}. This will throw an
85 * IllegalArgumentException if {@code index} is outside the range
86 * of tabs.
87 * @param index which TapStop to return
88 * @return the TabStop at index {@code index}
89 */
90 public TabStop getTab(int index) {
91 int numTabs = getTabCount();
92
93 if(index < 0 || index >= numTabs)
94 throw new IllegalArgumentException(index +
95 " is outside the range of tabs");
96 return tabs[index];
97 }
98
99 /**
100 * Returns the Tab instance after {@code location}. This will
101 * return null if there are no tabs after {@code location}.
102 * @param location location to find a Tab after
103 * @return the Tab instance after {@code location}
104 */
105 public TabStop getTabAfter(float location) {
106 int index = getTabIndexAfter(location);
107
108 return (index == -1) ? null : tabs[index];
109 }
110
111 /**
112 * Returns the index of the TabStop {@code tab}, or -1 if
113 * {@code tab} is not contained in the receiver.
114 * @param tab the TabStop to find
115 * @return the index of the TabStop {@code tab}, or -1 if
116 * {@code tab} is not contained in the receiver.
117 */
118 public int getTabIndex(TabStop tab) {
119 for(int counter = getTabCount() - 1; counter >= 0; counter--)
120 // should this use .equals?
121 if(getTab(counter) == tab)
122 return counter;
123 return -1;
124 }
125
126 /**
127 * Returns the index of the Tab to be used after {@code location}.
128 * This will return -1 if there are no tabs after {@code location}.
129 * @param location location to find a Tab after
130 * @return the index of the Tab to be used after {@code location}
131 */
132 public int getTabIndexAfter(float location) {
133 int current, min, max;
134
135 min = 0;
136 max = getTabCount();
137 while(min != max) {
138 current = (max - min) / 2 + min;
139 if(location > tabs[current].getPosition()) {
140 if(min == current)
141 min = max;
142 else
143 min = current;
144 }
145 else {
146 if(current == 0 || location > tabs[current - 1].getPosition())
147 return current;
148 max = current;
149 }
150 }
151 // no tabs after the passed in location.
152 return -1;
153 }
154
155 /**
156 * Indicates whether this {@code TabSet} is equal to another one.
157 * @param o the {@code TabSet} instance which this instance
158 * should be compared to.
159 * @return {@code true} if {@code o} is the instance of
160 * {@code TabSet}, has the same number of {@code TabStop}s
161 * and they are all equal, {@code false} otherwise.
162 *
163 * @since 1.5
164 */
165 public boolean equals(Object o) {
166 if (o == this) {
167 return true;
168 }
169 if (o instanceof TabSet) {
170 TabSet ts = (TabSet) o;
171 int count = getTabCount();
172 if (ts.getTabCount() != count) {
173 return false;
174 }
175 for (int i=0; i < count; i++) {
176 TabStop ts1 = getTab(i);
177 TabStop ts2 = ts.getTab(i);
178 if ((ts1 == null && ts2 != null) ||
179 (ts1 != null && !getTab(i).equals(ts.getTab(i)))) {
180 return false;
181 }
|