1. /*
  2. * @(#)HRuleView.java 1.24 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.awt.*;
  9. import javax.swing.BorderFactory;
  10. import javax.swing.border.*;
  11. import javax.swing.text.*;
  12. import java.util.Enumeration;
  13. import java.lang.Integer;
  14. /**
  15. * A view implementation to display an html horizontal
  16. * rule.
  17. *
  18. * @author Timothy Prinzing
  19. * @author Sara Swanson
  20. * @version 1.24 11/29/01
  21. */
  22. class HRuleView extends View {
  23. /**
  24. * Creates a new view that represents an <hr> element.
  25. *
  26. * @param elem the element to create a view for
  27. */
  28. public HRuleView(Element elem) {
  29. super(elem);
  30. AttributeSet attr = elem.getAttributes();
  31. if (attr != null) {
  32. margin_left = 0;
  33. margin_right = 0;
  34. alignment = StyleConstants.getAlignment(attr);
  35. noshade = (String) attr.getAttribute("noshade");
  36. String sizestr = (String)attr.getAttribute("size");
  37. if (sizestr != null)
  38. size = (Integer.valueOf(sizestr)).intValue();
  39. String hrwidthstr = (String)attr.getAttribute("width");
  40. if (hrwidthstr != null)
  41. hrwidth = (Integer.valueOf(hrwidthstr)).intValue();
  42. }
  43. bevel = BorderFactory.createLoweredBevelBorder();
  44. }
  45. // --- View methods ---------------------------------------------
  46. /**
  47. * Paints the view.
  48. *
  49. * @param g the graphics context
  50. * @param a the allocation region for the view
  51. * @see View#paint
  52. */
  53. public void paint(Graphics g, Shape a) {
  54. Rectangle alloc = a.getBounds();
  55. int x = 0;
  56. int y = alloc.y + SPACE_ABOVE;
  57. int width = alloc.width - (int)(margin_left + margin_right);
  58. if (hrwidth > 0)
  59. width = hrwidth;
  60. int height = alloc.height - (SPACE_ABOVE + SPACE_BELOW);
  61. if (size > 0)
  62. height = size;
  63. // Align the rule horizontally.
  64. switch (alignment) {
  65. case StyleConstants.ALIGN_CENTER:
  66. x = alloc.x + (alloc.width / 2) - (width / 2);
  67. break;
  68. case StyleConstants.ALIGN_RIGHT:
  69. x = alloc.x + alloc.width - hrwidth - (int)(margin_right);
  70. break;
  71. case StyleConstants.ALIGN_LEFT:
  72. default:
  73. x = alloc.x + (int)margin_left;
  74. break;
  75. }
  76. // Paint either a shaded rule or a solid line.
  77. if (noshade == HTML.NULL_ATTRIBUTE_VALUE)
  78. g.fillRect(x, y, width, height);
  79. else
  80. bevel.paintBorder(getContainer(), g, x, y, width, height);
  81. }
  82. /**
  83. * Calculates the desired shape of the rule... this is
  84. * basically the preferred size of the border.
  85. *
  86. * @param axis may be either X_AXIS or Y_AXIS
  87. * @return the desired span
  88. * @see View#getPreferredSpan
  89. */
  90. public float getPreferredSpan(int axis) {
  91. Insets i = bevel.getBorderInsets(getContainer());
  92. switch (axis) {
  93. case View.X_AXIS:
  94. return i.left + i.right;
  95. case View.Y_AXIS:
  96. if (size > 0) {
  97. return size + SPACE_ABOVE + SPACE_BELOW;
  98. } else {
  99. if (noshade == HTML.NULL_ATTRIBUTE_VALUE) {
  100. return 1 + SPACE_ABOVE + SPACE_BELOW;
  101. } else {
  102. return i.top + i.bottom + SPACE_ABOVE + SPACE_BELOW;
  103. }
  104. }
  105. default:
  106. throw new IllegalArgumentException("Invalid axis: " + axis);
  107. }
  108. }
  109. /**
  110. * Gets the resize weight for the axis.
  111. * The rule is: rigid vertically and flexible horizontally.
  112. *
  113. * @param axis may be either X_AXIS or Y_AXIS
  114. * @return the weight
  115. */
  116. public int getResizeWeight(int axis) {
  117. if (axis == View.X_AXIS) {
  118. return 1;
  119. } else if (axis == View.Y_AXIS) {
  120. return 0;
  121. } else {
  122. return 0;
  123. }
  124. }
  125. /**
  126. * Determines how attractive a break opportunity in
  127. * this view is. This is implemented to request a forced break.
  128. *
  129. * @param axis may be either View.X_AXIS or View.Y_AXIS
  130. * @param pos the potential location of the start of the
  131. * broken view >= 0. This may be useful for calculating tab
  132. * positions.
  133. * @param len specifies the relative length from <em>pos</em>
  134. * where a potential break is desired >= 0.
  135. * @return the weight, which should be a value between
  136. * ForcedBreakWeight and BadBreakWeight.
  137. */
  138. public int getBreakWeight(int axis, float pos, float len) {
  139. if (axis == X_AXIS) {
  140. return ForcedBreakWeight;
  141. }
  142. return BadBreakWeight;
  143. }
  144. public View breakView(int axis, int offset, float pos, float len) {
  145. return null;
  146. }
  147. /**
  148. * Provides a mapping from the document model coordinate space
  149. * to the coordinate space of the view mapped to it.
  150. *
  151. * @param pos the position to convert
  152. * @param a the allocated region to render into
  153. * @return the bounding box of the given position
  154. * @exception BadLocationException if the given position does not
  155. * represent a valid location in the associated document
  156. * @see View#modelToView
  157. */
  158. public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
  159. int p0 = getStartOffset();
  160. int p1 = getEndOffset();
  161. if ((pos >= p0) && (pos <= p1)) {
  162. Rectangle r = a.getBounds();
  163. if (pos == p1) {
  164. r.x += r.width;
  165. }
  166. r.width = 0;
  167. return r;
  168. }
  169. return null;
  170. }
  171. /**
  172. * Provides a mapping from the view coordinate space to the logical
  173. * coordinate space of the model.
  174. *
  175. * @param x the X coordinate
  176. * @param y the Y coordinate
  177. * @param a the allocated region to render into
  178. * @return the location within the model that best represents the
  179. * given point of view
  180. * @see View#viewToModel
  181. */
  182. public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
  183. Rectangle alloc = (Rectangle) a;
  184. if (x < alloc.x + (alloc.width / 2)) {
  185. bias[0] = Position.Bias.Forward;
  186. return getStartOffset();
  187. }
  188. bias[0] = Position.Bias.Backward;
  189. return getEndOffset();
  190. }
  191. // --- variables ------------------------------------------------
  192. private Border bevel;
  193. private float margin_left = 0;
  194. private float margin_right = 0;
  195. private int alignment = StyleConstants.ALIGN_LEFT;
  196. private String noshade = null;
  197. private int size = 0;
  198. private int hrwidth = 0;
  199. private static final int SPACE_ABOVE = 3;
  200. private static final int SPACE_BELOW = 3;
  201. }