KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)HiddenTagView.java 1.15 03/12/19
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  * HiddenTagView subclasses EditableView to contain a JTextField showing
22  * the element name. When the textfield is edited the element name is
23  * reset. As this inherits from EditableView if the JTextComponent is
24  * not editable, the textfield will not be visible.
25  *
26  * @author Scott Violet
27  * @version 1.15, 12/19/03
28  */

29 class HiddenTagView extends EditableView JavaDoc implements DocumentListener {
30     HiddenTagView(Element e) {
31     super(e);
32     yAlign = 1;
33     }
34
35     protected Component createComponent() {
36     JTextField tf = new JTextField(getElement().getName());
37     Document doc = getDocument();
38     Font font;
39     if (doc instanceof StyledDocument) {
40         font = ((StyledDocument)doc).getFont(getAttributes());
41         tf.setFont(font);
42     }
43     else {
44         font = tf.getFont();
45     }
46     tf.getDocument().addDocumentListener(this);
47     updateYAlign(font);
48
49     // Create a panel to wrap the textfield so that the textfields
50
// laf border shows through.
51
JPanel panel = new JPanel(new BorderLayout());
52     panel.setBackground(null);
53     if (isEndTag()) {
54         panel.setBorder(EndBorder);
55     }
56     else {
57         panel.setBorder(StartBorder);
58     }
59     panel.add(tf);
60     return panel;
61     }
62
63     public float getAlignment(int axis) {
64     if (axis == View.Y_AXIS) {
65         return yAlign;
66     }
67     return 0.5f;
68     }
69
70     public float getMinimumSpan(int axis) {
71     if (axis == View.X_AXIS && isVisible()) {
72         // Default to preferred.
73
return Math.max(30, super.getPreferredSpan(axis));
74     }
75     return super.getMinimumSpan(axis);
76     }
77
78     public float getPreferredSpan(int axis) {
79     if (axis == View.X_AXIS && isVisible()) {
80         return Math.max(30, super.getPreferredSpan(axis));
81     }
82     return super.getPreferredSpan(axis);
83     }
84
85     public float getMaximumSpan(int axis) {
86     if (axis == View.X_AXIS && isVisible()) {
87         // Default to preferred.
88
return Math.max(30, super.getMaximumSpan(axis));
89     }
90     return super.getMaximumSpan(axis);
91     }
92
93     // DocumentListener methods
94
public void insertUpdate(DocumentEvent e) {
95     updateModelFromText();
96     }
97
98     public void removeUpdate(DocumentEvent e) {
99     updateModelFromText();
100     }
101
102     public void changedUpdate(DocumentEvent e) {
103     updateModelFromText();
104     }
105
106     // View method
107
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
108     if (!isSettingAttributes) {
109         setTextFromModel();
110     }
111     }
112
113     // local methods
114

115     void updateYAlign(Font font) {
116         Container c = getContainer();
117     FontMetrics fm = (c != null) ? c.getFontMetrics(font) :
118             Toolkit.getDefaultToolkit().getFontMetrics(font);
119     float h = fm.getHeight();
120     float d = fm.getDescent();
121     yAlign = (h - d) / h;
122     }
123
124     void resetBorder() {
125     Component comp = getComponent();
126
127     if (comp != null) {
128         if (isEndTag()) {
129         ((JPanel)comp).setBorder(EndBorder);
130         }
131         else {
132         ((JPanel)comp).setBorder(StartBorder);
133         }
134     }
135     }
136
137     /**
138      * This resets the text on the text component we created to match
139      * that of the AttributeSet for the Element we represent.
140      * <p>If this is invoked on the event dispatching thread, this
141      * directly invokes <code>_setTextFromModel</code>, otherwise
142      * <code>SwingUtilities.invokeLater</code> is used to schedule execution
143      * of <code>_setTextFromModel</code>.
144      */

145     void setTextFromModel() {
146     if (SwingUtilities.isEventDispatchThread()) {
147         _setTextFromModel();
148     }
149     else {
150         SwingUtilities.invokeLater(new Runnable JavaDoc() {
151         public void run() {
152             _setTextFromModel();
153         }
154         });
155     }
156     }
157
158     /**
159      * This resets the text on the text component we created to match
160      * that of the AttributeSet for the Element we represent.
161      */

162     void _setTextFromModel() {
163     Document doc = getDocument();
164     try {
165         isSettingAttributes = true;
166         if (doc instanceof AbstractDocument) {
167         ((AbstractDocument)doc).readLock();
168         }
169         JTextComponent text = getTextComponent();
170         if (text != null) {
171         text.setText(getRepresentedText());
172         resetBorder();
173         Container host = getContainer();
174         if (host != null) {
175             preferenceChanged(this, true, true);
176             host.repaint();
177         }
178         }
179     }
180     finally {
181         isSettingAttributes = false;
182         if (doc instanceof AbstractDocument) {
183         ((AbstractDocument)doc).readUnlock();
184         }
185     }
186     }
187
188     /**
189      * This copies the text from the text component we've created
190      * to the Element's AttributeSet we represent.
191      * <p>If this is invoked on the event dispatching thread, this
192      * directly invokes <code>_updateModelFromText</code>, otherwise
193      * <code>SwingUtilities.invokeLater</code> is used to schedule execution
194      * of <code>_updateModelFromText</code>.
195      */

