root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / dal / feature / swing / table / ConfigurableFeatureTableModel.java @ 33331
History | View | Annotate | Download (9.72 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2008 {DiSiD Technologies} {Create a JTable TableModel for a FeatureQuery}
|
26 |
*/
|
27 |
package org.gvsig.fmap.mapcontrol.dal.feature.swing.table; |
28 |
|
29 |
import java.security.InvalidParameterException; |
30 |
import java.util.ArrayList; |
31 |
import java.util.HashMap; |
32 |
import java.util.Iterator; |
33 |
import java.util.List; |
34 |
import java.util.Map; |
35 |
|
36 |
import org.gvsig.fmap.dal.DataTypes; |
37 |
import org.gvsig.fmap.dal.exception.DataException; |
38 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
39 |
import org.gvsig.fmap.dal.feature.Feature; |
40 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
41 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
42 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
43 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
44 |
import org.gvsig.fmap.dal.feature.FeatureType; |
45 |
import org.gvsig.tools.exception.BaseException; |
46 |
|
47 |
/**
|
48 |
* Extends the FeatureTableModel to add more configurable options, like the
|
49 |
* visible columns, column name aliases and row order.
|
50 |
*
|
51 |
* TODO: a?adir la persistencia.
|
52 |
*
|
53 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
54 |
*/
|
55 |
public class ConfigurableFeatureTableModel extends FeatureTableModel { |
56 |
|
57 |
private static final long serialVersionUID = -8223987814719746492L; |
58 |
|
59 |
private List<String> columnNames; |
60 |
|
61 |
private List<String> visibleColumnNames; |
62 |
|
63 |
private Map<String, String> name2Alias; |
64 |
|
65 |
/**
|
66 |
* @see FeatureTableModel#FeatureTableModel(FeatureStore, FeatureQuery)
|
67 |
*/
|
68 |
public ConfigurableFeatureTableModel(FeatureStore featureStore,
|
69 |
FeatureQuery featureQuery) throws DataException {
|
70 |
super(featureStore, featureQuery);
|
71 |
} |
72 |
|
73 |
/**
|
74 |
* @see FeatureTableModel#FeatureTableModel(FeatureStore, FeatureQuery, int)
|
75 |
*/
|
76 |
public ConfigurableFeatureTableModel(FeatureStore featureStore,
|
77 |
FeatureQuery featureQuery, int pageSize) throws DataException { |
78 |
super(featureStore, featureQuery, pageSize);
|
79 |
} |
80 |
|
81 |
@Override
|
82 |
public int getColumnCount() { |
83 |
return visibleColumnNames.size();
|
84 |
} |
85 |
|
86 |
public int getOriginalColumnCount() { |
87 |
return super.getColumnCount(); |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public String getColumnName(int column) { |
92 |
int originalIndex = getOriginalColumnIndex(column);
|
93 |
return getAliasForColumn(getOriginalColumnName(originalIndex));
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public Class<?> getColumnClass(int columnIndex) { |
98 |
int originalIndex = getOriginalColumnIndex(columnIndex);
|
99 |
return super.getColumnClass(originalIndex); |
100 |
} |
101 |
|
102 |
@Override
|
103 |
public FeatureAttributeDescriptor getDescriptorForColumn(int columnIndex) { |
104 |
int originalIndex = getOriginalColumnIndex(columnIndex);
|
105 |
return super.getDescriptorForColumn(originalIndex); |
106 |
} |
107 |
|
108 |
/**
|
109 |
* Returns the original name of the column, ignoring the alias.
|
110 |
*
|
111 |
* @param column
|
112 |
* the original index of the column
|
113 |
* @return the original column name
|
114 |
*/
|
115 |
public String getOriginalColumnName(int column) { |
116 |
return super.getColumnName(column); |
117 |
} |
118 |
|
119 |
/**
|
120 |
* Sets the visibility of a table column.
|
121 |
*
|
122 |
* @param columnIndex
|
123 |
* the index of the column to update
|
124 |
* @param visible
|
125 |
* if the column will be visible or not
|
126 |
*/
|
127 |
public void setVisible(String name, boolean visible) { |
128 |
// If we don't have already the column as visible,
|
129 |
// add to the list, without order, and recreate
|
130 |
// the visible columns list in the original order
|
131 |
if (!columnNames.contains(name)) {
|
132 |
throw new InvalidParameterException(name); // FIXME |
133 |
} |
134 |
if (visible && !visibleColumnNames.contains(name)) {
|
135 |
visibleColumnNames.add(name); |
136 |
setVisibleColumns(visibleColumnNames); |
137 |
} else {
|
138 |
visibleColumnNames.remove(name); |
139 |
fireTableStructureChanged(); |
140 |
} |
141 |
|
142 |
} |
143 |
|
144 |
@SuppressWarnings("unchecked") |
145 |
@Override
|
146 |
public void setFeatureType(FeatureType featureType) { |
147 |
// checks if all columns was visible before change
|
148 |
boolean equalsBefore = this.columnNames.size() == this.visibleColumnNames |
149 |
.size() |
150 |
&& |
151 |
visibleColumnNames.containsAll(columnNames); |
152 |
|
153 |
// Update column names
|
154 |
columnNames.clear(); |
155 |
Iterator<FeatureAttributeDescriptor> attrIter = featureType.iterator();
|
156 |
String colName;
|
157 |
while (attrIter.hasNext()) {
|
158 |
colName = attrIter.next().getName(); |
159 |
columnNames.add(colName); |
160 |
if ((!visibleColumnNames.contains(colName)) && equalsBefore) {
|
161 |
// Add new columns
|
162 |
visibleColumnNames.add(colName); |
163 |
} |
164 |
} |
165 |
|
166 |
// remove from visible columns removed columns
|
167 |
visibleColumnNames.retainAll(columnNames); |
168 |
|
169 |
// remove from alias map removed columns
|
170 |
name2Alias.keySet().retainAll(visibleColumnNames); |
171 |
|
172 |
super.setFeatureType(featureType);
|
173 |
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* Sets the current visible columns list, in the original order.
|
178 |
*
|
179 |
* @param names
|
180 |
* the names of the columns to set as visible
|
181 |
*/
|
182 |
public void setVisibleColumns(List<String> names) { |
183 |
// Recreate the visible column names list
|
184 |
// to maintain the original order
|
185 |
visibleColumnNames = new ArrayList<String>(names.size()); |
186 |
for (int i = 0; i < columnNames.size(); i++) { |
187 |
String columnName = columnNames.get(i);
|
188 |
if (names.contains(columnName)) {
|
189 |
visibleColumnNames.add(columnName); |
190 |
} |
191 |
} |
192 |
fireTableStructureChanged(); |
193 |
} |
194 |
|
195 |
/**
|
196 |
* Changes all columns to be visible.
|
197 |
*/
|
198 |
public void setAllVisible() { |
199 |
visibleColumnNames.clear(); |
200 |
visibleColumnNames.addAll(columnNames); |
201 |
fireTableStructureChanged(); |
202 |
} |
203 |
|
204 |
/**
|
205 |
* Sets the alias for a column.
|
206 |
*
|
207 |
* @param name
|
208 |
* the name of the column
|
209 |
* @param alias
|
210 |
* the alias for the column
|
211 |
*/
|
212 |
public void setAlias(String name, String alias) { |
213 |
name2Alias.put(name, alias); |
214 |
fireTableStructureChanged(); |
215 |
} |
216 |
|
217 |
public void orderByColumn(String name, boolean ascending) |
218 |
throws BaseException {
|
219 |
FeatureQueryOrder order = getHelper().getFeatureQuery().getOrder(); |
220 |
if (order==null) { |
221 |
order = new FeatureQueryOrder();
|
222 |
getHelper().getFeatureQuery().setOrder(order); |
223 |
} |
224 |
order.clear(); |
225 |
order.add(name, ascending); |
226 |
getHelper().reload(); |
227 |
fireTableDataChanged(); |
228 |
} |
229 |
|
230 |
@Override
|
231 |
protected void initialize() { |
232 |
super.initialize();
|
233 |
|
234 |
initializeVisibleColumns(); |
235 |
initializeAliases(); |
236 |
} |
237 |
|
238 |
/**
|
239 |
* Returns if a column is visible.
|
240 |
*
|
241 |
* @param name
|
242 |
* the name of the column
|
243 |
* @return if the column is visible
|
244 |
*/
|
245 |
public boolean isVisible(String name) { |
246 |
return visibleColumnNames.contains(name);
|
247 |
} |
248 |
|
249 |
/**
|
250 |
* Initializes the table name aliases.
|
251 |
*/
|
252 |
private void initializeAliases() { |
253 |
int columns = super.getColumnCount(); |
254 |
name2Alias = new HashMap<String, String>(columns); |
255 |
//
|
256 |
// for (int i = 0; i < columns; i++) {
|
257 |
// String columnName = super.getColumnName(i);
|
258 |
// name2Alias.put(columnName, columnName);
|
259 |
// }
|
260 |
} |
261 |
|
262 |
/**
|
263 |
* Initializes the table visible columns.
|
264 |
*/
|
265 |
protected void initializeVisibleColumns() { |
266 |
int columns = super.getColumnCount(); |
267 |
columnNames = new ArrayList<String>(columns); |
268 |
visibleColumnNames = new ArrayList<String>(columns); |
269 |
|
270 |
for (int i = 0; i < columns; i++) { |
271 |
String columnName = super.getColumnName(i); |
272 |
columnNames.add(columnName); |
273 |
|
274 |
// By default, geometry columns will not be visible
|
275 |
FeatureAttributeDescriptor descriptor = super
|
276 |
.getDescriptorForColumn(i); |
277 |
if (descriptor.getType() != DataTypes.GEOMETRY) {
|
278 |
visibleColumnNames.add(columnName); |
279 |
} |
280 |
} |
281 |
} |
282 |
|
283 |
/**
|
284 |
* Returns the alias for the name of a column.
|
285 |
*
|
286 |
* @param name
|
287 |
* of the column
|
288 |
* @return the alias
|
289 |
*/
|
290 |
protected String getAliasForColumn(String name) { |
291 |
String alias = name2Alias.get(name);
|
292 |
return alias == null ? name : alias; |
293 |
} |
294 |
|
295 |
/**
|
296 |
* Returns the original position of a column.
|
297 |
*
|
298 |
* @param columnIndex
|
299 |
* the current visible column index
|
300 |
* @return the original column index
|
301 |
*/
|
302 |
protected int getOriginalColumnIndex(int columnIndex) { |
303 |
String columnName = visibleColumnNames.get(columnIndex);
|
304 |
return columnNames.indexOf(columnName);
|
305 |
} |
306 |
|
307 |
@Override
|
308 |
protected Object getFeatureValue(Feature feature, int columnIndex) { |
309 |
int realColumnIndex = getOriginalColumnIndex(columnIndex);
|
310 |
return super.getFeatureValue(feature, realColumnIndex); |
311 |
} |
312 |
|
313 |
@Override
|
314 |
protected EditableFeature setFeatureValue(Feature feature, int columnIndex, |
315 |
Object value) {
|
316 |
int realColumnIndex = getOriginalColumnIndex(columnIndex);
|
317 |
return super.setFeatureValue(feature, realColumnIndex, value); |
318 |
} |
319 |
} |