1. /*
  2. * @(#)LineView.java 1.12 00/02/02
  3. *
  4. * Copyright 1997-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.html;
  11. import java.util.Enumeration;
  12. import java.awt.*;
  13. import javax.swing.*;
  14. import javax.swing.border.*;
  15. import javax.swing.event.*;
  16. import javax.swing.text.*;
  17. /**
  18. * A view implementation to display an unwrapped
  19. * preformatted line.<p>
  20. * This subclasses ParagraphView, but this really only contains one
  21. * Row of text.
  22. *
  23. * @author Timothy Prinzing
  24. * @version 1.12 02/02/00
  25. */
  26. class LineView extends ParagraphView {
  27. /** Last place painted at. */
  28. int tabBase;
  29. /**
  30. * Creates a LineView object.
  31. *
  32. * @param elem the element to wrap in a view
  33. */
  34. public LineView(Element elem) {
  35. super(elem);
  36. }
  37. /**
  38. * Preformatted lines are not suppressed if they
  39. * have only whitespace, so they are always visible.
  40. */
  41. public boolean isVisible() {
  42. return true;
  43. }
  44. /**
  45. * Determines the minimum span for this view along an
  46. * axis. The preformatted line should refuse to be
  47. * sized less than the preferred size.
  48. *
  49. * @param axis may be either View.X_AXIS or View.Y_AXIS
  50. * @returns the minimum span the view can be rendered into.
  51. * @see View#getPreferredSpan
  52. */
  53. public float getMinimumSpan(int axis) {
  54. return getPreferredSpan(axis);
  55. }
  56. /**
  57. * Gets the resize weight for the specified axis.
  58. *
  59. * @param axis may be either X_AXIS or Y_AXIS
  60. * @return the weight
  61. */
  62. public int getResizeWeight(int axis) {
  63. switch (axis) {
  64. case View.X_AXIS:
  65. return 1;
  66. case View.Y_AXIS:
  67. return 0;
  68. default:
  69. throw new IllegalArgumentException("Invalid axis: " + axis);
  70. }
  71. }
  72. /**
  73. * Gets the alignment for an axis.
  74. *
  75. * @param axis may be either X_AXIS or Y_AXIS
  76. * @return the alignment
  77. */
  78. public float getAlignment(int axis) {
  79. if (axis == View.X_AXIS) {
  80. return 0;
  81. }
  82. return super.getAlignment(axis);
  83. }
  84. /**
  85. * Lays out the children. If the layout span has changed,
  86. * the rows are rebuilt. The superclass functionality
  87. * is called after checking and possibly rebuilding the
  88. * rows. If the height has changed, the
  89. * <code>preferenceChanged</code> method is called
  90. * on the parent since the vertical preference is
  91. * rigid.
  92. *
  93. * @param width the width to lay out against >= 0. This is
  94. * the width inside of the inset area.
  95. * @param height the height to lay out against >= 0 (not used
  96. * by paragraph, but used by the superclass). This
  97. * is the height inside of the inset area.
  98. */
  99. protected void layout(int width, int height) {
  100. super.layout(Integer.MAX_VALUE - 1, height);
  101. }
  102. /**
  103. * Returns the next tab stop position given a reference position.
  104. * This view implements the tab coordinate system, and calls
  105. * <code>getTabbedSpan</code> on the logical children in the process
  106. * of layout to determine the desired span of the children. The
  107. * logical children can delegate their tab expansion upward to
  108. * the paragraph which knows how to expand tabs.
  109. * <code>LabelView</code> is an example of a view that delegates
  110. * its tab expansion needs upward to the paragraph.
  111. * <p>
  112. * This is implemented to try and locate a <code>TabSet</code>
  113. * in the paragraph element's attribute set. If one can be
  114. * found, its settings will be used, otherwise a default expansion
  115. * will be provided. The base location for for tab expansion
  116. * is the left inset from the paragraphs most recent allocation
  117. * (which is what the layout of the children is based upon).
  118. *
  119. * @param x the X reference position
  120. * @param tabOffset the position within the text stream
  121. * that the tab occurred at >= 0.
  122. * @return the trailing end of the tab expansion >= 0
  123. * @see TabSet
  124. * @see TabStop
  125. * @see LabelView
  126. */
  127. public float nextTabStop(float x, int tabOffset) {
  128. // If the text isn't left justified, offset by 10 pixels!
  129. if (getTabSet() == null &&
  130. StyleConstants.getAlignment(getAttributes()) ==
  131. StyleConstants.ALIGN_LEFT) {
  132. return getPreTab(x, tabOffset);
  133. }
  134. return super.nextTabStop(x, tabOffset);
  135. }
  136. /**
  137. * Returns the location for the tab.
  138. */
  139. protected float getPreTab(float x, int tabOffset) {
  140. Document d = getDocument();
  141. View v = getViewAtPosition(tabOffset, null);
  142. if ((d instanceof StyledDocument) && v != null) {
  143. // Assume f is fixed point.
  144. Font f = ((StyledDocument)d).getFont(v.getAttributes());
  145. FontMetrics fm = Toolkit.getDefaultToolkit().
  146. getFontMetrics(f);
  147. int width = getCharactersPerTab() * fm.charWidth('W');
  148. int tb = (int)getTabBase();
  149. return (float)((((int)x - tb) / width + 1) * width + tb);
  150. }
  151. return 10.0f + x;
  152. }
  153. /**
  154. * @return number of characters per tab, 8.
  155. */
  156. protected int getCharactersPerTab() {
  157. return 8;
  158. }
  159. }