svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / dal / feature / swing / table / ConfigurableFeatureTableModel.java @ 39410
History | View | Annotate | Download (13.8 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 javax.swing.event.TableModelEvent; |
37 |
|
38 |
import org.gvsig.fmap.dal.DataTypes; |
39 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
40 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
41 |
import org.gvsig.fmap.dal.feature.Feature; |
42 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
43 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
44 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
45 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
46 |
import org.gvsig.fmap.dal.feature.FeatureType; |
47 |
import org.gvsig.tools.exception.BaseException; |
48 |
|
49 |
/**
|
50 |
* Extends the FeatureTableModel to add more configurable options, like the
|
51 |
* visible columns, column name aliases and row order.
|
52 |
*
|
53 |
* TODO: a?adir la persistencia.
|
54 |
*
|
55 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
56 |
*/
|
57 |
public class ConfigurableFeatureTableModel extends FeatureTableModel { |
58 |
|
59 |
private static final long serialVersionUID = -8223987814719746492L; |
60 |
|
61 |
private List<String> columnNames; |
62 |
|
63 |
private List<String> visibleColumnNames; |
64 |
|
65 |
private List<String> visibleColumnNamesOriginal; |
66 |
|
67 |
private Map<String, String> name2Alias; |
68 |
|
69 |
private Map<String, String> name2AliasOriginal; |
70 |
|
71 |
/**
|
72 |
* @see FeatureTableModel#FeatureTableModel(FeatureStore, FeatureQuery)
|
73 |
*/
|
74 |
public ConfigurableFeatureTableModel(FeatureStore featureStore,
|
75 |
FeatureQuery featureQuery) throws BaseException {
|
76 |
super(featureStore, featureQuery);
|
77 |
} |
78 |
|
79 |
/**
|
80 |
* @see FeatureTableModel#FeatureTableModel(FeatureStore, FeatureQuery, int)
|
81 |
*/
|
82 |
public ConfigurableFeatureTableModel(FeatureStore featureStore,
|
83 |
FeatureQuery featureQuery, int pageSize) throws BaseException { |
84 |
super(featureStore, featureQuery, pageSize);
|
85 |
} |
86 |
|
87 |
@Override
|
88 |
public int getColumnCount() { |
89 |
return visibleColumnNames.size();
|
90 |
} |
91 |
|
92 |
public int getOriginalColumnCount() { |
93 |
return super.getColumnCount(); |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public String getColumnName(int column) { |
98 |
int originalIndex = getOriginalColumnIndex(column);
|
99 |
return getAliasForColumn(getOriginalColumnName(originalIndex));
|
100 |
} |
101 |
|
102 |
@Override
|
103 |
public Class<?> getColumnClass(int columnIndex) { |
104 |
int originalIndex = getOriginalColumnIndex(columnIndex);
|
105 |
return super.getColumnClass(originalIndex); |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public FeatureAttributeDescriptor getDescriptorForColumn(int columnIndex) { |
110 |
int originalIndex = getOriginalColumnIndex(columnIndex);
|
111 |
return super.getDescriptorForColumn(originalIndex); |
112 |
} |
113 |
|
114 |
/**
|
115 |
* Returns the original name of the column, ignoring the alias.
|
116 |
*
|
117 |
* @param column
|
118 |
* the original index of the column
|
119 |
* @return the original column name
|
120 |
*/
|
121 |
public String getOriginalColumnName(int column) { |
122 |
return super.getColumnName(column); |
123 |
} |
124 |
|
125 |
/**
|
126 |
* Sets the visibility of a table column.
|
127 |
*
|
128 |
* @param columnIndex
|
129 |
* the index of the column to update
|
130 |
* @param visible
|
131 |
* if the column will be visible or not
|
132 |
*/
|
133 |
public void setVisible(String name, boolean visible) { |
134 |
// If we don't have already the column as visible,
|
135 |
// add to the list, without order, and recreate
|
136 |
// the visible columns list in the original order
|
137 |
if (!columnNames.contains(name)) {
|
138 |
throw new InvalidParameterException(name); // FIXME |
139 |
} |
140 |
if (visible && !visibleColumnNames.contains(name)) {
|
141 |
visibleColumnNames.add(name); |
142 |
setVisibleColumns(visibleColumnNames); |
143 |
} else {
|
144 |
visibleColumnNames.remove(name); |
145 |
fireTableStructureChanged(); |
146 |
} |
147 |
|
148 |
} |
149 |
|
150 |
@Override
|
151 |
public void setFeatureType(FeatureType featureType) { |
152 |
// Check if there is a new column name
|
153 |
List<String> newColumns = new ArrayList<String>(); |
154 |
List<String> renamedColumnsNewName = new ArrayList<String>(); |
155 |
|
156 |
@SuppressWarnings("unchecked") |
157 |
Iterator<FeatureAttributeDescriptor> attrIter = featureType.iterator();
|
158 |
FeatureAttributeDescriptor fad = null;
|
159 |
EditableFeatureAttributeDescriptor efad = null;
|
160 |
|
161 |
String colName;
|
162 |
while (attrIter.hasNext()) {
|
163 |
fad = attrIter.next(); |
164 |
colName = fad.getName(); |
165 |
if (!columnNames.contains(colName)) {
|
166 |
if (fad instanceof EditableFeatureAttributeDescriptor) { |
167 |
efad = (EditableFeatureAttributeDescriptor) fad; |
168 |
/*
|
169 |
* If editable att descriptor,
|
170 |
* check original name
|
171 |
*/
|
172 |
if (efad.getOriginalName() != null) { |
173 |
if (!columnNames.contains(efad.getOriginalName())) {
|
174 |
/*
|
175 |
* Check with original name but add current name
|
176 |
*/
|
177 |
newColumns.add(colName); |
178 |
} else {
|
179 |
/*
|
180 |
* List of new names of renamed columns
|
181 |
*/
|
182 |
renamedColumnsNewName.add(colName); |
183 |
} |
184 |
} else {
|
185 |
newColumns.add(colName); |
186 |
} |
187 |
} else {
|
188 |
newColumns.add(colName); |
189 |
} |
190 |
} |
191 |
} |
192 |
|
193 |
// Update column names
|
194 |
columnNames.clear(); |
195 |
@SuppressWarnings("unchecked") |
196 |
Iterator<FeatureAttributeDescriptor> visibleAttrIter =
|
197 |
featureType.iterator(); |
198 |
while (visibleAttrIter.hasNext()) {
|
199 |
fad = visibleAttrIter.next(); |
200 |
colName = fad.getName(); |
201 |
columnNames.add(colName); |
202 |
//If the column is added has to be visible
|
203 |
if (!visibleColumnNames.contains(colName)) {
|
204 |
|
205 |
if (newColumns.contains(colName)) {
|
206 |
// Add new columns
|
207 |
visibleColumnNames.add(colName); |
208 |
visibleColumnNamesOriginal.add(colName); |
209 |
} |
210 |
if (renamedColumnsNewName.contains(colName)) {
|
211 |
// Add new columns
|
212 |
insertWhereOldName(visibleColumnNames, colName, fad); |
213 |
insertWhereOldName(visibleColumnNamesOriginal, colName, fad); |
214 |
} |
215 |
} |
216 |
} |
217 |
|
218 |
// remove from visible columns removed columns
|
219 |
visibleColumnNames.retainAll(columnNames); |
220 |
visibleColumnNamesOriginal.retainAll(columnNames); |
221 |
|
222 |
// remove from alias map removed columns
|
223 |
name2Alias.keySet().retainAll(columnNames); |
224 |
name2AliasOriginal.keySet().retainAll(columnNames); |
225 |
|
226 |
super.setFeatureType(featureType);
|
227 |
|
228 |
} |
229 |
|
230 |
private void insertWhereOldName( |
231 |
List<String> str_list, |
232 |
String str,
|
233 |
FeatureAttributeDescriptor fad) { |
234 |
|
235 |
if (fad instanceof EditableFeatureAttributeDescriptor) { |
236 |
EditableFeatureAttributeDescriptor efad = |
237 |
(EditableFeatureAttributeDescriptor) fad; |
238 |
if (efad.getOriginalName() != null) { |
239 |
int old_ind = str_list.indexOf(efad.getOriginalName());
|
240 |
if (old_ind != -1) { |
241 |
// Insert before old name
|
242 |
str_list.add(old_ind, str); |
243 |
} else {
|
244 |
// Insert anyway (add)
|
245 |
str_list.add(str); |
246 |
} |
247 |
} else {
|
248 |
// Insert anyway (add)
|
249 |
str_list.add(str); |
250 |
} |
251 |
} else {
|
252 |
// Insert anyway (add)
|
253 |
str_list.add(str); |
254 |
} |
255 |
} |
256 |
|
257 |
/**
|
258 |
* Sets the current visible columns list, in the original order.
|
259 |
*
|
260 |
* @param names
|
261 |
* the names of the columns to set as visible
|
262 |
*/
|
263 |
public void setVisibleColumns(List<String> names) { |
264 |
// Recreate the visible column names list
|
265 |
// to maintain the original order
|
266 |
visibleColumnNames = new ArrayList<String>(names.size()); |
267 |
for (int i = 0; i < columnNames.size(); i++) { |
268 |
String columnName = columnNames.get(i);
|
269 |
if (names.contains(columnName)) {
|
270 |
visibleColumnNames.add(columnName); |
271 |
} |
272 |
} |
273 |
fireTableStructureChanged(); |
274 |
} |
275 |
|
276 |
/**
|
277 |
* Changes all columns to be visible.
|
278 |
*/
|
279 |
public void setAllVisible() { |
280 |
visibleColumnNames.clear(); |
281 |
visibleColumnNames.addAll(columnNames); |
282 |
fireTableStructureChanged(); |
283 |
} |
284 |
|
285 |
/**
|
286 |
* Sets the alias for a column.
|
287 |
*
|
288 |
* @param name
|
289 |
* the name of the column
|
290 |
* @param alias
|
291 |
* the alias for the column
|
292 |
*/
|
293 |
public void setAlias(String name, String alias) { |
294 |
name2Alias.put(name, alias); |
295 |
fireTableStructureChanged(); |
296 |
} |
297 |
|
298 |
public void orderByColumn(String name, boolean ascending) |
299 |
throws BaseException {
|
300 |
FeatureQueryOrder order = getHelper().getFeatureQuery().getOrder(); |
301 |
if (order == null) { |
302 |
order = new FeatureQueryOrder();
|
303 |
getHelper().getFeatureQuery().setOrder(order); |
304 |
} |
305 |
order.clear(); |
306 |
order.add(name, ascending); |
307 |
getHelper().reload(); |
308 |
fireTableChanged(new TableModelEvent(this, 0, getRowCount() - 1)); |
309 |
} |
310 |
|
311 |
@Override
|
312 |
protected void initialize() { |
313 |
super.initialize();
|
314 |
|
315 |
initializeVisibleColumns(); |
316 |
initializeAliases(); |
317 |
} |
318 |
|
319 |
/**
|
320 |
* Returns if a column is visible.
|
321 |
*
|
322 |
* @param name
|
323 |
* the name of the column
|
324 |
* @return if the column is visible
|
325 |
*/
|
326 |
public boolean isVisible(String name) { |
327 |
return visibleColumnNames.contains(name);
|
328 |
} |
329 |
|
330 |
/**
|
331 |
* Initializes the table name aliases.
|
332 |
*/
|
333 |
private void initializeAliases() { |
334 |
int columns = super.getColumnCount(); |
335 |
name2Alias = new HashMap<String, String>(columns); |
336 |
name2AliasOriginal = new HashMap<String, String>(columns); |
337 |
//
|
338 |
// for (int i = 0; i < columns; i++) {
|
339 |
// String columnName = super.getColumnName(i);
|
340 |
// name2Alias.put(columnName, columnName);
|
341 |
// }
|
342 |
} |
343 |
|
344 |
/**
|
345 |
* Initializes the table visible columns.
|
346 |
*/
|
347 |
protected void initializeVisibleColumns() { |
348 |
int columns = super.getColumnCount(); |
349 |
columnNames = new ArrayList<String>(columns); |
350 |
visibleColumnNames = new ArrayList<String>(columns); |
351 |
|
352 |
for (int i = 0; i < columns; i++) { |
353 |
String columnName = super.getColumnName(i); |
354 |
columnNames.add(columnName); |
355 |
|
356 |
// By default, geometry columns will not be visible
|
357 |
FeatureAttributeDescriptor descriptor = |
358 |
super.getDescriptorForColumn(i);
|
359 |
if (descriptor.getType() != DataTypes.GEOMETRY) {
|
360 |
visibleColumnNames.add(columnName); |
361 |
} |
362 |
} |
363 |
|
364 |
visibleColumnNamesOriginal = new ArrayList<String>(visibleColumnNames); |
365 |
} |
366 |
|
367 |
/**
|
368 |
* Returns the alias for the name of a column.
|
369 |
*
|
370 |
* @param name
|
371 |
* of the column
|
372 |
* @return the alias
|
373 |
*/
|
374 |
protected String getAliasForColumn(String name) { |
375 |
String alias = name2Alias.get(name);
|
376 |
return alias == null ? name : alias; |
377 |
} |
378 |
|
379 |
/**
|
380 |
* Returns the original position of a column.
|
381 |
*
|
382 |
* @param columnIndex
|
383 |
* the current visible column index
|
384 |
* @return the original column index
|
385 |
*/
|
386 |
protected int getOriginalColumnIndex(int columnIndex) { |
387 |
String columnName = visibleColumnNames.get(columnIndex);
|
388 |
return columnNames.indexOf(columnName);
|
389 |
} |
390 |
|
391 |
@Override
|
392 |
protected Object getFeatureValue(Feature feature, int columnIndex) { |
393 |
int realColumnIndex = getOriginalColumnIndex(columnIndex);
|
394 |
return super.getFeatureValue(feature, realColumnIndex); |
395 |
} |
396 |
|
397 |
@Override
|
398 |
protected EditableFeature setFeatureValue(Feature feature, int columnIndex, |
399 |
Object value) {
|
400 |
int realColumnIndex = getOriginalColumnIndex(columnIndex);
|
401 |
return super.setFeatureValue(feature, realColumnIndex, value); |
402 |
} |
403 |
|
404 |
/**
|
405 |
* Make current changes in configuration (visible columns and aliases)
|
406 |
* as definitive.
|
407 |
*/
|
408 |
public void acceptChanges() { |
409 |
visibleColumnNamesOriginal = new ArrayList<String>(visibleColumnNames); |
410 |
name2AliasOriginal = new HashMap<String, String>(name2Alias); |
411 |
} |
412 |
|
413 |
/**
|
414 |
* Cancel current changes in configuration (visible columns and aliases)
|
415 |
* and return to previous status.
|
416 |
*/
|
417 |
public void cancelChanges() { |
418 |
visibleColumnNames = new ArrayList<String>(visibleColumnNamesOriginal); |
419 |
name2Alias = new HashMap<String, String>(name2AliasOriginal); |
420 |
fireTableStructureChanged(); |
421 |
} |
422 |
} |