summaryrefslogtreecommitdiffstats
path: root/src/cppcheckplus/control/MyToolbarButton.java
blob: e709296a6504255266b94f9e5ef073a8677eec2a (plain) (blame)
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

package cppcheckplus.control;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.border.Border;

public class MyToolbarButton extends JButton
{
	private int buttonSize;
	private Color roverBorderColor;
	private Border roverBorder;
	private Border emptyBorder;

	public MyToolbarButton()
	{
		super();
		buttonSize = 20;
		roverBorderColor = MyContorlUtil.BUTTON_ROVER_COLOR;
		roverBorder = new Border() {

			public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
			{
				g.setColor(roverBorderColor);
				g.drawRect(x, y, width - 1, height - 1);
			}

			public Insets getBorderInsets(Component c)
			{
				return new Insets(1, 1, 1, 1);
			}

			public boolean isBorderOpaque()
			{
				return true;
			}
		};
		emptyBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);
		init();
	}

	private void init()
	{
		setVerticalAlignment(0);
		setFont(MyContorlUtil.FONT_12_BOLD);
		setOpaque(false);
		setBorder(emptyBorder);
		setContentAreaFilled(false);
		setFocusPainted(false);
		addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e)
			{
				setBorder(roverBorder);
			}

			public void mouseExited(MouseEvent e)
			{
				setBorder(emptyBorder);
			}
		});
	}

	@Override
	public void setIcon(Icon icon)
	{
		super.setIcon(icon);
		if (icon == null)
		{
			setPressedIcon(null);
			setRolloverIcon(null);
		} else
		{
			Icon pressedIcon = MyContorlUtil.createMovedIcon(icon);
			setPressedIcon(pressedIcon);
		}
	}


	@Override
	public Dimension getPreferredSize()
	{
		int width = super.getPreferredSize().width;
		width = Math.max(width, buttonSize);
		int height = buttonSize;
		return new Dimension(width, height);
	}
}