001 /*
002 * Copyright 2002-2006 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 java.awt.*;
029 import javax.swing.*;
030 import javax.swing.text.*;
031 import javax.swing.plaf.*;
032 import javax.swing.plaf.basic.BasicEditorPaneUI;
033 import java.beans.PropertyChangeEvent;
034 import sun.swing.plaf.synth.SynthUI;
035
036 /**
037 * Provides the look and feel for a JEditorPane in the
038 * Synth look and feel.
039 *
040 * @author Shannon Hickey
041 * @version 1.16 05/05/07
042 */
043 class SynthEditorPaneUI extends BasicEditorPaneUI implements SynthUI {
044 private SynthStyle style;
045 /*
046 * I would prefer to use UIResource instad of this.
047 * Unfortunately Boolean is a final class
048 */
049 private Boolean localTrue = new Boolean(true);
050 private Boolean localFalse = new Boolean(false);
051
052 /**
053 * Creates a UI for the JTextPane.
054 *
055 * @param c the JTextPane component
056 * @return the UI
057 */
058 public static ComponentUI createUI(JComponent c) {
059 return new SynthEditorPaneUI();
060 }
061
062 protected void installDefaults() {
063 // Installs the text cursor on the component
064 super .installDefaults();
065 JComponent c = getComponent();
066 Object clientProperty = c
067 .getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
068 if (clientProperty == null || clientProperty == localFalse) {
069 c.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
070 localTrue);
071 }
072 updateStyle((JTextComponent) getComponent());
073 }
074
075 protected void uninstallDefaults() {
076 SynthContext context = getContext(getComponent(), ENABLED);
077 JComponent c = getComponent();
078 c.putClientProperty("caretAspectRatio", null);
079
080 style.uninstallDefaults(context);
081 context.dispose();
082 style = null;
083
084 Object clientProperty = c
085 .getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
086 if (clientProperty == localTrue) {
087 getComponent()
088 .putClientProperty(
089 JEditorPane.HONOR_DISPLAY_PROPERTIES,
090 Boolean.FALSE);
091 }
092 super .uninstallDefaults();
093 }
094
095 /**
096 * This method gets called when a bound property is changed
097 * on the associated JTextComponent. This is a hook
098 * which UI implementations may change to reflect how the
099 * UI displays bound properties of JTextComponent subclasses.
100 * This is implemented to rebuild the ActionMap based upon an
101 * EditorKit change.
102 *
103 * @param evt the property change event
104 */
105 protected void propertyChange(PropertyChangeEvent evt) {
106 if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
107 updateStyle((JTextComponent) evt.getSource());
108 }
109 super .propertyChange(evt);
110 }
111
112 private void updateStyle(JTextComponent comp) {
113 SynthContext context = getContext(comp, ENABLED);
114 SynthStyle oldStyle = style;
115
116 style = SynthLookAndFeel.updateStyle(context, this );
117
118 if (style != oldStyle) {
119 SynthTextFieldUI.updateStyle(comp, context,
120 getPropertyPrefix());
121
122 if (oldStyle != null) {
123 uninstallKeyboardActions();
124 installKeyboardActions();
125 }
126 }
127 context.dispose();
128 }
129
130 public SynthContext getContext(JComponent c) {
131 return getContext(c, getComponentState(c));
132 }
133
134 private SynthContext getContext(JComponent c, int state) {
135 return SynthContext.getContext(SynthContext.class, c,
136 SynthLookAndFeel.getRegion(c), style, state);
137 }
138
139 private int getComponentState(JComponent c) {
140 return SynthLookAndFeel.getComponentState(c);
141 }
142
143 public void update(Graphics g, JComponent c) {
144 SynthContext context = getContext(c);
145
146 SynthLookAndFeel.update(context, g);
147 paintBackground(context, g, c);
148 paint(context, g);
149 context.dispose();
150 }
151
152 protected void paint(SynthContext context, Graphics g) {
153 super .paint(g, getComponent());
154 }
155
156 protected void paintBackground(Graphics g) {
157 // Overriden to do nothing, all our painting is done from update/paint.
158 }
159
160 void paintBackground(SynthContext context, Graphics g, JComponent c) {
161 context.getPainter().paintEditorPaneBackground(context, g, 0,
162 0, c.getWidth(), c.getHeight());
163 }
164
165 public void paintBorder(SynthContext context, Graphics g, int x,
166 int y, int w, int h) {
167 context.getPainter().paintEditorPaneBorder(context, g, x, y, w,
168 h);
169 }
170 }
|