File: UnifiedToolBar.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 (121 lines) | stat: -rw-r--r-- 4,790 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
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
package com.explodingpixels.macwidgets;

import com.explodingpixels.border.FocusStateMatteBorder;
import com.explodingpixels.widgets.WindowDragger;
import com.explodingpixels.widgets.WindowUtils;
import com.jgoodies.forms.factories.Borders;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import java.awt.Window;

/**
 * A Mac style Unified Tool Bar. For a full description of what a Unified Tool Bar is, see the
 * <a href="http://developer.apple.com/documentation/UserExperience/Conceptual/AppleHIGuidelines/XHIGWindows/chapter_18_section_4.html#//apple_ref/doc/uid/20000961-BABIFCFJ">Toolbars</a>
 * section of Apple's Human Interface Guidelines. Here's an example of the what this method creates:
 * <br>
 * <img src="../../../../graphics/UnifiedToolBar.png">
 * <br>
 * Here's a simple example that creates a Unified Tool Bar with a single button:
 * <pre>
 * UnifiedToolBar toolBar =  new UnifiedToolBar();
 * JButton button = new JButton("My Button");
 * button.putClientProperty("JButton.buttonType", "textured");
 * toolBar.addComponentToLeft(button);
 * </pre>
 */
public class UnifiedToolBar {

    private final TriAreaComponent fUnifiedToolBar = new TriAreaComponent(10);

    /**
     * Creates a {@code UnifiedToolBar}.
     */
    public UnifiedToolBar() {
        // TODO remove below call when Apple fixes bug in Java that doesn't correctly paint the
        // TODO textured window.
        fixUnifiedToolBarOnMacIfNeccessary(fUnifiedToolBar);
        fUnifiedToolBar.getComponent().setBorder(Borders.createEmptyBorder("3dlu, 4dlu, 3dlu, 4dlu"));
        installUnifiedToolBarBorder(fUnifiedToolBar.getComponent());
        WindowUtils.installJComponentRepainterOnWindowFocusChanged(fUnifiedToolBar.getComponent());
    }

    /**
     * Adds the given component to the left side of this {@code UnifiedToolbar}.
     *
     * @param toolToAdd the tool to add to this {@code UnifiedToolbar}.
     */
    public void addComponentToLeft(JComponent toolToAdd) {
        fUnifiedToolBar.addComponentToLeft(toolToAdd);
    }

    /**
     * Adds the given component to the side of this {@code UnifiedToolbar}.
     *
     * @param toolToAdd the tool to add to this {@code UnifiedToolbar}.
     */
    public void addComponentToCenter(JComponent toolToAdd) {
        fUnifiedToolBar.addComponentToCenter(toolToAdd);
    }

    /**
     * Adds the given component to the right side of this {@code UnifiedToolBar}.
     *
     * @param toolToAdd the tool to add to this {@code UnifiedToolBar}.
     */
    public void addComponentToRight(JComponent toolToAdd) {
        fUnifiedToolBar.addComponentToRight(toolToAdd);
    }

    /**
     * Installs a drag listener on this {@code UnifiedToolBar} such that if it is dragged, it will
     * move the given {@link Window}.
     *
     * @param window the {@code Window} to move when the this {@code UnifiedToolbar} is dragged.
     */
    public void installWindowDraggerOnWindow(Window window) {
        new WindowDragger(window, getComponent());
    }

    /**
     * Gets the user interface component representing this {@code UnifiedToolBar}. The returned
     * {@link JComponent} should be added to a container that will be displayed.
     *
     * @return the user interface component representing this {@code UnifiedToolBar}.
     */
    public JComponent getComponent() {
        return fUnifiedToolBar.getComponent();
    }

    /**
     * Disables any custom background painter that may be installed.
     */
    public void disableBackgroundPainter() {
        fUnifiedToolBar.setBackgroundPainter(null);
    }

    /**
     * Installs a custom painter on the given {@link TriAreaComponent} that paints the Mac style
     * unified toolbar gradient on non-Mac platforms as well as Mac platforms running using Java 6.
     *
     * @param unifiedToolBar the {@link TriAreaComponent} to install the custom painter on if
     *                       necessary.
     */
    private static void fixUnifiedToolBarOnMacIfNeccessary(TriAreaComponent unifiedToolBar) {
        // install the custom painter if on non-Mac platforms or in other various Mac cases.
        if (MacUtils.shouldManuallyPaintTexturedWindowBackground()) {
            unifiedToolBar.setBackgroundPainter(MacPainterFactory.createTexturedWindowWorkaroundPainter());
        }
    }

    static void installUnifiedToolBarBorder(final JComponent component) {

        FocusStateMatteBorder border = new FocusStateMatteBorder(0, 0, 1, 0,
                MacColorUtils.getTexturedWindowToolbarBorderFocusedColor(),
                MacColorUtils.getTexturedWindowToolbarBorderUnfocusedColor(),
                component);

        component.setBorder(BorderFactory.createCompoundBorder(
                border, component.getBorder()));
    }
}