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 / SimpleFeaturesTableModel.java @ 45201

History | View | Annotate | Download (10.3 KB)

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

    
3
import java.math.BigDecimal;
4
import java.util.ArrayList;
5
import java.util.Date;
6
import java.util.Iterator;
7
import java.util.List;
8
import java.util.logging.Level;
9
import javax.swing.JTable;
10
import javax.swing.table.AbstractTableModel;
11
import javax.swing.table.DefaultTableCellRenderer;
12
import javax.swing.table.TableCellRenderer;
13
import org.gvsig.fmap.dal.complements.Search;
14
import org.gvsig.fmap.dal.exception.DataException;
15
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
16
import org.gvsig.fmap.dal.feature.Feature;
17
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
18
import org.gvsig.fmap.dal.feature.FeatureStore;
19
import org.gvsig.fmap.dal.feature.FeatureType;
20
import org.gvsig.fmap.dal.feature.paging.FacadeOfAFeaturePagingHelper;
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;
25
import org.gvsig.tools.dispose.Disposable;
26
import org.gvsig.tools.dispose.DisposeUtils;
27
import org.gvsig.tools.logger.FilteredLogger;
28
import org.gvsig.tools.util.UnmodifiableBasicList;
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

    
32
/**
33
 *
34
 * @author jjdelcerro
35
 */
36
public class SimpleFeaturesTableModel
37
        extends AbstractTableModel
38
        implements UnmodifiableBasicList<Feature>, Disposable {
39

    
40
    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleFeaturesTableModel.class);
41

    
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();
50
        }
51

    
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);
61
                }
62
                setText((String) value);
63
            }
64
        }
65

    
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;
77
        }
78
        SimpleFeaturesTableModel model = (SimpleFeaturesTableModel) table.getModel();
79
        for (String columnName : model.columnNames) {
80
            try {
81
                FeatureAttributeDescriptor descriptor = model.featureType.getAttributeDescriptor(columnName);
82
                if (descriptor == null) {
83
                    descriptor = model.featureType.getExtraColumns().get(columnName);
84
                }
85
                switch (descriptor.getType()) {
86
                    case DataTypes.BYTEARRAY:
87
                        table.setDefaultRenderer(byte[].class, new FeatureAttributeCellRenderer(descriptor));
88
                        break;
89
                    case DataTypes.DATE:
90
                        table.setDefaultRenderer(java.sql.Date.class, new FeatureAttributeCellRenderer(descriptor));
91
                        break;
92
                    case DataTypes.TIME:
93
                        table.setDefaultRenderer(java.sql.Time.class, new FeatureAttributeCellRenderer(descriptor));
94
                        break;
95
                    case DataTypes.TIMESTAMP:
96
                        table.setDefaultRenderer(java.util.Date.class, new FeatureAttributeCellRenderer(descriptor));
97
                        table.setDefaultRenderer(java.sql.Timestamp.class, new FeatureAttributeCellRenderer(descriptor));
98
                        break;
99
                    case DataTypes.FLOAT:
100
                        table.setDefaultRenderer(Float.class, new FeatureAttributeCellRenderer(descriptor));
101
                        break;
102
                    case DataTypes.DECIMAL:
103
                        table.setDefaultRenderer(BigDecimal.class, new FeatureAttributeCellRenderer(descriptor));
104
                        break;
105
                    case DataTypes.DOUBLE:
106
                        table.setDefaultRenderer(Double.class, new FeatureAttributeCellRenderer(descriptor));
107
                        break;
108
                }
109
            } catch (Exception ex) {
110
                throw new RuntimeException("Not able to get type of descriptor for column", ex);
111
            }
112
        }
113
    }
114

    
115
    public SimpleFeaturesTableModel(FeatureStore store) throws DataException {
116
        this(store.getDefaultFeatureType(), null, store.getFeatures());
117
    }
118

    
119
    public SimpleFeaturesTableModel(FeatureType featureType) {
120
        this(featureType, null, null);
121
        this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10);
122
    }
123

    
124
    public SimpleFeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) {
125
        this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10);
126
        this.features = features;
127
        this.featureType = featureType;
128
        this.errors = false;
