1 7 package javax.swing.text.html; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.io.*; 12 import java.net.MalformedURLException ; 13 import java.net.URL ; 14 import javax.swing.text.*; 15 import javax.swing.*; 16 import javax.swing.border.*; 17 import javax.swing.event.*; 18 import java.util.*; 19 20 29 class CommentView extends HiddenTagView { 30 CommentView(Element e) { 31 super(e); 32 } 33 34 protected Component createComponent() { 35 Container host = getContainer(); 36 if (host != null && !((JTextComponent)host).isEditable()) { 37 return null; 38 } 39 JTextArea ta = new JTextArea(getRepresentedText()); 40 Document doc = getDocument(); 41 Font font; 42 if (doc instanceof StyledDocument) { 43 font = ((StyledDocument)doc).getFont(getAttributes()); 44 ta.setFont(font); 45 } 46 else { 47 font = ta.getFont(); 48 } 49 updateYAlign(font); 50 ta.setBorder(CBorder); 51 ta.getDocument().addDocumentListener(this); 52 ta.setFocusable(isVisible()); 53 return ta; 54 } 55 56 void resetBorder() { 57 } 58 59 63 void _updateModelFromText() { 64 JTextComponent textC = getTextComponent(); 65 Document doc = getDocument(); 66 if (textC != null && doc != null) { 67 String text = textC.getText(); 68 SimpleAttributeSet sas = new SimpleAttributeSet(); 69 isSettingAttributes = true; 70 try { 71 sas.addAttribute(HTML.Attribute.COMMENT, text); 72 ((StyledDocument)doc).setCharacterAttributes 73 (getStartOffset(), getEndOffset() - 74 getStartOffset(), sas, false); 75 } 76 finally { 77 isSettingAttributes = false; 78 } 79 } 80 } 81 82 JTextComponent getTextComponent() { 83 return (JTextComponent)getComponent(); 84 } 85 86 String getRepresentedText() { 87 AttributeSet as = getElement().getAttributes(); 88 if (as != null) { 89 Object comment = as.getAttribute(HTML.Attribute.COMMENT); 90 if (comment instanceof String ) { 91 return (String )comment; 92 } 93 } 94 return ""; 95 } 96 97 static final Border CBorder = new CommentBorder(); 98 static final int commentPadding = 3; 99 static final int commentPaddingD = commentPadding * 3; 100 101 static class CommentBorder extends LineBorder { 102 CommentBorder() { 103 super(Color.black, 1); 104 } 105 106 public void paintBorder(Component c, Graphics g, int x, int y, 107 int width, int height) { 108 super.paintBorder(c, g, x + commentPadding, y, 109 width - commentPaddingD, height); 110 } 111 112 public Insets getBorderInsets(Component c) { 113 Insets retI = super.getBorderInsets(c); 114 115 retI.left += commentPadding; 116 retI.right += commentPadding; 117 return retI; 118 } 119 120 public boolean isBorderOpaque() { 121 return false; 122 } 123 } } | Popular Tags |