1 package org.catacomb.druid.swing;
2
3 import org.catacomb.interlish.structure.MouseSource;
4 import org.catacomb.util.ColorUtil;
5
6
7 import java.awt.Color;
8 import java.awt.Font;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import java.awt.event.MouseEvent;
12 import java.awt.event.MouseListener;
13
14 import javax.swing.ImageIcon;
15 import javax.swing.border.Border;
16
17
18 public class DToggleButton extends DBaseButton implements ActionListener, MouseListener,
19 MouseSource {
20
21 boolean state;
22
23 Color bgColor;
24
25 private ImageIcon offIcon;
26 private ImageIcon onIcon;
27 static final long serialVersionUID = 1001;
28
29 boolean pressOn;
30 boolean mouseOn;
31
32 int padLeft;
33 int padRight;
34 int padTop;
35 int padBottom;
36
37
38 public DToggleButton(String lab) {
39 super();
40 if (lab != null && lab.length() > 0) {
41 setText(lab);
42 }
43 label = lab;
44
45 setFont(new Font("sansserif", Font.PLAIN, 12));
46
47 addActionListener(this);
48 setFocusPainted(false);
49 addMouseListener(this);
50 }
51
52
53 public void setTooltip(String s) {
54 setToolTipText(s);
55 }
56
57 public void setBg(Color c) {
58 bgColor = c;
59 setBackground(c);
60 }
61
62
63 public void setFg(Color c) {
64 setForeground(c);
65 }
66
67
68
69 public String toString() {
70 return "DToggleButton " + label;
71 }
72
73 public void suggest() {
74
75 }
76
77
78 public String getStringIdentifier() {
79 return label;
80 }
81
82
83
84 public void setPadding(int p) {
85 setPadding(p, p, p, p);
86 }
87
88
89
90 public void actionPerformed(ActionEvent aev) {
91 pressOn = !pressOn;
92 applyState();
93 deliverAction(pressOn);
94 }
95
96
97 public void setState(boolean b) {
98 pressOn = b;
99 applyState();
100 }
101
102
103 public void applyState() {
104 Color cbg = bgColor;
105 Color cbgd = ColorUtil.darker(cbg);
106 Color cbgb = ColorUtil.brighter(cbg);
107
108 Border b = null;
109
110 if (pressOn) {
111 setBackground(cbgd);
112 if (mouseOn) {
113 b = BorderUtil.makeEtchedUpBorder(cbgd);
114 } else {
115 b = BorderUtil.makeSunkenBorder(cbgd, cbg);
116 }
117
118 } else {
119 setBackground(cbg);
120 if (mouseOn) {
121 b = BorderUtil.makeEtchedUpBorder(cbg);
122
123 } else {
124 b = BorderUtil.makeRaisedBorder(cbg, cbgb);
125 }
126 }
127
128 Border bpad = BorderUtil.makeEmptyBorder(padLeft, padRight, padTop, padBottom);
129 Border btot = BorderUtil.makeCompoundBorder(bpad, b);
130 setBorder(btot);
131
132
133 if (pressOn) {
134 if (onIcon != null) {
135 setIcon(onIcon);
136 }
137 } else {
138 if (offIcon != null) {
139 setIcon(offIcon);
140 }
141 }
142 }
143
144
145
146 public void setOnIcon(ImageIcon icon) {
147 onIcon = icon;
148 if (state) {
149 applyState();
150 }
151 }
152
153
154 public void setOffIcon(ImageIcon icon) {
155 offIcon = icon;
156 if (!state) {
157 applyState();
158 }
159 }
160
161
162 public void mouseClicked(MouseEvent e) {
163 }
164
165
166 public void mousePressed(MouseEvent e) {
167 }
168
169
170 public void mouseReleased(MouseEvent e) {
171 }
172
173
174 public void mouseEntered(MouseEvent e) {
175 mouseOn = true;
176 applyState();
177 }
178
179
180 public void mouseExited(MouseEvent e) {
181 mouseOn = false;
182 applyState();
183 }
184
185
186 public void setPadding(int pl, int pr, int pt, int pb) {
187 padLeft = pl;
188 padRight = pr;
189 padTop = pt;
190 padBottom = pb;
191 applyState();
192 }
193
194
195 @Override
196 public void deSuggest() {
197
198
199 }
200
201 }