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
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.swing.plaf.basic.*;
0N/Aimport javax.swing.text.View;
0N/Aimport java.awt.Dimension;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.Insets;
0N/Aimport java.awt.Graphics;
0N/Aimport java.awt.FontMetrics;
0N/Aimport java.beans.PropertyChangeEvent;
0N/A
0N/A/**
1999N/A * Provides the Synth L&F UI delegate for
1999N/A * {@link javax.swing.JLabel}.
0N/A *
0N/A * @author Scott Violet
1999N/A * @since 1.7
0N/A */
1999N/Apublic class SynthLabelUI extends BasicLabelUI implements SynthUI {
0N/A private SynthStyle style;
0N/A
0N/A /**
0N/A * Returns the LabelUI implementation used for the skins look and feel.
1999N/A *
1999N/A * @param c component to create UI object for
1999N/A * @return the UI object
0N/A */
0N/A public static ComponentUI createUI(JComponent c){
0N/A return new SynthLabelUI();
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A protected void installDefaults(JLabel c) {
0N/A updateStyle(c);
0N/A }
0N/A
0N/A void updateStyle(JLabel c) {
0N/A SynthContext context = getContext(c, ENABLED);
0N/A style = SynthLookAndFeel.updateStyle(context, this);
0N/A context.dispose();
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A protected void uninstallDefaults(JLabel c){
0N/A SynthContext context = getContext(c, 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 private SynthContext getContext(JComponent c, int state) {
0N/A return SynthContext.getContext(SynthContext.class, c,
0N/A SynthLookAndFeel.getRegion(c), style, state);
0N/A }
0N/A
0N/A private int getComponentState(JComponent c) {
0N/A int state = SynthLookAndFeel.getComponentState(c);
5088N/A if (SynthLookAndFeel.getSelectedUI() == this &&
0N/A state == SynthConstants.ENABLED) {
5088N/A state = SynthLookAndFeel.getSelectedUIState() | SynthConstants.ENABLED;
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 JLabel label = (JLabel)c;
0N/A String text = label.getText();
0N/A if (text == null || "".equals(text)) {
0N/A return -1;
0N/A }
0N/A Insets i = label.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(label);
0N/A FontMetrics fm = context.getComponent().getFontMetrics(
0N/A context.getStyle().getFont(context));
0N/A context.getStyle().getGraphicsUtils(context).layoutText(
0N/A context, fm, label.getText(), label.getIcon(),
0N/A label.getHorizontalAlignment(), label.getVerticalAlignment(),
0N/A label.getHorizontalTextPosition(), label.getVerticalTextPosition(),
0N/A viewRect, iconRect, textRect, label.getIconTextGap());
0N/A View view = (View)label.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 /**
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 context.getPainter().paintLabelBackground(context,
0N/A g, 0, 0, c.getWidth(), c.getHeight());
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 JLabel label = (JLabel)context.getComponent();
0N/A Icon icon = (label.isEnabled()) ? label.getIcon() :
0N/A label.getDisabledIcon();
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, label.getText(), icon,
0N/A label.getHorizontalAlignment(), label.getVerticalAlignment(),
0N/A label.getHorizontalTextPosition(), label.getVerticalTextPosition(),
0N/A label.getIconTextGap(), label.getDisplayedMnemonicIndex(), 0);
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().paintLabelBorder(context, g, x, y, w, h);
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public Dimension getPreferredSize(JComponent c) {
0N/A JLabel label = (JLabel)c;
0N/A Icon icon = (label.isEnabled()) ? label.getIcon() :
0N/A label.getDisabledIcon();
0N/A SynthContext context = getContext(c);
0N/A Dimension size = context.getStyle().getGraphicsUtils(context).
0N/A getPreferredSize(
0N/A context, context.getStyle().getFont(context), label.getText(),
0N/A icon, label.getHorizontalAlignment(),
0N/A label.getVerticalAlignment(), label.getHorizontalTextPosition(),
0N/A label.getVerticalTextPosition(), label.getIconTextGap(),
0N/A label.getDisplayedMnemonicIndex());
0N/A
0N/A context.dispose();
0N/A return size;
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public Dimension getMinimumSize(JComponent c) {
0N/A JLabel label = (JLabel)c;
0N/A Icon icon = (label.isEnabled()) ? label.getIcon() :
0N/A label.getDisabledIcon();
0N/A SynthContext context = getContext(c);
0N/A Dimension size = context.getStyle().getGraphicsUtils(context).
0N/A getMinimumSize(
0N/A context, context.getStyle().getFont(context), label.getText(),
0N/A icon, label.getHorizontalAlignment(),
0N/A label.getVerticalAlignment(), label.getHorizontalTextPosition(),
0N/A label.getVerticalTextPosition(), label.getIconTextGap(),
0N/A label.getDisplayedMnemonicIndex());
0N/A
0N/A context.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 JLabel label = (JLabel)c;
0N/A Icon icon = (label.isEnabled()) ? label.getIcon() :
0N/A label.getDisabledIcon();
0N/A SynthContext context = getContext(c);
0N/A Dimension size = context.getStyle().getGraphicsUtils(context).
0N/A getMaximumSize(
0N/A context, context.getStyle().getFont(context), label.getText(),
0N/A icon, label.getHorizontalAlignment(),
0N/A label.getVerticalAlignment(), label.getHorizontalTextPosition(),
0N/A label.getVerticalTextPosition(), label.getIconTextGap(),
0N/A label.getDisplayedMnemonicIndex());
0N/A
0N/A context.dispose();
0N/A return size;
0N/A }
0N/A
1999N/A /**
1999N/A * @inheritDoc
1999N/A */
1999N/A @Override
0N/A public void propertyChange(PropertyChangeEvent e) {
0N/A super.propertyChange(e);
0N/A if (SynthLookAndFeel.shouldUpdateStyle(e)) {
0N/A updateStyle((JLabel)e.getSource());
0N/A }
0N/A }
0N/A}