1 /* 2 * Copyright (c) 2002, 2014, 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 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 48 * has been added to the <code>java.beans</code> package. 49 * Please see {@link java.beans.XMLEncoder}. 50 * 51 * @author Shannon Hickey 52 * @since 1.7 53 */ 54 @SuppressWarnings("serial") // Same-version serialization only 55 public class SynthTextAreaUI extends BasicTextAreaUI implements SynthUI { 56 private Handler handler = new Handler(); 57 private SynthStyle style; 58 private boolean updateKBAction = true; 59 /** 60 * Creates a UI object for a JTextArea. 61 * 62 * @param ta a text area 63 * @return the UI object 64 */ 65 public static ComponentUI createUI(JComponent ta) { 66 return new SynthTextAreaUI(); 67 } 68 69 /** 70 * {@inheritDoc} 71 */ 72 @Override 73 protected void installDefaults() { 74 // Installs the text cursor on the component 75 super.installDefaults(); 76 updateStyle(getComponent(), true); 77 getComponent().addFocusListener(handler); 78 } 79 80 /** 81 * {@inheritDoc} 82 */ 83 @Override 84 protected void uninstallDefaults() { 85 SynthContext context = getContext(getComponent(), ENABLED); 86 87 getComponent().putClientProperty("caretAspectRatio", null); 88 getComponent().removeFocusListener(handler); 89 90 style.uninstallDefaults(context); 91 style = null; 92 super.uninstallDefaults(); 93 } 94 95 private void updateStyle(JTextComponent comp, boolean updateKBAction) { 96 SynthContext context = getContext(comp, ENABLED); 97 SynthStyle oldStyle = style; 98 99 style = SynthLookAndFeel.updateStyle(context, this); 100 101 if (style != oldStyle) { 102 SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix()); 103 104 if (oldStyle != null && updateKBAction) { 105 uninstallKeyboardActions(); 106 installKeyboardActions(); 107 } 108 } 109 } 110 111 /** 112 * {@inheritDoc} 113 */ 114 @Override 115 public SynthContext getContext(JComponent c) { 116 return getContext(c, SynthLookAndFeel.getComponentState(c)); 117 } 118 119 private SynthContext getContext(JComponent c, int state) { 120 return SynthContext.getContext(c, style, state); 121 } 122 123 /** 124 * Notifies this UI delegate to repaint the specified component. 125 * This method paints the component background, then calls 126 * the {@link #paint(SynthContext,Graphics)} method. 127 * 128 * <p>In general, this method does not need to be overridden by subclasses. 129 * All Look and Feel rendering code should reside in the {@code paint} method. 130 * 131 * @param g the {@code Graphics} object used for painting 132 * @param c the component being painted 133 * @see #paint(SynthContext,Graphics) 134 */ 135 @Override 136 public void update(Graphics g, JComponent c) { 137 SynthContext context = getContext(c); 138 139 SynthLookAndFeel.update(context, g); 140 context.getPainter().paintTextAreaBackground(context, 141 g, 0, 0, c.getWidth(), c.getHeight()); 142 paint(context, g); 143 } 144 145 /** 146 * Paints the specified component. 147 * 148 * @param context context for the component being painted 149 * @param g the {@code Graphics} object used for painting 150 * @see #update(Graphics,JComponent) 151 */ 152 protected void paint(SynthContext context, Graphics g) { 153 super.paint(g, getComponent()); 154 } 155 156 /** 157 * {@inheritDoc} 158 * 159 * Overridden to do nothing. 160 */ 161 @Override 162 protected void paintBackground(Graphics g) { 163 // Overriden to do nothing, all our painting is done from update/paint. 164 } 165 166 /** 167 * {@inheritDoc} 168 */ 169 @Override 170 public void paintBorder(SynthContext context, Graphics g, int x, 171 int y, int w, int h) { 172 context.getPainter().paintTextAreaBorder(context, g, x, y, w, h); 173 } 174 175 /** 176 * This method gets called when a bound property is changed 177 * on the associated JTextComponent. This is a hook 178 * which UI implementations may change to reflect how the 179 * UI displays bound properties of JTextComponent subclasses. 180 * This is implemented to rebuild the View when the 181 * <em>WrapLine</em> or the <em>WrapStyleWord</em> property changes. 182 * 183 * @param evt the property change event 184 */ 185 @Override 186 protected void propertyChange(PropertyChangeEvent evt) { 187 if (evt.getPropertyName().equals("keymap")) { 188 if (evt.getNewValue() != null) 189 { 190 updateKBAction = false; 191 } else { 192 updateKBAction = true; 193 } 194 } 195 if (SynthLookAndFeel.shouldUpdateStyle(evt)) { 196 updateStyle((JTextComponent)evt.getSource(), updateKBAction); 197 } 198 super.propertyChange(evt); 199 } 200 201 private final class Handler implements FocusListener { 202 public void focusGained(FocusEvent e) { 203 getComponent().repaint(); 204 } 205 206 public void focusLost(FocusEvent e) { 207 getComponent().repaint(); 208 } 209 } 210 }