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 @ 44644
History | View | Annotate | Download (5.86 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 | import org.gvsig.fmap.dal.feature.Feature; |
||
10 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
11 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
12 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
13 | import org.gvsig.tools.ToolsLocator; |
||
14 | 44400 | jjdelcerro | import org.gvsig.tools.logger.FilteredLogger; |
15 | 44340 | jjdelcerro | import org.gvsig.tools.util.UnmodifiableBasicList; |
16 | 44400 | jjdelcerro | import org.slf4j.Logger; |
17 | import org.slf4j.LoggerFactory; |
||
18 | 44340 | jjdelcerro | |
19 | /**
|
||
20 | *
|
||
21 | * @author jjdelcerro
|
||
22 | */
|
||
23 | public class SimpleFeaturesTableModel |
||
24 | extends AbstractTableModel |
||
25 | implements UnmodifiableBasicList<Feature> {
|
||
26 | |||
27 | 44400 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(SimpleFeaturesTableModel.class); |
28 | |||
29 | 44340 | jjdelcerro | private final List<Feature> features; |
30 | private final List<String> columnNames; |
||
31 | private final FeatureType featureType; |
||
32 | 44400 | jjdelcerro | private FilteredLogger logger;
|
33 | 44340 | jjdelcerro | |
34 | public SimpleFeaturesTableModel(FeatureStore store) throws DataException { |
||
35 | this(store.getDefaultFeatureType(), null, store.getFeatures()); |
||
36 | } |
||
37 | |||
38 | public SimpleFeaturesTableModel(FeatureType featureType) {
|
||
39 | this(featureType, null, null); |
||
40 | 44400 | jjdelcerro | this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10); |
41 | 44340 | jjdelcerro | } |
42 | |||
43 | public SimpleFeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) { |
||
44 | 44400 | jjdelcerro | this.logger = new FilteredLogger(LOGGER, "SimpleFeaturesTableModel", 10); |
45 | 44340 | jjdelcerro | this.features = features;
|
46 | this.featureType = featureType;
|
||
47 | if (columnNames == null || columnNames.isEmpty()) { |
||
48 | this.columnNames = new ArrayList<>(); |
||
49 | Search search = (Search) ToolsLocator.getComplementsManager().get( |
||
50 | Search.COMPLEMENT_MANE, featureType |
||
51 | ); |
||
52 | List<Search.OrderedAttribute> attributos = search.getOrderedAttributes(
|
||
53 | Search.BASIC_TYPES_FILTER, |
||
54 | Search.STR_INT_LONG_LABEL_ORDER, |
||
55 | 12
|
||
56 | ); |
||
57 | for (Search.OrderedAttribute attrdesc : attributos) {
|
||
58 | this.columnNames.add(attrdesc.getDescriptor().getName());
|
||
59 | } |
||
60 | } else {
|
||
61 | this.columnNames = columnNames;
|
||
62 | } |
||
63 | } |
||
64 | |||
65 | public List<Feature> getFeatures() { |
||
66 | return this.features; |
||
67 | } |
||
68 | |||
69 | @Override
|
||
70 | public int getRowCount() { |
||
71 | if (this.features == null) { |
||
72 | return 0; |
||
73 | } |
||
74 | return this.features.size(); |
||
75 | } |
||
76 | |||
77 | @Override
|
||
78 | public int getColumnCount() { |
||
79 | return this.columnNames.size(); |
||
80 | } |
||
81 | |||
82 | @Override
|
||
83 | public String getColumnName(int columnIndex) { |
||
84 | String attrName = this.columnNames.get(columnIndex); |
||
85 | if (this.featureType == null) { |
||
86 | return attrName;
|
||
87 | } |
||
88 | FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
||
89 | if (attrdesc == null) { |
||
90 | return "C" + columnIndex; |
||
91 | } |
||
92 | return attrdesc.getLocalizedShortLabel();
|
||
93 | } |
||
94 | |||
95 | @Override
|
||
96 | public Class<?> getColumnClass(int columnIndex) { |
||
97 | if (this.featureType == null) { |
||
98 | return String.class; |
||
99 | } |
||
100 | String attrName = this.columnNames.get(columnIndex); |
||
101 | FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
||
102 | if (attrdesc == null) { |
||
103 | return String.class; |
||
104 | } |
||
105 | if( attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList() ) {
|
||
106 | return String.class; |
||
107 | } |
||
108 | Class theClass = attrdesc.getDataType().getDefaultClass();
|
||
109 | if( theClass==null ) { |
||
110 | return String.class; |
||
111 | } |
||
112 | return theClass;
|
||
113 | } |
||
114 | |||
115 | @Override
|
||
116 | public boolean isCellEditable(int rowIndex, int columnIndex) { |
||
117 | return false; |
||
118 | } |
||
119 | |||
120 | @Override
|
||
121 | public Feature get(int position) { |
||
122 | if (this.features == null) { |
||
123 | return null; |
||
124 | } |
||
125 | Feature feature = this.features.get(position);
|
||
126 | return feature;
|
||
127 | } |
||
128 | |||
129 | @Override
|
||
130 | public Object getValueAt(int rowIndex, int columnIndex) { |
||
131 | if (this.features == null) { |
||
132 | return null; |
||
133 | } |
||
134 | try {
|
||
135 | Feature feature = this.features.get(rowIndex);
|
||
136 | String attrName = this.columnNames.get(columnIndex); |
||
137 | Object value = feature.get(attrName);
|
||
138 | FeatureAttributeDescriptor attrdesc = this.featureType.getAttributeDescriptor(attrName);
|
||
139 | if (attrdesc != null) { |
||
140 | if( attrdesc.isForeingKey() && attrdesc.getForeingKey().isClosedList() ) {
|
||
141 | value = attrdesc.getForeingKey().getLabelForValue(value); |
||
142 | } |
||
143 | } |
||
144 | return value;
|
||
145 | } catch (Throwable th) { |
||
146 | 44400 | jjdelcerro | logger.warn("Can't get cell value at "+rowIndex+", "+columnIndex+".", th); |
147 | 44340 | jjdelcerro | return null; |
148 | } |
||
149 | } |
||
150 | |||
151 | @Override
|
||
152 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
||
153 | |||
154 | } |
||
155 | |||
156 | @Override
|
||
157 | public List<Feature> toList() { |
||
158 | return this.features; |
||
159 | } |
||
160 | |||
161 | @Override
|
||
162 | public boolean isEmpty() { |
||
163 | return this.features.isEmpty(); |
||
164 | } |
||
165 | |||
166 | @Override
|
||
167 | public int size() { |
||
168 | return this.features.size(); |
||
169 | } |
||
170 | |||
171 | @Override
|
||
172 | public Iterator<Feature> iterator() { |
||
173 | return this.features.iterator(); |
||
174 | } |
||
175 | |||
176 | } |