129
        if (columnNames == null || columnNames.isEmpty()) {
130
            this.columnNames = new ArrayList<>();
131
            Search search = (Search) ToolsLocator.getComplementsManager().get(
132
                    Search.COMPLEMENT_MANE, featureType
133
            );
134
            List<Search.OrderedAttribute> attributos = search.getOrderedAttributes(
135
                    Search.BASIC_TYPES_FILTER,
136
                    Search.STR_INT_LONG_LABEL_ORDER,
137
                    12
138
            );
139
            for (Search.OrderedAttribute attrdesc : attributos) {
140
                this.columnNames.add(attrdesc.getDescriptor().getName());
141
            }
142
        } else {
143
            this.columnNames = columnNames;
144
        }
145
    }
146

    
147
    public List<Feature> getFeatures() {
148
        return this.features;
149
    }
150

    
151
    @Override
152
    public int getRowCount() {
153
        if (this.features == null) {
154
            return 0;
155
        }
156
        try {
157
            return this.features.size();
158
        } catch (Throwable ex) {
159
            this.errors = true;
160
            LOGGER.warn("Can't calculate row count.", ex);
161
            return 0;
162
        }
163
    }
164

    
165
    public boolean hasErrors() {
166
        return this.errors;
167
    }
168

    
169
    @Override
170
    public int getColumnCount() {
171
        return this.columnNames.size();
172
    }
173

    
174
    @Override
175
    public String getColumnName(int columnIndex) {
176
        String attrName = this.columnNames.get(columnIndex);
177
        if (this.featureType == null) {
178
            return attrName;
179
        }
180
        FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
181
        if (attrdesc == null) {
182
            EditableFeatureAttributeDescriptor extraCol = this.featureType.getExtraColumns().get(attrName);
183
            if (extraCol != null) {
184
                return extraCol.getLocalizedShortLabel();
185
            }
186
            if (attrName == null) {
187
                return "Column" + columnIndex;
188
            }
189
            return attrName;
190
        }
191
        return attrdesc.getLocalizedShortLabel();
192
    }
193

    
194
    @Override
195
    public Class<?> getColumnClass(int columnIndex) {
196
        if (this.featureType == null) {
197
            return String.class;
198
        }
199
        try {
200
            String attrName = this.columnNames.get(columnIndex);
201
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
202
            if (attrdesc == null) {
203
                int extraIndex = featureType.getExtraColumns().getIndexOf(attrName);
204
                if (extraIndex != -1) {
205
                    attrdesc = featureType.getExtraColumns().get(extraIndex);
206
                }
207
            }
208
            if (attrdesc == null) {
209
                return String.class;
210
            }
211
            if (attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList()) {
212
                return String.class;
213
            }
214
            Class theClass = attrdesc.getDataType().getDefaultClass();
215
            if (theClass == null) {
216
                return String.class;
217
            }
218
            return theClass;
219
        } catch (Exception ex) {
220
            return String.class;
221
        }
222
    }
223

    
224
    @Override
225
    public boolean isCellEditable(int rowIndex, int columnIndex) {
226
        return false;
227
    }
228

    
229
    @Override
230
    public Feature get(int position) {
231
        if (this.features == null) {
232
            return null;
233
        }
234
        Feature feature = this.features.get(position);
235
        return feature;
236
    }
237

    
238
    @Override
239
    public Object getValueAt(int rowIndex, int columnIndex) {
240
        if (this.features == null) {
241
            return null;
242
        }
243
        try {
244
            Feature feature = this.features.get(rowIndex);
245
            String attrName = this.columnNames.get(columnIndex);
246
            Object value = null;
247
            value = feature.get(attrName);
248
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
249
            if (attrdesc != null) {
250
                if (attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList()) {
251
                    value = attrdesc.getForeingKey().getLabelForValue(value);
252
                }
253
            }
254
            return value;
255
        } catch (Throwable th) {
256
            this.errors = true;
257
            logger.warn("Can't get cell value at " + rowIndex + ", " + columnIndex + ".", th);
258
            return null;
259
        }
260
    }
261

    
262
    @Override
263
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
264

    
265
    }
266

    
267
    @Override
268
    public List<Feature> toList() {
269
        return this.features;
270
    }
271

    
272
    @Override
273
    public boolean isEmpty() {
274
        return this.features.isEmpty();
275
    }
276

    
277
    @Override
278
    public int size() {
279
        return this.features.size();
280
    }
281

    
282
    @Override
283
    public Iterator<Feature> iterator() {
284
        return this.features.iterator();
285
    }
286

    
287
    @Override
288
    public void dispose() {
289
        if (this.features != null) {
290
            DisposeUtils.disposeQuietly(((FacadeOfAFeaturePagingHelper) features).getFeaturePagingHelper());
291
            this.features = null;
292
        }
293
        this.features = null;
294
    }
295

    
296
}