Statistics
| Revision:

svn-gvsig-desktop / 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 @ 41704

History | View | Annotate | Download (4.42 KB)

1
package org.gvsig.fmap.mapcontrol.dal.feature.swing.table;
2

    
3
import java.awt.Color;
4
import java.awt.Component;
5
import java.text.DateFormat;
6
import java.text.NumberFormat;
7
import java.text.ParseException;
8
import java.text.ParsePosition;
9
import java.text.SimpleDateFormat;
10
import java.util.Date;
11
import java.util.Locale;
12

    
13
import javax.swing.DefaultCellEditor;
14
import javax.swing.JComponent;
15
import javax.swing.JFormattedTextField;
16
import javax.swing.JTable;
17
import javax.swing.border.LineBorder;
18
import javax.swing.text.DateFormatter;
19
import javax.swing.text.NumberFormatter;
20

    
21
import org.apache.log4j.lf5.util.DateFormatManager;
22
import org.gvsig.tools.dataTypes.DataTypes;
23

    
24
public class FormattedCellEditor extends DefaultCellEditor {
25

    
26
        /**
27
         *
28
         */
29
        private static final long serialVersionUID = -1409667557139440155L;
30
        private ConfigurableFeatureTableModel tableModel;
31
        private String pattern;
32
        private int type;
33

    
34
        public FormattedCellEditor(ConfigurableFeatureTableModel model) {
35
                super(new JFormattedTextField());
36
                this.tableModel = model;
37
        }
38

    
39
        public Component getTableCellEditorComponent(JTable table, Object value,
40
                        boolean isSelected, int row, int column) {
41

    
42
        ((JComponent)getComponent()).setBorder(new LineBorder(Color.black));
43

    
44
            JFormattedTextField editor = (JFormattedTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);
45
            Locale localeOfData = this.tableModel.getLocaleOfData();
46
            this.pattern = this.tableModel.getFormattingPattern(column);
47

    
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
                    }
60

    
61
                editor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
62
                                new NumberFormatter(numberFormatB)));
63

    
64
                editor.setValue(value);
65
            }
66
            if (value instanceof Date){
67
                    this.type = DataTypes.DATE;
68

    
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)));
73

    
74
                editor.setValue(value);
75
            }
76
            return editor;
77
        }
78

    
79
        public Object getCellEditorValue() {
80
                Object superValue = super.getCellEditorValue();
81
            String str = (String) superValue;
82
            if (str == null) {
83
                return null;
84
            }
85

    
86
            if (str.length() == 0) {
87
                return null;
88
            }
89

    
90
            Locale localeOfData = this.tableModel.getLocaleOfData();
91

    
92
            try {
93
                ParsePosition pos = new ParsePosition(0);
94

    
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
                                }
103

    
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
                                }
111

    
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
                                }
119

    
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
                                }
127

    
128
                                return new Long(n.longValue());
129

    
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
}