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. import java.io.Serializable;
  9. /**
  10. * This class encapsulates a single tab stop (basically as tab stops
  11. * are thought of by RTF). A tab stop is at a specified distance from the
  12. * left margin, aligns text in a specified way, and has a specified leader.
  13. * TabStops are immutable, and usually contained in TabSets.
  14. * <p>
  15. * <strong>Warning:</strong>
  16. * Serialized objects of this class will not be compatible with
  17. * future Swing releases. The current serialization support is
  18. * appropriate for short term storage or RMI between applications running
  19. * the same version of Swing. As of 1.4, support for long term storage
  20. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  21. * has been added to the <code>java.beans</code> package.
  22. * Please see {@link java.beans.XMLEncoder}.
  23. *
  24. * @version 1.20 12/19/03
  25. */
  26. public class TabStop implements Serializable {
  27. /** Character following tab is positioned at location. */
  28. public static final int ALIGN_LEFT = 0;
  29. /** Characters following tab are positioned such that all following
  30. * characters up to next tab/newline end at location. */
  31. public static final int ALIGN_RIGHT = 1;
  32. /** Characters following tab are positioned such that all following
  33. * characters up to next tab/newline are centered around the tabs
  34. * location. */
  35. public static final int ALIGN_CENTER = 2;
  36. /** Characters following tab are aligned such that next
  37. * decimal/tab/newline is at the tab location, very similar to
  38. * RIGHT_TAB, just includes decimal as additional character to look for.
  39. */
  40. public static final int ALIGN_DECIMAL = 4;
  41. public static final int ALIGN_BAR = 5;
  42. /* Bar tabs (whatever they are) are actually a separate kind of tab
  43. in the RTF spec. However, being a bar tab and having alignment
  44. properties are mutually exclusive, so the reader treats barness
  45. as being a kind of alignment. */
  46. public static final int LEAD_NONE = 0;
  47. public static final int LEAD_DOTS = 1;
  48. public static final int LEAD_HYPHENS = 2;
  49. public static final int LEAD_UNDERLINE = 3;
  50. public static final int LEAD_THICKLINE = 4;
  51. public static final int LEAD_EQUALS = 5;
  52. /** Tab type. */
  53. private int alignment;
  54. /** Location, from the left margin, that tab is at. */
  55. private float position;
  56. private int leader;
  57. /**
  58. * Creates a tab at position <code>pos</code> with a default alignment
  59. * and default leader.
  60. */
  61. public TabStop(float pos) {
  62. this(pos, ALIGN_LEFT, LEAD_NONE);
  63. }
  64. /**
  65. * Creates a tab with the specified position <code>pos</code>,
  66. * alignment <code>align</code> and leader <code>leader</code>.
  67. */
  68. public TabStop(float pos, int align, int leader) {
  69. alignment = align;
  70. this.leader = leader;
  71. position = pos;
  72. }
  73. /**
  74. * Returns the position, as a float, of the tab.
  75. * @return the position of the tab
  76. */
  77. public float getPosition() {
  78. return position;
  79. }
  80. /**
  81. * Returns the alignment, as an integer, of the tab.
  82. * @return the alignment of the tab
  83. */
  84. public int getAlignment() {
  85. return alignment;
  86. }
  87. /**
  88. * Returns the leader of the tab.
  89. * @return the leader of the tab
  90. */
  91. public int getLeader() {
  92. return leader;
  93. }
  94. /**
  95. * Returns true if the tabs are equal.
  96. * @return true if the tabs are equal, otherwise false
  97. */
  98. public boolean equals(Object other)
  99. {
  100. if (other == this) {
  101. return true;
  102. }
  103. if (other instanceof TabStop) {
  104. TabStop o = (TabStop)other;
  105. return ( (alignment == o.alignment) &&
  106. (leader == o.leader) &&
  107. (position == o.position) ); /* TODO: epsilon */
  108. }
  109. return false;
  110. }
  111. /**
  112. * Returns the hashCode for the object. This must be defined
  113. * here to ensure 100% pure.
  114. *
  115. * @return the hashCode for the object
  116. */
  117. public int hashCode() {
  118. return alignment ^ leader ^ Math.round(position);
  119. }
  120. /* This is for debugging; perhaps it should be removed before release */
  121. public String toString() {
  122. String buf;
  123. switch(alignment) {
  124. default:
  125. case ALIGN_LEFT:
  126. buf = "";
  127. break;
  128. case ALIGN_RIGHT:
  129. buf = "right ";
  130. break;
  131. case ALIGN_CENTER:
  132. buf = "center ";
  133. break;
  134. case ALIGN_DECIMAL:
  135. buf = "decimal ";
  136. break;
  137. case ALIGN_BAR:
  138. buf = "bar ";
  139. break;
  140. }
  141. buf = buf + "tab @" + String.valueOf(position);
  142. if (leader != LEAD_NONE)
  143. buf = buf + " (w/leaders)";
  144. return buf;
  145. }
  146. }