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