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