1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
/* * @(#)TabSet.java 1.16 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing.text; import java.io.Serializable; /** * A TabSet is comprised of many TabStops. It offers methods for locating the * closest TabStop to a given position and finding all the potential TabStops. * It is also immutable. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @author Scott Violet * @version 1.16 11/17/05 */ public class TabSet implements Serializable { /** TabStops this TabSet contains. */ private TabStop[] tabs; /** * Since this class is immutable the hash code could be * calculated once. MAX_VALUE means that it was not initialized * yet. Hash code shouldn't has MAX_VALUE value. */ private int hashCode = Integer.MAX_VALUE; /** * Creates and returns an instance of TabSet. The array of Tabs * passed in must be sorted in ascending order. */ public TabSet(TabStop[] tabs) { // PENDING(sky): If this becomes a problem, make it sort. if(tabs != null) { int tabCount = tabs.length; this.tabs = new TabStop[tabCount]; System.arraycopy(tabs, 0, this.tabs, 0, tabCount); } else this.tabs = null; } /** * Returns the number of Tab instances the receiver contains. */ public int getTabCount() { return (tabs == null) ? 0 : tabs.length; } /** * Returns the TabStop at index <code>index</code>. This will throw an * IllegalArgumentException if <code>index</code> is outside the range * of tabs. */ public TabStop getTab(int index) { int numTabs = getTabCount(); if(index < 0 || index >= numTabs) throw new IllegalArgumentException(index + " is outside the range of tabs"); return tabs[index]; } /** * Returns the Tab instance after <code>location</code>. This will * return null if there are no tabs after <code>location</code>. */ public TabStop getTabAfter(float location) { int index = getTabIndexAfter(location); return (index == -1) ? null : tabs[index]; } /** * @return the index of the TabStop <code>tab</code>, or -1 if * <code>tab</code> is not contained in the receiver. */ public int getTabIndex(TabStop tab) { for(int counter = getTabCount() - 1; counter >= 0; counter--) // should this use .equals? if(getTab(counter) == tab) return counter; return -1; } /** * Returns the index of the Tab to be used after <code>location</code>. * This will return -1 if there are no tabs after <code>location</code>. */ public int getTabIndexAfter(float location) { int current, min, max; min = 0; max = getTabCount(); while(min != max) { current = (max - min) / 2 + min; if(location > tabs[current].getPosition()) { if(min == current) min = max; else min = current; } else { if(current == 0 || location > tabs[current - 1].getPosition()) return current; max = current; } } // no tabs after the passed in location. return -1; } /** * Indicates whether this <code>TabSet</code> is equal to another one. * @param o the <code>TabSet</code> instance which this instance * should be compared to. * @return <code>true</code> if <code>o</code> is the instance of * <code>TabSet</code>, has the same number of <code>TabStop</code>s * and they are all equal, <code>false</code> otherwise. * * @since 1.5 */ public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof TabSet) { TabSet ts = (TabSet) o; int count = getTabCount(); if (ts.getTabCount() != count) { return false; } for (int i=0; i < count; i++) { TabStop ts1 = getTab(i); TabStop ts2 = ts.getTab(i); if ((ts1 == null && ts2 != null) || (ts1 != null && !getTab(i).equals(ts.getTab(i)))) { return false; } } return true; } return false; } /** * Returns a hashcode for this set of TabStops. * @return a hashcode value for this set of TabStops. * * @since 1.5 */ public int hashCode() { if (hashCode == Integer.MAX_VALUE) { hashCode = 0; int len = getTabCount(); for (int i = 0; i < len; i++) { TabStop ts = getTab(i); hashCode ^= ts != null ? getTab(i).hashCode() : 0; } if (hashCode == Integer.MAX_VALUE) { hashCode -= 1; } } return hashCode; } /** * Returns the string representation of the set of tabs. */ public String toString() { int tabCount = getTabCount(); StringBuffer buffer = new StringBuffer("[ "); for(int counter = 0; counter < tabCount; counter++) { if(counter > 0) buffer.append(" - "); buffer.append(getTab(counter).toString()); } buffer.append(" ]"); return buffer.toString(); } }