Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / featuretable / table / editors / FormattedCellEditor.java @ 42775

History | View | Annotate | Download (5.64 KB)

1
package org.gvsig.fmap.dal.swing.impl.featuretable.table.editors;
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
import java.util.TimeZone;
13

    
14
import javax.swing.DefaultCellEditor;
15
import javax.swing.JComponent;
16
import javax.swing.JFormattedTextField;
17
import javax.swing.JTable;
18
import javax.swing.border.LineBorder;
19
import javax.swing.text.DateFormatter;
20
import javax.swing.text.DefaultFormatterFactory;
21
import javax.swing.text.NumberFormatter;
22
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
23
import org.gvsig.fmap.dal.swing.FeatureTableModel;
24
import org.gvsig.fmap.dal.swing.impl.featuretable.table.DefaultFeatureTableModel;
25

    
26
import org.gvsig.tools.dataTypes.DataTypes;
27

    
28
public class FormattedCellEditor extends DefaultCellEditor {
29

    
30
    /**
31
     *
32
     */
33
    private static final long serialVersionUID = -1409667557139440155L;
34
    private final FeatureTableModel tableModel;
35
    private final SimpleDateFormat dateformat;
36
    private final NumberFormat numberFormat;
37
    private String pattern;
38
    private int type;
39

    
40
    public FormattedCellEditor(DefaultFeatureTableModel model) {
41
        super(new JFormattedTextField());
42
        this.tableModel = model;
43

    
44
        Locale localeOfData = this.tableModel.getLocaleOfData();
45
        
46
        this.dateformat = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance(
47
                DateFormat.MEDIUM,
48
                DateFormat.MEDIUM,
49
                localeOfData
50
        );
51
        this.dateformat.setTimeZone(TimeZone.getDefault());
52
        
53
        this.numberFormat = NumberFormat.getInstance(localeOfData);
54
        this.numberFormat.setMaximumFractionDigits(Integer.MAX_VALUE);
55
    }
56

    
57
    @Override
58
    public Component getTableCellEditorComponent(JTable table, Object value,
59
            boolean isSelected, int row, int column) {
60

    
61
        ((JComponent) getComponent()).setBorder(new LineBorder(Color.black));
62

    
63
        JFormattedTextField editor = (JFormattedTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);
64
        this.pattern = this.tableModel.getColumnFormattingPattern(column);
65

    
66
        if( value == null ) {
67
            int attrindex = this.tableModel.getOriginalColumnIndex(column);
68
            FeatureAttributeDescriptor attrdesc = this.tableModel.getFeatureType().getAttributeDescriptor(attrindex);
69
            value = attrdesc.getDefaultValue();
70
        }
71
        if ( value instanceof Number ) {
72
            if ( value instanceof Float ) {
73
                this.type = DataTypes.FLOAT;
74
            } else if ( value instanceof Integer ) {
75
                this.type = DataTypes.INT;
76
            } else if ( value instanceof Long ) {
77
                this.type = DataTypes.LONG;
78
            } else {
79
                this.type = DataTypes.DOUBLE;
80
            }
81

    
82
            editor.setFormatterFactory(new DefaultFormatterFactory(
83
                    new NumberFormatter(numberFormat)));
84

    
85
            editor.setValue(value);
86
        }
87
        if ( value instanceof Date ) {
88
            this.type = DataTypes.DATE;
89
            dateformat.applyPattern(this.pattern);
90
            editor.setFormatterFactory(new DefaultFormatterFactory(
91
                    new DateFormatter(dateformat)));
92

    
93
            editor.setValue(value);
94
        }
95
        return editor;
96
    }
97

    
98
    @Override
99
    public Object getCellEditorValue() {
100
        Object superValue = super.getCellEditorValue();
101
        String str = (String) superValue;
102
        if ( str == null ) {
103
            return null;
104
        }
105

    
106
        if ( str.length() == 0 ) {
107
            return null;
108
        }
109

    
110
        try {
111
            ParsePosition pos = new ParsePosition(0);
112

    
113
            Number n = null;
114
            switch (this.type) {
115
            case DataTypes.DOUBLE:
116
                n = numberFormat.parse(str, pos);
117
                if ( pos.getIndex() != str.length() ) {
118
                    throw new ParseException("parsing incomplete", pos.getIndex());
119
                }
120
                return new Double(n.doubleValue());
121
                
122
            case DataTypes.FLOAT:
123
                n = numberFormat.parse(str, pos);
124
                if ( pos.getIndex() != str.length() ) {
125
                    throw new ParseException("parsing incomplete", pos.getIndex());
126
                }
127
                return new Float(n.floatValue());
128
                
129
            case DataTypes.INT:
130
                n = numberFormat.parse(str, pos);
131
                if ( pos.getIndex() != str.length() ) {
132
                    throw new ParseException("parsing incomplete", pos.getIndex());
133
                }
134
                return new Integer(n.intValue());
135
                
136
            case DataTypes.LONG:
137
                n = numberFormat.parse(str, pos);
138
                if ( pos.getIndex() != str.length() ) {
139
                    throw new ParseException("parsing incomplete", pos.getIndex());
140
                }
141
                return new Long(n.longValue());
142

    
143
            case DataTypes.DATE://                
144
                dateformat.applyPattern(this.pattern);
145
                Date d = dateformat.parse(str);
146
                return d;
147

    
148
            default:
149
                return superValue;
150
            }
151

    
152
        } catch (ParseException pex) {
153
            throw new RuntimeException(pex);
154
        }
155
    }
156

    
157
    public boolean stopCellEditing() {
158
        try {
159
            this.getCellEditorValue();
160
            return super.stopCellEditing();
161
        } catch (Exception ex) {
162
            ((JComponent) getComponent()).setBorder(new LineBorder(Color.red));
163
            return false;
164
        }
165

    
166
    }
167

    
168
}