1. /*
  2. * @(#)CommentView.java 1.13 04/03/05
  3. *
  4. * Copyright 2004 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 java.awt.event.*;
  10. import java.io.*;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import javax.swing.text.*;
  14. import javax.swing.*;
  15. import javax.swing.border.*;
  16. import javax.swing.event.*;
  17. import java.util.*;
  18. /**
  19. * CommentView subclasses HiddenTagView to contain a JTextArea showing
  20. * a comment. When the textarea is edited the comment is
  21. * reset. As this inherits from EditableView if the JTextComponent is
  22. * not editable, the textarea will not be visible.
  23. *
  24. * @author Scott Violet
  25. * @version 1.13, 03/05/04
  26. */
  27. class CommentView extends HiddenTagView {
  28. CommentView(Element e) {
  29. super(e);
  30. }
  31. protected Component createComponent() {
  32. Container host = getContainer();
  33. if (host != null && !((JTextComponent)host).isEditable()) {
  34. return null;
  35. }
  36. JTextArea ta = new JTextArea(getRepresentedText());
  37. Document doc = getDocument();
  38. Font font;
  39. if (doc instanceof StyledDocument) {
  40. font = ((StyledDocument)doc).getFont(getAttributes());
  41. ta.setFont(font);
  42. }
  43. else {
  44. font = ta.getFont();
  45. }
  46. updateYAlign(font);
  47. ta.setBorder(CBorder);
  48. ta.getDocument().addDocumentListener(this);
  49. ta.setFocusable(isVisible());
  50. return ta;
  51. }
  52. void resetBorder() {
  53. }
  54. /**
  55. * This is subclassed to put the text on the Comment attribute of
  56. * the Element's AttributeSet.
  57. */
  58. void _updateModelFromText() {
  59. JTextComponent textC = getTextComponent();
  60. Document doc = getDocument();
  61. if (textC != null && doc != null) {
  62. String text = textC.getText();
  63. SimpleAttributeSet sas = new SimpleAttributeSet();
  64. isSettingAttributes = true;
  65. try {
  66. sas.addAttribute(HTML.Attribute.COMMENT, text);
  67. ((StyledDocument)doc).setCharacterAttributes
  68. (getStartOffset(), getEndOffset() -
  69. getStartOffset(), sas, false);
  70. }
  71. finally {
  72. isSettingAttributes = false;
  73. }
  74. }
  75. }
  76. JTextComponent getTextComponent() {
  77. return (JTextComponent)getComponent();
  78. }
  79. String getRepresentedText() {
  80. AttributeSet as = getElement().getAttributes();
  81. if (as != null) {
  82. Object comment = as.getAttribute(HTML.Attribute.COMMENT);
  83. if (comment instanceof String) {
  84. return (String)comment;
  85. }
  86. }
  87. return "";
  88. }
  89. static final Border CBorder = new CommentBorder();
  90. static final int commentPadding = 3;
  91. static final int commentPaddingD = commentPadding * 3;
  92. static class CommentBorder extends LineBorder {
  93. CommentBorder() {
  94. super(Color.black, 1);
  95. }
  96. public void paintBorder(Component c, Graphics g, int x, int y,
  97. int width, int height) {
  98. super.paintBorder(c, g, x + commentPadding, y,
  99. width - commentPaddingD, height);
  100. }
  101. public Insets getBorderInsets(Component c) {
  102. Insets retI = super.getBorderInsets(c);
  103. retI.left += commentPadding;
  104. retI.right += commentPadding;
  105. return retI;
  106. }
  107. public boolean isBorderOpaque() {
  108. return false;
  109. }
  110. } // End of class CommentView.CommentBorder
  111. } // End of CommentView