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

History | View | Annotate | Download (7.71 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.fmap.dal.feature.paging.FacadeOfAFeaturePagingHelper;
15
import org.gvsig.tools.ToolsLocator;
16
import org.gvsig.tools.dispose.Disposable;
17
import org.gvsig.tools.dispose.DisposeUtils;
18
import org.gvsig.tools.logger.FilteredLogger;
19
import org.gvsig.tools.util.UnmodifiableBasicList;
20
import org.slf4j.Logger;
21
import org.slf4j.LoggerFactory;
22

    
23
/**
24
 *
25
 * @author jjdelcerro
26
 */
27
public class SimpleFeaturesTableModel 
28
        extends AbstractTableModel
29
        implements UnmodifiableBasicList<Feature>, Disposable {
30

    
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;
38

    
39
        public SimpleFeaturesTableModel(FeatureStore store) throws DataException {
40
            this(store.getDefaultFeatureType(), null, store.getFeatures());
41
        }
42
        
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());
65
                }
66
            } else {
67
                this.columnNames = columnNames;
68
            }
69
        }
70

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

    
75
        @Override
76
        public int getRowCount() {
77
            if (this.features == null) {
78
                return 0;
79
            }
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
            }
87
        }
88

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

    
98
        @Override
99
        public String getColumnName(int columnIndex) {
100
            String attrName = this.columnNames.get(columnIndex);
101
            if (this.featureType == null) {
102
                return attrName;
103
            }
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;
114
            }
115
            return attrdesc.getLocalizedShortLabel();
116
        }
117

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

    
144
        @Override
145
        public boolean isCellEditable(int rowIndex, int columnIndex) {
146
            return false;
147
        }
148

    
149
        @Override
150
        public Feature get(int position) {
151
            if (this.features == null) {
152
                return null;
153
            }
154
            Feature feature = this.features.get(position);
155
            return feature;
156
        }
157

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

    
190
        @Override
191
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
192

    
193
        }
194

    
195
    @Override
196
    public List<Feature> toList() {
197
        return this.features;
198
    }
199

    
200
    @Override
201
    public boolean isEmpty() {
202
        return this.features.isEmpty();
203
    }
204

    
205
    @Override
206
    public int size() {
207
        return this.features.size();
208
    }
209

    
210
    @Override
211
    public Iterator<Feature> iterator() {
212
        return this.features.iterator();
213
    }
214

    
215
    @Override
216
    public void dispose() {
217
        if( this.features!=null ) {
218
            DisposeUtils.disposeQuietly(((FacadeOfAFeaturePagingHelper) features).getFeaturePagingHelper());
219
            this.features = null;
220
        }
221
        this.features = null;
222
    }
223
    
224
}