1. /*
  2. * @(#)CommentView.java 1.10 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.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.10, 01/23/03
  26. */
  27. class CommentView extends HiddenTagView {
  28. CommentView(Element e) {
  29. super(e);
  30. }
  31. protected Component createComponent() {
  32. JTextArea ta = new JTextArea(getRepresentedText());
  33. Document doc = getDocument();
  34. Font font;
  35. if (doc instanceof StyledDocument) {
  36. font = ((StyledDocument)doc).getFont(getAttributes());
  37. ta.setFont(font);
  38. }
  39. else {
  40. font = ta.getFont();
  41. }
  42. updateYAlign(font);
  43. ta.setBorder(CBorder);
  44. ta.getDocument().addDocumentListener(this);
  45. return ta;
  46. }
  47. void resetBorder() {
  48. }
  49. /**
  50. * This is subclassed to put the text on the Comment attribute of
  51. * the Element's AttributeSet.
  52. */
  53. void _updateModelFromText() {
  54. JTextComponent textC = getTextComponent();
  55. Document doc = getDocument();
  56. if (textC != null && doc != null) {
  57. String text = textC.getText();
  58. SimpleAttributeSet sas = new SimpleAttributeSet();
  59. isSettingAttributes = true;
  60. try {
  61. sas.addAttribute(HTML.Attribute.COMMENT, text);
  62. ((StyledDocument)doc).setCharacterAttributes
  63. (getStartOffset(), getEndOffset() -
  64. getStartOffset(), sas, false);
  65. }
  66. finally {
  67. isSettingAttributes = false;
  68. }
  69. }
  70. }
  71. JTextComponent getTextComponent() {
  72. return (JTextComponent)getComponent();
  73. }
  74. String getRepresentedText() {
  75. AttributeSet as = getElement().getAttributes();
  76. if (as != null) {
  77. Object comment = as.getAttribute(HTML.Attribute.COMMENT);
  78. if (comment instanceof String) {
  79. return (String)comment;
  80. }
  81. }
  82. return "";
  83. }
  84. static final Border CBorder = new CommentBorder();
  85. static final int commentPadding = 3;
  86. static final int commentPaddingD = commentPadding * 3;
  87. static class CommentBorder extends LineBorder {
  88. CommentBorder() {
  89. super(Color.black, 1);
  90. }
  91. public void paintBorder(Component c, Graphics g, int x, int y,
  92. int width, int height) {
  93. super.paintBorder(c, g, x + commentPadding, y,
  94. width - commentPaddingD, height);
  95. }
  96. public Insets getBorderInsets(Component c) {
  97. Insets retI = super.getBorderInsets(c);
  98. retI.left += commentPadding;
  99. retI.right += commentPadding;
  100. return retI;
  101. }
  102. public boolean isBorderOpaque() {
  103. return false;
  104. }
  105. } // End of class CommentView.CommentBorder
  106. } // End of CommentView