Source for javax.swing.text.TabSet

   1: /* TabSet.java --
   2:    Copyright (C) 2004, 2006, Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.swing.text;
  39: 
  40: import gnu.java.lang.CPStringBuilder;
  41: 
  42: import java.io.Serializable;
  43: 
  44: /**
  45:  * A set of tab stops.  Instances of this class are immutable.
  46:  */
  47: public class TabSet implements Serializable
  48: {
  49:   /** The serialization UID (compatible with JDK1.5). */
  50:   private static final long serialVersionUID = 2367703481999080593L;
  51: 
  52:   /** Storage for the tab stops. */
  53:   TabStop[] tabs;
  54: 
  55:   /**
  56:    * Creates a new <code>TabSet</code> containing the specified tab stops.
  57:    *
  58:    * @param t  the tab stops (<code>null</code> permitted).
  59:    */
  60:   public TabSet(TabStop[] t)
  61:   {
  62:     if (t != null)
  63:       tabs = (TabStop[]) t.clone();
  64:     else
  65:       tabs = new TabStop[0];
  66:   }
  67: 
  68:   /**
  69:    * Returns the tab stop with the specified index.
  70:    *
  71:    * @param i  the index.
  72:    *
  73:    * @return The tab stop.
  74:    *
  75:    * @throws IllegalArgumentException if <code>i</code> is not in the range
  76:    *     <code>0</code> to <code>getTabCount() - 1</code>.
  77:    */
  78:   public TabStop getTab(int i)
  79:   {
  80:     if (i < 0 || i >= tabs.length)
  81:       throw new IllegalArgumentException("Index out of bounds.");
  82:     return tabs[i];
  83:   }
  84: 
  85:   /**
  86:    * Returns the tab following the specified location.
  87:    *
  88:    * @param location  the location.
  89:    *
  90:    * @return The tab following the specified location (or <code>null</code>).
  91:    */
  92:   public TabStop getTabAfter(float location)
  93:   {
  94:     int idx = getTabIndexAfter(location);
  95:     if (idx == -1)
  96:       return null;
  97:     else
  98:       return tabs[idx];
  99:   }
 100: 
 101:   /**
 102:    * Returns the number of tab stops in this tab set.
 103:    *
 104:    * @return The number of tab stops in this tab set.
 105:    */
 106:   public int getTabCount()
 107:   {
 108:     return tabs.length;
 109:   }
 110: 
 111:   /**
 112:    * Returns the index of the specified tab, or -1 if the tab is not found.
 113:    *
 114:    * @param tab  the tab (<code>null</code> permitted).
 115:    *
 116:    * @return The index of the specified tab, or -1.
 117:    */
 118:   public int getTabIndex(TabStop tab)
 119:   {
 120:     for (int i = 0; i < tabs.length; ++i)
 121:       if (tabs[i] == tab)
 122:         return i;
 123:     return -1;
 124:   }
 125: 
 126:   /**
 127:    * Returns the index of the tab at or after the specified location.
 128:    *
 129:    * @param location  the tab location.
 130:    *
 131:    * @return The index of the tab stop, or -1.
 132:    */
 133:   public int getTabIndexAfter(float location)
 134:   {
 135:     for (int i = 0; i < tabs.length; i++)
 136:       {
 137:         if (location <= tabs[i].getPosition())
 138:           return i;
 139:       }
 140:     return -1;
 141:   }
 142: 
 143:   /**
 144:    * Tests this <code>TabSet</code> for equality with an arbitrary object.
 145:    *
 146:    * @param obj  the object (<code>null</code> permitted).
 147:    *
 148:    * @return <code>true</code> if this <code>TabSet</code> is equal to
 149:    *     <code>obj</code>, and <code>false</code> otherwise.
 150:    *
 151:    * @since 1.5
 152:    */
 153:   public boolean equals(Object obj)
 154:   {
 155:     if (obj == this)
 156:       return true;
 157:     if (!(obj instanceof TabSet))
 158:       return false;
 159:     TabSet that = (TabSet) obj;
 160:     int tabCount = getTabCount();
 161:     if (tabCount != that.getTabCount())
 162:       return false;
 163:     for (int i = 0; i < tabCount; i++)
 164:       {
 165:         if (!this.getTab(i).equals(that.getTab(i)))
 166:           return false;
 167:       }
 168:     return true;
 169:   }
 170: 
 171:   /**
 172:    * Returns a hash code for this <code>TabSet</code>.
 173:    *
 174:    * @return A hash code.
 175:    *
 176:    * @since 1.5
 177:    */
 178:   public int hashCode()
 179:   {
 180:     // this hash code won't match Sun's, but that shouldn't matter...
 181:     int result = 193;
 182:     int tabs = getTabCount();
 183:     for (int i = 0; i < tabs; i++)
 184:       {
 185:         TabStop t = getTab(i);
 186:         if (t != null)
 187:           result = 37 * result + t.hashCode();
 188:       }
 189:     return result;
 190:   }
 191: 
 192:   /**
 193:    * Returns a string representation of this <code>TabSet</code>.
 194:    *
 195:    * @return A string representation of this <code>TabSet</code>.
 196:    */
 197:   public String toString()
 198:   {
 199:     CPStringBuilder sb = new CPStringBuilder();
 200:     sb.append("[ ");
 201:     for (int i = 0; i < tabs.length; ++i)
 202:       {
 203:         if (i != 0)
 204:           sb.append(" - ");
 205:         sb.append(tabs[i].toString());
 206:       }
 207:     sb.append(" ]");
 208:     return sb.toString();
 209:   }
 210: }