File: IAppScrollBarUI.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 (164 lines) | stat: -rw-r--r-- 8,553 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.explodingpixels.macwidgets.plaf;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Image;

import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;

import com.explodingpixels.macwidgets.MacFontUtils;
import com.explodingpixels.painter.ImagePainter;
import com.explodingpixels.painter.MacWidgetsPainter;
import com.explodingpixels.widgets.ImageBasedJComponent;
import com.explodingpixels.widgets.plaf.ButtonsSeparateScrollBarSkin;
import com.explodingpixels.widgets.plaf.ButtonsTogetherScrollBarSkin;
import com.explodingpixels.widgets.plaf.ScrollBarOrientation;
import com.explodingpixels.widgets.plaf.ScrollBarSkin;
import com.explodingpixels.widgets.plaf.ScrollThumbImagePainter;
import com.explodingpixels.widgets.plaf.SkinnableScrollBarUI;

/**
 * Creates an iApp style scroll bar, either horizontal or vertical based on
 * {@link javax.swing.JScrollBar#getOrientation()}.
 * <br>
 * <img src="../../../../../graphics/iAppHorizontalScrollBar.png">
 * <img src="../../../../../graphics/iAppVerticalScrollBar.png">
 */
public class IAppScrollBarUI extends SkinnableScrollBarUI {

    private static boolean fButtonsSeparate = initializeSeparateStatus();

    public IAppScrollBarUI() {
        super(createScrollBarSkinProvider());
    }

    public static ComponentUI createUI(JComponent c) {
        return new IAppScrollBarUI();
    }

    public static boolean areButtonsSeparate() {
        return fButtonsSeparate;
    }

    public static void setButtonsSeparate(boolean buttonsSeparate) {
        IAppScrollBarUI.fButtonsSeparate = buttonsSeparate;
    }

    private static ScrollBarSkinProvider createScrollBarSkinProvider() {
        return new ScrollBarSkinProvider() {
            public ScrollBarSkin provideSkin(ScrollBarOrientation orientation) {
                ScrollBarSkin skin;
                if (fButtonsSeparate) {
                    if (orientation == ScrollBarOrientation.HORIZONTAL) {
                        skin = createHorizontalSeparateSkin();
                    } else {
                        skin = createVerticalSeparateSkin();
                    }
                } else {
                    if (orientation == ScrollBarOrientation.HORIZONTAL) {
                        skin = createHorizontalTogetherSkin();
                    } else {
                        skin = createVerticalTogetherSkin();
                    }
                }
                return skin;
            }
        };
    }

    private static ScrollBarSkin createHorizontalSeparateSkin() {
        Dimension minimumThumbSize = IAppScrollBarArtworkUtils.getHorizontalScrollBarMinimumSize();
        AbstractButton decrementButton = IAppScrollBarArtworkUtils.createHorizontalSeparateDecrementButton();
        AbstractButton incrementButton = IAppScrollBarArtworkUtils.createHorizontalSeparateIncrementButton();
        MacWidgetsPainter<Component> trackPainter = new ImagePainter(IAppScrollBarArtworkUtils.getHorizontalTrack().getImage());
        ScrollThumbImagePainter scrollerThumb = IAppScrollBarArtworkUtils.createHorizontalScrollerThumb();
        int decrementButtonRecess = IAppScrollBarArtworkUtils.getScrollBarTopCapRecess();
        int incrementButtonRecess = IAppScrollBarArtworkUtils.getDecrementButtonRecess();

        // FIXME - Hack because some LAFs failed to set default fonts for everything AND Java 7 font behavior changed.
        decrementButton.setFont(MacFontUtils.DEFAULT_BUTTON_FONT);
        
        Dimension preferredSize = new Dimension(100, decrementButton.getPreferredSize().height);

        return new ButtonsSeparateScrollBarSkin(decrementButton, incrementButton, trackPainter, scrollerThumb,
                decrementButtonRecess, incrementButtonRecess, minimumThumbSize, preferredSize);
    }

