KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > SynthTextPaneUI


1 /*
2  * @(#)SynthTextPaneUI.java 1.8 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
8 package javax.swing.plaf.synth;
9
10 import javax.swing.*;
11 import javax.swing.text.*;
12 import javax.swing.plaf.*;
13 import java.beans.PropertyChangeEvent JavaDoc;
14 import java.awt.*;
15
16 /**
17  * Provides the look and feel for a styled text editor in the
18  * Synth look and feel.
19  * <p>
20  * <strong>Warning:</strong>
21  * Serialized objects of this class will not be compatible with
22  * future Swing releases. The current serialization support is
23  * appropriate for short term storage or RMI between applications running
24  * the same version of Swing. As of 1.4, support for long term storage
25  * of all JavaBeans<sup><font size="-2">TM</font></sup>
26  * has been added to the <code>java.beans</code> package.
27  * Please see {@link java.beans.XMLEncoder}.
28  *
29  * @author Shannon Hickey
30  * @version 1.8 12/19/03
31  */

32 class SynthTextPaneUI extends SynthEditorPaneUI JavaDoc {
33
34     /**
35      * Creates a UI for the JTextPane.
36      *
37      * @param c the JTextPane object
38      * @return the UI
39      */

40     public static ComponentUI createUI(JComponent c) {
41         return new SynthTextPaneUI JavaDoc();
42     }
43
44     /**
45      * Fetches the name used as a key to lookup properties through the
46      * UIManager. This is used as a prefix to all the standard
47      * text properties.
48      *
49      * @return the name ("TextPane")
50      */

51     protected String JavaDoc getPropertyPrefix() {
52         return "TextPane";
53     }
54
55     public void installUI(JComponent c) {
56         super.installUI(c);
57         updateForeground(c.getForeground());
58         updateFont(c.getFont());
59     }
60
61     /**
62      * This method gets called when a bound property is changed
63      * on the associated JTextComponent. This is a hook
64      * which UI implementations may change to reflect how the
65      * UI displays bound properties of JTextComponent subclasses.
66      * If the font, foreground or document has changed, the
67      * the appropriate property is set in the default style of
68      * the document.
69      *
70      * @param evt the property change event
71      */

72     protected void propertyChange(PropertyChangeEvent JavaDoc evt) {
73         super.propertyChange(evt);
74
75         String JavaDoc name = evt.getPropertyName();
76
77         if (name.equals("foreground")) {
78             updateForeground((Color)evt.getNewValue());
79         } else if (name.equals("font")) {
80             updateFont((Font)evt.getNewValue());
81         } else if (name.equals("document")) {
82             JComponent comp = getComponent();
83             updateForeground(comp.getForeground());
84             updateFont(comp.getFont());
85         }
86     }
87
88     /**
89      * Update the color in the default style of the document.
90      *
91      * @param color the new color to use or null to remove the color attribute
92      * from the document's style
93      */

94     private void updateForeground(Color color) {
95         StyledDocument doc = (StyledDocument)getComponent().getDocument();
96         Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
97
98         if (style == null) {
99             return;
100         }
101
102         if (color == null) {
103             style.removeAttribute(StyleConstants.Foreground);
104         } else {
105             StyleConstants.setForeground(style, color);
106         }
107     }
108     
109     /**
110      * Update the font in the default style of the document.
111      *
112      * @param font the new font to use or null to remove the font attribute
113      * from the document's style
114      */

115     private void updateFont(Font font) {
116         StyledDocument doc = (StyledDocument)getComponent().getDocument();
117         Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
118
119         if (style == null) {
120             return;
121         }
122
123         if (font == null) {
124             style.removeAttribute(StyleConstants.FontFamily);
125             style.removeAttribute(StyleConstants.FontSize);
126             style.removeAttribute(StyleConstants.Bold);
127             style.removeAttribute(StyleConstants.Italic);
128         } else {
129             StyleConstants.setFontFamily(style, font.getName());
130             StyleConstants.setFontSize(style, font.getSize());
131             StyleConstants.setBold(style, font.isBold());
132             StyleConstants.setItalic(style, font.isItalic());
133         }
134     }
135
136     void paintBackground(SynthContext JavaDoc context, Graphics g, JComponent c) {
137         context.getPainter().paintTextPaneBackground(context, g, 0, 0,
138                                                   c.getWidth(), c.getHeight());
139     }
140
141     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
142                             int y, int w, int h) {
143         context.getPainter().paintTextPaneBorder(context, g, x, y, w, h);
144     }
145 }
146
Popular Tags