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 | 44340 | jjdelcerro | 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 | 44794 | omartinez | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
10 | 44340 | jjdelcerro | 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 | 45100 | jjdelcerro | import org.gvsig.fmap.dal.feature.paging.FacadeOfAFeaturePagingHelper; |
15 | 44340 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
16 | 45100 | jjdelcerro | import org.gvsig.tools.dispose.Disposable; |
17 | import org.gvsig.tools.dispose.DisposeUtils; |
||
18 | 44400 | jjdelcerro | import org.gvsig.tools.logger.FilteredLogger; |
19 | 44340 | jjdelcerro | import org.gvsig.tools.util.UnmodifiableBasicList; |
20 | 44400 | jjdelcerro | import org.slf4j.Logger; |
21 | import org.slf4j.LoggerFactory; |
||
22 | 44340 | jjdelcerro | |
23 | /**
|
||
24 | *
|
||
25 | * @author jjdelcerro
|
||
26 | */
|
||
27 | public class SimpleFeaturesTableModel |
||
28 | extends AbstractTableModel |
||
29 | 45100 | jjdelcerro | implements UnmodifiableBasicList<Feature>, Disposable {
|
30 | 44340 | jjdelcerro | |
31 | 44400 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(SimpleFeaturesTableModel.class); |
32 | |||
33 | 45100 | jjdelcerro | private List<Feature> features; |
34 | 44340 | jjdelcerro | private final List<String> columnNames; |
35 | private final FeatureType featureType; |
||
36 | 44400 | jjdelcerro | private FilteredLogger logger;
|
37 | 44836 | jjdelcerro | private boolean errors; |
38 | 44340 | jjdelcerro | |
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 | 44400 | jjdelcerro | this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10); |
46 | 44340 | jjdelcerro | } |
47 | |||
48 | public SimpleFeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) { |
||
49 | 44400 | jjdelcerro | this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10); |
50 | 44340 | jjdelcerro | this.features = features;
|
51 | this.featureType = featureType;
|
||
52 | 44836 | jjdelcerro | this.errors = false; |
53 | 44340 | jjdelcerro | 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 | 44836 | jjdelcerro | 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 | 44340 | jjdelcerro | } |
88 | |||
89 | 44836 | jjdelcerro | public boolean hasErrors() { |
90 | return this.errors; |
||
91 | } |
||
92 | |||
93 | 44340 | jjdelcerro | @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 | 44829 | omartinez | EditableFeatureAttributeDescriptor extraCol = this.featureType.getExtraColumns().get(attrName);
|
107 | 44794 | omartinez | if (extraCol!=null) { |
108 | return extraCol.getLocalizedShortLabel();
|
||
109 | } |
||
110 | 45155 | omartinez | if (attrName==null) { |
111 | return "Column"+columnIndex; |
||
112 | } |
||
113 | return attrName;
|
||
114 | 44340 | jjdelcerro | } |
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 | 44829 | omartinez | int extraIndex = featureType.getExtraColumns().getIndexOf(attrName);
|
127 | 44794 | omartinez | if (extraIndex != -1) { |
128 | 44829 | omartinez | attrdesc = featureType.getExtraColumns().get(extraIndex); |
129 | 44794 | omartinez | } |
130 | } |
||
131 | if (attrdesc == null) { |
||
132 | 44340 | jjdelcerro | 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 | 44794 | omartinez | Object value = null; |
167 | 44340 | jjdelcerro | FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
168 | 44794 | omartinez | if (attrdesc ==null) { |
169 | 44829 | omartinez | int extraIndex = featureType.getExtraColumns().getIndexOf(attrName);
|
170 | 44794 | omartinez | if (extraIndex != -1) { |
171 | 44829 | omartinez | attrdesc = featureType.getExtraColumns().get(extraIndex); |
172 | 44794 | omartinez | } |
173 | 45155 | omartinez | value = feature.getExtraValue(attrName); |
174 | 44794 | omartinez | } else {
|
175 | value = feature.get(attrName); |
||
176 | } |
||
177 | 44340 | jjdelcerro | 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 | 44836 | jjdelcerro | this.errors = true; |
185 | 44400 | jjdelcerro | logger.warn("Can't get cell value at "+rowIndex+", "+columnIndex+".", th); |
186 | 44340 | jjdelcerro | 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 | 45100 | jjdelcerro | |
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 | 44340 | jjdelcerro | |
224 | } |