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 | 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 | import org.gvsig.tools.ToolsLocator; |
||
15 | 44400 | jjdelcerro | import org.gvsig.tools.logger.FilteredLogger; |
16 | 44340 | jjdelcerro | import org.gvsig.tools.util.UnmodifiableBasicList; |
17 | 44400 | jjdelcerro | import org.slf4j.Logger; |
18 | import org.slf4j.LoggerFactory; |
||
19 | 44340 | jjdelcerro | |
20 | /**
|
||
21 | *
|
||
22 | * @author jjdelcerro
|
||
23 | */
|
||
24 | public class SimpleFeaturesTableModel |
||
25 | extends AbstractTableModel |
||
26 | implements UnmodifiableBasicList<Feature> {
|
||
27 | |||
28 | 44400 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(SimpleFeaturesTableModel.class); |
29 | |||
30 | 44340 | jjdelcerro | private final List<Feature> features; |
31 | private final List<String> columnNames; |
||
32 | private final FeatureType featureType; |
||
33 | 44400 | jjdelcerro | private FilteredLogger logger;
|
34 | 44836 | jjdelcerro | private boolean errors; |
35 | 44340 | jjdelcerro | |
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 | 44400 | jjdelcerro | this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10); |
43 | 44340 | jjdelcerro | } |
44 | |||
45 | public SimpleFeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) { |
||
46 | 44400 | jjdelcerro | this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10); |
47 | 44340 | jjdelcerro | this.features = features;
|
48 | this.featureType = featureType;
|
||
49 | 44836 | jjdelcerro | this.errors = false; |
50 | 44340 | jjdelcerro | 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 | 44836 | jjdelcerro | 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 | 44340 | jjdelcerro | } |
85 | |||
86 | 44836 | jjdelcerro | public boolean hasErrors() { |
87 | return this.errors; |
||
88 | } |
||
89 | |||
90 | 44340 | jjdelcerro | @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 | 44829 | omartinez | EditableFeatureAttributeDescriptor extraCol = this.featureType.getExtraColumns().get(attrName);
|
104 | 44794 | omartinez | if (extraCol!=null) { |
105 | return extraCol.getLocalizedShortLabel();
|
||
106 | } |
||
107 | 44340 | jjdelcerro | 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 | 44829 | omartinez | int extraIndex = featureType.getExtraColumns().getIndexOf(attrName);
|
121 | 44794 | omartinez | if (extraIndex != -1) { |
122 | 44829 | omartinez | attrdesc = featureType.getExtraColumns().get(extraIndex); |
123 | 44794 | omartinez | } |
124 | } |
||
125 | if (attrdesc == null) { |
||
126 | 44340 | jjdelcerro | 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 | 44794 | omartinez | Object value = null; |
161 | 44340 | jjdelcerro | FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
162 | 44794 | omartinez | if (attrdesc ==null) { |
163 | 44829 | omartinez | int extraIndex = featureType.getExtraColumns().getIndexOf(attrName);
|
164 | 44794 | omartinez | if (extraIndex != -1) { |
165 | 44829 | omartinez | attrdesc = featureType.getExtraColumns().get(extraIndex); |
166 | 44794 | omartinez | value = feature.getExtraValue(attrName); |
167 | } |
||
168 | } else {
|
||
169 | value = feature.get(attrName); |
||
170 | } |
||
171 | 44340 | jjdelcerro | 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 | 44836 | jjdelcerro | this.errors = true; |
179 | 44400 | jjdelcerro | logger.warn("Can't get cell value at "+rowIndex+", "+columnIndex+".", th); |
180 | 44340 | jjdelcerro | 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 | } |