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