File: UnifiedToolbarButtonUI.java

package info (click to toggle)
mac-widgets 0.10.0%2Bsvn416-dfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,968 kB
  • sloc: java: 9,909; makefile: 13; sh: 12
file content (110 lines) | stat: -rw-r--r-- 3,976 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.explodingpixels.macwidgets.plaf;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicGraphicsUtils;

import com.explodingpixels.macwidgets.MacColorUtils;
import com.explodingpixels.macwidgets.MacFontUtils;

public class UnifiedToolbarButtonUI extends BasicButtonUI {

    private static final Color PRESSED_BUTTON_MASK_COLOR = new Color(0, 0, 0, 128);

    private static final Color DISABLED_BUTTON_MASK_COLOR = new Color(255, 255, 255, 128);

    @Override
    protected void installDefaults(AbstractButton b) {
        super.installDefaults(b);

        // TODO save original values.

        b.setHorizontalTextPosition(AbstractButton.CENTER);
        b.setVerticalTextPosition(AbstractButton.BOTTOM);
        b.setIconTextGap(0);
        b.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        b.setOpaque(false);
        b.setFocusable(false);
        // TODO make the font derivation more robust.
        b.setFont(MacFontUtils.DEFAULT_BUTTON_FONT);
    }

    @Override
    protected void uninstallDefaults(AbstractButton b) {
        super.uninstallDefaults(b);
        // TODO implement.
    }

    @Override
    protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) {

        AbstractButton b = (AbstractButton) c;
        ButtonModel model = b.getModel();

        // create a buffered image to draw the icon and mask into.
        BufferedImage image = new BufferedImage(iconRect.width, iconRect.height,
                BufferedImage.TYPE_INT_ARGB);
        // create a graphics context from the buffered image.
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        // paint the icon into the buffered image.
        b.getIcon().paintIcon(c, graphics, 0, 0);

        // set the composite on the graphics context to SrcAtop which blends the
        // source with the destination, and thus transparent pixels in the
        // destination, remain transparent.
        graphics.setComposite(AlphaComposite.SrcAtop);

        // set the mask color based on the button models state.
        if (!model.isEnabled()) {
            graphics.setColor(DISABLED_BUTTON_MASK_COLOR);
        } else if (model.isArmed()) {
            graphics.setColor(PRESSED_BUTTON_MASK_COLOR);
        } else {
            graphics.setColor(new Color(0, 0, 0, 0));
        }

        // fill a rectangle with the mask color.
        graphics.fillRect(0, 0, iconRect.width, iconRect.height);

        graphics.dispose();
        g.drawImage(image, iconRect.x, iconRect.y, null);
    }

    @Override
    protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
        MacFontUtils.enableAntialiasing((Graphics2D) g);
        Graphics2D graphics = (Graphics2D) g.create();

        AbstractButton b = (AbstractButton) c;
        ButtonModel model = b.getModel();
        FontMetrics fm = c.getFontMetrics(c.getFont());

        // 1) Draw the emphasis text.
        graphics.setColor(model.isArmed()
                ? MacColorUtils.EMPTY_COLOR
                : EmphasizedLabelUI.DEFAULT_EMPHASIS_COLOR);
        BasicGraphicsUtils.drawStringUnderlineCharAt(graphics, text, -1,
                textRect.x, textRect.y + 1 + fm.getAscent());

        // 2) Draw the text.
        graphics.setColor(model.isEnabled()
                ? EmphasizedLabelUI.DEFAULT_FOCUSED_FONT_COLOR
                : EmphasizedLabelUI.DEFAULT_DISABLED_FONT_COLOR);
        BasicGraphicsUtils.drawStringUnderlineCharAt(graphics, text, -1,
                textRect.x, textRect.y + fm.getAscent());

        graphics.dispose();
    }
}