KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > TabStop


1 /*
2  * @(#)TabStop.java 1.20 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text;
8
9 import java.io.Serializable JavaDoc;
10
11 /**
12  * This class encapsulates a single tab stop (basically as tab stops
13  * are thought of by RTF). A tab stop is at a specified distance from the
14  * left margin, aligns text in a specified way, and has a specified leader.
15  * TabStops are immutable, and usually contained in TabSets.
16  * <p>
17  * <strong>Warning:</strong>
18  * Serialized objects of this class will not be compatible with
19  * future Swing releases. The current serialization support is
20  * appropriate for short term storage or RMI between applications running
21  * the same version of Swing. As of 1.4, support for long term storage
22  * of all JavaBeans<sup><font size="-2">TM</font></sup>
23  * has been added to the <code>java.beans</code> package.
24  * Please see {@link java.beans.XMLEncoder}.
25  *
26  * @version 1.20 12/19/03
27  */

28 public class TabStop implements Serializable JavaDoc {
29
30     /** Character following tab is positioned at location. */
31     public static final int ALIGN_LEFT = 0;
32     /** Characters following tab are positioned such that all following
33      * characters up to next tab/newline end at location. */

34     public static final int ALIGN_RIGHT = 1;
35     /** Characters following tab are positioned such that all following
36      * characters up to next tab/newline are centered around the tabs
37      * location. */

38     public static final int ALIGN_CENTER = 2;
39     /** Characters following tab are aligned such that next
40      * decimal/tab/newline is at the tab location, very similar to
41      * RIGHT_TAB, just includes decimal as additional character to look for.
42      */

43     public static final int ALIGN_DECIMAL = 4;
44     public static final int ALIGN_BAR = 5;
45
46     /* Bar tabs (whatever they are) are actually a separate kind of tab
47        in the RTF spec. However, being a bar tab and having alignment
48        properties are mutually exclusive, so the reader treats barness
49        as being a kind of alignment. */

50
51     public static final int LEAD_NONE = 0;
52     public static final int LEAD_DOTS = 1;
53     public static final int LEAD_HYPHENS = 2;
54     public static final int LEAD_UNDERLINE = 3;
55     public static final int LEAD_THICKLINE = 4;
56     public static final int LEAD_EQUALS = 5;
57
58     /** Tab type. */
59     private int alignment;
60     /** Location, from the left margin, that tab is at. */
61     private float position;
62     private int leader;
63
64     /**
65      * Creates a tab at position <code>pos</code> with a default alignment
66      * and default leader.
67      */

68     public TabStop(float pos) {
69         this(pos, ALIGN_LEFT, LEAD_NONE);
70     }
71
72     /**
73      * Creates a tab with the specified position <code>pos</code>,
74      * alignment <code>align</code> and leader <code>leader</code>.
75      */

76     public TabStop(float pos, int align, int leader) {
77     alignment = align;
78     this.leader = leader;
79     position = pos;
80     }
81
82     /**
83      * Returns the position, as a float, of the tab.
84      * @return the position of the tab
85      */

86     public float getPosition() {
87     return position;
88     }
89
90     /**
91      * Returns the alignment, as an integer, of the tab.
92      * @return the alignment of the tab
93      */

94     public int getAlignment() {
95     return alignment;
96     }
97
98     /**
99      * Returns the leader of the tab.
100      * @return the leader of the tab
101      */

102     public int getLeader() {
103     return leader;
104     }
105
106     /**
107      * Returns true if the tabs are equal.
108      * @return true if the tabs are equal, otherwise false
109      */

110     public boolean equals(Object JavaDoc other)
111     {
112     if (other == this) {
113         return true;
114     }
115     if (other instanceof TabStop JavaDoc) {
116         TabStop JavaDoc o = (TabStop JavaDoc)other;
117         return ( (alignment == o.alignment) &&
118              (leader == o.leader) &&
119              (position == o.position) ); /* TODO: epsilon */
120     }
121     return false;
122     }
123
124     /**
125      * Returns the hashCode for the object. This must be defined
126      * here to ensure 100% pure.
127      *
128      * @return the hashCode for the object
129      */

130     public int hashCode() {
131     return alignment ^ leader ^ Math.round(position);
132     }
133
134     /* This is for debugging; perhaps it should be removed before release */
135     public String JavaDoc toString() {
136     String JavaDoc buf;
137     switch(alignment) {
138       default:
139       case ALIGN_LEFT:
140         buf = "";
141         break;
142       case ALIGN_RIGHT:
143         buf = "right ";
144         break;
145       case ALIGN_CENTER:
146         buf = "center ";
147         break;
148       case ALIGN_DECIMAL:
149         buf = "decimal ";
150         break;
151       case ALIGN_BAR:
152         buf = "bar ";
153         break;
154     }
155     buf = buf + "tab @" + String.valueOf(position);
156     if (leader != LEAD_NONE)
157         buf = buf + " (w/leaders)";
158     return buf;
159     }
160 }
161
162
Popular Tags