254N/A/*
553N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
254N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
254N/A *
254N/A * This code is free software; you can redistribute it and/or modify it
254N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
254N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
254N/A *
254N/A * This code is distributed in the hope that it will be useful, but WITHOUT
254N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
254N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
254N/A * version 2 for more details (a copy is included in the LICENSE file that
254N/A * accompanied this code).
254N/A *
254N/A * You should have received a copy of the GNU General Public License version
254N/A * 2 along with this work; if not, write to the Free Software Foundation,
254N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
254N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
254N/A */
254N/A
254N/Apackage javax.swing.plaf.synth;
254N/A
254N/Aimport javax.swing.*;
971N/Aimport javax.swing.text.*;
254N/Aimport javax.swing.plaf.*;
580N/Aimport javax.swing.plaf.basic.BasicTextFieldUI;
580N/Aimport java.awt.*;
254N/Aimport java.awt.event.FocusEvent;
254N/Aimport java.awt.event.FocusListener;
254N/Aimport java.beans.PropertyChangeEvent;
254N/A
254N/A
254N/A/**
254N/A * Provides the Synth L&F UI delegate for {@link javax.swing.JTextField}.
254N/A * <p>
254N/A * <strong>Warning:</strong>
254N/A * Serialized objects of this class will not be compatible with
254N/A * future Swing releases. The current serialization support is
254N/A * appropriate for short term storage or RMI between applications running
254N/A * the same version of Swing. As of 1.4, support for long term storage
254N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
254N/A * has been added to the <code>java.beans</code> package.
254N/A * Please see {@link java.beans.XMLEncoder}.
254N/A *
254N/A * @author Shannon Hickey
254N/A * @since 1.7
254N/A */
254N/Apublic class SynthTextFieldUI extends BasicTextFieldUI implements SynthUI {
254N/A private Handler handler = new Handler();
254N/A private SynthStyle style;
254N/A
254N/A /**
254N/A * Creates a UI for a JTextField.
254N/A *
254N/A * @param c the text field
254N/A * @return the UI object
254N/A */
254N/A public static ComponentUI createUI(JComponent c) {
254N/A return new SynthTextFieldUI();
254N/A }
254N/A
254N/A private void updateStyle(JTextComponent comp) {
254N/A SynthContext context = getContext(comp, ENABLED);
254N/A SynthStyle oldStyle = style;
254N/A
254N/A style = SynthLookAndFeel.updateStyle(context, this);
254N/A
254N/A if (style != oldStyle) {
254N/A SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix());
254N/A
254N/A if (oldStyle != null) {
254N/A uninstallKeyboardActions();
254N/A installKeyboardActions();
254N/A }
254N/A }
254N/A context.dispose();
254N/A }
254N/A
254N/A static void updateStyle(JTextComponent comp, SynthContext context,
254N/A String prefix) {
254N/A SynthStyle style = context.getStyle();
254N/A
254N/A Color color = comp.getCaretColor();
254N/A if (color == null || color instanceof UIResource) {
254N/A comp.setCaretColor(
254N/A (Color)style.get(context, prefix + ".caretForeground"));
254N/A }
254N/A
254N/A Color fg = comp.getForeground();
254N/A if (fg == null || fg instanceof UIResource) {
254N/A fg = style.getColorForState(context, ColorType.TEXT_FOREGROUND);
254N/A if (fg != null) {
254N/A comp.setForeground(fg);
254N/A }
254N/A }
254N/A
254N/A Object ar = style.get(context, prefix + ".caretAspectRatio");
254N/A if (ar instanceof Number) {
254N/A comp.putClientProperty("caretAspectRatio", ar);
254N/A }
254N/A
254N/A context.setComponentState(SELECTED | FOCUSED);
254N/A
436N/A Color s = comp.getSelectionColor();
254N/A if (s == null || s instanceof UIResource) {
436N/A comp.setSelectionColor(
254N/A style.getColor(context, ColorType.TEXT_BACKGROUND));
254N/A }
254N/A
254N/A Color sfg = comp.getSelectedTextColor();
254N/A if (sfg == null || sfg instanceof UIResource) {
254N/A comp.setSelectedTextColor(
254N/A style.getColor(context, ColorType.TEXT_FOREGROUND));
254N/A }
254N/A
254N/A context.setComponentState(DISABLED);
254N/A
254N/A Color dfg = comp.getDisabledTextColor();
254N/A if (dfg == null || dfg instanceof UIResource) {
254N/A comp.setDisabledTextColor(
254N/A style.getColor(context, ColorType.TEXT_FOREGROUND));
254N/A }
254N/A
254N/A Insets margin = comp.getMargin();
254N/A if (margin == null || margin instanceof UIResource) {
254N/A margin = (Insets)style.get(context, prefix + ".margin");
254N/A
254N/A if (margin == null) {
254N/A // Some places assume margins are non-null.
254N/A margin = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
254N/A }
254N/A comp.setMargin(margin);
254N/A }
254N/A
254N/A Caret caret = comp.getCaret();
254N/A if (caret instanceof UIResource) {
254N/A Object o = style.get(context, prefix + ".caretBlinkRate");
254N/A if (o != null && o instanceof Integer) {
254N/A Integer rate = (Integer)o;
254N/A caret.setBlinkRate(rate.intValue());
254N/A }
254N/A }
254N/A }
254N/A
254N/A /**
254N/A * @inheritDoc
254N/A */
254N/A @Override
254N/A public SynthContext getContext(JComponent c) {
254N/A return getContext(c, SynthLookAndFeel.getComponentState(c));
254N/A }
254N/A
254N/A private SynthContext getContext(JComponent c, int state) {
254N/A return SynthContext.getContext(SynthContext.class, c,
254N/A SynthLookAndFeel.getRegion(c), style, state);
254N/A }
254N/A
254N/A /**
254N/A * Notifies this UI delegate to repaint the specified component.
254N/A * This method paints the component background, then calls
254N/A * the {@link #paint(SynthContext,Graphics)} method.
254N/A *
254N/A * <p>In general, this method does not need to be overridden by subclasses.
254N/A * All Look and Feel rendering code should reside in the {@code paint} method.
254N/A *
254N/A * @param g the {@code Graphics} object used for painting
254N/A * @param c the component being painted
254N/A * @see #paint(SynthContext,Graphics)
254N/A */
254N/A @Override
254N/A public void update(Graphics g, JComponent c) {
254N/A SynthContext context = getContext(c);
254N/A
254N/A SynthLookAndFeel.update(context, g);
254N/A paintBackground(context, g, c);
254N/A paint(context, g);
254N/A context.dispose();
254N/A }
254N/A
254N/A /**
254N/A * Paints the specified component.
254N/A * <p>This is routed to the {@link #paintSafely} method under
254N/A * the guarantee that the model does not change from the view of this
254N/A * thread while it is rendering (if the associated model is
254N/A * derived from {@code AbstractDocument}). This enables the
254N/A * model to potentially be updated asynchronously.
254N/A *
254N/A * @param context context for the component being painted
254N/A * @param g the {@code Graphics} object used for painting
254N/A * @see #update(Graphics,JComponent)
254N/A */
254N/A protected void paint(SynthContext context, Graphics g) {
254N/A super.paint(g, getComponent());
254N/A }
254N/A
254N/A void paintBackground(SynthContext context, Graphics g, JComponent c) {
254N/A context.getPainter().paintTextFieldBackground(context, g, 0, 0,
254N/A c.getWidth(), c.getHeight());
254N/A }
254N/A
254N/A /**
254N/A * @inheritDoc
254N/A */
254N/A @Override
254N/A public void paintBorder(SynthContext context, Graphics g, int x,
254N/A int y, int w, int h) {
254N/A context.getPainter().paintTextFieldBorder(context, g, x, y, w, h);
254N/A }
254N/A
254N/A /**
254N/A * @inheritDoc
254N/A * Overridden to do nothing.
254N/A */
254N/A @Override
254N/A protected void paintBackground(Graphics g) {
254N/A // Overriden to do nothing, all our painting is done from update/paint.
254N/A }
254N/A
254N/A /**
254N/A * This method gets called when a bound property is changed
254N/A * on the associated JTextComponent. This is a hook
254N/A * which UI implementations may change to reflect how the
254N/A * UI displays bound properties of JTextComponent subclasses.
254N/A * This is implemented to do nothing (i.e. the response to
254N/A * properties in JTextComponent itself are handled prior
254N/A * to calling this method).
254N/A *
254N/A * @param evt the property change event
254N/A */
254N/A @Override
254N/A protected void propertyChange(PropertyChangeEvent evt) {
254N/A if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
254N/A updateStyle((JTextComponent)evt.getSource());
254N/A }
254N/A super.propertyChange(evt);
254N/A }
254N/A
254N/A /**
254N/A * @inheritDoc
254N/A */
254N/A @Override
254N/A protected void installDefaults() {
254N/A // Installs the text cursor on the component
254N/A super.installDefaults();
254N/A updateStyle(getComponent());
254N/A getComponent().addFocusListener(handler);
254N/A }
254N/A
254N/A /**
254N/A * @inheritDoc
254N/A */
254N/A @Override
254N/A protected void uninstallDefaults() {
254N/A SynthContext context = getContext(getComponent(), ENABLED);
254N/A
254N/A getComponent().putClientProperty("caretAspectRatio", null);
254N/A getComponent().removeFocusListener(handler);
254N/A
254N/A style.uninstallDefaults(context);
254N/A context.dispose();
254N/A style = null;
254N/A super.uninstallDefaults();
254N/A }
254N/A
254N/A private final class Handler implements FocusListener {
254N/A public void focusGained(FocusEvent e) {
254N/A getComponent().repaint();
254N/A }
254N/A
254N/A public void focusLost(FocusEvent e) {
254N/A getComponent().repaint();
254N/A }
254N/A }
254N/A}
254N/A