    private static ScrollBarSkin createVerticalSeparateSkin() {
        Dimension minimumThumbSize = IAppScrollBarArtworkUtils.getVerticalScrollBarMinimumSize();
        AbstractButton decrementButton = IAppScrollBarArtworkUtils.createVerticalSeparateDecrementButton();
        AbstractButton incrementButton = IAppScrollBarArtworkUtils.createVerticalSeparateIncrementButton();
        MacWidgetsPainter<Component> trackPainter = new ImagePainter(IAppScrollBarArtworkUtils.getVerticalTrack().getImage());
        ScrollThumbImagePainter scrollerThumb = IAppScrollBarArtworkUtils.createVerticalScrollerThumb();
        int decrementButtonRecess = IAppScrollBarArtworkUtils.getScrollBarTopCapRecess();
        int incrementButtonRecess = IAppScrollBarArtworkUtils.getDecrementButtonRecess();

        // FIXME - Hack because some LAFs failed to set default fonts for everything AND Java 7 font behavior changed.
        decrementButton.setFont(MacFontUtils.DEFAULT_BUTTON_FONT);

        Dimension preferredSize = new Dimension(decrementButton.getPreferredSize().width, 100);

        return new ButtonsSeparateScrollBarSkin(decrementButton, incrementButton, trackPainter, scrollerThumb,
                decrementButtonRecess, incrementButtonRecess, minimumThumbSize, preferredSize);
    }

    private static ScrollBarSkin createHorizontalTogetherSkin() {
        JComponent topCap = new ImageBasedJComponent(IAppScrollBarArtworkUtils.getScrollBarLeftCap().getImage());

        Dimension minimumThumbSize = IAppScrollBarArtworkUtils.getHorizontalScrollBarMinimumSize();
        AbstractButton decrementButton = IAppScrollBarArtworkUtils.createHorizontalTogetherDecrementButton();
        AbstractButton incrementButton = IAppScrollBarArtworkUtils.createHorizontalTogetherIncrementButton();
        MacWidgetsPainter<Component> trackPainter = new ImagePainter(IAppScrollBarArtworkUtils.getHorizontalTrack().getImage());
        ScrollThumbImagePainter scrollerThumb = IAppScrollBarArtworkUtils.createHorizontalScrollerThumb();
        int topCapRecess = IAppScrollBarArtworkUtils.getScrollBarTopCapRecess();
        int decrementButtonRecess = IAppScrollBarArtworkUtils.getDecrementButtonRecess();

        // FIXME - Hack because some LAFs failed to set default fonts for everything AND Java 7 font behavior changed.
        decrementButton.setFont(MacFontUtils.DEFAULT_BUTTON_FONT);

        Dimension preferredSize = new Dimension(100, decrementButton.getPreferredSize().height);

        return new ButtonsTogetherScrollBarSkin(
                topCap, decrementButton, incrementButton, trackPainter, scrollerThumb,
                topCapRecess, decrementButtonRecess, minimumThumbSize, preferredSize);
    }

    private static ScrollBarSkin createVerticalTogetherSkin() {
        Image topCapImage = IAppScrollBarArtworkUtils.getScrollBarTopCap().getImage();
        JComponent topCap = new ImageBasedJComponent(topCapImage);

        Dimension minimumThumbSize = IAppScrollBarArtworkUtils.getVerticalScrollBarMinimumSize();
        AbstractButton decrementButton = IAppScrollBarArtworkUtils.createVerticalTogetherDecrementButton();
        AbstractButton incrementButton = IAppScrollBarArtworkUtils.createVerticalTogetherIncrementButton();
        MacWidgetsPainter<Component> trackPainter = new ImagePainter(IAppScrollBarArtworkUtils.getVerticalTrack().getImage());
        ScrollThumbImagePainter scrollerThumb = IAppScrollBarArtworkUtils.createVerticalScrollerThumb();
        int topCapRecess = IAppScrollBarArtworkUtils.getScrollBarTopCapRecess();
        int decrementButtonRecess = IAppScrollBarArtworkUtils.getDecrementButtonRecess();

        // FIXME - Hack because some LAFs failed to set default fonts for everything AND Java 7 font behavior changed.
        decrementButton.setFont(MacFontUtils.DEFAULT_BUTTON_FONT);

        Dimension preferredSize = new Dimension(decrementButton.getPreferredSize().width, 100);

        return new ButtonsTogetherScrollBarSkin(
                topCap, decrementButton, incrementButton, trackPainter, scrollerThumb,
                topCapRecess, decrementButtonRecess, minimumThumbSize, preferredSize);
    }

    /**
     * In practice we should initialize this from the AppleScrollBarVariant
     * property in $HOME/Library/Preferences/.GlobalPreferences.plist, which
     * will be either Single (for separate) or DoubleMax (for together).
     *
     * @return <code>true</code> if buttons should be separate,
     *         <code>false</code> if buttons should be placed together at right
     *         or bottom.
     */
    private static boolean initializeSeparateStatus() {
        return false;
    }
}