gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / pickercontroller / nullablespinner / NullableNumberEditor.java @ 2491
History | View | Annotate | Download (2.76 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.tools.swing.impl.pickercontroller.nullablespinner; |
7 |
|
8 |
import java.text.DecimalFormat; |
9 |
import java.text.NumberFormat; |
10 |
import java.text.ParseException; |
11 |
import java.util.Locale; |
12 |
import javax.swing.JFormattedTextField; |
13 |
import javax.swing.JSpinner; |
14 |
import javax.swing.JTextField; |
15 |
import javax.swing.text.DefaultFormatterFactory; |
16 |
import javax.swing.text.NumberFormatter; |
17 |
|
18 |
/**
|
19 |
*
|
20 |
* @author jjdelcerro
|
21 |
*/
|
22 |
public class NullableNumberEditor extends NullableEditor { |
23 |
|
24 |
private static String getDefaultPattern(Locale loc) { |
25 |
if (loc == null) { |
26 |
loc = Locale.getDefault();
|
27 |
} |
28 |
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(loc); |
29 |
// return nf.toLocalizedPattern();
|
30 |
return nf.toPattern();
|
31 |
} |
32 |
|
33 |
public NullableNumberEditor(JSpinner spinner) { |
34 |
this(spinner, getDefaultPattern(spinner.getLocale()));
|
35 |
} |
36 |
|
37 |
public NullableNumberEditor(JSpinner spinner, String decimalFormatPattern) { |
38 |
this(spinner, new DecimalFormat(decimalFormatPattern)); |
39 |
} |
40 |
|
41 |
private NullableNumberEditor(JSpinner spinner, DecimalFormat format) { |
42 |
super(spinner);
|
43 |
if (!(spinner.getModel() instanceof NullableSpinnerNumberModel)) { |
44 |
throw new IllegalArgumentException( |
45 |
"model not a NullableSpinnerNumberModel");
|
46 |
} |
47 |
|
48 |
NullableSpinnerNumberModel model = (NullableSpinnerNumberModel) spinner.getModel(); |
49 |
NumberFormatter formatter = new NullableNumberEditorFormatter(model,format, this.getTextField()); |
50 |
DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter); |
51 |
JFormattedTextField ftf = getTextField();
|
52 |
ftf.setEditable(true);
|
53 |
ftf.setFormatterFactory(factory); |
54 |
ftf.setHorizontalAlignment(JTextField.RIGHT);
|
55 |
|
56 |
/* TBD - initializing the column width of the text field
|
57 |
* is imprecise and doing it here is tricky because
|
58 |
* the developer may configure the formatter later.
|
59 |
*/
|
60 |
try {
|
61 |
String maxString = formatter.valueToString(model.getMinimum());
|
62 |
String minString = formatter.valueToString(model.getMaximum());
|
63 |
ftf.setColumns(Math.max(maxString.length(),
|
64 |
minString.length())); |
65 |
} catch (ParseException e) { |
66 |
// TBD should throw a chained error here
|
67 |
} |
68 |
|
69 |
} |
70 |
|
71 |
public DecimalFormat getFormat() { |
72 |
return (DecimalFormat) ((NumberFormatter) (getTextField().getFormatter())).getFormat(); |
73 |
} |
74 |
|
75 |
public NullableSpinnerNumberModel getModel() {
|
76 |
return (NullableSpinnerNumberModel) (getSpinner().getModel());
|
77 |
} |
78 |
|
79 |
} |