196     void updateModelFromText() {
197     if (!isSettingAttributes) {
198         if (SwingUtilities.isEventDispatchThread()) {
199         _updateModelFromText();
200         }
201         else {
202         SwingUtilities.invokeLater(new Runnable JavaDoc() {
203             public void run() {
204             _updateModelFromText();
205             }
206         });
207         }
208     }
209     }
210
211     /**
212      * This copies the text from the text component we've created
213      * to the Element's AttributeSet we represent.
214      */

215     void _updateModelFromText() {
216     Document doc = getDocument();
217     Object JavaDoc name = getElement().getAttributes().getAttribute
218         (StyleConstants.NameAttribute);
219     if ((name instanceof HTML.UnknownTag JavaDoc) &&
220         (doc instanceof StyledDocument)) {
221         SimpleAttributeSet sas = new SimpleAttributeSet();
222         JTextComponent textComponent = getTextComponent();
223         if (textComponent != null) {
224         String JavaDoc text = textComponent.getText();
225         isSettingAttributes = true;
226         try {
227             sas.addAttribute(StyleConstants.NameAttribute,
228                      new HTML.UnknownTag JavaDoc(text));
229             ((StyledDocument)doc).setCharacterAttributes
230             (getStartOffset(), getEndOffset() -
231              getStartOffset(), sas, false);
232         }
233         finally {
234             isSettingAttributes = false;
235         }
236         }
237     }
238     }
239
240     JTextComponent getTextComponent() {
241     Component comp = getComponent();
242
243     return (comp == null) ? null : (JTextComponent)((Container)comp).
244                                    getComponent(0);
245     }
246
247     String JavaDoc getRepresentedText() {
248     String JavaDoc retValue = getElement().getName();
249     return (retValue == null) ? "" : retValue;
250     }
251
252     boolean isEndTag() {
253     AttributeSet as = getElement().getAttributes();
254     if (as != null) {
255         Object JavaDoc end = as.getAttribute(HTML.Attribute.ENDTAG);
256         if (end != null && (end instanceof String JavaDoc) &&
257         ((String JavaDoc)end).equals("true")) {
258         return true;
259         }
260     }
261     return false;
262     }
263
264     /** Alignment along the y axis, based on the font of the textfield. */
265     float yAlign;
266     /** Set to true when setting attributes. */
267     boolean isSettingAttributes;
268
269
270     // Following are for Borders that used for Unknown tags and comments.
271
//
272
// Border defines
273
static final int circleR = 3;
274     static final int circleD = circleR * 2;
275     static final int tagSize = 6;
276     static final int padding = 3;
277     static final Color UnknownTagBorderColor = Color.black;
278     static final Border StartBorder = new StartTagBorder();
279     static final Border EndBorder = new EndTagBorder();
280
281
282     static class StartTagBorder implements Border, Serializable {
283     public void paintBorder(Component c, Graphics g, int x, int y,
284                 int width, int height) {
285         g.setColor(UnknownTagBorderColor);
286         x += padding;
287         width -= (padding * 2);
288         g.drawLine(x, y + circleR,
289                x, y + height - circleR);
290         g.drawArc(x, y + height - circleD - 1,
291               circleD, circleD, 180, 90);
292         g.drawArc(x, y, circleD, circleD, 90, 90);
293         g.drawLine(x + circleR, y, x + width - tagSize, y);
294         g.drawLine(x + circleR, y + height - 1,
295                x + width - tagSize, y + height - 1);
296         
297         g.drawLine(x + width - tagSize, y,
298                x + width - 1, y + height / 2);
299         g.drawLine(x + width - tagSize, y + height,
300                x + width - 1, y + height / 2);
301     }
302
303     public Insets getBorderInsets(Component c) {
304         return new Insets(2, 2 + padding, 2, tagSize + 2 + padding);
305     }
306
307     public boolean isBorderOpaque() {
308         return false;
309     }
310     } // End of class HiddenTagView.StartTagBorder
311

312
313     static class EndTagBorder implements Border, Serializable {
314     public void paintBorder(Component c, Graphics g, int x, int y,
315                 int width, int height) {
316         g.setColor(UnknownTagBorderColor);
317         x += padding;
318         width -= (padding * 2);
319         g.drawLine(x + width - 1, y + circleR,
320                x + width - 1, y + height - circleR);
321         g.drawArc(x + width - circleD - 1, y + height - circleD - 1,
322               circleD, circleD, 270, 90);
323         g.drawArc(x + width - circleD - 1, y, circleD, circleD, 0, 90);
324         g.drawLine(x + tagSize, y, x + width - circleR, y);
325         g.drawLine(x + tagSize, y + height - 1,
326                x + width - circleR, y + height - 1);
327         
328         g.drawLine(x + tagSize, y,
329                x, y + height / 2);
330         g.drawLine(x + tagSize, y + height,
331                x, y + height / 2);
332     }
333
334     public Insets getBorderInsets(Component c) {
335         return new Insets(2, tagSize + 2 + padding, 2, 2 + padding);
336     }
337
338     public boolean isBorderOpaque() {
339         return false;
340     }
341     } // End of class HiddenTagView.EndTagBorder
342

343
344 } // End of HiddenTagView
345
Popular Tags