1. /*
  2. * @(#)TabSet.java 1.13 03/01/23
  3. *
  4. * Copyright 2003 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. * A TabSet is comprised of many TabStops. It offers methods for locating the
  11. * closest TabStop to a given position and finding all the potential TabStops.
  12. * It is also immutable.
  13. * <p>
  14. * <strong>Warning:</strong>
  15. * Serialized objects of this class will not be compatible with
  16. * future Swing releases. The current serialization support is
  17. * appropriate for short term storage or RMI between applications running
  18. * the same version of Swing. As of 1.4, support for long term storage
  19. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  20. * has been added to the <code>java.beans</code> package.
  21. * Please see {@link java.beans.XMLEncoder}.
  22. *
  23. * @author Scott Violet
  24. * @version 1.13 01/23/03
  25. */
  26. public class TabSet implements Serializable
  27. {
  28. /** TabStops this TabSet contains. */
  29. private TabStop[] tabs;
  30. /**
  31. * Creates and returns an instance of TabSet. The array of Tabs
  32. * passed in must be sorted in ascending order.
  33. */
  34. public TabSet(TabStop[] tabs) {
  35. // PENDING(sky): If this becomes a problem, make it sort.
  36. if(tabs != null) {
  37. int tabCount = tabs.length;
  38. this.tabs = new TabStop[tabCount];
  39. System.arraycopy(tabs, 0, this.tabs, 0, tabCount);
  40. }
  41. else
  42. this.tabs = null;
  43. }
  44. /**
  45. * Returns the number of Tab instances the receiver contains.
  46. */
  47. public int getTabCount() {
  48. return (tabs == null) ? 0 : tabs.length;
  49. }
  50. /**
  51. * Returns the TabStop at index <code>index</code>. This will throw an
  52. * IllegalArgumentException if <code>index</code> is outside the range
  53. * of tabs.
  54. */
  55. public TabStop getTab(int index) {
  56. int numTabs = getTabCount();
  57. if(index < 0 || index >= numTabs)
  58. throw new IllegalArgumentException(index +
  59. " is outside the range of tabs");
  60. return tabs[index];
  61. }
  62. /**
  63. * Returns the Tab instance after <code>location</code>. This will
  64. * return null if there are no tabs after <code>location</code>.
  65. */
  66. public TabStop getTabAfter(float location) {
  67. int index = getTabIndexAfter(location);
  68. return (index == -1) ? null : tabs[index];
  69. }
  70. /**
  71. * @return the index of the TabStop <code>tab</code>, or -1 if
  72. * <code>tab</code> is not contained in the receiver.
  73. */
  74. public int getTabIndex(TabStop tab) {
  75. for(int counter = getTabCount() - 1; counter >= 0; counter--)
  76. // should this use .equals?
  77. if(getTab(counter) == tab)
  78. return counter;
  79. return -1;
  80. }
  81. /**
  82. * Returns the index of the Tab to be used after <code>location</code>.
  83. * This will return -1 if there are no tabs after <code>location</code>.
  84. */
  85. public int getTabIndexAfter(float location) {
  86. int current, min, max;
  87. min = 0;
  88. max = getTabCount();
  89. while(min != max) {
  90. current = (max - min) / 2 + min;
  91. if(location > tabs[current].getPosition()) {
  92. if(min == current)
  93. min = max;
  94. else
  95. min = current;
  96. }
  97. else {
  98. if(current == 0 || location > tabs[current - 1].getPosition())
  99. return current;
  100. max = current;
  101. }
  102. }
  103. // no tabs after the passed in location.
  104. return -1;
  105. }
  106. /**
  107. * Returns the string representation of the set of tabs.
  108. */
  109. public String toString() {
  110. int tabCount = getTabCount();
  111. StringBuffer buffer = new StringBuffer("[ ");
  112. for(int counter = 0; counter < tabCount; counter++) {
  113. if(counter > 0)
  114. buffer.append(" - ");
  115. buffer.append(getTab(counter).toString());
  116. }
  117. buffer.append(" ]");
  118. return buffer.toString();
  119. }
  120. }