Skip to content

Commit 17fbcab

Browse files
committed
SwingNumberWidget: reorganize JSpinner hacks
1 parent ff1fa86 commit 17fbcab

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

src/main/java/org/scijava/ui/swing/widget/SwingNumberWidget.java

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ else if (model.isStyle(NumberWidget.SLIDER_STYLE)) {
130130
final SpinnerNumberModel spinnerModel =
131131
new SpinnerNumberModelFactory().createModel(value, min, max, stepSize);
132132
spinner = new JSpinner(spinnerModel);
133-
134-
fixSpinnerFocus();
135-
136133
fixSpinner(type);
137134
setToolTip(spinner);
138135
getComponent().add(spinner);
@@ -195,57 +192,65 @@ private void limitWidth(final int maxWidth) {
195192
}
196193
}
197194

195+
/** Improves behavior of the {@link JSpinner} widget. */
196+
private void fixSpinner(final Class<?> type) {
197+
fixSpinnerType(type);
198+
fixSpinnerFocus();
199+
}
200+
198201
/**
199202
* Fixes spinners that display {@link BigDecimal} or {@link BigInteger}
200203
* values. This is a HACK to work around the fact that
201204
* {@link DecimalFormat#parse(String, ParsePosition)} uses {@link Double}
202205
* and/or {@link Long} by default, hence losing precision.
203206
*/
204-
private void fixSpinner(final Class<?> type) {
205-
if (BigDecimal.class.isAssignableFrom(type) ||
206-
BigInteger.class.isAssignableFrom(type))
207+
private void fixSpinnerType(final Class<?> type) {
208+
if (!BigDecimal.class.isAssignableFrom(type) &&
209+
!BigInteger.class.isAssignableFrom(type))
207210
{
208-
final JComponent editor = spinner.getEditor();
209-
final JSpinner.NumberEditor numberEditor = (JSpinner.NumberEditor) editor;
210-
final DecimalFormat decimalFormat = numberEditor.getFormat();
211-
decimalFormat.setParseBigDecimal(true);
211+
return;
212212
}
213+
final JComponent editor = spinner.getEditor();
214+
final JSpinner.NumberEditor numberEditor = (JSpinner.NumberEditor) editor;
215+
final DecimalFormat decimalFormat = numberEditor.getFormat();
216+
decimalFormat.setParseBigDecimal(true);
213217
}
214218

215219
/**
216-
* Adapted from <a href=
217-
* "http://stackoverflow.com/q/20971050/1027800"
218-
* >this SO post</a>.
219-
*
220-
* Tries to ensure that the text of a {@link JSpinner} is selected when it
221-
* has focus.
220+
* Tries to ensure that the text of a {@link JSpinner} becomes selected when
221+
* it first receives the focus.
222+
* <p>
223+
* Adapted from <a href="http://stackoverflow.com/q/20971050">this SO
224+
* post</a>.
222225
*/
223226
private void fixSpinnerFocus() {
224227
for (final Component c : spinner.getEditor().getComponents()) {
225-
if (JTextField.class.isAssignableFrom(c.getClass())) {
226-
c.addFocusListener(new FocusListener() {
227-
228-
@Override
229-
public void focusGained(final FocusEvent e) {
230-
queueSelection();
231-
}
232-
233-
@Override
234-
public void focusLost(final FocusEvent e) {
235-
queueSelection();
236-
}
237-
238-
private void queueSelection() {
239-
threadService.queue(new Runnable() {
240-
@Override
241-
public void run() {
242-
((JTextField) c).selectAll();
243-
}
244-
});
245-
}
246-
247-
});
248-
}
228+
if (!(c instanceof JTextField)) continue;
229+
final JTextField textField = (JTextField) c;
230+
231+
textField.addFocusListener(new FocusListener() {
232+
233+
@Override
234+
public void focusGained(final FocusEvent e) {
235+
queueSelection();
236+
}
237+
238+
@Override
239+
public void focusLost(final FocusEvent e) {
240+
queueSelection();
241+
}
242+
243+
private void queueSelection() {
244+
threadService.queue(new Runnable() {
245+
246+
@Override
247+
public void run() {
248+
textField.selectAll();
249+
}
250+
});
251+
}
252+
253+
});
249254
}
250255
}
251256

0 commit comments

Comments
 (0)