1. /*
  2. * @(#)InlineView.java 1.13 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.Shape;
  9. import java.awt.FontMetrics;
  10. import java.text.BreakIterator;
  11. import javax.swing.event.DocumentEvent;
  12. import javax.swing.text.*;
  13. /**
  14. * Displays the <dfn>inline element</dfn> styles
  15. * based upon css attributes.
  16. *
  17. * @author Timothy Prinzing
  18. * @version 1.13 11/29/01
  19. */
  20. public class InlineView extends LabelView {
  21. /**
  22. * Constructs a new view wrapped on an element.
  23. *
  24. * @param elem the element
  25. */
  26. public InlineView(Element elem) {
  27. super(elem);
  28. StyleSheet sheet = getStyleSheet();
  29. attr = sheet.getViewAttributes(this);
  30. }
  31. /**
  32. * Gives notification from the document that attributes were changed
  33. * in a location that this view is responsible for.
  34. *
  35. * @param e the change information from the associated document
  36. * @param a the current allocation of the view
  37. * @param f the factory to use to rebuild if the view has children
  38. * @see View#changedUpdate
  39. */
  40. public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
  41. super.changedUpdate(e, a, f);
  42. StyleSheet sheet = getStyleSheet();
  43. attr = sheet.getViewAttributes(this);
  44. preferenceChanged(null, true, true);
  45. }
  46. /**
  47. * Fetches the attributes to use when rendering. This is
  48. * implemented to multiplex the attributes specified in the
  49. * model with a StyleSheet.
  50. */
  51. public AttributeSet getAttributes() {
  52. return attr;
  53. }
  54. /**
  55. * Fetch the span of the longest word in the view.
  56. */
  57. float getLongestWordSpan() {
  58. // find the longest word
  59. float span = 0;
  60. try {
  61. Document doc = getDocument();
  62. int p0 = getStartOffset();
  63. int p1 = getEndOffset();
  64. String s = doc.getText(p0, p1 - p0);
  65. int word0 = p0;
  66. int word1 = p0;
  67. if(s != null && s.length() > 0) {
  68. BreakIterator words = BreakIterator.getWordInstance();
  69. words.setText(s);
  70. int start = words.first();
  71. for (int end = words.next(); end != BreakIterator.DONE;
  72. start = end, end = words.next()) {
  73. // update longest word boundary
  74. if ((end - start) > (word1 - word0)) {
  75. word0 = start;
  76. word1 = end;
  77. }
  78. }
  79. }
  80. // calculate the minimum
  81. if ((word1 - word0) > 0) {
  82. FontMetrics metrics = getFontMetrics();
  83. String word = s.substring(word0, word1);
  84. span = metrics.stringWidth(word);
  85. }
  86. } catch (BadLocationException ble) {
  87. // If the text can't be retrieved, it can't influence the size.
  88. }
  89. return span;
  90. }
  91. /**
  92. * Set the cached properties from the attributes.
  93. */
  94. protected void setPropertiesFromAttributes() {
  95. super.setPropertiesFromAttributes();
  96. AttributeSet a = getAttributes();
  97. Object decor = a.getAttribute(CSS.Attribute.TEXT_DECORATION);
  98. boolean u = (decor != null) ?
  99. (decor.toString().indexOf("underline") >= 0) : false;
  100. setUnderline(u);
  101. boolean s = (decor != null) ?
  102. (decor.toString().indexOf("line-through") >= 0) : false;
  103. setStrikeThrough(s);
  104. Object vAlign = a.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
  105. s = (vAlign != null) ? (vAlign.toString().indexOf("sup") >= 0) : false;
  106. setSuperscript(s);
  107. s = (vAlign != null) ? (vAlign.toString().indexOf("sub") >= 0) : false;
  108. setSubscript(s);
  109. }
  110. protected StyleSheet getStyleSheet() {
  111. HTMLDocument doc = (HTMLDocument) getDocument();
  112. return doc.getStyleSheet();
  113. }
  114. AttributeSet attr;
  115. }