-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathNButton.java
More file actions
289 lines (253 loc) · 8.48 KB
/
NButton.java
File metadata and controls
289 lines (253 loc) · 8.48 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package nodebox.client;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
/**
* A custom button that uses an icon and shadowed text.
*/
public class NButton extends JComponent implements MouseListener {
public static final int BUTTON_HEIGHT = 21;
public static final int IMAGE_TEXT_MARGIN = 3;
public static final int TEXT_BASELINE = 14;
public static Image checkOn, checkOff, checkDisabledOn, checkDisabledOff;
static {
try {
checkOn = ImageIO.read(new File("res/check-on.png"));
checkOff = ImageIO.read(new File("res/check-off.png"));
checkDisabledOn = ImageIO.read(new File("res/check-disabled-on.png"));
checkDisabledOff = ImageIO.read(new File("res/check-disabled-off.png"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static enum Mode {
PUSH, CHECK
}
private String text;
private Image normalImage, checkedImage;
private Object actionObject;
private Method actionMethod;
private Mode mode;
private boolean armed = false;
private boolean pressed = false;
private boolean checked = false;
private boolean warn = false;
/**
* Create a push button.
*
* @param text the button label
* @param imageFile the button image
*/
public NButton(String text, String imageFile) {
this(Mode.PUSH, text, imageFile, null);
}
public NButton(String text, Image image) {
init(Mode.PUSH, text, image, null);
}
/**
* Create a check button.
*
* @param text the button label
* @param normalImage the image to show when unchecked
* @param checkedImage the image to show when checked
*/
public NButton(String text, String normalImage, String checkedImage) {
this(Mode.CHECK, text, normalImage, checkedImage);
}
public NButton(Mode mode, String text) {
if (mode != Mode.CHECK)
throw new AssertionError("Only use Mode.CHECK.");
init(mode, text, checkOff, checkOn);
}
private NButton(Mode mode, String text, String normalImage, String checkedImage) {
Image normal, checked = null;
try {
normal = ImageIO.read(new File(normalImage));
if (checkedImage != null)
checked = ImageIO.read(new File(checkedImage));
} catch (IOException e) {
throw new IllegalArgumentException("Cannot load image.", e);
}
init(mode, text, normal, checked);
}
private void init(Mode mode, String text, Image normalImage, Image checkedImage) {
this.mode = mode;
this.text = text;
this.normalImage = normalImage;
this.checkedImage = checkedImage;
int width = measureWidth();
Dimension d = new Dimension(width, BUTTON_HEIGHT);
setSize(d);
setPreferredSize(d);
setMinimumSize(d);
setMaximumSize(d);
addMouseListener(this);
setFocusable(true);
this.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "pressed");
this.getInputMap().put(KeyStroke.getKeyStroke("released SPACE"), "released");
this.getActionMap().put("pressed", new PressedAction());
this.getActionMap().put("released", new ReleasedAction());
}
private int measureWidth() {
// To measure text we need a graphics context. Create an image and use its' graphics context.
BufferedImage tmp = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = tmp.createGraphics();
g2.setFont(Theme.SMALL_BOLD_FONT);
int width = normalImage.getWidth(null);
width += IMAGE_TEXT_MARGIN;
width += (int) g2.getFontMetrics().stringWidth(text);
width += 1; // Anti-aliasing can take up an extra pixel.
return width;
}
/**
* Set the method that will be called when the button is pressed.
*
* @param obj the instance
* @param methodName the name of the method on the instance. The method should not take any arguments.
*/
public void setActionMethod(Object obj, String methodName) {
actionObject = obj;
try {
actionMethod = obj.getClass().getMethod(methodName);
} catch (NullPointerException e) {
throw new IllegalArgumentException("Ojbect or method name cannot be null.");
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("Cannot find method " + methodName + " on object " + actionObject);
}
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
repaint();
}
public Mode getMode() {
return mode;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean v) {
if (v == checked) return;
checked = v;
repaint();
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
int width = measureWidth();
Dimension d = new Dimension(width, BUTTON_HEIGHT);
setSize(d);
setPreferredSize(d);
setMinimumSize(d);
setMaximumSize(d);
}
public boolean isWarning() {
return warn;
}
public void setWarning(boolean warn) {
if (warn == this.warn) return;
this.warn = warn;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
if (isEnabled()) {
if (checked) {
g2.drawImage(checkedImage, 0, 0, null);
} else {
g2.drawImage(normalImage, 0, 0, null);
}
} else {
if (mode == Mode.CHECK && checked) {
g2.drawImage(checkDisabledOn, 0, 0, null);
} else if (mode == Mode.CHECK && !checked) {
g2.drawImage(checkDisabledOff, 0, 0, null);
} else {
Composite oldComposite = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g2.drawImage(normalImage, 0, 0, null);
g2.setComposite(oldComposite);
}
}
int w = normalImage.getWidth(null);
g2.setFont(Theme.SMALL_BOLD_FONT);
if (warn) {
g2.setColor(Theme.TEXT_WARNING_COLOR);
} else if (armed) {
g2.setColor(Theme.TEXT_ARMED_COLOR);
} else if(!isEnabled()) {
g2.setColor(Theme.TEXT_DISABLED_COLOR);
} else {
g2.setColor(Theme.TEXT_NORMAL_COLOR);
}
SwingUtils.drawShadowText(g2, text, w + IMAGE_TEXT_MARGIN, TEXT_BASELINE);
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
onPressed();
}
private void onPressed() {
if (!isEnabled()) return;
requestFocus();
pressed = true;
armed = true;
repaint();
}
public void mouseReleased(MouseEvent e) {
onReleased();
}
private void onReleased() {
if (!isEnabled()) return;
pressed = false;
if (armed) {
armed = false;
if (mode == Mode.CHECK)
checked = !checked;
try {
actionMethod.invoke(actionObject);
} catch (Exception e1) {
throw new RuntimeException("Could not invoke method " + actionMethod + " on object " + actionObject, e1);
}
repaint();
}
}
public void mouseEntered(MouseEvent e) {
if (!isEnabled()) return;
if (pressed) {
armed = true;
repaint();
}
}
public void mouseExited(MouseEvent e) {
if (!isEnabled()) return;
armed = false;
repaint();
}
private class PressedAction extends AbstractAction {
private PressedAction() {
super("Pressed");
}
public void actionPerformed(ActionEvent e) {
onPressed();
}
}
private class ReleasedAction extends AbstractAction {
private ReleasedAction() {
super("Released");
}
public void actionPerformed(ActionEvent e) {
onReleased();
}
}
}