0N/A/*
3261N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing.plaf.synth;
0N/A
1999N/Aimport javax.swing.*;
0N/Aimport java.awt.*;
0N/Aimport java.beans.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.swing.plaf.basic.BasicButtonUI;
0N/Aimport javax.swing.plaf.basic.BasicHTML;
0N/Aimport javax.swing.text.View;
0N/A
0N/A/**
1999N/A * Provides the Synth L&F UI delegate for
1999N/A * {@link javax.swing.JButton}.
0N/A *
0N/A * @author Scott Violet
1999N/A * @since 1.7
0N/A */
1999N/Apublic class SynthButtonUI extends BasicButtonUI implements
0N/A PropertyChangeListener, SynthUI {
0N/A private SynthStyle style;
0N/A
1999N/A /**
1999N/A * Creates a new UI object for the given component.
1999N/A *
1999N/A * @param c component to create UI object for
1999N/A * @return the UI object
1999N/A */
0N/A public static ComponentUI createUI(JComponent c) {
0N/A return new SynthButtonUI();
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A protected void installDefaults(AbstractButton b) {
0N/A updateStyle(b);
0N/A
0N/A LookAndFeel.installProperty(b, "rolloverEnabled", Boolean.TRUE);
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A protected void installListeners(AbstractButton b) {
0N/A super.installListeners(b);
0N/A b.addPropertyChangeListener(this);
0N/A }
0N/A
0N/A void updateStyle(AbstractButton b) {
0N/A SynthContext context = getContext(b, SynthConstants.ENABLED);
0N/A SynthStyle oldStyle = style;
0N/A style = SynthLookAndFeel.updateStyle(context, this);
0N/A if (style != oldStyle) {
0N/A if (b.getMargin() == null ||
0N/A (b.getMargin() instanceof UIResource)) {
0N/A Insets margin = (Insets)style.get(context,getPropertyPrefix() +
0N/A "margin");
0N/A
0N/A if (margin == null) {
0N/A // Some places assume margins are non-null.
0N/A margin = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
0N/A }
0N/A b.setMargin(margin);
0N/A }
0N/A
0N/A Object value = style.get(context, getPropertyPrefix() + "iconTextGap");
0N/A if (value != null) {
0N/A LookAndFeel.installProperty(b, "iconTextGap", value);
0N/A }
0N/A
0N/A value = style.get(context, getPropertyPrefix() + "contentAreaFilled");
0N/A LookAndFeel.installProperty(b, "contentAreaFilled",
0N/A value != null? value : Boolean.TRUE);
0N/A
0N/A if (oldStyle != null) {
0N/A uninstallKeyboardActions(b);
0N/A installKeyboardActions(b);
0N/A }
0N/A
0N/A }
0N/A context.dispose();
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A protected void uninstallListeners(AbstractButton b) {
0N/A super.uninstallListeners(b);
0N/A b.removePropertyChangeListener(this);
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A protected void uninstallDefaults(AbstractButton b) {
0N/A SynthContext context = getContext(b, ENABLED);
0N/A
0N/A style.uninstallDefaults(context);
0N/A context.dispose();
0N/A style = null;
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public SynthContext getContext(JComponent c) {
0N/A return getContext(c, getComponentState(c));
0N/A }
0N/A
0N/A SynthContext getContext(JComponent c, int state) {
1999N/A Region region = SynthLookAndFeel.getRegion(c);
0N/A return SynthContext.getContext(SynthContext.class, c, region,
0N/A style, state);
0N/A }
0N/A
0N/A /**
0N/A * Returns the current state of the passed in <code>AbstractButton</code>.
0N/A */
0N/A private int getComponentState(JComponent c) {
0N/A int state = ENABLED;
0N/A
0N/A if (!c.isEnabled()) {
0N/A state = DISABLED;
0N/A }
5088N/A if (SynthLookAndFeel.getSelectedUI() == this) {
5088N/A return SynthLookAndFeel.getSelectedUIState() | SynthConstants.ENABLED;
0N/A }
0N/A AbstractButton button = (AbstractButton) c;
0N/A ButtonModel model = button.getModel();
0N/A
0N/A if (model.isPressed()) {
0N/A if (model.isArmed()) {
0N/A state = PRESSED;
0N/A }
0N/A else {
0N/A state = MOUSE_OVER;
0N/A }
0N/A }
0N/A if (model.isRollover()) {
0N/A state |= MOUSE_OVER;
0N/A }
0N/A if (model.isSelected()) {
0N/A state |= SELECTED;
0N/A }
0N/A if (c.isFocusOwner() && button.isFocusPainted()) {
0N/A state |= FOCUSED;
0N/A }
0N/A if ((c instanceof JButton) && ((JButton)c).isDefaultButton()) {
0N/A state |= DEFAULT;
0N/A }
0N/A return state;
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public int getBaseline(JComponent c, int width, int height) {
0N/A if (c == null) {
0N/A throw new NullPointerException("Component must be non-null");
0N/A }
0N/A if (width < 0 || height < 0) {
0N/A throw new IllegalArgumentException(
0N/A "Width and height must be >= 0");
0N/A }
0N/A AbstractButton b = (AbstractButton)c;
0N/A String text = b.getText();
0N/A if (text == null || "".equals(text)) {
0N/A return -1;
0N/A }
0N/A Insets i = b.getInsets();
0N/A Rectangle viewRect = new Rectangle();
0N/A Rectangle textRect = new Rectangle();
0N/A Rectangle iconRect = new Rectangle();
0N/A viewRect.x = i.left;
0N/A viewRect.y = i.top;
0N/A viewRect.width = width - (i.right + viewRect.x);
0N/A viewRect.height = height - (i.bottom + viewRect.y);
0N/A
0N/A // layout the text and icon
0N/A SynthContext context = getContext(b);
0N/A FontMetrics fm = context.getComponent().getFontMetrics(
0N/A context.getStyle().getFont(context));
0N/A context.getStyle().getGraphicsUtils(context).layoutText(
0N/A context, fm, b.getText(), b.getIcon(),
0N/A b.getHorizontalAlignment(), b.getVerticalAlignment(),
0N/A b.getHorizontalTextPosition(), b.getVerticalTextPosition(),
0N/A viewRect, iconRect, textRect, b.getIconTextGap());
0N/A View view = (View)b.getClientProperty(BasicHTML.propertyKey);
0N/A int baseline;
0N/A if (view != null) {
0N/A baseline = BasicHTML.getHTMLBaseline(view, textRect.width,
0N/A textRect.height);
0N/A if (baseline >= 0) {
0N/A baseline += textRect.y;
0N/A }
0N/A }
0N/A else {
0N/A baseline = textRect.y + fm.getAscent();
0N/A }
0N/A context.dispose();
0N/A return baseline;
0N/A }
0N/A
0N/A // ********************************
0N/A // Paint Methods
0N/A // ********************************
0N/A
1999N/A /**
2146N/A * Notifies this UI delegate to repaint the specified component.
2146N/A * This method paints the component background, then calls
2146N/A * the {@link #paint(SynthContext,Graphics)} method.
2146N/A *
2146N/A * <p>In general, this method does not need to be overridden by subclasses.
2146N/A * All Look and Feel rendering code should reside in the {@code paint} method.
2146N/A *
2146N/A * @param g the {@code Graphics} object used for painting
2146N/A * @param c the component being painted
2146N/A * @see #paint(SynthContext,Graphics)
1999N/A */
1999N/A @Override
0N/A public void update(Graphics g, JComponent c) {
0N/A SynthContext context = getContext(c);
0N/A
0N/A SynthLookAndFeel.update(context, g);
0N/A paintBackground(context, g, c);
0N/A paint(context, g);
0N/A context.dispose();
0N/A }
0N/A
1999N/A /**
2146N/A * Paints the specified component according to the Look and Feel.
2146N/A * <p>This method is not used by Synth Look and Feel.
2146N/A * Painting is handled by the {@link #paint(SynthContext,Graphics)} method.
2146N/A *
2146N/A * @param g the {@code Graphics} object used for painting
2146N/A * @param c the component being painted
2146N/A * @see #paint(SynthContext,Graphics)
1999N/A */
1999N/A @Override
0N/A public void paint(Graphics g, JComponent c) {
0N/A SynthContext context = getContext(c);
0N/A
0N/A paint(context, g);
0N/A context.dispose();
0N/A }
0N/A
1999N/A /**
1999N/A * Paints the specified component.
1999N/A *
1999N/A * @param context context for the component being painted
2146N/A * @param g the {@code Graphics} object used for painting
2146N/A * @see #update(Graphics,JComponent)
1999N/A */
0N/A protected void paint(SynthContext context, Graphics g) {
0N/A AbstractButton b = (AbstractButton)context.getComponent();
0N/A
0N/A g.setColor(context.getStyle().getColor(context,
0N/A ColorType.TEXT_FOREGROUND));
0N/A g.setFont(style.getFont(context));
0N/A context.getStyle().getGraphicsUtils(context).paintText(
0N/A context, g, b.getText(), getIcon(b),
0N/A b.getHorizontalAlignment(), b.getVerticalAlignment(),
0N/A b.getHorizontalTextPosition(), b.getVerticalTextPosition(),
0N/A b.getIconTextGap(), b.getDisplayedMnemonicIndex(),
0N/A getTextShiftOffset(context));
0N/A }
0N/A
0N/A void paintBackground(SynthContext context, Graphics g, JComponent c) {
0N/A if (((AbstractButton) c).isContentAreaFilled()) {
0N/A context.getPainter().paintButtonBackground(context, g, 0, 0,
0N/A c.getWidth(),
0N/A c.getHeight());
0N/A }
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public void paintBorder(SynthContext context, Graphics g, int x,
0N/A int y, int w, int h) {
0N/A context.getPainter().paintButtonBorder(context, g, x, y, w, h);
0N/A }
0N/A
0N/A /**
1999N/A * Returns the default icon. This should not callback
0N/A * to the JComponent.
0N/A *
1999N/A * @param b button the icon is associated with
0N/A * @return default icon
0N/A */
0N/A protected Icon getDefaultIcon(AbstractButton b) {
0N/A SynthContext context = getContext(b);
0N/A Icon icon = context.getStyle().getIcon(context, getPropertyPrefix() + "icon");
0N/A context.dispose();
0N/A return icon;
0N/A }
0N/A
0N/A /**
1999N/A * Returns the Icon to use for painting the button. The icon is chosen with
1999N/A * respect to the current state of the button.
1999N/A *
1999N/A * @param b button the icon is associated with
1999N/A * @return an icon
0N/A */
0N/A protected Icon getIcon(AbstractButton b) {
0N/A Icon icon = b.getIcon();
0N/A ButtonModel model = b.getModel();
0N/A
0N/A if (!model.isEnabled()) {
0N/A icon = getSynthDisabledIcon(b, icon);
0N/A } else if (model.isPressed() && model.isArmed()) {
0N/A icon = getPressedIcon(b, getSelectedIcon(b, icon));
0N/A } else if (b.isRolloverEnabled() && model.isRollover()) {
0N/A icon = getRolloverIcon(b, getSelectedIcon(b, icon));
0N/A } else if (model.isSelected()) {
0N/A icon = getSelectedIcon(b, icon);
0N/A } else {
0N/A icon = getEnabledIcon(b, icon);
0N/A }
0N/A if(icon == null) {
0N/A return getDefaultIcon(b);
0N/A }
0N/A return icon;
0N/A }
0N/A
0N/A /**
0N/A * This method will return the icon that should be used for a button. We
0N/A * only want to use the synth icon defined by the style if the specific
0N/A * icon has not been defined for the button state and the backup icon is a
0N/A * UIResource (we set it, not the developer).
0N/A *
0N/A * @param b button
0N/A * @param specificIcon icon returned from the button for the specific state
0N/A * @param defaultIcon fallback icon
0N/A * @param state the synth state of the button
0N/A */
0N/A private Icon getIcon(AbstractButton b, Icon specificIcon, Icon defaultIcon,
0N/A int state) {
0N/A Icon icon = specificIcon;
0N/A if (icon == null) {
0N/A if (defaultIcon instanceof UIResource) {
0N/A icon = getSynthIcon(b, state);
0N/A if (icon == null) {
0N/A icon = defaultIcon;
0N/A }
0N/A } else {
0N/A icon = defaultIcon;
0N/A }
0N/A }
0N/A return icon;
0N/A }
0N/A
0N/A private Icon getSynthIcon(AbstractButton b, int synthConstant) {
0N/A return style.getIcon(getContext(b, synthConstant), getPropertyPrefix() + "icon");
0N/A }
0N/A
0N/A private Icon getEnabledIcon(AbstractButton b, Icon defaultIcon) {
0N/A if (defaultIcon == null) {
0N/A defaultIcon = getSynthIcon(b, SynthConstants.ENABLED);
0N/A }
0N/A return defaultIcon;
0N/A }
0N/A
0N/A private Icon getSelectedIcon(AbstractButton b, Icon defaultIcon) {
0N/A return getIcon(b, b.getSelectedIcon(), defaultIcon,
0N/A SynthConstants.SELECTED);
0N/A }
0N/A
0N/A private Icon getRolloverIcon(AbstractButton b, Icon defaultIcon) {
0N/A ButtonModel model = b.getModel();
0N/A Icon icon;
0N/A if (model.isSelected()) {
0N/A icon = getIcon(b, b.getRolloverSelectedIcon(), defaultIcon,
0N/A SynthConstants.MOUSE_OVER | SynthConstants.SELECTED);
0N/A } else {
0N/A icon = getIcon(b, b.getRolloverIcon(), defaultIcon,
0N/A SynthConstants.MOUSE_OVER);
0N/A }
0N/A return icon;
0N/A }
0N/A
0N/A private Icon getPressedIcon(AbstractButton b, Icon defaultIcon) {
0N/A return getIcon(b, b.getPressedIcon(), defaultIcon,
0N/A SynthConstants.PRESSED);
0N/A }
0N/A
0N/A private Icon getSynthDisabledIcon(AbstractButton b, Icon defaultIcon) {
0N/A ButtonModel model = b.getModel();
0N/A Icon icon;
0N/A if (model.isSelected()) {
0N/A icon = getIcon(b, b.getDisabledSelectedIcon(), defaultIcon,
0N/A SynthConstants.DISABLED | SynthConstants.SELECTED);
0N/A } else {
0N/A icon = getIcon(b, b.getDisabledIcon(), defaultIcon,
0N/A SynthConstants.DISABLED);
0N/A }
0N/A return icon;
0N/A }
0N/A
0N/A /**
0N/A * Returns the amount to shift the text/icon when painting.
0N/A */
1999N/A private int getTextShiftOffset(SynthContext state) {
0N/A AbstractButton button = (AbstractButton)state.getComponent();
0N/A ButtonModel model = button.getModel();
0N/A
0N/A if (model.isArmed() && model.isPressed() &&
0N/A button.getPressedIcon() == null) {
0N/A return state.getStyle().getInt(state, getPropertyPrefix() +
0N/A "textShiftOffset", 0);
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A // ********************************
0N/A // Layout Methods
0N/A // ********************************
1999N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public Dimension getMinimumSize(JComponent c) {
0N/A if (c.getComponentCount() > 0 && c.getLayout() != null) {
0N/A return null;
0N/A }
0N/A AbstractButton b = (AbstractButton)c;
0N/A SynthContext ss = getContext(c);
0N/A Dimension size = ss.getStyle().getGraphicsUtils(ss).getMinimumSize(
0N/A ss, ss.getStyle().getFont(ss), b.getText(), getSizingIcon(b),
0N/A b.getHorizontalAlignment(), b.getVerticalAlignment(),
0N/A b.getHorizontalTextPosition(),
0N/A b.getVerticalTextPosition(), b.getIconTextGap(),
0N/A b.getDisplayedMnemonicIndex());
0N/A
0N/A ss.dispose();
0N/A return size;
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public Dimension getPreferredSize(JComponent c) {
0N/A if (c.getComponentCount() > 0 && c.getLayout() != null) {
0N/A return null;
0N/A }
0N/A AbstractButton b = (AbstractButton)c;
0N/A SynthContext ss = getContext(c);
0N/A Dimension size = ss.getStyle().getGraphicsUtils(ss).getPreferredSize(
0N/A ss, ss.getStyle().getFont(ss), b.getText(), getSizingIcon(b),
0N/A b.getHorizontalAlignment(), b.getVerticalAlignment(),
0N/A b.getHorizontalTextPosition(),
0N/A b.getVerticalTextPosition(), b.getIconTextGap(),
0N/A b.getDisplayedMnemonicIndex());
0N/A
0N/A ss.dispose();
0N/A return size;
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public Dimension getMaximumSize(JComponent c) {
0N/A if (c.getComponentCount() > 0 && c.getLayout() != null) {
0N/A return null;
0N/A }
0N/A
0N/A AbstractButton b = (AbstractButton)c;
0N/A SynthContext ss = getContext(c);
0N/A Dimension size = ss.getStyle().getGraphicsUtils(ss).getMaximumSize(
0N/A ss, ss.getStyle().getFont(ss), b.getText(), getSizingIcon(b),
0N/A b.getHorizontalAlignment(), b.getVerticalAlignment(),
0N/A b.getHorizontalTextPosition(),
0N/A b.getVerticalTextPosition(), b.getIconTextGap(),
0N/A b.getDisplayedMnemonicIndex());
0N/A
0N/A ss.dispose();
0N/A return size;
0N/A }
0N/A
0N/A /**
1999N/A * Returns the Icon used in calculating the
1999N/A * preferred/minimum/maximum size.
0N/A */
0N/A protected Icon getSizingIcon(AbstractButton b) {
620N/A Icon icon = getEnabledIcon(b, b.getIcon());
0N/A if (icon == null) {
0N/A icon = getDefaultIcon(b);
0N/A }
0N/A return icon;
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public void propertyChange(PropertyChangeEvent e) {
0N/A if (SynthLookAndFeel.shouldUpdateStyle(e)) {
0N/A updateStyle((AbstractButton)e.getSource());
0N/A }
0N/A }
0N/A}