1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.gui;
21
22 import java.awt.Color;
23 import java.awt.Graphics;
24 import java.awt.Insets;
25 import java.awt.event.MouseEvent;
26 import javax.swing.JToggleButton;
27 import org.xnap.commons.gui.action.ToggleAction;
28
29 /***
30 * This class provides a toggelable toolbar button with an appropriate sized
31 * icon that can display a little arrow as a hint for a menu. The border of the
32 * button is only visible when the mouse hovers over the button.
33 */
34 public class ToolBarToggleButton extends JToggleButton {
35
36 private boolean showBorder;
37 private boolean showMenuHint;
38
39 public ToolBarToggleButton(ToggleAction action, boolean showMenuHint)
40 {
41 super(action);
42
43 this.showMenuHint = showMenuHint;
44
45 setContentAreaFilled(false);
46 setText(null);
47 setMargin(new Insets(1, 1, 1, 1));
48
49 putClientProperty("hideActionText", Boolean.TRUE);
50 }
51
52 /***
53 * Returns true, if the mouse is currently over the button.
54 */
55 public boolean isMouseOver()
56 {
57 return showBorder;
58 }
59
60 @Override
61 protected void paintBorder(Graphics g)
62 {
63 if (showBorder || isSelected()) {
64 super.paintBorder(g);
65 }
66 }
67
68 @Override
69 protected void paintComponent(Graphics g)
70 {
71 super.paintComponent(g);
72
73 if (showMenuHint) {
74 int w = getWidth();
75 int h = getHeight();
76
77 g.setColor(Color.black);
78
79
80 for (int i = 0; i < 4; i++) {
81 g.drawLine(w - 8 - i, h - 3 - i, w - 8 + i, h - 3 - i);
82 }
83 g.drawLine(w - 8 - 4, h - 3 - 4, w - 8 + 4, h - 3 - 4);
84 }
85 }
86
87 @Override
88 protected void processMouseEvent(MouseEvent e)
89 {
90 super.processMouseEvent(e);
91
92 if (e.getID() == MouseEvent.MOUSE_ENTERED) {
93 showBorder = true;
94 setContentAreaFilled(true);
95 repaint();
96 }
97 else if (e.getID() == MouseEvent.MOUSE_EXITED) {
98 showBorder = false;
99 setContentAreaFilled(false);
100 repaint();
101 }
102 }
103
104 }