File: IAppWidgetFactory.java

package info (click to toggle)
mac-widgets 0.10.0%2Bsvn416-dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,968 kB
  • ctags: 2,003
  • sloc: java: 9,909; makefile: 13; sh: 12
file content (109 lines) | stat: -rw-r--r-- 4,567 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
package com.explodingpixels.macwidgets;

import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JScrollPane;

import com.explodingpixels.macwidgets.plaf.IAppScrollBarUI;
import com.explodingpixels.widgets.ImageBasedJComponent;

/**
 * A factory for iApp style widgets.
 */
public class IAppWidgetFactory {

    private IAppWidgetFactory() {
        // utility class - no constructor needed.
    }

    /**
     * Creates an iApp style {@link JScrollPane}, with vertical and horizontal scrollbars shown as
     * needed. The increment/decrement buttons will be placed together or separatebased on the value
     * of {@link IAppScrollBarUI#areButtonsSeparate()}.
     *
     * @param view the view to wrap inside the {@code JScrollPane}.
     * @return an iApp style {@code JScrollPane}.
     * @see #makeIAppScrollPane
     */
    public static JScrollPane createScrollPane(Component view) {
        return createScrollPane(view, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    }

    /**
     * Creates an iApp style {@link JScrollPane} using the given scroll bar policies. The
     * increment/decrement buttons will be placed together or separatebased on the value of
     * {@link IAppScrollBarUI#areButtonsSeparate()}.
     *
     * @param view                      the view to wrap inside the {@code JScrollPane}.
     * @param verticalScrollBarPolicy   the vertical scroll bar policy.
     * @param horizontalScrollBarPolicy the horizontal scroll bar policy.
     * @return an iApp style {@code JScrollPane} using the given scroll bar policies.
     * @see #makeIAppScrollPane
     */
    public static JScrollPane createScrollPane(Component view, int verticalScrollBarPolicy,
                                               int horizontalScrollBarPolicy) {
        JScrollPane retVal = new JScrollPane(view, verticalScrollBarPolicy, horizontalScrollBarPolicy);
        makeIAppScrollPane(retVal);
        return retVal;
    }

    /**
     * Makes the given {@link JScrollPane} an iApp style scroll pane that looks like this:
     * <br>
     * <img src="../../../../graphics/iAppScrollbars.png">
     *
     * @param scrollPane the {@code JScrollPane} to make an iApp style scroll pane.
     * @return an iApp style scroll pane.
     */
    public static JScrollPane makeIAppScrollPane(JScrollPane scrollPane) {
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        installUIDelegates(scrollPane);
        scrollPane.setCorner(JScrollPane.LOWER_RIGHT_CORNER, createScrollPaneCorner());
        scrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, createScrollPaneCornerLowerLeft());
        // TODO listen for scrollBar.setUI calls in order to reinstall UI delegates.
        return scrollPane;
    }

    /**
     * Sets the default "buttons separate" status for scroll bars. The default value is
     * {@code false}, meaning that the buttons will be placed together at the right or bottom of the
     * scroll bar. A value of {@code true} means that the buttons will be placed at opposite ends of
     * the scroll bar.
     *
     * @param buttonsApart the "buttons apart" status.
     */
    public static void setIAppScrollBarButtonsSeparate(boolean buttonsApart) {
        IAppScrollBarUI.setButtonsSeparate(buttonsApart);
    }

    // ScrollBarUI creation methods ///////////////////////////////////////////////////////////////

    private static void installUIDelegates(JScrollPane scrollPane) {
        scrollPane.getVerticalScrollBar().setUI(new IAppScrollBarUI());
        scrollPane.getHorizontalScrollBar().setUI(new IAppScrollBarUI());
    }

    /**
     * Creates an iApp style scrollpane corner.
     *
     * @return returns a {@link JComponent} that represents the scroll pane corner.
     */
    public static JComponent createScrollPaneCorner() {
        return new ImageBasedJComponent(new ImageIcon(IAppWidgetFactory.class.getResource(
                "/com/explodingpixels/macwidgets/images/iapp_scrollpane_corner.png")).getImage());
    }
    
    /**
     * Creates an iApp style scrollpane corner.
     *
     * @return returns a {@link JComponent} that represents the scroll pane corner.
     */
    public static JComponent createScrollPaneCornerLowerLeft() {
        return new ImageBasedJComponent(new ImageIcon(IAppWidgetFactory.class.getResource(
                "/com/explodingpixels/macwidgets/images/iapp_scrollpane_corner_left.png")).getImage());
    }
}