1 /*
   2  * Copyright (c) 1997, 2002, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  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 package javax.swing.text;
  26 
  27 import java.io.Serializable;
  28 
  29 /**
  30  * This class encapsulates a single tab stop (basically as tab stops
  31  * are thought of by RTF). A tab stop is at a specified distance from the
  32  * left margin, aligns text in a specified way, and has a specified leader.
  33  * TabStops are immutable, and usually contained in TabSets.
  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&trade;
  41  * has been added to the <code>java.beans</code> package.
  42  * Please see {@link java.beans.XMLEncoder}.
  43  *
  44  */
  45 public class TabStop implements Serializable {
  46 
  47     /** Character following tab is positioned at location. */
  48     public static final int ALIGN_LEFT    = 0;
  49     /** Characters following tab are positioned such that all following
  50      * characters up to next tab/newline end at location. */
  51     public static final int ALIGN_RIGHT   = 1;
  52     /** Characters following tab are positioned such that all following
  53      * characters up to next tab/newline are centered around the tabs
  54      * location. */
  55     public static final int ALIGN_CENTER  = 2;
  56     /** Characters following tab are aligned such that next
  57      * decimal/tab/newline is at the tab location, very similar to
  58      * RIGHT_TAB, just includes decimal as additional character to look for.
  59      */
  60     public static final int ALIGN_DECIMAL = 4;
  61     public static final int ALIGN_BAR     = 5;
  62 
  63     /* Bar tabs (whatever they are) are actually a separate kind of tab
  64        in the RTF spec. However, being a bar tab and having alignment
  65        properties are mutually exclusive, so the reader treats barness
  66        as being a kind of alignment. */
  67 
  68     public static final int LEAD_NONE      = 0;
  69     public static final int LEAD_DOTS      = 1;
  70     public static final int LEAD_HYPHENS   = 2;
  71     public static final int LEAD_UNDERLINE = 3;
  72     public static final int LEAD_THICKLINE = 4;
  73     public static final int LEAD_EQUALS    = 5;
  74 
  75     /** Tab type. */
  76     private int alignment;
  77     /** Location, from the left margin, that tab is at. */
  78     private float position;
  79     private int leader;
  80 
  81     /**
  82      * Creates a tab at position <code>pos</code> with a default alignment
  83      * and default leader.
  84      */
  85     public TabStop(float pos) {
  86         this(pos, ALIGN_LEFT, LEAD_NONE);
  87     }
  88 
  89     /**
  90      * Creates a tab with the specified position <code>pos</code>,
  91      * alignment <code>align</code> and leader <code>leader</code>.
  92      */
  93     public TabStop(float pos, int align, int leader) {
  94         alignment = align;
  95         this.leader = leader;
  96         position = pos;
  97     }
  98 
  99     /**
 100      * Returns the position, as a float, of the tab.
 101      * @return the position of the tab
 102      */
 103     public float getPosition() {
 104         return position;
 105     }
 106 
 107     /**
 108      * Returns the alignment, as an integer, of the tab.
 109      * @return the alignment of the tab
 110      */
 111     public int getAlignment() {
 112         return alignment;
 113     }
 114 
 115     /**
 116      * Returns the leader of the tab.
 117      * @return the leader of the tab
 118      */
 119     public int getLeader() {
 120         return leader;
 121     }
 122 
 123     /**
 124      * Returns true if the tabs are equal.
 125      * @return true if the tabs are equal, otherwise false
 126      */
 127     public boolean equals(Object other)
 128     {
 129         if (other == this) {
 130             return true;
 131         }
 132         if (other instanceof TabStop) {
 133             TabStop o = (TabStop)other;
 134             return ( (alignment == o.alignment) &&
 135                      (leader == o.leader) &&
 136                      (position == o.position) );  /* TODO: epsilon */
 137         }
 138         return false;
 139     }
 140 
 141     /**
 142      * Returns the hashCode for the object.  This must be defined
 143      * here to ensure 100% pure.
 144      *
 145      * @return the hashCode for the object
 146      */
 147     public int hashCode() {
 148         return alignment ^ leader ^ Math.round(position);
 149     }
 150 
 151     /* This is for debugging; perhaps it should be removed before release */
 152     public String toString() {
 153         String buf;
 154         switch(alignment) {
 155           default:
 156           case ALIGN_LEFT:
 157             buf = "";
 158             break;
 159           case ALIGN_RIGHT:
 160             buf = "right ";
 161             break;
 162           case ALIGN_CENTER:
 163             buf = "center ";
 164             break;
 165           case ALIGN_DECIMAL:
 166             buf = "decimal ";
 167             break;
 168           case ALIGN_BAR:
 169             buf = "bar ";
 170             break;
 171         }
 172         buf = buf + "tab @" + String.valueOf(position);
 173         if (leader != LEAD_NONE)
 174             buf = buf + " (w/leaders)";
 175         return buf;
 176     }
 177 }