Revision 45185 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/SimpleFeaturesTableModel.java

View differences:

SimpleFeaturesTableModel.java
1 1
package org.gvsig.fmap.dal.swing.impl.featuretable;
2 2

  
3
import java.math.BigDecimal;
3 4
import java.util.ArrayList;
5
import java.util.Date;
4 6
import java.util.Iterator;
5 7
import java.util.List;
8
import java.util.logging.Level;
9
import javax.swing.JTable;
6 10
import javax.swing.table.AbstractTableModel;
11
import javax.swing.table.DefaultTableCellRenderer;
12
import javax.swing.table.TableCellRenderer;
7 13
import org.gvsig.fmap.dal.complements.Search;
8 14
import org.gvsig.fmap.dal.exception.DataException;
9 15
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
......
13 19
import org.gvsig.fmap.dal.feature.FeatureType;
14 20
import org.gvsig.fmap.dal.feature.paging.FacadeOfAFeaturePagingHelper;
15 21
import org.gvsig.tools.ToolsLocator;
22
import org.gvsig.tools.dataTypes.Coercion;
23
import org.gvsig.tools.dataTypes.CoercionException;
24
import org.gvsig.tools.dataTypes.DataTypes;
16 25
import org.gvsig.tools.dispose.Disposable;
17 26
import org.gvsig.tools.dispose.DisposeUtils;
18 27
import org.gvsig.tools.logger.FilteredLogger;
......
24 33
 *
25 34
 * @author jjdelcerro
26 35
 */
27
public class SimpleFeaturesTableModel 
36
public class SimpleFeaturesTableModel
28 37
        extends AbstractTableModel
29 38
        implements UnmodifiableBasicList<Feature>, Disposable {
30 39

  
31
        private static final Logger LOGGER = LoggerFactory.getLogger(SimpleFeaturesTableModel.class);
32
    
33
        private List<Feature> features;
34
        private final List<String> columnNames;
35
        private final FeatureType featureType;
36
        private FilteredLogger logger;
37
        private boolean errors;
40
    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleFeaturesTableModel.class);
38 41

  
39
        public SimpleFeaturesTableModel(FeatureStore store) throws DataException {
40
            this(store.getDefaultFeatureType(), null, store.getFeatures());
42
    private static class FeatureAttributeCellRenderer extends DefaultTableCellRenderer {
43

  
44
        private final FeatureAttributeDescriptor descriptor;
45
        private final Coercion toStr;
46

  
47
        public FeatureAttributeCellRenderer(FeatureAttributeDescriptor descriptor) {
48
            this.descriptor = descriptor;
49
            this.toStr = ToolsLocator.getDataTypesManager().get(DataTypes.STRING).getCoercion();
41 50
        }
42 51
        
43
        public SimpleFeaturesTableModel(FeatureType featureType) {
44
            this(featureType, null, null);
45
            this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10);
46
        }
47
        
48
        public SimpleFeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) {
49
            this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10);
50
            this.features = features;
51
            this.featureType = featureType;
52
            this.errors = false;
