1 /*
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
26 package javax.swing.plaf.synth;
27
28 import java.awt;
29 import javax.swing;
30 import javax.swing.text;
31 import javax.swing.plaf;
32 import javax.swing.plaf.basic.BasicEditorPaneUI;
33 import java.beans.PropertyChangeEvent;
34 import sun.swing.plaf.synth.SynthUI;
35
36 /**
37 * Provides the look and feel for a JEditorPane in the
38 * Synth look and feel.
39 *
40 * @author Shannon Hickey
41 */
42 class SynthEditorPaneUI extends BasicEditorPaneUI implements SynthUI {
43 private SynthStyle style;
44 /*
45 * I would prefer to use UIResource instad of this.
46 * Unfortunately Boolean is a final class
47 */
48 private Boolean localTrue = new Boolean(true);
49 private Boolean localFalse = new Boolean(false);
50
51 /**
52 * Creates a UI for the JTextPane.
53 *
54 * @param c the JTextPane component
55 * @return the UI
56 */
57 public static ComponentUI createUI(JComponent c) {
58 return new SynthEditorPaneUI();
59 }
60
61 protected void installDefaults() {
62 // Installs the text cursor on the component
63 super.installDefaults();
64 JComponent c = getComponent();
65 Object clientProperty =
66 c.getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
67 if (clientProperty == null
68 || clientProperty == localFalse) {
69 c.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
70 localTrue);
71 }
72 updateStyle((JTextComponent)getComponent());
73 }
74
75 protected void uninstallDefaults() {
76 SynthContext context = getContext(getComponent(), ENABLED);
77 JComponent c = getComponent();
78 c.putClientProperty("caretAspectRatio", null);
79
80 style.uninstallDefaults(context);
81 context.dispose();
82 style = null;
83
84 Object clientProperty =
85 c.getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
86 if (clientProperty == localTrue) {
87 getComponent().putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
88 Boolean.FALSE);
89 }
90 super.uninstallDefaults();
91 }
92
93 /**
94 * This method gets called when a bound property is changed
95 * on the associated JTextComponent. This is a hook
96 * which UI implementations may change to reflect how the
97 * UI displays bound properties of JTextComponent subclasses.
98 * This is implemented to rebuild the ActionMap based upon an
99 * EditorKit change.
100 *
101 * @param evt the property change event
102 */
103 protected void propertyChange(PropertyChangeEvent evt) {
104 if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
105 updateStyle((JTextComponent)evt.getSource());
106 }
107 super.propertyChange(evt);
108 }
109
110 private void updateStyle(JTextComponent comp) {
111 SynthContext context = getContext(comp, ENABLED);
112 SynthStyle oldStyle = style;
113
114 style = SynthLookAndFeel.updateStyle(context, this);
115
116 if (style != oldStyle) {
117 SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix());
118
119 if (oldStyle != null) {
120 uninstallKeyboardActions();
121 installKeyboardActions();
122 }
123 }
124 context.dispose();
125 }
126
127 public SynthContext getContext(JComponent c) {
128 return getContext(c, getComponentState(c));
129 }
130
131 private SynthContext getContext(JComponent c, int state) {
132 return SynthContext.getContext(SynthContext.class, c,
133 SynthLookAndFeel.getRegion(c), style, state);
134 }
135
136 private int getComponentState(JComponent c) {
137 return SynthLookAndFeel.getComponentState(c);
138 }
139
140 public void update(Graphics g, JComponent c) {
141 SynthContext context = getContext(c);
142
143 SynthLookAndFeel.update(context, g);
144 paintBackground(context, g, c);
145 paint(context, g);
146 context.dispose();
147 }
148
149 protected void paint(SynthContext context, Graphics g) {
150 super.paint(g, getComponent());
151 }
152
153 protected void paintBackground(Graphics g) {
154 // Overriden to do nothing, all our painting is done from update/paint.
155 }
156
157 void paintBackground(SynthContext context, Graphics g, JComponent c) {
158 context.getPainter().paintEditorPaneBackground(context, g, 0, 0,
159 c.getWidth(), c.getHeight());
160 }
161
162 public void paintBorder(SynthContext context, Graphics g, int x,
163 int y, int w, int h) {
164 context.getPainter().paintEditorPaneBorder(context, g, x, y, w, h);
165 }
166 }