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 @ 44794
History | View | Annotate | Download (6.82 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 |
|
35 |
public SimpleFeaturesTableModel(FeatureStore store) throws DataException { |
36 |
this(store.getDefaultFeatureType(), null, store.getFeatures()); |
37 |
} |
38 |
|
39 |
public SimpleFeaturesTableModel(FeatureType featureType) {
|
40 |
this(featureType, null, null); |
41 |
this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10); |
42 |
} |
43 |
|
44 |
public SimpleFeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) { |
45 |
this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10); |
46 |
this.features = features;
|
47 |
this.featureType = featureType;
|
48 |
if (columnNames == null || columnNames.isEmpty()) { |
49 |
this.columnNames = new ArrayList<>(); |
50 |
Search search = (Search) ToolsLocator.getComplementsManager().get( |
51 |
Search.COMPLEMENT_MANE, featureType |
52 |
); |
53 |
List<Search.OrderedAttribute> attributos = search.getOrderedAttributes(
|
54 |
Search.BASIC_TYPES_FILTER, |
55 |
Search.STR_INT_LONG_LABEL_ORDER, |
56 |
12
|
57 |
); |
58 |
for (Search.OrderedAttribute attrdesc : attributos) {
|
59 |
this.columnNames.add(attrdesc.getDescriptor().getName());
|
60 |
} |
61 |
} else {
|
62 |
this.columnNames = columnNames;
|
63 |
} |
64 |
} |
65 |
|
66 |
public List<Feature> getFeatures() { |
67 |
return this.features; |
68 |
} |
69 |
|
70 |
@Override
|
71 |
public int getRowCount() { |
72 |
if (this.features == null) { |
73 |
return 0; |
74 |
} |
75 |
return this.features.size(); |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public int getColumnCount() { |
80 |
return this.columnNames.size(); |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public String getColumnName(int columnIndex) { |
85 |
String attrName = this.columnNames.get(columnIndex); |
86 |
if (this.featureType == null) { |
87 |
return attrName;
|
88 |
} |
89 |
FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
90 |
if (attrdesc == null) { |
91 |
EditableFeatureAttributeDescriptor extraCol = this.featureType.getExtraColumn().get(attrName);
|
92 |
if (extraCol!=null) { |
93 |
return extraCol.getLocalizedShortLabel();
|
94 |
} |
95 |
return "C" + columnIndex; |
96 |
} |
97 |
return attrdesc.getLocalizedShortLabel();
|
98 |
} |
99 |
|
100 |
@Override
|
101 |
public Class<?> getColumnClass(int columnIndex) { |
102 |
if (this.featureType == null) { |
103 |
return String.class; |
104 |
} |
105 |
String attrName = this.columnNames.get(columnIndex); |
106 |
FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
107 |
if (attrdesc == null) { |
108 |
int extraIndex = featureType.getExtraColumn().getIndexOf(attrName);
|
109 |
if (extraIndex != -1) { |
110 |
attrdesc = featureType.getExtraColumn().get(extraIndex); |
111 |
} |
112 |
} |
113 |
if (attrdesc == null) { |
114 |
return String.class; |
115 |
} |
116 |
if( attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList() ) {
|
117 |
return String.class; |
118 |
} |
119 |
Class theClass = attrdesc.getDataType().getDefaultClass();
|
120 |
if( theClass==null ) { |
121 |
return String.class; |
122 |
} |
123 |
return theClass;
|
124 |
} |
125 |
|
126 |
@Override
|
127 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
128 |
return false; |
129 |
} |
130 |
|
131 |
@Override
|
132 |
public Feature get(int position) { |
133 |
if (this.features == null) { |
134 |
return null; |
135 |
} |
136 |
Feature feature = this.features.get(position);
|
137 |
return feature;
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
public Object getValueAt(int rowIndex, int columnIndex) { |
142 |
if (this.features == null) { |
143 |
return null; |
144 |
} |
145 |
try {
|
146 |
Feature feature = this.features.get(rowIndex);
|
147 |
String attrName = this.columnNames.get(columnIndex); |
148 |
Object value = null; |
149 |
FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
150 |
if (attrdesc ==null) { |
151 |
int extraIndex = featureType.getExtraColumn().getIndexOf(attrName);
|
152 |
if (extraIndex != -1) { |
153 |
attrdesc = featureType.getExtraColumn().get(extraIndex); |
154 |
value = feature.getExtraValue(attrName); |
155 |
} |
156 |
} else {
|
157 |
value = feature.get(attrName); |
158 |
} |
159 |
if (attrdesc != null) { |
160 |
if( attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList() ) {
|
161 |
value = attrdesc.getForeingKey().getLabelForValue(value); |
162 |
} |
163 |
} |
164 |
return value;
|
165 |
} catch (Throwable th) { |
166 |
logger.warn("Can't get cell value at "+rowIndex+", "+columnIndex+".", th); |
167 |
return null; |
168 |
} |
169 |
} |
170 |
|
171 |
@Override
|
172 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
173 |
|
174 |
} |
175 |
|
176 |
@Override
|
177 |
public List<Feature> toList() { |
178 |
return this.features; |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public boolean isEmpty() { |
183 |
return this.features.isEmpty(); |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public int size() { |
188 |
return this.features.size(); |
189 |
} |
190 |
|
191 |
@Override
|
192 |
public Iterator<Feature> iterator() { |
193 |
return this.features.iterator(); |
194 |
} |
195 |
|
196 |
} |