blob: f52d3a68b0dbca2daeff61a7c62bec2b1c20f77a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2006 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.event.*;
31import javax.swing.plaf.*;
32import javax.swing.plaf.basic.BasicTextAreaUI;
33import java.awt.*;
34import java.beans.PropertyChangeEvent;
35import sun.swing.plaf.synth.SynthUI;
36
37/**
38 * Provides the look and feel for a plain text editor in the
39 * Synth look and feel. In this implementation the default UI
40 * is extended to act as a simple view factory.
41 * <p>
42 * <strong>Warning:</strong>
43 * Serialized objects of this class will not be compatible with
44 * future Swing releases. The current serialization support is
45 * appropriate for short term storage or RMI between applications running
46 * the same version of Swing. As of 1.4, support for long term storage
47 * of all JavaBeans<sup><font size="-2">TM</font></sup>
48 * has been added to the <code>java.beans</code> package.
49 * Please see {@link java.beans.XMLEncoder}.
50 *
51 * @author Shannon Hickey
52 */
53class SynthTextAreaUI extends BasicTextAreaUI implements SynthUI {
54 private SynthStyle style;
55
56 /**
57 * Creates a UI for a JTextArea.
58 *
59 * @param ta a text area
60 * @return the UI
61 */
62 public static ComponentUI createUI(JComponent ta) {
63 return new SynthTextAreaUI();
64 }
65
66 protected void installDefaults() {
67 // Installs the text cursor on the component
68 super.installDefaults();
69 updateStyle((JTextComponent)getComponent());
70 }
71
72 protected void uninstallDefaults() {
73 SynthContext context = getContext(getComponent(), ENABLED);
74
75 getComponent().putClientProperty("caretAspectRatio", null);
76
77 style.uninstallDefaults(context);
78 context.dispose();
79 style = null;
80 super.uninstallDefaults();
81 }
82
83 public void installUI(JComponent c) {
84 super.installUI(c);
85 }
86
87 private void updateStyle(JTextComponent comp) {
88 SynthContext context = getContext(comp, ENABLED);
89 SynthStyle oldStyle = style;
90
91 style = SynthLookAndFeel.updateStyle(context, this);
92
93 if (style != oldStyle) {
94 SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix());
95
96 if (oldStyle != null) {
97 uninstallKeyboardActions();
98 installKeyboardActions();
99 }
100 }
101 context.dispose();
102 }
103
104 public SynthContext getContext(JComponent c) {
105 return getContext(c, getComponentState(c));
106 }
107
108 private SynthContext getContext(JComponent c, int state) {
109 return SynthContext.getContext(SynthContext.class, c,
110 SynthLookAndFeel.getRegion(c), style, state);
111 }
112
113 private int getComponentState(JComponent c) {
114 return SynthLookAndFeel.getComponentState(c);
115 }
116
117 public void update(Graphics g, JComponent c) {
118 SynthContext context = getContext(c);
119
120 SynthLookAndFeel.update(context, g);
121 context.getPainter().paintTextAreaBackground(context,
122 g, 0, 0, c.getWidth(), c.getHeight());
123 paint(context, g);
124 context.dispose();
125 }
126
127 protected void paint(SynthContext context, Graphics g) {
128 super.paint(g, getComponent());
129 }
130
131 protected void paintBackground(Graphics g) {
132 // Overriden to do nothing, all our painting is done from update/paint.
133 }
134
135 public void paintBorder(SynthContext context, Graphics g, int x,
136 int y, int w, int h) {
137 context.getPainter().paintTextAreaBorder(context, g, x, y, w, h);
138 }
139
140 /**
141 * This method gets called when a bound property is changed
142 * on the associated JTextComponent. This is a hook
143 * which UI implementations may change to reflect how the
144 * UI displays bound properties of JTextComponent subclasses.
145 * This is implemented to rebuild the View when the
146 * <em>WrapLine</em> or the <em>WrapStyleWord</em> property changes.
147 *
148 * @param evt the property change event
149 */
150 protected void propertyChange(PropertyChangeEvent evt) {
151 if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
152 updateStyle((JTextComponent)evt.getSource());
153 }
154 super.propertyChange(evt);
155 }
156}