-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathDraggableNumber.java
More file actions
401 lines (338 loc) · 12.1 KB
/
DraggableNumber.java
File metadata and controls
401 lines (338 loc) · 12.1 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package nodebox.client;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
/**
* DraggableNumber represents a number that can be edited in a variety of interesting ways:
* by dragging, selecting the arrow buttons, or double-clicking to do direct input.
*/
public class DraggableNumber extends JComponent implements MouseListener, MouseMotionListener, ComponentListener, FocusListener {
private static Image draggerLeft, draggerRight, draggerBackground;
private static int draggerLeftWidth, draggerRightWidth, draggerHeight;
private static Cursor dragCursor;
static {
Image dragCursorImage;
try {
draggerLeft = ImageIO.read(new File("res/dragger-left.png"));
draggerRight = ImageIO.read(new File("res/dragger-right.png"));
draggerBackground = ImageIO.read(new File("res/dragger-background.png"));
draggerLeftWidth = draggerLeft.getWidth(null);
draggerRightWidth = draggerRight.getWidth(null);
draggerHeight = draggerBackground.getHeight(null);
dragCursorImage = ImageIO.read(new File("res/dragger-cursor.png"));
Toolkit toolkit = Toolkit.getDefaultToolkit();
dragCursor = toolkit.createCustomCursor(dragCursorImage, new Point(16, 17), "DragCursor");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// todo: could use something like BoundedRangeModel (but then for floats) for checking bounds.
private JTextField numberField;
private double oldValue, value;
private int previousX;
private Double minimumValue;
private Double maximumValue;
/**
* Only one <code>ChangeEvent</code> is needed per slider instance since the
* event's only (read-only) state is the source property. The source
* of events generated here is always "this". The event is lazily
* created the first time that an event notification is fired.
*
* @see #fireStateChanged
*/
protected transient ChangeEvent changeEvent = null;
private NumberFormat numberFormat;
public DraggableNumber() {
setLayout(null);
setCursor(dragCursor);
addMouseListener(this);
addMouseMotionListener(this);
addComponentListener(this);
setFocusable(true);
addFocusListener(this);
Dimension d = new Dimension(87, 20);
setPreferredSize(d);
numberField = new JTextField();
numberField.putClientProperty("JComponent.sizeVariant", "small");
numberField.setFont(Theme.SMALL_BOLD_FONT);
numberField.setHorizontalAlignment(JTextField.CENTER);
numberField.setVisible(false);
numberField.addKeyListener(new EscapeListener());
numberField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
breakFocusCycle();
commitNumberField();
}
});
numberField.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent e) {
if (numberField.isVisible())
commitNumberField();
setFocusable(true);
}
});
add(numberField);
numberFormat = NumberFormat.getNumberInstance();
numberFormat.setMinimumFractionDigits(2);
numberFormat.setMaximumFractionDigits(2);
setValue(0);
// Set the correct size for the numberField.
componentResized(null);
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
if (!enabled) cancelNumberField();
}
//// Value ranges ////
public Double getMinimumValue() {
return minimumValue;
}
public boolean hasMinimumValue() {
return minimumValue == null;
}
public void setMinimumValue(double minimumValue) {
this.minimumValue = minimumValue;
}
public void clearMinimumValue() {
this.minimumValue = null;
}
public Double getMaximumValue() {
return maximumValue;
}
public boolean hasMaximumValue() {
return maximumValue == null;
}
public void setMaximumValue(double maximumValue) {
this.maximumValue = maximumValue;
}
public void clearMaximumValue() {
this.maximumValue = null;
}
//// Value ////
public double getValue() {
return value;
}
public double clampValue(double value) {
if (minimumValue != null && value < minimumValue)
value = minimumValue;
if (maximumValue != null && value > maximumValue)
value = maximumValue;
return value;
}
public void setValue(double value) {
this.value = clampValue(value);
repaint();
}
public void setValueFromString(String s) throws NumberFormatException {
setValue(Double.parseDouble(s));
}
public String valueAsString() {
return numberFormat.format(value);
}
//// Number formatting ////
public NumberFormat getNumberFormat() {
return numberFormat;
}
public void setNumberFormat(NumberFormat numberFormat) {
this.numberFormat = numberFormat;
// Refresh the label
setValue(getValue());
}
private void showNumberField() {
numberField.setText(valueAsString());
numberField.setVisible(true);
numberField.requestFocus();
numberField.selectAll();
componentResized(null);
repaint();
}
private void commitNumberField() {
numberField.setVisible(false);
String s = numberField.getText();
try {
setValueFromString(s);
fireStateChanged();
} catch (NumberFormatException e) {
Toolkit.getDefaultToolkit().beep();
}
}
private void cancelNumberField() {
numberField.setVisible(false);
}
//// Component paint ////
private Rectangle getLeftButtonRect() {
return new Rectangle(0, 0, draggerLeftWidth, draggerHeight);
}
private Rectangle getRightButtonRect() {
Rectangle r = getBounds();
return new Rectangle(r.width - draggerRightWidth, r.y, draggerRightWidth, draggerHeight);
}
public void focusGained(FocusEvent e) {
showNumberField();
setFocusable(false);
}
public void focusLost(FocusEvent e) {
}
// We want to move focus to a sibling focusable control using TAB only, not by hitting
// Enter or Escape. In these cases we need to break out of the current focus cycle.
private void breakFocusCycle() {
Container o = getParent();
while (o != null) {
if (o != null && o.isFocusable())
break;
o = o.getParent();
}
if (o != null)
o.requestFocus();
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Rectangle r = getBounds();
int centerWidth = r.width - draggerLeftWidth - draggerRightWidth;
g2.drawImage(draggerLeft, 0, 0, null);
g2.drawImage(draggerRight, r.width - draggerRightWidth, 0, null);
g2.drawImage(draggerBackground, draggerLeftWidth, 0, centerWidth, draggerHeight, null);
g2.setFont(Theme.SMALL_BOLD_FONT);
if (isEnabled()) {
g2.setColor(Theme.TEXT_NORMAL_COLOR);
} else {
g2.setColor(Theme.TEXT_DISABLED_COLOR);
}
SwingUtils.drawCenteredShadowText(g2, valueAsString(), r.width / 2, 14, Theme.DRAGGABLE_NUMBER_HIGHLIGHT_COLOR);
}
//// Component size ////
@Override
public Dimension getPreferredSize() {
// The control is actually 20 pixels high, but setting the height to 30 will leave a nice margin.
return new Dimension(120, 30);
}
//// Component listeners
public void componentResized(ComponentEvent e) {
numberField.setBounds(draggerLeftWidth, 1, getWidth() - draggerLeftWidth - draggerRightWidth, draggerHeight - 2);
}
public void componentMoved(ComponentEvent e) {
}
public void componentShown(ComponentEvent e) {
}
public void componentHidden(ComponentEvent e) {
}
//// Mouse listeners ////
public void mousePressed(MouseEvent e) {
if (!isEnabled()) return;
if (e.getButton() == MouseEvent.BUTTON1) {
oldValue = getValue();
previousX = e.getX();
}
SwingUtilities.getRootPane(this).setCursor(dragCursor);
}
public void mouseClicked(MouseEvent e) {
if (!isEnabled()) return;
float dx = 1.0F;
if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) > 0) {
dx = 10F;
} else if ((e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) > 0) {
dx = 0.01F;
}
if (getLeftButtonRect().contains(e.getPoint())) {
setValue(getValue() - dx);
fireStateChanged();
} else if (getRightButtonRect().contains(e.getPoint())) {
setValue(getValue() + dx);
fireStateChanged();
} else if (e.getClickCount() >= 2) {
showNumberField();
}
}
public void mouseReleased(MouseEvent e) {
if (!isEnabled()) return;
SwingUtilities.getRootPane(this).setCursor(Cursor.getDefaultCursor());
if (oldValue != value)
fireStateChanged();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
if (!isEnabled()) return;
float deltaX = e.getX() - previousX;
if (deltaX == 0F) return;
if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) > 0) {
deltaX *= 10;
} else if ((e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) > 0) {
deltaX *= 0.01;
}
setValue(getValue() + deltaX);
previousX = e.getX();
fireStateChanged();
}
/**
* Adds a ChangeListener to the slider.
*
* @param l the ChangeListener to add
* @see #fireStateChanged
* @see #removeChangeListener
*/
public void addChangeListener(ChangeListener l) {
listenerList.add(ChangeListener.class, l);
}
/**
* Removes a ChangeListener from the slider.
*
* @param l the ChangeListener to remove
* @see #fireStateChanged
* @see #addChangeListener
*/
public void removeChangeListener(ChangeListener l) {
listenerList.remove(ChangeListener.class, l);
}
/**
* Send a ChangeEvent, whose source is this Slider, to
* each listener. This method method is called each time
* a ChangeEvent is received from the model.
*
* @see #addChangeListener
* @see javax.swing.event.EventListenerList
*/
protected void fireStateChanged() {
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ChangeListener.class) {
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((ChangeListener) listeners[i + 1]).stateChanged(changeEvent);
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new DraggableNumber());
frame.pack();
frame.setVisible(true);
}
/**
* When the escape key is pressed in the numberField, ignore the change and "close" the field.
*/
private class EscapeListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
breakFocusCycle();
numberField.setVisible(false);
}
}
}
}