File: FocusStatePainter.java

package info (click to toggle)
mac-widgets 0.9.5%2Bsvn369-dfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,920 kB
  • sloc: java: 8,318; makefile: 13; sh: 12
file content (81 lines) | stat: -rw-r--r-- 3,551 bytes parent folder | download
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
package com.explodingpixels.painter;

import com.explodingpixels.widgets.WindowUtils;

import java.awt.Component;
import java.awt.Graphics2D;

/**
 * An implementation of {@link Painter} that delegates to given {@code Painter} based on the focused
 * state of the {@link Component} supplied in the
 * {@link #paint(java.awt.Graphics2D, java.awt.Component, int, int)} method.
 */
public class FocusStatePainter implements Painter<Component> {

    private Painter<Component> fComponentFocusedPainter;
    private Painter<Component> fWindowFocusedPainter;
    private Painter<Component> fWindowUnfocusedPainter;

    /**
     * Creates a {@link Painter} that delegates to the given {@code Painter}s based on the focus
     * state of the supplied {@link Component} or the focus state of it's parent
     * {@link java.awt.Window}.
     *
     * @param componentFocusedPainter the {@code Painter} to use when the given {@code Component} is
     *                                focused or it's parent {@code java.awt.Window} is focused.
     * @param windowUnfocusedPainter  the {@code Painter} to use when the given {@code Component}'s
     *                                parent {@code java.awt.Window} is unfocused.
     */
    public FocusStatePainter(Painter<Component> componentFocusedPainter,
                             Painter<Component> windowUnfocusedPainter) {
        this(componentFocusedPainter, windowUnfocusedPainter, windowUnfocusedPainter);
    }

    /**
     * Creates a {@link Painter} that delegates to the given {@code Painter}s based on the focus
     * state of the supplied {@link Component} or the focus state of it's parent
     * {@link java.awt.Window}.
     *
     * @param componentFocusedPainter the {@code Painter} to use when the given {@code Component} is
     *                                focused.
     * @param windowFocusedPainter    the {@code Painter} to use when the given {@code Component} is
     *                                unfocused but the {@code Component}'s parent window is focused.
     * @param windowUnfocusedPainter  the {@code Painter} to use when the given {@code Component}'s
     *                                parent {@code java.awt.Window} is unfocused.
     */
    public FocusStatePainter(Painter<Component> componentFocusedPainter,
                             Painter<Component> windowFocusedPainter,
                             Painter<Component> windowUnfocusedPainter) {

        if (componentFocusedPainter == null) {
            throw new IllegalArgumentException("Component focused Painter cannot be null.");
        }

        if (windowFocusedPainter == null) {
            throw new IllegalArgumentException("Window focused Painter cannot be null.");
        }

        if (windowUnfocusedPainter == null) {
            throw new IllegalArgumentException("Window unfocused Painter cannot be null.");
        }

        fComponentFocusedPainter = componentFocusedPainter;
        fWindowFocusedPainter = windowFocusedPainter;
        fWindowUnfocusedPainter = windowUnfocusedPainter;

    }

    public void paint(Graphics2D g, Component component, int width, int height) {
        Painter<Component> painterToUse;

        if (component.hasFocus()) {
            painterToUse = fComponentFocusedPainter;
        } else if (WindowUtils.isParentWindowFocused(component)) {
            painterToUse = fWindowFocusedPainter;
        } else {
            painterToUse = fWindowUnfocusedPainter;
        }

        painterToUse.paint(g, component, width, height);
    }
}