Subversion Repositories svn.mios

Rev

Rev 649 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
649 adamjking 1
/*
2
 * @(#)
3
 *
4
 * Copyright (C) 2006  Adam King (adamjking@optusnet.com.au)
5
 *
6
 * This application is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this application; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
package org.midibox.utils.gui;
22
 
23
import java.awt.Color;
24
import java.awt.Dimension;
25
import java.awt.Graphics;
26
import java.awt.Rectangle;
27
 
28
import javax.swing.ImageIcon;
29
import javax.swing.JSlider;
30
import javax.swing.plaf.basic.BasicSliderUI;
31
 
32
public class FaderUI extends BasicSliderUI {
33
 
34
    private ImageIcon vertThumbIcon2;
35
 
36
    private ImageIcon horizThumbIcon2;
37
 
38
    private boolean drawGroove;
39
 
40
    private Color darkShadow = new Color(102, 102, 102);
41
 
42
    private Color shadow = new Color(55, 55, 55);
43
 
44
    private Color highlight = Color.WHITE;
45
 
657 adamjking 46
    private int trackWidth;
47
 
649 adamjking 48
    public FaderUI(ImageIcon horizThumbIcon2, ImageIcon vertThumbIcon2,
49
            boolean drawGroove) {
50
        super(null);
51
        this.horizThumbIcon2 = horizThumbIcon2;
52
        this.vertThumbIcon2 = vertThumbIcon2;
657 adamjking 53
 
54
        this.trackWidth = 5;
55
 
649 adamjking 56
        this.drawGroove = drawGroove;
57
    }
58
 
59
    public void paintThumb(Graphics g) {
60
 
61
        Rectangle knobBounds = thumbRect;
62
 
63
        g.translate(knobBounds.x, knobBounds.y);
64
 
65
        if (slider.getOrientation() == JSlider.HORIZONTAL) {
66
            horizThumbIcon2.paintIcon(slider, g, 0, 0);
67
        } else {
68
            vertThumbIcon2.paintIcon(slider, g, 0, 0);
69
        }
70
        g.translate(-knobBounds.x, -knobBounds.y);
71
        g.dispose();
72
    }
73
 
74
    public void paintTrack(Graphics g) {
75
        Color trackColor = !slider.isEnabled() ? shadow : slider
76
                .getForeground();
77
 
78
        boolean leftToRight = slider.getComponentOrientation().isLeftToRight();
79
 
80
        g.translate(trackRect.x, trackRect.y);
81
 
82
        int trackLeft = 0;
83
        int trackTop = 0;
84
        int trackRight = 0;
85
        int trackBottom = 0;
86
 
87
        // Draw the track
88
        if (slider.getOrientation() == JSlider.HORIZONTAL) {
89
            trackTop = (int) (getThumbSize().getHeight() / 2)
90
                    - (getTrackWidth() / 2);
91
            trackBottom = (trackTop + getTrackWidth());
92
            trackRight = trackRect.width - 1;
93
        } else {
94
            trackLeft = ((int) (getThumbSize().getWidth() / 2) - (getTrackWidth() / 2));
95
            trackRight = (trackLeft + getTrackWidth());
96
            trackBottom = trackRect.height - 1;
97
        }
98
 
99
        if (slider.isEnabled()) {
100
            g.setColor(darkShadow);
101
            g.drawRect(trackLeft, trackTop, (trackRight - trackLeft) - 1,
102
                    (trackBottom - trackTop) - 1);
103
 
104
            g.setColor(highlight);
105
            g.drawLine(trackLeft + 1, trackBottom, trackRight, trackBottom);
106
            g.drawLine(trackRight, trackTop + 1, trackRight, trackBottom);
107
 
108
            g.setColor(shadow);
109
            g.drawLine(trackLeft + 1, trackTop + 1, trackRight - 2,
110
                    trackTop + 1);
111
            g.drawLine(trackLeft + 1, trackTop + 1, trackLeft + 1,
112
                    trackBottom - 2);
113
        } else {
114
            g.setColor(shadow);
115
            g.drawRect(trackLeft, trackTop, (trackRight - trackLeft) - 1,
116
                    (trackBottom - trackTop) - 1);
117
        }
118
 
119
        int xoff;
120
        int yoff;
121
 
122
        if (slider.getOrientation() == JSlider.HORIZONTAL) {
123
            xoff = 0;
124
            yoff = getTrackWidth() / 2;
125
        } else {
126
            xoff = getTrackWidth() / 2;
127
            yoff = 0;
128
        }
129
 
130
        if (drawGroove) {
131
 
132
            int grooveLeft = (trackLeft + xoff)
133
                    - (int) getThumbSize().getWidth() / 2;
134
            int grooveTop = (trackTop + yoff)
135
                    - (int) getThumbSize().getHeight() / 2;
136
            int grooveRight = (trackRight - xoff)
137
                    + (int) getThumbSize().getWidth() / 2;
138
            ;
139
            int grooveBottom = (trackBottom - yoff)
140
                    + (int) getThumbSize().getHeight() / 2;
141
 
142
            if (slider.isEnabled()) {
143
                g.setColor(darkShadow);
144
                g.drawRect(grooveLeft, grooveTop,
145
                        (grooveRight - grooveLeft) - 1,
146
                        (grooveBottom - grooveTop) - 1);
147
 
148
                g.setColor(highlight);
149
                g.drawLine(grooveLeft + 1, grooveBottom, grooveRight,
150
                        grooveBottom);
151
                g.drawLine(grooveRight, grooveTop + 1, grooveRight,
152
                        grooveBottom);
153
 
154
                g.setColor(shadow);
155
                g.drawLine(grooveLeft + 1, grooveTop + 1, grooveRight - 2,
156
                        grooveTop + 1);
157
                g.drawLine(grooveLeft + 1, grooveTop + 1, grooveLeft + 1,
158
                        grooveBottom - 2);
159
            } else {
160
                g.setColor(shadow);
161
                g.drawRect(grooveLeft, grooveTop,
162
                        (grooveRight - grooveLeft) - 1,
163
                        (grooveBottom - grooveTop) - 1);
164
            }
165
        }
166
 
167
        // Draw the fill
168
        int middleOfThumb = 0;
169
        int fillTop = 0;
170
        int fillLeft = 0;
171
        int fillBottom = 0;
172
        int fillRight = 0;
173
 
174
        if (slider.getOrientation() == JSlider.HORIZONTAL) {
175
            middleOfThumb = thumbRect.x + (thumbRect.width / 2);
176
            middleOfThumb -= trackRect.x; // To compensate for the
177
            // g.translate()
178
            fillTop = !slider.isEnabled() ? trackTop : trackTop + 1;
179
            fillBottom = !slider.isEnabled() ? trackBottom - 1
180
                    : trackBottom - 2;
181
 
182
            if (!drawInverted()) {
183
                fillLeft = !slider.isEnabled() ? trackLeft : trackLeft + 1;
184
                fillRight = middleOfThumb;
185
            } else {
186
                fillLeft = middleOfThumb;
187
                fillRight = !slider.isEnabled() ? trackRight - 1
188
                        : trackRight - 2;
189
            }
190
        } else {
191
            middleOfThumb = thumbRect.y + (thumbRect.height / 2);
192
            middleOfThumb -= trackRect.y; // To compensate for the
193
            // g.translate()
194
            fillLeft = !slider.isEnabled() ? trackLeft : trackLeft + 1;
195
            fillRight = !slider.isEnabled() ? trackRight - 1 : trackRight - 2;
196
 
197
            if (!drawInverted()) {
198
                fillTop = middleOfThumb;
199
                fillBottom = !slider.isEnabled() ? trackBottom - 1
200
                        : trackBottom - 2;
201
            } else {
202
                fillTop = !slider.isEnabled() ? trackTop : trackTop + 1;
203
                fillBottom = middleOfThumb;
204
            }
205
        }
206
 
207
        if (slider.isEnabled()) {
208
            g.setColor(slider.getBackground());
209
            g.drawLine(fillLeft, fillTop, fillRight, fillTop);
210
            g.drawLine(fillLeft, fillTop, fillLeft, fillBottom);
211
 
212
            g.setColor(shadow);
213
            g.fillRect(fillLeft + 1, fillTop + 1, fillRight - fillLeft,
214
                    fillBottom - fillTop);
215
        }
216
 
217
        g.setColor(shadow);
218
        g.fillRect(trackLeft + 1, trackTop + 1, (trackRight - trackLeft) - 2,
219
                (trackBottom - trackTop) - 2);
220
 
221
        g.translate(-trackRect.x, -trackRect.y);
222
    }
223
 
224
    public Dimension getThumbSize() {
225
        if (slider.getOrientation() == JSlider.HORIZONTAL) {
226
            return new Dimension(horizThumbIcon2.getIconWidth(),
227
                    horizThumbIcon2.getIconHeight());
228
        } else {
229
            return new Dimension(vertThumbIcon2.getIconWidth(), vertThumbIcon2
230
                    .getIconHeight());
231
        }
232
    }
233
 
657 adamjking 234
    public void setTrackWidth(int trackWidth) {
235
        this.trackWidth = trackWidth;
649 adamjking 236
    }
237
 
657 adamjking 238
    public int getTrackWidth() {
239
        return trackWidth;
240
    }
241
 
649 adamjking 242
    /**
243
     * Returns the longer dimension of the slide bar. (The slide bar is only the
244
     * part that runs directly under the thumb)
245
     */
246
    protected int getTrackLength() {
247
        if (slider.getOrientation() == JSlider.HORIZONTAL) {
248
            return trackRect.width;
249
        }
250
        return trackRect.height;
251
    }
252
}