1. /*
  2. * @(#)LineView.java 1.15 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.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.15 01/23/03
  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 <code>View.X_AXIS</code> or
  47. * <code>View.Y_AXIS</code>
  48. * @return the minimum span the view can be rendered into
  49. * @see View#getPreferredSpan
  50. */
  51. public float getMinimumSpan(int axis) {
  52. return getPreferredSpan(axis);
  53. }
  54. /**
  55. * Gets the resize weight for the specified axis.
  56. *
  57. * @param axis may be either X_AXIS or Y_AXIS
  58. * @return the weight
  59. */
  60. public int getResizeWeight(int axis) {
  61. switch (axis) {
  62. case View.X_AXIS:
  63. return 1;
  64. case View.Y_AXIS:
  65. return 0;
  66. default:
  67. throw new IllegalArgumentException("Invalid axis: " + axis);
  68. }
  69. }
  70. /**
  71. * Gets the alignment for an axis.
  72. *
  73. * @param axis may be either X_AXIS or Y_AXIS
  74. * @return the alignment
  75. */
  76. public float getAlignment(int axis) {
  77. if (axis == View.X_AXIS) {
  78. return 0;
  79. }
  80. return super.getAlignment(axis);
  81. }
  82. /**
  83. * Lays out the children. If the layout span has changed,
  84. * the rows are rebuilt. The superclass functionality
  85. * is called after checking and possibly rebuilding the
  86. * rows. If the height has changed, the
  87. * <code>preferenceChanged</code> method is called
  88. * on the parent since the vertical preference is
  89. * rigid.
  90. *
  91. * @param width the width to lay out against >= 0. This is
  92. * the width inside of the inset area.
  93. * @param height the height to lay out against >= 0 (not used
  94. * by paragraph, but used by the superclass). This
  95. * is the height inside of the inset area.
  96. */
  97. protected void layout(int width, int height) {
  98. super.layout(Integer.MAX_VALUE - 1, height);
  99. }
  100. /**
  101. * Returns the next tab stop position given a reference position.
  102. * This view implements the tab coordinate system, and calls
  103. * <code>getTabbedSpan</code> on the logical children in the process
  104. * of layout to determine the desired span of the children. The
  105. * logical children can delegate their tab expansion upward to
  106. * the paragraph which knows how to expand tabs.
  107. * <code>LabelView</code> is an example of a view that delegates
  108. * its tab expansion needs upward to the paragraph.
  109. * <p>
  110. * This is implemented to try and locate a <code>TabSet</code>
  111. * in the paragraph element's attribute set. If one can be
  112. * found, its settings will be used, otherwise a default expansion
  113. * will be provided. The base location for for tab expansion
  114. * is the left inset from the paragraphs most recent allocation
  115. * (which is what the layout of the children is based upon).
  116. *
  117. * @param x the X reference position
  118. * @param tabOffset the position within the text stream
  119. * that the tab occurred at >= 0.
  120. * @return the trailing end of the tab expansion >= 0
  121. * @see TabSet
  122. * @see TabStop
  123. * @see LabelView
  124. */
  125. public float nextTabStop(float x, int tabOffset) {
  126. // If the text isn't left justified, offset by 10 pixels!
  127. if (getTabSet() == null &&
  128. StyleConstants.getAlignment(getAttributes()) ==
  129. StyleConstants.ALIGN_LEFT) {
  130. return getPreTab(x, tabOffset);
  131. }
  132. return super.nextTabStop(x, tabOffset);
  133. }
  134. /**
  135. * Returns the location for the tab.
  136. */
  137. protected float getPreTab(float x, int tabOffset) {
  138. Document d = getDocument();
  139. View v = getViewAtPosition(tabOffset, null);
  140. if ((d instanceof StyledDocument) && v != null) {
  141. // Assume f is fixed point.
  142. Font f = ((StyledDocument)d).getFont(v.getAttributes());
  143. FontMetrics fm = Toolkit.getDefaultToolkit().
  144. getFontMetrics(f);
  145. int width = getCharactersPerTab() * fm.charWidth('W');
  146. int tb = (int)getTabBase();
  147. return (float)((((int)x - tb) / width + 1) * width + tb);
  148. }
  149. return 10.0f + x;
  150. }
  151. /**
  152. * @return number of characters per tab, 8.
  153. */
  154. protected int getCharactersPerTab() {
  155. return 8;
  156. }
  157. }