001 /*
002 * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.plaf.synth;
027
028 import javax.swing.*;
029 import javax.swing.text.*;
030 import javax.swing.plaf.*;
031 import java.beans.PropertyChangeEvent;
032 import java.awt.*;
033
034 /**
035 * Provides the look and feel for a styled text editor in the
036 * Synth look and feel.
037 * <p>
038 * <strong>Warning:</strong>
039 * Serialized objects of this class will not be compatible with
040 * future Swing releases. The current serialization support is
041 * appropriate for short term storage or RMI between applications running
042 * the same version of Swing. As of 1.4, support for long term storage
043 * of all JavaBeans<sup><font size="-2">TM</font></sup>
044 * has been added to the <code>java.beans</code> package.
045 * Please see {@link java.beans.XMLEncoder}.
046 *
047 * @author Shannon Hickey
048 * @version 1.15 05/05/07
049 */
050 class SynthTextPaneUI extends SynthEditorPaneUI {
051
052 /**
053 * Creates a UI for the JTextPane.
054 *
055 * @param c the JTextPane object
056 * @return the UI
057 */
058 public static ComponentUI createUI(JComponent c) {
059 return new SynthTextPaneUI();
060 }
061
062 /**
063 * Fetches the name used as a key to lookup properties through the
064 * UIManager. This is used as a prefix to all the standard
065 * text properties.
066 *
067 * @return the name ("TextPane")
068 */
069 protected String getPropertyPrefix() {
070 return "TextPane";
071 }
072
073 public void installUI(JComponent c) {
074 super .installUI(c);
075 updateForeground(c.getForeground());
076 updateFont(c.getFont());
077 }
078
079 /**
080 * This method gets called when a bound property is changed
081 * on the associated JTextComponent. This is a hook
082 * which UI implementations may change to reflect how the
083 * UI displays bound properties of JTextComponent subclasses.
084 * If the font, foreground or document has changed, the
085 * the appropriate property is set in the default style of
086 * the document.
087 *
088 * @param evt the property change event
089 */
090 protected void propertyChange(PropertyChangeEvent evt) {
091 super .propertyChange(evt);
092
093 String name = evt.getPropertyName();
094
095 if (name.equals("foreground")) {
096 updateForeground((Color) evt.getNewValue());
097 } else if (name.equals("font")) {
098 updateFont((Font) evt.getNewValue());
099 } else if (name.equals("document")) {
100 JComponent comp = getComponent();
101 updateForeground(comp.getForeground());
102 updateFont(comp.getFont());
103 }
104 }
105
106 /**
107 * Update the color in the default style of the document.
108 *
109 * @param color the new color to use or null to remove the color attribute
110 * from the document's style
111 */
112 private void updateForeground(Color color) {
113 StyledDocument doc = (StyledDocument) getComponent()
114 .getDocument();
115 Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
116
117 if (style == null) {
118 return;
119 }
120
121 if (color == null) {
122 style.removeAttribute(StyleConstants.Foreground);
123 } else {
124 StyleConstants.setForeground(style, color);
125 }
126 }
127
128 /**
129 * Update the font in the default style of the document.
130 *
131 * @param font the new font to use or null to remove the font attribute
132 * from the document's style
133 */
134 private void updateFont(Font font) {
135 StyledDocument doc = (StyledDocument) getComponent()
136 .getDocument();
137 Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
138
139 if (style == null) {
140 return;
141 }
142
143 if (font == null) {
144 style.removeAttribute(StyleConstants.FontFamily);
145 style.removeAttribute(StyleConstants.FontSize);
146 style.removeAttribute(StyleConstants.Bold);
147 style.removeAttribute(StyleConstants.Italic);
148 } else {
149 StyleConstants.setFontFamily(style, font.getName());
150 StyleConstants.setFontSize(style, font.getSize());
151 StyleConstants.setBold(style, font.isBold());
152 StyleConstants.setItalic(style, font.isItalic());
153 }
154 }
155
156 void paintBackground(SynthContext context, Graphics g, JComponent c) {
157 context.getPainter().paintTextPaneBackground(context, g, 0, 0,
158 c.getWidth(), c.getHeight());
159 }
160
161 public void paintBorder(SynthContext context, Graphics g, int x,
162 int y, int w, int h) {
163 context.getPainter()
164 .paintTextPaneBorder(context, g, x, y, w, h);
165 }
166 }
|