File: ButtonsSeparateScrollBarSkin.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 (156 lines) | stat: -rw-r--r-- 6,773 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
package com.explodingpixels.widgets.plaf;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.MouseListener;

import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollBar;

import com.explodingpixels.painter.MacWidgetsPainter;
import com.explodingpixels.swingx.EPPanel;

/**
 * A {@link ScrollBarSkin} with the buttons placed at the bottom or right of the scroll bar.
 */
public class ButtonsSeparateScrollBarSkin implements ScrollBarSkin {

    private JComponent fThumbContainer = new JPanel();

    private EPPanel fThumb = new EPPanel();

    private EPPanel fTrack = new EPPanel();

    private AbstractButton fDecrementButton;

    private AbstractButton fIncrementButton;

    private Dimension fMinimumThumbSize;

    private int fDecrementButtonTrackRecess;

    private int fIncrementButtonTrackRecess;

    private final Dimension fPreferredSize;

    private static final Rectangle EMPTY_BOUNDS = new Rectangle(0, 0, 0, 0);

    /**
     * Creates a {@code ButtonsTogetherScrollBarSkin} using the given parameters.
     *
     * @param decrementButton       the button to cause a decrement in the scroll bar to occur.
     * @param incrementButton       the button to cause a increment in the scroll bar to occur.
     * @param trackPainter          the {@link com.explodingpixels.painter.MacWidgetsPainter} to use to paint the track.
     * @param scrollThumbPainter    the {@link com.explodingpixels.painter.MacWidgetsPainter} to use to paint the scroll thumb.
     * @param decrementButtonRecess the number of pixels to allow the scrollbar to "recess" into the
     *                              decrement button. this is useful when using scroll bars with rounded ends.
     * @param incrementButtonRecess the number of pixels to allow the scrollbar to "recess" into the
     *                              increment button. this is useful when using scroll bars with rounded ends.
     * @param minimumThumbSize      the minimum size that the scroll thumb can be.
     * @param preferredSize         the preferred size of this skin.
     */
    public ButtonsSeparateScrollBarSkin(
            AbstractButton decrementButton, AbstractButton incrementButton, MacWidgetsPainter<Component> trackPainter,
            MacWidgetsPainter<Component> scrollThumbPainter, int decrementButtonRecess,
            int incrementButtonRecess, Dimension minimumThumbSize, Dimension preferredSize) {
        fDecrementButton = decrementButton;
        fIncrementButton = incrementButton;
        fTrack.setBackgroundPainter(trackPainter);
        fThumb.setBackgroundPainter(scrollThumbPainter);
        fDecrementButtonTrackRecess = decrementButtonRecess;
        fIncrementButtonTrackRecess = incrementButtonRecess;
        fMinimumThumbSize = minimumThumbSize;
        fPreferredSize = preferredSize;

        fThumbContainer.setLayout(null);
        fThumbContainer.setOpaque(false);

        fThumb.setOpaque(false);

//        fThumbContainer.setBorder(BorderFactory.createLineBorder(Color.RED));
//        fTrack.setBorder(BorderFactory.createLineBorder(Color.RED));
    }

    // ScrollBarSkin implementation. //////////////////////////////////////////////////////////////

    public Dimension getMinimumThumbSize() {
        return fMinimumThumbSize;
    }

    public Dimension getPreferredSize() {
        return fPreferredSize;
    }

    public Rectangle getScrollThumbBounds() {
        return fThumb.getBounds();
    }

    public Rectangle getTrackBounds() {
        return fThumbContainer.getBounds();
    }

    public void installComponents(JScrollBar scrollBar) {
        // add the components to the scrollbar. order matters here - components added first, are
        // drawn last (on top).
        scrollBar.add(fThumbContainer);
        scrollBar.add(fIncrementButton);
        scrollBar.add(fDecrementButton);
        scrollBar.add(fTrack);

        // add the actual scroller thumb (the component that will be painted) to the scroller thumb
        // container.
        fThumbContainer.add(fThumb);
    }

    public void layoutTrackOnly(JScrollBar scrollBar, ScrollBarOrientation orientation) {
        fIncrementButton.setBounds(EMPTY_BOUNDS);
        fDecrementButton.setBounds(EMPTY_BOUNDS);
        fThumbContainer.setBounds(EMPTY_BOUNDS);

        Rectangle r = scrollBar.getBounds();
        fTrack.setBounds(0, 0, r.width, r.height);
    }

    public void layoutEverything(JScrollBar scrollBar, ScrollBarOrientation orientation) {
        // 1) layout the scrollbar buttons.
        int incrementButtonHeight = orientation.getLength(fIncrementButton.getPreferredSize());
        int decrementButtonHeight = orientation.getLength(fDecrementButton.getPreferredSize());
        int scrollBarLength = orientation.getLength(scrollBar.getSize());
        int incrementButtonPosition = scrollBarLength - incrementButtonHeight;
        int decrementButtonPosition = 0;

        fIncrementButton.setBounds(
                orientation.createBounds(scrollBar, incrementButtonPosition, incrementButtonHeight));
        fDecrementButton.setBounds(
                orientation.createBounds(scrollBar, decrementButtonPosition, decrementButtonHeight));

        // 2) layout the track and the scroller thumb container.
        // start the track and the scroller bar container overlapping the decrement button.
        // this handles a top cap that isn't square and is intended to receive part of the scroller
        // thumb.
        int trackAndThumbPosition = decrementButtonHeight - fDecrementButtonTrackRecess;
        // the height of the track and scroll bar container should be slightly greater than that of
        // the empty space between the top cap, and the decrement button, if recesses have been
        // specified. that is, the track and scroll bar container will overlap the top cap and
        // decrement buttons if a non-zero recess has been specified.
        int trackLength = incrementButtonPosition + fIncrementButtonTrackRecess - trackAndThumbPosition;
        Rectangle trackAndThumbBounds =
                orientation.createBounds(scrollBar, trackAndThumbPosition, trackLength);
        fTrack.setBounds(trackAndThumbBounds);
        fThumbContainer.setBounds(trackAndThumbBounds);
    }

    public void installMouseListenersOnButtons(MouseListener decrementMoustListener,
                                               MouseListener incrementMouseListener) {
        fDecrementButton.addMouseListener(decrementMoustListener);
        fIncrementButton.addMouseListener(incrementMouseListener);
    }

    public void setScrollThumbBounds(Rectangle bounds) {
        fThumb.setBounds(bounds);
    }

}