1. /*
  2. * @(#)CommentView.java 1.6 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 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.6, 11/29/01
  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. void pushTextToModel() {
  50. if (!isSettingAttributes) {
  51. SimpleAttributeSet sas = new SimpleAttributeSet();
  52. String text = getTextComponent().getText();
  53. isSettingAttributes = true;
  54. try {
  55. sas.addAttribute(HTML.Attribute.COMMENT, text);
  56. ((StyledDocument)getDocument()).setCharacterAttributes
  57. (getStartOffset(), getEndOffset() -
  58. getStartOffset(), sas, false);
  59. }
  60. finally {
  61. isSettingAttributes = false;
  62. }
  63. }
  64. }
  65. JTextComponent getTextComponent() {
  66. return (JTextComponent)getComponent();
  67. }
  68. String getRepresentedText() {
  69. AttributeSet as = getElement().getAttributes();
  70. if (as != null) {
  71. Object comment = as.getAttribute(HTML.Attribute.COMMENT);
  72. if (comment instanceof String) {
  73. return (String)comment;
  74. }
  75. }
  76. return "";
  77. }
  78. static final Border CBorder = new CommentBorder();
  79. static final int commentPadding = 3;
  80. static final int commentPaddingD = commentPadding * 3;
  81. static class CommentBorder extends LineBorder {
  82. CommentBorder() {
  83. super(Color.black, 1);
  84. }
  85. public void paintBorder(Component c, Graphics g, int x, int y,
  86. int width, int height) {
  87. super.paintBorder(c, g, x + commentPadding, y,
  88. width - commentPaddingD, height);
  89. }
  90. public Insets getBorderInsets(Component c) {
  91. Insets retI = super.getBorderInsets(c);
  92. retI.left += commentPadding;
  93. retI.right += commentPadding;
  94. return retI;
  95. }
  96. public boolean isBorderOpaque() {
  97. return false;
  98. }
  99. } // End of class CommentView.CommentBorder
  100. } // End of CommentView