KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > html > CommentView


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
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.io.*;
12 import java.net.MalformedURLException JavaDoc;
13 import java.net.URL JavaDoc;
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 /**
21  * CommentView subclasses HiddenTagView to contain a JTextArea showing
22  * a comment. When the textarea is edited the comment is
23  * reset. As this inherits from EditableView if the JTextComponent is
24  * not editable, the textarea will not be visible.
25  *
26  * @author Scott Violet
27  * @version 1.13, 03/05/04
28  */

29 class CommentView extends HiddenTagView JavaDoc {
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     /**
60      * This is subclassed to put the text on the Comment attribute of
61      * the Element's AttributeSet.
62      */

63     void _updateModelFromText() {
64     JTextComponent textC = getTextComponent();
65     Document doc = getDocument();
66     if (textC != null && doc != null) {
67         String JavaDoc 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 JavaDoc getRepresentedText() {
87     AttributeSet as = getElement().getAttributes();
88     if (as != null) {
89         Object JavaDoc comment = as.getAttribute(HTML.Attribute.COMMENT);
90         if (comment instanceof String JavaDoc) {
91         return (String JavaDoc)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     } // End of class CommentView.CommentBorder
124
} // End of CommentView
125
Popular Tags