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