1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.component;
21
22 import java.awt.Color;
23 import java.awt.Graphics;
24 import java.awt.Insets;
25 import java.awt.event.MouseEvent;
26 import java.beans.PropertyChangeEvent;
27 import java.beans.PropertyChangeListener;
28
29 import javax.swing.JToggleButton;
30
31 import org.xnap.action.ToggleAction;
32 import org.xnap.gui.action.PanelAction;
33 import org.xnap.gui.util.IconHelper;
34
35 /***
36 * This class provides a toolbar button with an appropriate sized icon.
37 */
38 public class XNapToolBarToggleButton extends JToggleButton
39 {
40
41
42
43
44
45 private boolean showBorder = false;
46 private boolean showMenuHint;
47
48
49
50 public XNapToolBarToggleButton(ToggleAction action)
51 {
52 super(action);
53
54 String filename = (String)action.getValue(IconHelper.XNAP_ICON);
55 setIcon(IconHelper.getToolBarIcon(filename));
56
57 setText(null);
58 setMargin(new Insets(1, 1, 1, 1));
59
60 setSelected(action.isSelected());
61 action.addPropertyChangeListener(new SelectionListener());
62
63 showMenuHint = (action instanceof PanelAction);
64 }
65
66
67
68 /***
69 * Needed by {@link PanelAction}
70 */
71 public boolean isMouseOver()
72 {
73 return showBorder;
74 }
75
76 protected void paintBorder(Graphics g)
77 {
78 if (showBorder || isSelected()) {
79 super.paintBorder(g);
80 }
81 }
82
83 protected void paintComponent(Graphics g)
84 {
85 super.paintComponent(g);
86
87 if (showMenuHint) {
88 int w = getWidth();
89 int h = getHeight();
90
91 g.setColor(Color.black);
92
93
94 for (int i = 0; i < 4; i++) {
95 g.drawLine(w - 8 - i, h - 3 - i, w - 8 + i, h - 3 - i);
96 }
97 g.drawLine(w - 8 - 4, h - 3 - 4, w - 8 + 4, h - 3 - 4);
98 }
99 }
100
101 protected void processMouseEvent(MouseEvent e)
102 {
103 super.processMouseEvent(e);
104
105 if (e.getID() == MouseEvent.MOUSE_ENTERED) {
106 showBorder = true;
107 repaint();
108 }
109 else if (e.getID() == MouseEvent.MOUSE_EXITED) {
110 showBorder = false;
111 repaint();
112 }
113 }
114
115
116
117 private class SelectionListener implements PropertyChangeListener
118 {
119
120 public void propertyChange(PropertyChangeEvent e)
121 {
122 if (e.getPropertyName().equals("selected")) {
123 setSelected(((Boolean)e.getNewValue()).booleanValue());
124 }
125 }
126
127 }
128
129 }