File: PreferencesTabBarButtonUI.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 (138 lines) | stat: -rw-r--r-- 5,974 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.explodingpixels.macwidgets.plaf;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.JComponent;

import com.explodingpixels.widgets.WindowUtils;

public class PreferencesTabBarButtonUI extends UnifiedToolbarButtonUI {

    /* a fully transparent color to use when fading colors out.*/
    private static final Color TRANSPARENT_COLOR =
            new Color(0,0,0,0);

    /* colors to use when the button's window doesn't have focus. */
    private static final Color UNFOCUSED_BACKGROUND_CENTER_COLOR =
            new Color(0,0,0,29);
    private static final Color UNFOCUSED_INNER_BORDER_COLOR =
            new Color(0,0,0,38);
    private static final Color UNFOCUSED_OUTER_BORDER_COLOR =
            new Color(0,0,0,63);

    /* colors to use when the button's window does have focus. */
    private static final Color FOCUSED_BACKGROUND_CENTER_COLOR =
            new Color(0,0,0,56);
    private static final Color FOCUSED_INNER_BORDER_COLOR =
            new Color(0,0,0,80);
    private static final Color FOCUSED_OUTER_BORDER_COLOR =
            new Color(0,0,0,130);

    @Override
    protected void installDefaults(AbstractButton b) {
        super.installDefaults(b);
        // TODO save original values.
        b.setBorder(BorderFactory.createEmptyBorder(5,5,4,5));
        b.setOpaque(false);
    }

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

    @Override
    public void paint(Graphics g, JComponent c) {
        AbstractButton b = (AbstractButton) c;
        ButtonModel model = b.getModel();

        // if the button is selected, draw the selection background.
        if (model.isSelected()) {
            Graphics2D graphics = (Graphics2D) g.create();
            paintSelectedButtonBackground(b, graphics);
            graphics.dispose();
        }

        super.paint(g, c);
    }

    private static void paintSelectedButtonBackground(AbstractButton button,
                                                      Graphics2D graphics) {
        // determine if the containing window has focus.
        boolean isButtonsWindowFocused =
                WindowUtils.isParentWindowFocused(button);
        // get center graident colors, based on the focus state of the
        // containing window.
        Color centerColor = isButtonsWindowFocused
                ? FOCUSED_BACKGROUND_CENTER_COLOR
                : UNFOCUSED_BACKGROUND_CENTER_COLOR;
        Color innerBorderColor = isButtonsWindowFocused
                ? FOCUSED_INNER_BORDER_COLOR
                : UNFOCUSED_INNER_BORDER_COLOR;
        Color outterBorderColor = isButtonsWindowFocused
                ? FOCUSED_OUTER_BORDER_COLOR
                : UNFOCUSED_OUTER_BORDER_COLOR;

        // calculate the first gradient's stop y position, and the second
        // gradient's start y position. thesve values, shouldn't overlap, as
        // transparent colors are addative, and would thus result in
        // bleed-through.
        int topMiddleY = button.getHeight()/2;
        int bottomMiddleY = button.getHeight()/2+1;

        // create the top and bottom fill paint.
        Paint topCenterPaint = new GradientPaint(
                0f,0f,TRANSPARENT_COLOR,1f,topMiddleY,centerColor);
        Paint bottomCenterPaint = new GradientPaint(
                0f,bottomMiddleY,centerColor,1f,button.getHeight(),TRANSPARENT_COLOR);

        // draw the selection background gradient. note that we don't want to
        // draw where the border is as tranparent colors will bleed together.
        graphics.setPaint(topCenterPaint);
        int borderWidth = 2;
        int fillWidth = button.getWidth() - borderWidth * 2;
        graphics.fillRect(borderWidth,0,fillWidth,topMiddleY);
        graphics.setPaint(bottomCenterPaint);
        graphics.fillRect(borderWidth,topMiddleY,fillWidth,button.getHeight());

        // create the outter border top and bottom paint.
        Paint topOuterBorderPaint = new GradientPaint(
                0f,0f,TRANSPARENT_COLOR,1f,topMiddleY,outterBorderColor);
        Paint bottomOuterBorderPaint = new GradientPaint(
                0f,bottomMiddleY,outterBorderColor,1f,button.getHeight(),TRANSPARENT_COLOR);

        // draw the outter border line.
        graphics.setPaint(topOuterBorderPaint);
        int outterLeftBorderX = 0;
        int outterRightBorderX = button.getWidth() - 1;
        graphics.drawLine(outterLeftBorderX,0,outterLeftBorderX,topMiddleY);
        graphics.drawLine(outterRightBorderX,0,outterRightBorderX,topMiddleY);
        graphics.setPaint(bottomOuterBorderPaint);
        graphics.drawLine(outterLeftBorderX,bottomMiddleY,outterLeftBorderX,button.getHeight());
        graphics.drawLine(outterRightBorderX,bottomMiddleY,outterRightBorderX,button.getHeight());

        // create the inner border top and bottom paint.
        Paint topInnerBorderPaint = new GradientPaint(
                0f,0f,TRANSPARENT_COLOR,1f,topMiddleY,innerBorderColor);
        Paint bottomInnerBorderPaint = new GradientPaint(
                0f,bottomMiddleY,innerBorderColor,1f,button.getHeight(),TRANSPARENT_COLOR);

        // draw the inner border line.
        graphics.setPaint(topInnerBorderPaint);
        int innerLeftBorderX = 1;
        int innerRightBorderX = button.getWidth() - 2;
        graphics.drawLine(innerLeftBorderX,0,innerLeftBorderX,topMiddleY);
        graphics.drawLine(innerRightBorderX,0,innerRightBorderX,topMiddleY);
        graphics.setPaint(bottomInnerBorderPaint);
        graphics.drawLine(innerLeftBorderX,bottomMiddleY,innerLeftBorderX,button.getHeight());
        graphics.drawLine(innerRightBorderX,bottomMiddleY,innerRightBorderX,button.getHeight());
    }
}