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

History | View | Annotate | Download (7.43 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
            try {
124
            String attrName = this.columnNames.get(columnIndex);
125
            FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
126
            if (attrdesc == null) {
127
                int extraIndex = featureType.getExtraColumns().getIndexOf(attrName);
128
                if (extraIndex != -1) {
129
                    attrdesc = featureType.getExtraColumns().get(extraIndex);
130
                }
131
            }
132
            if (attrdesc == null) {
133
                return String.class;
134
            }
135
            if( attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList() ) {
136
                return String.class;
137
            }
138
            Class theClass = attrdesc.getDataType().getDefaultClass();
139
            if( theClass==null ) {
140
                return String.class;
141
            }
142
            return theClass;
143
            } catch (Exception ex) {
144
                return String.class;
145
            }
146
        }
147

    
148
        @Override
149
        public boolean isCellEditable(int rowIndex, int columnIndex) {
150
            return false;
151
        }
152

    
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;
160
        }
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
                    }
177
                }
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
            }
184
        }
185

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

    
189
        }
190

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

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

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

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

    
211
    @Override
212
    public void dispose() {
213
        if( this.features!=null ) {
214
            DisposeUtils.disposeQuietly(((FacadeOfAFeaturePagingHelper) features).getFeaturePagingHelper());
215
            this.features = null;
216
        }
217
        this.features = null;
218
    }
219
    
220
}