blob: ad2e51b4d34c4385cd7de0538624e255e2e63c6f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.swing.plaf.synth;
27
28import javax.swing.*;
29import javax.swing.text.*;
30import javax.swing.plaf.*;
31import java.beans.PropertyChangeEvent;
32import java.awt.*;
33
34/**
35 * Provides the look and feel for a styled text editor in the
36 * Synth look and feel.
37 * <p>
38 * <strong>Warning:</strong>
39 * Serialized objects of this class will not be compatible with
40 * future Swing releases. The current serialization support is
41 * appropriate for short term storage or RMI between applications running
42 * the same version of Swing. As of 1.4, support for long term storage
43 * of all JavaBeans<sup><font size="-2">TM</font></sup>
44 * has been added to the <code>java.beans</code> package.
45 * Please see {@link java.beans.XMLEncoder}.
46 *
47 * @author Shannon Hickey
48 */
49class SynthTextPaneUI extends SynthEditorPaneUI {
50
51 /**
52 * Creates a UI for the JTextPane.
53 *
54 * @param c the JTextPane object
55 * @return the UI
56 */
57 public static ComponentUI createUI(JComponent c) {
58 return new SynthTextPaneUI();
59 }
60
61 /**
62 * Fetches the name used as a key to lookup properties through the
63 * UIManager. This is used as a prefix to all the standard
64 * text properties.
65 *
66 * @return the name ("TextPane")
67 */
68 protected String getPropertyPrefix() {
69 return "TextPane";
70 }
71
72 public void installUI(JComponent c) {
73 super.installUI(c);
74 updateForeground(c.getForeground());
75 updateFont(c.getFont());
76 }
77
78 /**
79 * This method gets called when a bound property is changed
80 * on the associated JTextComponent. This is a hook
81 * which UI implementations may change to reflect how the
82 * UI displays bound properties of JTextComponent subclasses.
83 * If the font, foreground or document has changed, the
84 * the appropriate property is set in the default style of
85 * the document.
86 *
87 * @param evt the property change event
88 */
89 protected void propertyChange(PropertyChangeEvent evt) {
90 super.propertyChange(evt);
91
92 String name = evt.getPropertyName();
93
94 if (name.equals("foreground")) {
95 updateForeground((Color)evt.getNewValue());
96 } else if (name.equals("font")) {
97 updateFont((Font)evt.getNewValue());
98 } else if (name.equals("document")) {
99 JComponent comp = getComponent();
100 updateForeground(comp.getForeground());
101 updateFont(comp.getFont());
102 }
103 }
104
105 /**
106 * Update the color in the default style of the document.
107 *
108 * @param color the new color to use or null to remove the color attribute
109 * from the document's style
110 */
111 private void updateForeground(Color color) {
112 StyledDocument doc = (StyledDocument)getComponent().getDocument();
113 Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
114
115 if (style == null) {
116 return;
117 }
118
119 if (color == null) {
120 style.removeAttribute(StyleConstants.Foreground);
121 } else {
122 StyleConstants.setForeground(style, color);
123 }
124 }
125
126 /**
127 * Update the font in the default style of the document.
128 *
129 * @param font the new font to use or null to remove the font attribute
130 * from the document's style
131 */
132 private void updateFont(Font font) {
133 StyledDocument doc = (StyledDocument)getComponent().getDocument();
134 Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
135
136 if (style == null) {
137 return;
138 }
139
140 if (font == null) {
141 style.removeAttribute(StyleConstants.FontFamily);
142 style.removeAttribute(StyleConstants.FontSize);
143 style.removeAttribute(StyleConstants.Bold);
144 style.removeAttribute(StyleConstants.Italic);
145 } else {
146 StyleConstants.setFontFamily(style, font.getName());
147 StyleConstants.setFontSize(style, font.getSize());
148 StyleConstants.setBold(style, font.isBold());
149 StyleConstants.setItalic(style, font.isItalic());
150 }
151 }
152
153 void paintBackground(SynthContext context, Graphics g, JComponent c) {
154 context.getPainter().paintTextPaneBackground(context, g, 0, 0,
155 c.getWidth(), c.getHeight());
156 }
157
158 public void paintBorder(SynthContext context, Graphics g, int x,
159 int y, int w, int h) {
160 context.getPainter().paintTextPaneBorder(context, g, x, y, w, h);
161 }
162}