Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
250 rutgerv 1
/*
2
 * @(#)MyButtonUI.java  beta7   2006/04/23
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.BasicStroke;
24
import java.awt.Color;
25
import java.awt.Dimension;
26
import java.awt.Graphics;
27
import java.awt.Graphics2D;
28
 
29
import javax.swing.AbstractButton;
30
import javax.swing.ButtonModel;
31
import javax.swing.ImageIcon;
32
import javax.swing.JComponent;
33
import javax.swing.UIManager;
34
import javax.swing.plaf.basic.BasicButtonUI;
35
 
36
public class SeqButtonUI extends BasicButtonUI {
37
 
38
    private final static Color FOCUS_COLOR = UIManager.getColor("Slider.focus");
39
 
40
    protected ImageIcon icon0;
41
    protected ImageIcon icon1;
42
    protected ImageIcon icon2;
43
    protected ImageIcon icon3;
44
    protected int realValue = 0;
45
    protected Dimension d;
46
 
47
    public SeqButtonUI() {
48
        icon0 = ImageLoader.getImageIcon("button.png");
49
        icon1 = ImageLoader.getImageIcon("button_on.png");
50
        icon2 = ImageLoader.getImageIcon("button_a.png");
51
        icon3 = ImageLoader.getImageIcon("button_s.png");
52
        d = new Dimension(icon0.getIconWidth() + 4, icon0.getIconHeight() + 4);
53
    }
54
 
55
    public void paint(Graphics g, JComponent c) {
56
        AbstractButton b = (AbstractButton) c;
57
        ButtonModel model = b.getModel();
58
        c.setBorder(null);
59
        c.setOpaque(false);
60
 
628 adamjking 61
        switch (realValue) {
62
        case 0:
63
            icon0.paintIcon(c, g, (c.getWidth() - icon0.getIconWidth()) / 2, (c
64
                    .getHeight() - icon0.getIconHeight()) / 2);
65
            break;
66
        case 1:
67
            icon1.paintIcon(c, g, (c.getWidth() - icon1.getIconWidth()) / 2, (c
68
                    .getHeight() - icon1.getIconHeight()) / 2);
69
            break;
70
        case 2:
71
            icon2.paintIcon(c, g, (c.getWidth() - icon2.getIconWidth()) / 2, (c
72
                    .getHeight() - icon2.getIconHeight()) / 2);
73
            break;
74
        case 3:
75
            icon3.paintIcon(c, g, (c.getWidth() - icon3.getIconWidth()) / 2, (c
76
                    .getHeight() - icon3.getIconHeight()) / 2);
77
            break;
78
        }
79
 
250 rutgerv 80
        if (b.hasFocus()) {
81
            paintFocus(g, b);
82
        }
83
        g.dispose();
84
    }
85
 
86
    public void setValue(int i) {
628 adamjking 87
        realValue = i;
250 rutgerv 88
    }
89
 
90
    protected void paintFocus(Graphics g, AbstractButton c) {
91
        AbstractButton b = (AbstractButton) c;
92
 
93
        g.setColor(FOCUS_COLOR);
94
        Graphics2D g2d = (Graphics2D) g;
95
 
96
        g2d.setStroke(new BasicStroke(1f, BasicStroke.CAP_BUTT,
97
                BasicStroke.JOIN_ROUND, 1f, new float[] { 1f }, 0f));
98
 
99
        int width = b.getWidth();
100
        int height = b.getHeight();
101
 
102
        g2d.drawLine(0, 0, width, 0);
103
        g2d.drawLine(width - 1, 0, width - 1, height);
104
        g2d.drawLine(0, height - 1, width, height - 1);
105
        g2d.drawLine(0, 0, 0, height);
106
 
107
        g2d.setStroke(new BasicStroke());
108
        g2d.setColor(Color.BLACK);
109
    }
110
 
111
    public Dimension getPreferredSize(JComponent c) {
112
        return d;
113
    }
114
 
115
    public Dimension getMinimumSize(JComponent c) {
116
        return d;
117
    }
118
}