1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.ledder;
21 import java.awt.Color;
22 import java.awt.Font;
23 import java.awt.event.MouseEvent;
24 import java.awt.event.MouseListener;
25
26 import javax.swing.BorderFactory;
27 import javax.swing.Icon;
28 import javax.swing.JLabel;
29 import javax.swing.SwingConstants;
30 import javax.swing.SwingUtilities;
31 import javax.swing.border.BevelBorder;
32
33 import javax.swing.border.Border;
34 import javax.swing.border.CompoundBorder;
35 import javax.swing.border.LineBorder;
36
37 import com.cosylab.gui.components.util.PopupManager;
38 import com.cosylab.gui.components.AbstractDisplayerPanel;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class Led extends JLabel {
56
57
58 private Icon iconWhenOff = null;
59
60 private Icon iconWhenOn = null;
61
62
63 private boolean on = false;
64
65 private CustomBorder border;
66 private boolean editable = false;
67
68
69
70
71
72
73
74 public Led() {
75 this(Color.GREEN, Color.RED);
76
77 }
78
79
80
81
82
83
84
85 public void setEditable(boolean editable){
86 this.editable=editable;
87 if (editable) {
88 setBorder(getCustomBorder());
89 } else {
90 setBorder(null);
91 }
92 }
93
94
95
96
97
98
99
100 public Led(Color colorWhenOn, Color colorWhenOff) {
101
102 this("< No description >", colorWhenOn, colorWhenOff);
103
104 }
105
106
107
108
109
110
111
112
113 public Led(String description, Color colorWhenOn, Color colorWhenOff) {
114 super();
115
116 setInheritsPopupMenu(true);
117
118 setFont(getFont().deriveFont(Font.PLAIN,12f));
119
120 setText(description);
121 setVerticalAlignment(SwingConstants.CENTER);
122 setVerticalTextPosition(SwingConstants.CENTER);
123
124 iconWhenOff = LedIconFactory.createIcon(colorWhenOff);
125 iconWhenOn = LedIconFactory.createIcon(colorWhenOn);
126
127 updateIcon();
128 setOpaque(false);
129
130 addMouseListener(new MouseListener() {
131
132 public void mouseClicked(MouseEvent e) {
133
134
135
136 }
137
138 public void mouseEntered(MouseEvent e) {
139 if(editable){
140 getCustomBorder().setVisible(true);
141 repaint();
142 }
143 }
144
145 public void mouseExited(MouseEvent e) {
146 if(editable){
147 getCustomBorder().setVisible(false);
148 repaint();
149 }
150 }
151
152 public void mousePressed(MouseEvent e) {
153 if(SwingUtilities.isLeftMouseButton(e)){
154 if(editable){
155 if(isOn()){
156 setOn(false);
157
158 }else {
159 setOn(true);
160 }
161
162 }
163 }
164 }
165
166 public void mouseReleased(MouseEvent e) {
167
168
169 }
170
171
172 });
173
174
175 }
176
177
178
179
180
181
182
183 public boolean isOn() {
184 return on;
185 }
186
187
188
189
190
191
192
193 public void setOn(boolean value) {
194 on = value;
195 updateIcon();
196 }
197
198
199
200
201 private void updateIcon() {
202 setIcon(on ? iconWhenOn : iconWhenOff);
203 }
204
205
206
207
208
209
210
211 public void setText(String s) {
212 super.setText(s);
213 setToolTipText(s);
214 }
215
216
217
218
219
220
221 public static void main(String[] args) {
222 javax.swing.JDialog dialog = new javax.swing.JDialog();
223 dialog.setModal(true);
224 dialog.setSize(200, 200);
225
226 dialog.getContentPane().setLayout(new java.awt.FlowLayout());
227
228 Led l = new Led();
229 l.setText("Transmit");
230 dialog.getContentPane().add(l);
231
232 l = new Led(Color.YELLOW, Color.BLUE);
233 l.setText("Receive");
234 dialog.getContentPane().add(l);
235
236 l = new Led("Status", Color.RED, Color.GREEN);
237 l.setOn(false);
238 dialog.getContentPane().add(l);
239
240 l = new Led(Color.blue, Color.blue);
241 l.setText("On");
242 dialog.getContentPane().add(l);
243
244 l = new Led(Color.GREEN, Color.RED);
245 l.setText("Off");
246 dialog.getContentPane().add(l);
247
248 l = new Led(Color.yellow, Color.blue);
249 l.setText("Power");
250 dialog.getContentPane().add(l);
251
252 dialog.show();
253
254 System.exit(0);
255 }
256
257
258
259
260 private CustomBorder getCustomBorder() {
261 if (border == null) {
262 border = new CustomBorder();
263 border.setVisible(false);
264 }
265
266 return border;
267 }
268
269
270
271
272
273
274 public boolean isEditable() {
275 return editable;
276 }
277
278
279 }
280