1. /*
  2. * @(#)InlineView.java 1.19 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.html;
  11. import java.awt.Shape;
  12. import java.awt.FontMetrics;
  13. import java.text.BreakIterator;
  14. import javax.swing.event.DocumentEvent;
  15. import javax.swing.text.*;
  16. /**
  17. * Displays the <dfn>inline element</dfn> styles
  18. * based upon css attributes.
  19. *
  20. * @author Timothy Prinzing
  21. * @version 1.19 02/02/00
  22. */
  23. public class InlineView extends LabelView {
  24. /**
  25. * Constructs a new view wrapped on an element.
  26. *
  27. * @param elem the element
  28. */
  29. public InlineView(Element elem) {
  30. super(elem);
  31. StyleSheet sheet = getStyleSheet();
  32. attr = sheet.getViewAttributes(this);
  33. }
  34. /**
  35. * Gives notification from the document that attributes were changed
  36. * in a location that this view is responsible for.
  37. *
  38. * @param e the change information from the associated document
  39. * @param a the current allocation of the view
  40. * @param f the factory to use to rebuild if the view has children
  41. * @see View#changedUpdate
  42. */
  43. public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  44. super.changedUpdate(e, a, f);
  45. StyleSheet sheet = getStyleSheet();
  46. attr = sheet.getViewAttributes(this);
  47. preferenceChanged(null, true, true);
  48. }
  49. /**
  50. * Fetches the attributes to use when rendering. This is
  51. * implemented to multiplex the attributes specified in the
  52. * model with a StyleSheet.
  53. */
  54. public AttributeSet getAttributes() {
  55. return attr;
  56. }
  57. /**
  58. * Determines how attractive a break opportunity in
  59. * this view is. This can be used for determining which
  60. * view is the most attractive to call <code>breakView</code>
  61. * on in the process of formatting. A view that represents
  62. * text that has whitespace in it might be more attractive
  63. * than a view that has no whitespace, for example. The
  64. * higher the weight, the more attractive the break. A
  65. * value equal to or lower than <code>BadBreakWeight</code>
  66. * should not be considered for a break. A value greater
  67. * than or equal to <code>ForcedBreakWeight</code> should
  68. * be broken.
  69. * <p>
  70. * This is implemented to provide the default behavior
  71. * of returning <code>BadBreakWeight</code> unless the length
  72. * is greater than the length of the view in which case the
  73. * entire view represents the fragment. Unless a view has
  74. * been written to support breaking behavior, it is not
  75. * attractive to try and break the view. An example of
  76. * a view that does support breaking is <code>LabelView</code>.
  77. * An example of a view that uses break weight is
  78. * <code>ParagraphView</code>.
  79. *
  80. * @param axis may be either View.X_AXIS or View.Y_AXIS
  81. * @param pos the potential location of the start of the
  82. * broken view >= 0. This may be useful for calculating tab
  83. * positions.
  84. * @param len specifies the relative length from <em>pos</em>
  85. * where a potential break is desired >= 0.
  86. * @return the weight, which should be a value between
  87. * ForcedBreakWeight and BadBreakWeight.
  88. * @see LabelView
  89. * @see ParagraphView
  90. * @see javax.swing.text.View#BadBreakWeight
  91. * @see javax.swing.text.View#GoodBreakWeight
  92. * @see javax.swing.text.View#ExcellentBreakWeight
  93. * @see javax.swing.text.View#ForcedBreakWeight
  94. */
  95. public int getBreakWeight(int axis, float pos, float len) {
  96. if (nowrap) {
  97. return BadBreakWeight;
  98. }
  99. return super.getBreakWeight(axis, pos, len);
  100. }
  101. /**
  102. * Fetch the span of the longest word in the view.
  103. */
  104. float getLongestWordSpan() {
  105. // find the longest word
  106. float span = 0;
  107. try {
  108. Document doc = getDocument();
  109. int p0 = getStartOffset();
  110. int p1 = getEndOffset();
  111. if (p1 > p0) {
  112. Segment segment = new Segment();
  113. doc.getText(p0, p1 - p0, segment);
  114. int word0 = p0;
  115. int word1 = p0;
  116. BreakIterator words = BreakIterator.getWordInstance();
  117. words.setText(segment);
  118. int start = words.first();
  119. for (int end = words.next(); end != BreakIterator.DONE;
  120. start = end, end = words.next()) {
  121. // update longest word boundary
  122. if ((end - start) > (word1 - word0)) {
  123. word0 = start;
  124. word1 = end;
  125. }
  126. }
  127. // calculate the minimum
  128. if ((word1 - word0) > 0) {
  129. FontMetrics metrics = getFontMetrics();
  130. int offs = segment.offset + word0 - segment.getBeginIndex();
  131. span = metrics.charsWidth(segment.array, offs, word1 - word0);
  132. }
  133. }
  134. } catch (BadLocationException ble) {
  135. // If the text can't be retrieved, it can't influence the size.
  136. }
  137. return span;
  138. }
  139. /**
  140. * Set the cached properties from the attributes.
  141. */
  142. protected void setPropertiesFromAttributes() {
  143. super.setPropertiesFromAttributes();
  144. AttributeSet a = getAttributes();
  145. Object decor = a.getAttribute(CSS.Attribute.TEXT_DECORATION);
  146. boolean u = (decor != null) ?
  147. (decor.toString().indexOf("underline") >= 0) : false;
  148. setUnderline(u);
  149. boolean s = (decor != null) ?
  150. (decor.toString().indexOf("line-through") >= 0) : false;
  151. setStrikeThrough(s);
  152. Object vAlign = a.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
  153. s = (vAlign != null) ? (vAlign.toString().indexOf("sup") >= 0) : false;
  154. setSuperscript(s);
  155. s = (vAlign != null) ? (vAlign.toString().indexOf("sub") >= 0) : false;
  156. setSubscript(s);
  157. Object whitespace = a.getAttribute(CSS.Attribute.WHITE_SPACE);
  158. if ((whitespace != null) && whitespace.equals("nowrap")) {
  159. nowrap = true;
  160. } else {
  161. nowrap = false;
  162. }
  163. }
  164. protected StyleSheet getStyleSheet() {
  165. HTMLDocument doc = (HTMLDocument) getDocument();
  166. return doc.getStyleSheet();
  167. }
  168. private boolean nowrap;
  169. private AttributeSet attr;
  170. }