Subversion Repositories svn.mios

Rev

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

Rev Author Line No. Line
213 tk 1
/*
628 adamjking 2
 * @(#)MyButtonUI.java  beta8   2006/04/23
213 tk 3
 *
628 adamjking 4
 * Copyright (C) 2008    Adam King (adamjking@optusnet.com.au)
213 tk 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.BasicToggleButtonUI;
35
 
36
public class MyButtonUI extends BasicToggleButtonUI {
37
 
38
    private final static Color FOCUS_COLOR = UIManager.getColor("Slider.focus");
39
 
40
    protected ImageIcon onIcon;
41
 
42
    protected ImageIcon offIcon;
43
 
44
    protected Dimension d;
45
 
46
    public MyButtonUI(ImageIcon on, ImageIcon off) {
47
        onIcon = on;
48
        offIcon = off;
49
        d = new Dimension(onIcon.getIconWidth() + 4, onIcon.getIconHeight() + 4);
50
    }
51
 
52
    public void paint(Graphics g, JComponent c) {
53
        AbstractButton b = (AbstractButton) c;
54
        ButtonModel model = b.getModel();
55
        c.setBorder(null);
56
        c.setOpaque(false);
57
 
58
        if (model.isSelected() || model.isPressed()) {
59
            onIcon.paintIcon(c, g, (c.getWidth() - onIcon.getIconWidth()) / 2,
60
                    (c.getHeight() - onIcon.getIconHeight()) / 2);
61
        } else {
62
            offIcon.paintIcon(c, g,
63
                    (c.getWidth() - offIcon.getIconWidth()) / 2,
64
                    (c.getHeight() - offIcon.getIconHeight()) / 2);
65
        }
66
 
67
        if (b.hasFocus()) {
68
            paintFocus(g, b);
69
        }
70
        g.dispose();
71
    }
72
 
73
    protected void paintFocus(Graphics g, AbstractButton c) {
74
        AbstractButton b = (AbstractButton) c;
75
 
76
        g.setColor(FOCUS_COLOR);
77
        Graphics2D g2d = (Graphics2D) g;
78
 
79
        g2d.setStroke(new BasicStroke(1f, BasicStroke.CAP_BUTT,
80
                BasicStroke.JOIN_ROUND, 1f, new float[] { 1f }, 0f));
81
 
82
        int width = b.getWidth();
83
        int height = b.getHeight();
84
 
85
        g2d.drawLine(0, 0, width, 0);
86
        g2d.drawLine(width - 1, 0, width - 1, height);
87
        g2d.drawLine(0, height - 1, width, height - 1);
88
        g2d.drawLine(0, 0, 0, height);
89
 
90
        g2d.setStroke(new BasicStroke());
91
        g2d.setColor(Color.BLACK);
92
    }
93
 
94
    public Dimension getPreferredSize(JComponent c) {
95
        return d;
96
    }
97
 
98
    public Dimension getMinimumSize(JComponent c) {
99
        return d;
100
    }
101
}