53
            if (columnNames == null || columnNames.isEmpty()) {
54
                this.columnNames = new ArrayList<>();
55
                Search search = (Search) ToolsLocator.getComplementsManager().get(
56
                        Search.COMPLEMENT_MANE, featureType
57
                );
58
                List<Search.OrderedAttribute> attributos = search.getOrderedAttributes(
59
                        Search.BASIC_TYPES_FILTER,
60
                        Search.STR_INT_LONG_LABEL_ORDER,
61
                        12
62
                );
63
                for (Search.OrderedAttribute attrdesc : attributos) {
64
                    this.columnNames.add(attrdesc.getDescriptor().getName());
52
        @Override
53
        protected void setValue(Object value) {
54
            if (value == null) {
55
                setText("");
56
            } else {
57
                try {
58
                    value = toStr.coerce(value);
59
                } catch (CoercionException ex) {
60
                    LOGGER.debug("Can't coerce value to string.", ex);
65 61
                }
66
            } else {
67
                this.columnNames = columnNames;
62
                setText((String) value);
68 63
            }
69 64
        }
70 65

  
71
        public List<Feature> getFeatures() {
72
            return this.features;
66
    }
67

  
68
    private List<Feature> features;
69
    private final List<String> columnNames;
70
    private final FeatureType featureType;
71
    private FilteredLogger logger;
72
    private boolean errors;
73

  
74
    public static void setCellRenderers(JTable table) {
75
        if( !(table.getModel() instanceof SimpleFeaturesTableModel) ) {
76
            return;
73 77
        }
78
        SimpleFeaturesTableModel model = (SimpleFeaturesTableModel) table.getModel();
79
        for (String columnName : model.columnNames) {
80
            FeatureAttributeDescriptor descriptor = model.featureType.getAttributeDescriptor(columnName);
81
            switch(descriptor.getType()) {
82
                case DataTypes.BYTEARRAY:
83
                    table.setDefaultRenderer(byte[].class, new FeatureAttributeCellRenderer(descriptor));
84
                    break;
85
                case DataTypes.DATE:
86
                    table.setDefaultRenderer(java.sql.Date.class, new FeatureAttributeCellRenderer(descriptor));
87
                    break;
88
                case DataTypes.TIME:
89
                    table.setDefaultRenderer(java.sql.Time.class, new FeatureAttributeCellRenderer(descriptor));
90
                    break;
91
                case DataTypes.TIMESTAMP:
92
                    table.setDefaultRenderer(java.util.Date.class, new FeatureAttributeCellRenderer(descriptor));
93
                    table.setDefaultRenderer(java.sql.Timestamp.class, new FeatureAttributeCellRenderer(descriptor));
94
                    break;
95
                case DataTypes.FLOAT:
96
                    table.setDefaultRenderer(Float.class, new FeatureAttributeCellRenderer(descriptor));
97
                    break;
98
                case DataTypes.DECIMAL:
99
                    table.setDefaultRenderer(BigDecimal.class, new FeatureAttributeCellRenderer(descriptor));
100
                    break;
101
                case DataTypes.DOUBLE:
102
                    table.setDefaultRenderer(Double.class, new FeatureAttributeCellRenderer(descriptor));
103
                    break;
104
            }
105
        }
106
    }
74 107

  
75
        @Override
76
        public int getRowCount() {
77
            if (this.features == null) {
78
                return 0;
108
    public SimpleFeaturesTableModel(FeatureStore store) throws DataException {
109
        this(store.getDefaultFeatureType(), null, store.getFeatures());
110
    }
111

  
112
    public SimpleFeaturesTableModel(FeatureType featureType) {
113
        this(featureType, null, null);
114
        this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10);
115
    }
116

  
117
    public SimpleFeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) {
118
        this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10);
119
        this.features = features;
120
        this.featureType = featureType;
121
        this.errors = false;
122
        if (columnNames == null || columnNames.isEmpty()) {
123
            this.columnNames = new ArrayList<>();
124
            Search search = (Search) ToolsLocator.getComplementsManager().get(
125
                    Search.COMPLEMENT_MANE, featureType
126
            );
127
            List<Search.OrderedAttribute> attributos = search.getOrderedAttributes(
128
                    Search.BASIC_TYPES_FILTER,
129
                    Search.STR_INT_LONG_LABEL_ORDER,
130
                    12
131
            );
132
            for (Search.OrderedAttribute attrdesc : attributos) {
133
                this.columnNames.add(attrdesc.getDescriptor().getName());
79 134
            }
80
            try {
81
              return this.features.size();
82
            } catch(Throwable ex) {
83
              this.errors = true;
84
              LOGGER.warn("Can't calculate row count.",ex);
85
              return 0;
86
            }
135
        } else {
136
            this.columnNames = columnNames;
87 137
        }
138
    }
88 139

  
89
        public boolean hasErrors() {
90
          return this.errors;
140
    public List<Feature> getFeatures() {
141
        return this.features;
142
    }
143

  
144
    @Override
145
    public int getRowCount() {
146
        if (this.features == null) {
147
            return 0;
91 148
        }
92
        
93
        @Override
94
        public int getColumnCount() {
95
            return this.columnNames.size();
149
        try {
150
            return this.features.size();
151
        } catch (Throwable ex) {
152
            this.errors = true;
153
            LOGGER.warn("Can't calculate row count.", ex);
154
            return 0;
96 155
        }
156
    }
97 157

  
98
        @Override
99
        public String getColumnName(int columnIndex) {
100
            String attrName = this.columnNames.get(columnIndex);
101
            if (this.featureType == null) {
102
                return attrName;
158
    public boolean hasErrors() {
159
        return this.errors;
160
    }
161

  
162
    @Override
163
    public int getColumnCount() {
164
        return this.columnNames.size();
165
    }
166

  
167
    @Override
168
    public String getColumnName(int columnIndex) {
169
        String attrName = this.columnNames.get(columnIndex);
170
        if (this.featureType == null) {
171
            return attrName;
172
        }
173
        FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
174
        if (attrdesc == null) {
175
            EditableFeatureAttributeDescriptor extraCol = this.featureType.getExtraColumns().get(attrName);
176
            if (extraCol != null) {
177
                return extraCol.getLocalizedShortLabel();
103 178
            }
104
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
105
            if (attrdesc == null) {
106
                EditableFeatureAttributeDescriptor extraCol = this.featureType.getExtraColumns().get(attrName);
107
                if (extraCol!=null) {
108
                    return extraCol.getLocalizedShortLabel();
109
                }
110
                if (attrName==null) {
111
                    return "Column"+columnIndex;
112
                }
113
                return attrName;
179
            if (attrName == null) {
180
                return "Column" + columnIndex;
114 181
            }
115
            return attrdesc.getLocalizedShortLabel();
182
            return attrName;
116 183
        }
184
        return attrdesc.getLocalizedShortLabel();
185
    }
117 186

  
118
        @Override
119
        public Class<?> getColumnClass(int columnIndex) {
120
            if (this.featureType == null) {
121
                return String.class;
122
            }
123
            try {
187
    @Override
188
    public Class<?> getColumnClass(int columnIndex) {
189
        if (this.featureType == null) {
190
            return String.class;
191
        }
192
        try {
124 193
            String attrName = this.columnNames.get(columnIndex);
125 194
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
126 195
            if (attrdesc == null) {
......
132 201
            if (attrdesc == null) {
133 202
                return String.class;
134 203
            }
135
            if( attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList() ) {
204
            if (attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList()) {
136 205
                return String.class;
137 206
            }
138 207
            Class theClass = attrdesc.getDataType().getDefaultClass();
139
            if( theClass==null ) {
208
            if (theClass == null) {
140 209
                return String.class;
141 210
            }
142 211
            return theClass;
143
            } catch (Exception ex) {
144
                return String.class;
145
            }
212
        } catch (Exception ex) {
213
            return String.class;
146 214
        }
215
    }
147 216

  
148
        @Override
149
        public boolean isCellEditable(int rowIndex, int columnIndex) {
150
            return false;
217
    @Override
218
    public boolean isCellEditable(int rowIndex, int columnIndex) {
219
        return false;
220
    }
221

  
222
    @Override
223
    public Feature get(int position) {
224
        if (this.features == null) {
225
            return null;
151 226
        }
227
        Feature feature = this.features.get(position);
228
        return feature;
229
    }
152 230

  
153
        @Override
154
        public Feature get(int position) {
155
            if (this.features == null) {
156
                return null;
157
            }
158
            Feature feature = this.features.get(position);
159
            return feature;
231
    @Override
232
    public Object getValueAt(int rowIndex, int columnIndex) {
233
        if (this.features == null) {
234
            return null;
160 235
        }
161

  
162
        @Override
163
        public Object getValueAt(int rowIndex, int columnIndex) {
164
            if (this.features == null) {
165
                return null;
166
            }
167
            try {
168
                Feature feature = this.features.get(rowIndex);
169
                String attrName = this.columnNames.get(columnIndex);
170
                Object value = null;
171
                value = feature.get(attrName);
172
                FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
173
                if (attrdesc != null) {
174
                    if( attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList() ) {
175
                        value = attrdesc.getForeingKey().getLabelForValue(value);
176
                    }
236
        try {
237
            Feature feature = this.features.get(rowIndex);
238
            String attrName = this.columnNames.get(columnIndex);
239
            Object value = null;
240
            value = feature.get(attrName);
241
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
242
            if (attrdesc != null) {
243
                if (attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList()) {
244
                    value = attrdesc.getForeingKey().getLabelForValue(value);
177 245
                }
178
                return value;
179
            } catch (Throwable th) {
180
                this.errors = true;
181
                logger.warn("Can't get cell value at "+rowIndex+", "+columnIndex+".", th);
182
                return null;
183 246
            }
247
            return value;
248
        } catch (Throwable th) {
249
            this.errors = true;
250
            logger.warn("Can't get cell value at " + rowIndex + ", " + columnIndex + ".", th);
251
            return null;
184 252
        }
253
    }
185 254

  
186
        @Override
187
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
255
    @Override
256
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
188 257

  
189
        }
258
    }
190 259

  
191 260
    @Override
192 261
    public List<Feature> toList() {
......
210 279

  
211 280
    @Override
212 281
    public void dispose() {
213
        if( this.features!=null ) {
282
        if (this.features != null) {
214 283
            DisposeUtils.disposeQuietly(((FacadeOfAFeaturePagingHelper) features).getFeaturePagingHelper());
215 284
            this.features = null;
216 285
        }
217 286
        this.features = null;
218 287
    }
219
    
288

  
220 289
}

Also available in: Unified diff