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 @ 44836

History | View | Annotate | Download (7.19 KB)

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

    
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6
import javax.swing.table.AbstractTableModel;
7
import org.gvsig.fmap.dal.complements.Search;
8
import org.gvsig.fmap.dal.exception.DataException;
9
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
10
import org.gvsig.fmap.dal.feature.Feature;
11
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
12
import org.gvsig.fmap.dal.feature.FeatureStore;
13
import org.gvsig.fmap.dal.feature.FeatureType;
14
import org.gvsig.tools.ToolsLocator;
15
import org.gvsig.tools.logger.FilteredLogger;
16
import org.gvsig.tools.util.UnmodifiableBasicList;
17
import org.slf4j.Logger;
18
import org.slf4j.LoggerFactory;
19

    
20
/**
21
 *
22
 * @author jjdelcerro
23
 */
24
public class SimpleFeaturesTableModel 
25
        extends AbstractTableModel
26
        implements UnmodifiableBasicList<Feature> {
27

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

    
36
        public SimpleFeaturesTableModel(FeatureStore store) throws DataException {
37
            this(store.getDefaultFeatureType(), null, store.getFeatures());
38
        }
39
        
40
        public SimpleFeaturesTableModel(FeatureType featureType) {
41
            this(featureType, null, null);
42
            this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10);
43
        }
44
        
45
        public SimpleFeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) {
46
            this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10);
47
            this.features = features;
48
            this.featureType = featureType;
49
            this.errors = false;
50
            if (columnNames == null || columnNames.isEmpty()) {
51
                this.columnNames = new ArrayList<>();
52
                Search search = (Search) ToolsLocator.getComplementsManager().get(
53
                        Search.COMPLEMENT_MANE, featureType
54
                );
55
                List<Search.OrderedAttribute> attributos = search.getOrderedAttributes(
56
                        Search.BASIC_TYPES_FILTER,
57
                        Search.STR_INT_LONG_LABEL_ORDER,
58
                        12
59
                );
60
                for (Search.OrderedAttribute attrdesc : attributos) {
61
                    this.columnNames.add(attrdesc.getDescriptor().getName());
62
                }
63
            } else {
64
                this.columnNames = columnNames;
65
            }
66
        }
67

    
68
        public List<Feature> getFeatures() {
69
            return this.features;
70
        }
71

    
72
        @Override
73
        public int getRowCount() {
74
            if (this.features == null) {
75
                return 0;
76
            }
77
            try {
78
              return this.features.size();
79
            } catch(Throwable ex) {
80
              this.errors = true;
81
              LOGGER.warn("Can't calculate row count.",ex);
82
              return 0;
83
            }
84
        }
85

    
86
        public boolean hasErrors() {
87
          return this.errors;
88
        }
89
        
90
        @Override
91
        public int getColumnCount() {
92
            return this.columnNames.size();
93
        }
94

    
95
        @Override
96
        public String getColumnName(int columnIndex) {
97
            String attrName = this.columnNames.get(columnIndex);
98
            if (this.featureType == null) {
99
                return attrName;
100
            }
101
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
102
            if (attrdesc == null) {
103
                EditableFeatureAttributeDescriptor extraCol = this.featureType.getExtraColumns().get(attrName);
104
                if (extraCol!=null) {
105
                    return extraCol.getLocalizedShortLabel();
106
                }
107
                return "C" + columnIndex;
108
            }
109
            return attrdesc.getLocalizedShortLabel();
110
        }
111

    
112
        @Override
113
        public Class<?> getColumnClass(int columnIndex) {
114
            if (this.featureType == null) {
115
                return String.class;
116
            }
117
            String attrName = this.columnNames.get(columnIndex);
118
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
119
            if (attrdesc == null) {
120
                int extraIndex = featureType.getExtraColumns().getIndexOf(attrName);
121
                if (extraIndex != -1) {
122
                    attrdesc = featureType.getExtraColumns().get(extraIndex);
123
                }
124
            }
125
            if (attrdesc == null) {
126
                return String.class;
127
            }
128
            if( attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList() ) {
129
                return String.class;
130
            }
131
            Class theClass = attrdesc.getDataType().getDefaultClass();
132
            if( theClass==null ) {
133
                return String.class;
134
            }
135
            return theClass;
136
        }
137

    
138
        @Override
139
        public boolean isCellEditable(int rowIndex, int columnIndex) {
140
            return false;
141
        }
142

    
143
        @Override
144
        public Feature get(int position) {
145
            if (this.features == null) {
146
                return null;
147
            }
148
            Feature feature = this.features.get(position);
149
            return feature;
150
        }
151

    
152
        @Override
153
        public Object getValueAt(int rowIndex, int columnIndex) {
154
            if (this.features == null) {
155
                return null;
156
            }
157
            try {
158
                Feature feature = this.features.get(rowIndex);
159
                String attrName = this.columnNames.get(columnIndex);
160
                Object value = null;
161
                FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
162
                if (attrdesc ==null) {
163
                    int extraIndex = featureType.getExtraColumns().getIndexOf(attrName);
164
                    if (extraIndex != -1) {
165
                        attrdesc = featureType.getExtraColumns().get(extraIndex);
166
                        value = feature.getExtraValue(attrName);
167
                    }
168
                } else {
169
                    value = feature.get(attrName);
170
                }
171
                if (attrdesc != null) {
172
                    if( attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList() ) {
173
                        value = attrdesc.getForeingKey().getLabelForValue(value);
174
                    }
175
                }
176
                return value;
177
            } catch (Throwable th) {
178
                this.errors = true;
179
                logger.warn("Can't get cell value at "+rowIndex+", "+columnIndex+".", th);
180
                return null;
181
            }
182
        }
183

    
184
        @Override
185
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
186

    
187
        }
188

    
189
    @Override
190
    public List<Feature> toList() {
191
        return this.features;
192
    }
193

    
194
    @Override
195
    public boolean isEmpty() {
196
        return this.features.isEmpty();
197
    }
198

    
199
    @Override
200
    public int size() {
201
        return this.features.size();
202
    }
203

    
204
    @Override
205
    public Iterator<Feature> iterator() {
206
        return this.features.iterator();
207
    }
208
    
209
}