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