Revision 41707 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/feature/swing/table/FormattedCellEditor.java
FormattedCellEditor.java | ||
---|---|---|
9 | 9 |
import java.text.SimpleDateFormat; |
10 | 10 |
import java.util.Date; |
11 | 11 |
import java.util.Locale; |
12 |
import java.util.TimeZone; |
|
12 | 13 |
|
13 | 14 |
import javax.swing.DefaultCellEditor; |
14 | 15 |
import javax.swing.JComponent; |
... | ... | |
16 | 17 |
import javax.swing.JTable; |
17 | 18 |
import javax.swing.border.LineBorder; |
18 | 19 |
import javax.swing.text.DateFormatter; |
20 |
import javax.swing.text.DefaultFormatterFactory; |
|
19 | 21 |
import javax.swing.text.NumberFormatter; |
22 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
20 | 23 |
|
21 |
import org.apache.log4j.lf5.util.DateFormatManager; |
|
22 | 24 |
import org.gvsig.tools.dataTypes.DataTypes; |
23 | 25 |
|
24 | 26 |
public class FormattedCellEditor extends DefaultCellEditor { |
25 | 27 |
|
26 |
/** |
|
27 |
* |
|
28 |
*/ |
|
29 |
private static final long serialVersionUID = -1409667557139440155L; |
|
30 |
private ConfigurableFeatureTableModel tableModel; |
|
31 |
private String pattern; |
|
32 |
private int type; |
|
28 |
/** |
|
29 |
* |
|
30 |
*/ |
|
31 |
private static final long serialVersionUID = -1409667557139440155L; |
|
32 |
private final ConfigurableFeatureTableModel tableModel; |
|
33 |
private final SimpleDateFormat dateformat; |
|
34 |
private final NumberFormat numberFormat; |
|
35 |
private String pattern; |
|
36 |
private int type; |
|
33 | 37 |
|
34 |
public FormattedCellEditor(ConfigurableFeatureTableModel model) { |
|
35 |
super(new JFormattedTextField()); |
|
36 |
this.tableModel = model; |
|
37 |
} |
|
38 |
public FormattedCellEditor(ConfigurableFeatureTableModel model) { |
|
39 |
super(new JFormattedTextField()); |
|
40 |
this.tableModel = model; |
|
38 | 41 |
|
39 |
public Component getTableCellEditorComponent(JTable table, Object value, |
|
40 |
boolean isSelected, int row, int column) { |
|
42 |
Locale localeOfData = this.tableModel.getLocaleOfData(); |
|
43 |
|
|
44 |
this.dateformat = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance( |
|
45 |
DateFormat.MEDIUM, |
|
46 |
DateFormat.MEDIUM, |
|
47 |
localeOfData |
|
48 |
); |
|
49 |
this.dateformat.setTimeZone(TimeZone.getDefault()); |
|
50 |
|
|
51 |
this.numberFormat = NumberFormat.getInstance(localeOfData); |
|
52 |
this.numberFormat.setMaximumFractionDigits(Integer.MAX_VALUE); |
|
53 |
} |
|
41 | 54 |
|
42 |
((JComponent)getComponent()).setBorder(new LineBorder(Color.black)); |
|
55 |
@Override |
|
56 |
public Component getTableCellEditorComponent(JTable table, Object value, |
|
57 |
boolean isSelected, int row, int column) { |
|
43 | 58 |
|
44 |
JFormattedTextField editor = (JFormattedTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column); |
|
45 |
Locale localeOfData = this.tableModel.getLocaleOfData(); |
|
46 |
this.pattern = this.tableModel.getFormattingPattern(column); |
|
59 |
((JComponent) getComponent()).setBorder(new LineBorder(Color.black)); |
|
47 | 60 |
|
48 |
if (value instanceof Number){ |
|
49 |
NumberFormat numberFormatB = NumberFormat.getInstance(localeOfData); |
|
50 |
numberFormatB.setMaximumFractionDigits(Integer.MAX_VALUE); |
|
51 |
if (value instanceof Float){ |
|
52 |
this.type = DataTypes.FLOAT; |
|
53 |
} else if (value instanceof Integer){ |
|
54 |
this.type = DataTypes.INT; |
|
55 |
}else if (value instanceof Long){ |
|
56 |
this.type = DataTypes.LONG; |
|
57 |
}else { |
|
58 |
this.type = DataTypes.DOUBLE; |
|
59 |
} |
|
61 |
JFormattedTextField editor = (JFormattedTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column); |
|
62 |
this.pattern = this.tableModel.getFormattingPattern(column); |
|
60 | 63 |
|
61 |
editor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory( |
|
62 |
new NumberFormatter(numberFormatB))); |
|
64 |
if( value == null ) { |
|
65 |
int attrindex = this.tableModel.getOriginalColumnIndex(column); |
|
66 |
FeatureAttributeDescriptor attrdesc = this.tableModel.getFeatureType().getAttributeDescriptor(attrindex); |
|
67 |
value = attrdesc.getDefaultValue(); |
|
68 |
} |
|
69 |
if ( value instanceof Number ) { |
|
70 |
if ( value instanceof Float ) { |
|
71 |
this.type = DataTypes.FLOAT; |
|
72 |
} else if ( value instanceof Integer ) { |
|
73 |
this.type = DataTypes.INT; |
|
74 |
} else if ( value instanceof Long ) { |
|
75 |
this.type = DataTypes.LONG; |
|
76 |
} else { |
|
77 |
this.type = DataTypes.DOUBLE; |
|
78 |
} |
|
63 | 79 |
|
64 |
editor.setValue(value); |
|
65 |
} |
|
66 |
if (value instanceof Date){ |
|
67 |
this.type = DataTypes.DATE; |
|
80 |
editor.setFormatterFactory(new DefaultFormatterFactory( |
|
81 |
new NumberFormatter(numberFormat))); |
|
68 | 82 |
|
69 |
SimpleDateFormat dateformat = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM, localeOfData); |
|
70 |
dateformat.applyPattern(this.pattern); |
|
71 |
editor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory( |
|
72 |
new DateFormatter(dateformat))); |
|
83 |
editor.setValue(value); |
|
84 |
} |
|
85 |
if ( value instanceof Date ) { |
|
86 |
this.type = DataTypes.DATE; |
|
87 |
dateformat.applyPattern(this.pattern); |
|
88 |
editor.setFormatterFactory(new DefaultFormatterFactory( |
|
89 |
new DateFormatter(dateformat))); |
|
73 | 90 |
|
74 |
editor.setValue(value);
|
|
75 |
}
|
|
76 |
return editor;
|
|
77 |
}
|
|
91 |
editor.setValue(value);
|
|
92 |
}
|
|
93 |
return editor;
|
|
94 |
}
|
|
78 | 95 |
|
79 |
public Object getCellEditorValue() { |
|
80 |
Object superValue = super.getCellEditorValue(); |
|
81 |
String str = (String) superValue; |
|
82 |
if (str == null) { |
|
83 |
return null; |
|
84 |
} |
|
96 |
@Override |
|
97 |
public Object getCellEditorValue() { |
|
98 |
Object superValue = super.getCellEditorValue(); |
|
99 |
String str = (String) superValue; |
|
100 |
if ( str == null ) { |
|
101 |
return null; |
|
102 |
} |
|
85 | 103 |
|
86 |
if (str.length() == 0) {
|
|
87 |
return null;
|
|
88 |
}
|
|
104 |
if ( str.length() == 0 ) {
|
|
105 |
return null;
|
|
106 |
}
|
|
89 | 107 |
|
90 |
Locale localeOfData = this.tableModel.getLocaleOfData(); |
|
108 |
try { |
|
109 |
ParsePosition pos = new ParsePosition(0); |
|
91 | 110 |
|
92 |
try { |
|
93 |
ParsePosition pos = new ParsePosition(0); |
|
111 |
Number n = null; |
|
112 |
switch (this.type) { |
|
113 |
case DataTypes.DOUBLE: |
|
114 |
n = numberFormat.parse(str, pos); |
|
115 |
if ( pos.getIndex() != str.length() ) { |
|
116 |
throw new ParseException("parsing incomplete", pos.getIndex()); |
|
117 |
} |
|
118 |
return new Double(n.doubleValue()); |
|
119 |
|
|
120 |
case DataTypes.FLOAT: |
|
121 |
n = numberFormat.parse(str, pos); |
|
122 |
if ( pos.getIndex() != str.length() ) { |
|
123 |
throw new ParseException("parsing incomplete", pos.getIndex()); |
|
124 |
} |
|
125 |
return new Float(n.floatValue()); |
|
126 |
|
|
127 |
case DataTypes.INT: |
|
128 |
n = numberFormat.parse(str, pos); |
|
129 |
if ( pos.getIndex() != str.length() ) { |
|
130 |
throw new ParseException("parsing incomplete", pos.getIndex()); |
|
131 |
} |
|
132 |
return new Integer(n.intValue()); |
|
133 |
|
|
134 |
case DataTypes.LONG: |
|
135 |
n = numberFormat.parse(str, pos); |
|
136 |
if ( pos.getIndex() != str.length() ) { |
|
137 |
throw new ParseException("parsing incomplete", pos.getIndex()); |
|
138 |
} |
|
139 |
return new Long(n.longValue()); |
|
94 | 140 |
|
95 |
Number n = null; |
|
96 |
switch (this.type) { |
|
97 |
case DataTypes.DOUBLE: |
|
98 |
n = NumberFormat.getInstance(localeOfData).parse(str, pos); |
|
99 |
if (pos.getIndex() != str.length()) { |
|
100 |
throw new ParseException( |
|
101 |
"parsing incomplete", pos.getIndex()); |
|
102 |
} |
|
141 |
case DataTypes.DATE:// |
|
142 |
dateformat.applyPattern(this.pattern); |
|
143 |
Date d = dateformat.parse(str); |
|
144 |
return d; |
|
103 | 145 |
|
104 |
return new Double(n.doubleValue()); |
|
105 |
case DataTypes.FLOAT: |
|
106 |
n = NumberFormat.getInstance(localeOfData).parse(str, pos); |
|
107 |
if (pos.getIndex() != str.length()) { |
|
108 |
throw new ParseException( |
|
109 |
"parsing incomplete", pos.getIndex()); |
|
110 |
} |
|
146 |
default: |
|
147 |
return superValue; |
|
148 |
} |
|
111 | 149 |
|
112 |
return new Float(n.floatValue()); |
|
113 |
case DataTypes.INT: |
|
114 |
n = NumberFormat.getInstance(localeOfData).parse(str, pos); |
|
115 |
if (pos.getIndex() != str.length()) { |
|
116 |
throw new ParseException( |
|
117 |
"parsing incomplete", pos.getIndex()); |
|
118 |
} |
|
150 |
} catch (ParseException pex) { |
|
151 |
throw new RuntimeException(pex); |
|
152 |
} |
|
153 |
} |
|
119 | 154 |
|
120 |
return new Integer(n.intValue()); |
|
121 |
case DataTypes.LONG: |
|
122 |
n = NumberFormat.getInstance(localeOfData).parse(str, pos); |
|
123 |
if (pos.getIndex() != str.length()) { |
|
124 |
throw new ParseException( |
|
125 |
"parsing incomplete", pos.getIndex()); |
|
126 |
} |
|
155 |
public boolean stopCellEditing() { |
|
156 |
try { |
|
157 |
this.getCellEditorValue(); |
|
158 |
return super.stopCellEditing(); |
|
159 |
} catch (Exception ex) { |
|
160 |
((JComponent) getComponent()).setBorder(new LineBorder(Color.red)); |
|
161 |
return false; |
|
162 |
} |
|
127 | 163 |
|
128 |
return new Long(n.longValue());
|
|
164 |
}
|
|
129 | 165 |
|
130 |
case DataTypes.DATE: |
|
131 |
DateFormatManager fm = new DateFormatManager(localeOfData, this.pattern); |
|
132 |
Date d = fm.parse(str); |
|
133 |
return d; |
|
134 |
|
|
135 |
default: |
|
136 |
return superValue; |
|
137 |
} |
|
138 |
|
|
139 |
} catch (ParseException pex) { |
|
140 |
throw new RuntimeException(pex); |
|
141 |
} |
|
142 |
} |
|
143 |
|
|
144 |
public boolean stopCellEditing() { |
|
145 |
try { |
|
146 |
this.getCellEditorValue(); |
|
147 |
return super.stopCellEditing(); |
|
148 |
} catch (Exception ex) { |
|
149 |
((JComponent)getComponent()).setBorder(new LineBorder(Color.red)); |
|
150 |
return false; |
|
151 |
} |
|
152 |
|
|
153 |
} |
|
154 |
|
|
155 | 166 |
} |
Also available in: Unified diff