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