/* * Copyright 2005 MH-Software-Entwicklung. All rights reserved. * Use is subject to license terms. */ package com.jtattoo.plaf; import java.awt.*; import javax.swing.*; import javax.swing.text.*; import javax.swing.plaf.*; import javax.swing.plaf.basic.*; /** * @author Michael Hagen */ public class BaseButtonUI extends BasicButtonUI { protected static Rectangle viewRect = new Rectangle(); protected static Rectangle textRect = new Rectangle(); protected static Rectangle iconRect = new Rectangle(); public static ComponentUI createUI(JComponent c) { return new BaseButtonUI(); } public void installDefaults(AbstractButton b) { super.installDefaults(b); b.setOpaque(false); } public void uninstallDefaults(AbstractButton b) { super.uninstallDefaults(b); b.setOpaque(true); } protected BasicButtonListener createButtonListener(AbstractButton b) { return new BaseButtonListener(b); } protected void paintBackground(Graphics g, AbstractButton b) { if (!b.isContentAreaFilled() || (b.getParent() instanceof JMenuBar)) { return; } int width = b.getWidth(); int height = b.getHeight(); Color colors[] = null; ButtonModel model = b.getModel(); if (b.isEnabled()) { if (b.getBackground() instanceof ColorUIResource) { if (model.isPressed() && model.isArmed()) { colors = AbstractLookAndFeel.getTheme().getPressedColors(); } else { if (model.isRollover()) { colors = AbstractLookAndFeel.getTheme().getRolloverColors(); } else { if (AbstractLookAndFeel.getTheme().doShowFocusFrame() && b.hasFocus()) { colors = AbstractLookAndFeel.getTheme().getFocusColors(); } else { colors = AbstractLookAndFeel.getTheme().getButtonColors(); } } } } else { if (model.isPressed() && model.isArmed()) { colors = ColorHelper.createColorArr(b.getBackground(), ColorHelper.darker(b.getBackground(), 50), 20); } else { if (model.isRollover()) { colors = ColorHelper.createColorArr(ColorHelper.brighter(b.getBackground(), 80), ColorHelper.brighter(b.getBackground(), 20), 20); } else { colors = ColorHelper.createColorArr(ColorHelper.brighter(b.getBackground(), 40), ColorHelper.darker(b.getBackground(), 20), 20); } } } } else { // disabled colors = AbstractLookAndFeel.getTheme().getDisabledColors(); } if (b.isBorderPainted() && (b.getBorder() != null)) { Insets insets = b.getBorder().getBorderInsets(b); int x = insets.left > 0 ? 1 : 0; int y = insets.top > 0 ? 1 : 0; int w = insets.right > 0 ? width - 1 : width; int h = insets.bottom > 0 ? height - 1 : height; JTattooUtilities.fillHorGradient(g, colors, x, y, w - x, h - y); } else { JTattooUtilities.fillHorGradient(g, colors, 0, 0, width, height); } } protected void paintText(Graphics g, AbstractButton b, Rectangle textRect) { ButtonModel model = b.getModel(); FontMetrics fm = g.getFontMetrics(); int mnemIndex = -1; if (JTattooUtilities.getJavaVersion() >= 1.4) { mnemIndex = b.getDisplayedMnemonicIndex(); } else { mnemIndex = JTattooUtilities.findDisplayedMnemonicIndex(b.getText(), model.getMnemonic()); } if (model.isEnabled()) { int offs = 0; if (model.isArmed() && model.isPressed()) { offs = 1; } g.setColor(b.getForeground()); JTattooUtilities.drawStringUnderlineCharAt(b, g, b.getText(), mnemIndex, textRect.x + offs, textRect.y + offs + fm.getAscent()); } else { g.setColor(Color.white); JTattooUtilities.drawStringUnderlineCharAt(b, g, b.getText(), mnemIndex, textRect.x + 1, textRect.y + 1 + fm.getAscent()); g.setColor(AbstractLookAndFeel.getDisabledForegroundColor()); JTattooUtilities.drawStringUnderlineCharAt(b, g, b.getText(), mnemIndex, textRect.x, textRect.y + fm.getAscent()); } } protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect) { g.setColor(AbstractLookAndFeel.getFocusColor()); BasicGraphicsUtils.drawDashedRect(g, 4, 3, b.getWidth() - 8, b.getHeight() - 6); } public void paint(Graphics g, JComponent c) { Graphics2D g2D = (Graphics2D) g; AbstractButton b = (AbstractButton) c; Font f = c.getFont(); g.setFont(f); FontMetrics fm = g.getFontMetrics(); Insets insets = c.getInsets(); viewRect.x = insets.left; viewRect.y = insets.top; viewRect.width = b.getWidth() - (insets.right + viewRect.x); viewRect.height = b.getHeight() - (insets.bottom + viewRect.y); textRect.x = textRect.y = textRect.width = textRect.height = 0; iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0; int iconTextGap = defaultTextIconGap; if (JTattooUtilities.getJavaVersion() >= 1.4) { iconTextGap = b.getIconTextGap(); } String text = SwingUtilities.layoutCompoundLabel( c, fm, b.getText(), b.getIcon(), b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, b.getText() == null ? 0 : iconTextGap); paintBackground(g, b); if (b.getIcon() != null) { if (!b.isEnabled()) { Composite composite = g2D.getComposite(); AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f); g2D.setComposite(alpha); paintIcon(g, c, iconRect); g2D.setComposite(composite); } else { if (b.getModel().isPressed() && b.getModel().isRollover()) { iconRect.x++; iconRect.y++; } paintIcon(g, c, iconRect); } } if (text != null && !text.equals("")) { View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { Object savedRenderingHint = null; if (AbstractLookAndFeel.getTheme().isTextAntiAliasingOn()) { savedRenderingHint = g2D.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING); g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } v.paint(g, textRect); if (AbstractLookAndFeel.getTheme().isTextAntiAliasingOn()) { g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, savedRenderingHint); } } else { paintText(g, b, textRect); } } if (b.isFocusPainted() && b.hasFocus()) { paintFocus(g, b, viewRect, textRect, iconRect); } } }