Source for javax.swing.text.TabStop

   1: /* TabStop.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 java.io.Serializable;
  41: 
  42: /**
  43:  * Represents a tab position in some text.
  44:  */
  45: public class TabStop implements Serializable
  46: {
  47:   /** The serialization UID (compatible with JDK1.5). */
  48:   private static final long serialVersionUID = -5381995917363605058L;
  49: 
  50:   public static final int ALIGN_LEFT = 0;
  51:   public static final int ALIGN_RIGHT = 1;
  52:   public static final int ALIGN_CENTER = 2;
  53:   public static final int ALIGN_DECIMAL = 4;
  54:   public static final int ALIGN_BAR = 5;
  55: 
  56:   public static final int LEAD_NONE = 0;
  57:   public static final int LEAD_DOTS = 1;
  58:   public static final int LEAD_HYPHENS = 2;
  59:   public static final int LEAD_UNDERLINE = 3;
  60:   public static final int LEAD_THICKLINE = 4;
  61:   public static final int LEAD_EQUALS = 5;
  62: 
  63:   float pos;
  64:   int align;
  65:   int leader;
  66: 
  67:   /**
  68:    * Creates a new <code>TabStop</code> for the specified tab position.
  69:    * 
  70:    * @param pos  the tab position.
  71:    */
  72:   public TabStop(float pos) 
  73:   {
  74:     this(pos, ALIGN_LEFT, LEAD_NONE);
  75:   }
  76:   
  77:   /**
  78:    * Creates a new <code>TabStop</code> with the specified attributes.
  79:    * 
  80:    * @param pos  the tab position.
  81:    * @param align  the alignment (one of {@link #ALIGN_LEFT}, 
  82:    *     {@link #ALIGN_CENTER}, {@link #ALIGN_RIGHT}, {@link #ALIGN_DECIMAL} 
  83:    *     or {@link #ALIGN_BAR}).
  84:    * @param leader  the leader (one of {@link #LEAD_NONE}, {@link #LEAD_DOTS}, 
  85:    *     {@link #LEAD_EQUALS}, {@link #LEAD_HYPHENS}, {@link #LEAD_THICKLINE} 
  86:    *     or {@link #LEAD_UNDERLINE}).
  87:    */
  88:   public TabStop(float pos, int align, int leader)
  89:   {
  90:     this.pos = pos;
  91:     this.align = align;
  92:     this.leader = leader;
  93:   }
  94:   
  95:   /**
  96:    * Tests this <code>TabStop</code> for equality with an arbitrary object.
  97:    * 
  98:    * @param other  the other object (<code>null</code> permitted).
  99:    * 
 100:    * @return <code>true</code> if this <code>TabStop</code> is equal to 
 101:    *     the specified object, and <code>false</code> otherwise.
 102:    */
 103:   public boolean equals(Object other) 
 104:   {
 105:     return (other != null)
 106:       && (other instanceof TabStop)
 107:       && (((TabStop)other).getPosition() == this.getPosition())
 108:       && (((TabStop)other).getLeader() == this.getLeader())
 109:       && (((TabStop)other).getAlignment() == this.getAlignment());
 110:   }
 111: 
 112:   /**
 113:    * Returns the tab alignment.  This should be one of {@link #ALIGN_LEFT}, 
 114:    * {@link #ALIGN_CENTER}, {@link #ALIGN_RIGHT}, {@link #ALIGN_DECIMAL} or 
 115:    * {@link #ALIGN_BAR}.
 116:    * 
 117:    * @return The tab alignment.
 118:    */
 119:   public int getAlignment() 
 120:   {
 121:     return align;
 122:   }
 123: 
 124:   /**
 125:    * Returns the leader type.  This should be one of {@link #LEAD_NONE}, 
 126:    * {@link #LEAD_DOTS}, {@link #LEAD_EQUALS}, {@link #LEAD_HYPHENS}, 
 127:    * {@link #LEAD_THICKLINE} or {@link #LEAD_UNDERLINE}.
 128:    * 
 129:    * @return The leader type.
 130:    */
 131:   public int getLeader() 
 132:   {
 133:     return leader;
 134:   }
 135: 
 136:   /**
 137:    * Returns the tab position.
 138:    * 
 139:    * @return The tab position.
 140:    */
 141:   public float getPosition() 
 142:   {
 143:     return pos;
 144:   }
 145: 
 146:   /**
 147:    * Returns a hash code for this <code>TabStop</code>.
 148:    * 
 149:    * @return A hash code.
 150:    */
 151:   public int hashCode() 
 152:   {
 153:     return (int) pos + (int) leader + (int) align;
 154:   }
 155: 
 156:   /**
 157:    * Returns a string describing this <code>TabStop</code>.
 158:    * 
 159:    * @return A string describing this <code>TabStop</code>.
 160:    */
 161:   public String toString() 
 162:   {
 163:     String prefix = "";
 164:     switch (align)
 165:       {
 166:       case ALIGN_RIGHT:
 167:         prefix = "right ";
 168:         break;
 169: 
 170:       case ALIGN_CENTER:
 171:         prefix = "center ";
 172:         break;
 173: 
 174:       case ALIGN_DECIMAL:
 175:         prefix = "decimal ";
 176:         break;
 177:         
 178:       case ALIGN_BAR:
 179:         prefix = "bar ";
 180:         break;
 181: 
 182:       default:
 183:         break;
 184:       }
 185:     
 186:     return prefix + "tab @" + pos 
 187:         + ((leader == LEAD_NONE) ? "" : " (w/leaders)");
 188:   }
 189: 
 190: }