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 / table / EmptyFeatureTableModel.java @ 46447
History | View | Annotate | Download (12 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
package org.gvsig.fmap.dal.swing.impl.featuretable.table; |
26 |
|
27 |
import java.awt.event.ActionListener; |
28 |
import java.security.InvalidParameterException; |
29 |
import java.text.SimpleDateFormat; |
30 |
import java.util.ArrayList; |
31 |
import java.util.HashMap; |
32 |
import java.util.List; |
33 |
import java.util.Locale; |
34 |
import java.util.Map; |
35 |
import javax.swing.table.AbstractTableModel; |
36 |
|
37 |
import org.gvsig.fmap.dal.DataTypes; |
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.FeatureSelection; |
43 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
44 |
import org.gvsig.fmap.dal.feature.FeatureType; |
45 |
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper; |
46 |
import org.gvsig.tools.dynobject.DynStruct; |
47 |
import org.gvsig.tools.exception.BaseException; |
48 |
import org.gvsig.tools.observer.ComplexObserver; |
49 |
import org.gvsig.tools.observer.Observable; |
50 |
import org.slf4j.Logger; |
51 |
import org.slf4j.LoggerFactory; |
52 |
|
53 |
public class EmptyFeatureTableModel extends AbstractTableModel implements org.gvsig.fmap.dal.swing.FeatureTableModel, ComplexObserver { |
54 |
|
55 |
private static final long serialVersionUID = -8223987814719746492L; |
56 |
|
57 |
private static final Logger logger = LoggerFactory.getLogger(EmptyFeatureTableModel.class); |
58 |
|
59 |
private List<String> columnNames; |
60 |
|
61 |
private List<String> visibleColumnNames; |
62 |
|
63 |
private List<String> visibleColumnNamesOriginal; |
64 |
|
65 |
private Map<String, String> name2Alias; |
66 |
|
67 |
private Map<String, String> name2AliasOriginal; |
68 |
|
69 |
private Map<String,String> patterns = null; |
70 |
|
71 |
private Locale localeOfData; |
72 |
|
73 |
private DynStruct struct = null; |
74 |
|
75 |
|
76 |
public EmptyFeatureTableModel(DynStruct struct) {
|
77 |
this.struct = struct;
|
78 |
this.localeOfData = Locale.getDefault(); |
79 |
this.initialize();
|
80 |
} |
81 |
|
82 |
private void initialize() { |
83 |
|
84 |
int columns = this.getOriginalColumnCount(); |
85 |
|
86 |
// Initilize visible columns
|
87 |
columnNames = new ArrayList<>(columns); |
88 |
visibleColumnNames = new ArrayList<>(columns); |
89 |
for (int i = 0; i < columns; i++) { |
90 |
String columnName = super.getColumnName(i); |
91 |
columnNames.add(columnName); |
92 |
|
93 |
// By default, geometry columns will not be visible
|
94 |
FeatureAttributeDescriptor descriptor = this.getInternalColumnDescriptor(i);
|
95 |
if (descriptor.getType() != DataTypes.GEOMETRY) {
|
96 |
visibleColumnNames.add(columnName); |
97 |
} |
98 |
} |
99 |
visibleColumnNamesOriginal = new ArrayList<>(visibleColumnNames); |
100 |
|
101 |
// Initialize alias
|
102 |
name2Alias = new HashMap<>(columns); |
103 |
name2AliasOriginal = new HashMap<>(columns); |
104 |
|
105 |
// Initialize formating patters
|
106 |
this.patterns = new HashMap<>(); |
107 |
for (int i = 0; i < columns; i++) { |
108 |
FeatureAttributeDescriptor descriptor = this.getInternalColumnDescriptor(i);
|
109 |
String columnName = descriptor.getName();
|
110 |
switch(descriptor.getDataType().getType()) {
|
111 |
case DataTypes.BYTE:
|
112 |
case DataTypes.INT:
|
113 |
case DataTypes.LONG:
|
114 |
String defaultIntegerPattern = "#,##0"; |
115 |
this.patterns.put(columnName,defaultIntegerPattern);
|
116 |
break;
|
117 |
case DataTypes.DOUBLE:
|
118 |
case DataTypes.FLOAT:
|
119 |
String defaultDecimalPattern = "#,##0.000"; |
120 |
this.patterns.put(columnName,defaultDecimalPattern);
|
121 |
break;
|
122 |
case DataTypes.DATE:
|
123 |
String defaultDatePattern = new SimpleDateFormat().toPattern(); |
124 |
this.patterns.put(columnName,defaultDatePattern);
|
125 |
break;
|
126 |
default:
|
127 |
this.patterns.put(columnName,null); |
128 |
} |
129 |
} |
130 |
|
131 |
updatePagerWithHiddenColums(); |
132 |
} |
133 |
|
134 |
private void updatePagerWithHiddenColums() { |
135 |
FeatureQuery query = this.getFeaturePager().getFeatureQuery();
|
136 |
if (this.getFeaturePager().getFeatureStore().isEditing()) { |
137 |
if (query.hasConstantsAttributeNames()) {
|
138 |
query.clearConstantsAttributeNames(); |
139 |
} |
140 |
} else {
|
141 |
query.setConstantsAttributeNames(this.getHiddenColumnNames());
|
142 |
} |
143 |
try {
|
144 |
this.getFeaturePager().reload();
|
145 |
} catch (BaseException ex) {
|
146 |
logger.warn("Can't reload paging-helper.", ex);
|
147 |
} |
148 |
} |
149 |
|
150 |
@Override
|
151 |
public FeaturePagingHelper getFeaturePager() {
|
152 |
return null; |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public FeatureQuery getFeatureQuery() {
|
157 |
return null; |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public FeatureType getFeatureType() {
|
162 |
return null; |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public FeatureStore getFeatureStore() {
|
167 |
return null; |
168 |
} |
169 |
|
170 |
@Override
|
171 |
public FeatureSelection getFeatureSelection() {
|
172 |
return null; |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public void setFeatureSelection(FeatureSelection selection) { |
177 |
// Do nothing
|
178 |
} |
179 |
|
180 |
@Override
|
181 |
public int getColumnCount() { |
182 |
return visibleColumnNames.size();
|
183 |
} |
184 |
|
185 |
public int getOriginalColumnCount() { |
186 |
return this.struct.getDynFields().length; |
187 |
} |
188 |
|
189 |
@Override
|
190 |
public String getColumnName(int column) { |
191 |
String columName = getOriginalColumnName(column);
|
192 |
return this.getColumnAlias(columName); |
193 |
} |
194 |
|
195 |
@Override
|
196 |
public Class<?> getColumnClass(int columnIndex) { |
197 |
int originalIndex = getOriginalColumnIndex(columnIndex);
|
198 |
|
199 |
// Return the class of the FeatureAttributeDescriptor for the value
|
200 |
FeatureAttributeDescriptor attributeDesc = this.getInternalColumnDescriptor(originalIndex);
|
201 |
if (attributeDesc == null) { |
202 |
return super.getColumnClass(originalIndex); |
203 |
} |
204 |
Class<?> clazz = attributeDesc.getObjectClass();
|
205 |
return (clazz == null ? super.getColumnClass(originalIndex) : clazz); |
206 |
} |
207 |
|
208 |
@Override
|
209 |
public FeatureAttributeDescriptor getColumnDescriptor(int columnIndex) { |
210 |
int originalIndex = getOriginalColumnIndex(columnIndex);
|
211 |
return this.getInternalColumnDescriptor(originalIndex); |
212 |
} |
213 |
|
214 |
protected FeatureAttributeDescriptor getInternalColumnDescriptor(int columnIndex) { |
215 |
return new DynFieldFacadeOfAFeatureAttributeDescriptor(this.struct.getDynFields()[columnIndex]); |
216 |
} |
217 |
|
218 |
@Override
|
219 |
public String getOriginalColumnName(int column) { |
220 |
return this.struct.getDynFields()[column].getName(); |
221 |
} |
222 |
|
223 |
@Override
|
224 |
public void setColumnVisible(String name, boolean visible) { |
225 |
// If we don't have already the column as visible,
|
226 |
// add to the list, without order, and recreate
|
227 |
// the visible columns list in the original order
|
228 |
if (!columnNames.contains(name)) {
|
229 |
throw new InvalidParameterException(name); // FIXME |
230 |
} |
231 |
if (visible && !visibleColumnNames.contains(name)) {
|
232 |
visibleColumnNames.add(name); |
233 |
setVisibleColumns(visibleColumnNames); |
234 |
} else {
|
235 |
visibleColumnNames.remove(name); |
236 |
setVisibleColumns(visibleColumnNames); |
237 |
fireTableStructureChanged(); |
238 |
} |
239 |
|
240 |
} |
241 |
|
242 |
public void setFeatureType(FeatureType featureType) { |
243 |
} |
244 |
|
245 |
private void setVisibleColumns(List<String> names) { |
246 |
// Recreate the visible column names list
|
247 |
// to maintain the original order
|
248 |
visibleColumnNames = new ArrayList<>(names.size()); |
249 |
for (String columnName : columnNames) { |
250 |
if (names.contains(columnName)) {
|
251 |
visibleColumnNames.add(columnName); |
252 |
} |
253 |
} |
254 |
updatePagerWithHiddenColums(); |
255 |
fireTableStructureChanged(); |
256 |
} |
257 |
|
258 |
protected String[] getHiddenColumnNames() { |
259 |
List<String> hiddenColumns = new ArrayList<>(); |
260 |
hiddenColumns.addAll(columnNames); |
261 |
|
262 |
for (String columnName : visibleColumnNames) { |
263 |
hiddenColumns.remove(columnName); |
264 |
} |
265 |
if( hiddenColumns.size()<1 ) { |
266 |
return null; |
267 |
} |
268 |
return (String[]) hiddenColumns.toArray(new String[hiddenColumns.size()]); |
269 |
} |
270 |
|
271 |
/**
|
272 |
* Changes all columns to be visible.
|
273 |
*/
|
274 |
@Override
|
275 |
public void setAllVisible() { |
276 |
visibleColumnNames.clear(); |
277 |
visibleColumnNames.addAll(columnNames); |
278 |
fireTableStructureChanged(); |
279 |
} |
280 |
|
281 |
@Override
|
282 |
public void setColumnOrder(String name, boolean ascending) |
283 |
throws BaseException {
|
284 |
} |
285 |
|
286 |
@Override
|
287 |
public int getRowCount() { |
288 |
return 0; |
289 |
} |
290 |
|
291 |
@Override
|
292 |
public boolean isColumnVisible(String name) { |
293 |
return visibleColumnNames.contains(name);
|
294 |
} |
295 |
|
296 |
@Override
|
297 |
public String getColumnAlias(String name) { |
298 |
String alias = name2Alias.get(name);
|
299 |
return alias == null ? name : alias; |
300 |
} |
301 |
|
302 |
@Override
|
303 |
public void setColumnAlias(String name, String alias) { |
304 |
name2Alias.put(name, alias); |
305 |
fireTableStructureChanged(); |
306 |
} |
307 |
|
308 |
@Override
|
309 |
public int getOriginalColumnIndex(int columnIndex) { |
310 |
String columnName = visibleColumnNames.get(columnIndex);
|
311 |
return columnNames.indexOf(columnName);
|
312 |
} |
313 |
|
314 |
@Override
|
315 |
public Object getValueAt(int rowIndex, int columnIndex) { |
316 |
return null; |
317 |
} |
318 |
|
319 |
@Override
|
320 |
public Feature getFeatureAt(int rowIndex) { |
321 |
return null; |
322 |
} |
323 |
|
324 |
protected Object getFeatureValue(Feature feature, int columnIndex) { |
325 |
return null; |
326 |
} |
327 |
|
328 |
protected EditableFeature setFeatureValue(Feature feature, int columnIndex, |
329 |
Object value) {
|
330 |
return null; |
331 |
} |
332 |
|
333 |
|
334 |
public void acceptChanges() { |
335 |
visibleColumnNamesOriginal = new ArrayList<>(visibleColumnNames); |
336 |
name2AliasOriginal = new HashMap<>(name2Alias); |
337 |
} |
338 |
|
339 |
public void cancelChanges() { |
340 |
visibleColumnNames = new ArrayList<>(visibleColumnNamesOriginal); |
341 |
name2Alias = new HashMap<>(name2AliasOriginal); |
342 |
fireTableStructureChanged(); |
343 |
} |
344 |
|
345 |
|
346 |
@Override
|
347 |
public String getColumnFormattingPattern(int column) { |
348 |
String columnName = this.visibleColumnNames.get(column); |
349 |
return this.getColumnFormattingPattern(columnName); |
350 |
} |
351 |
|
352 |
@Override
|
353 |
public String getColumnFormattingPattern(String columnName) { |
354 |
String pattern = this.patterns.get(columnName); |
355 |
return pattern;
|
356 |
} |
357 |
|
358 |
@Override
|
359 |
public void setColumnFormattingPattern(String columnName, String pattern) { |
360 |
this.patterns.put(columnName,pattern);
|
361 |
} |
362 |
|
363 |
@Override
|
364 |
public Locale getLocaleOfData() { |
365 |
return this.localeOfData; |
366 |
} |
367 |
|
368 |
@Override
|
369 |
public void setLocaleOfData(Locale locale) { |
370 |
this.localeOfData = locale;
|
371 |
} |
372 |
|
373 |
@Override
|
374 |
public boolean isSelectionLocked() { |
375 |
return false; |
376 |
} |
377 |
|
378 |
@Override
|
379 |
public boolean isSelectionUp() { |
380 |
return false; |
381 |
} |
382 |
|
383 |
@Override
|
384 |
public void setSelectionUp(boolean selectionUp) { |
385 |
} |
386 |
|
387 |
@Override
|
388 |
public void update(final Observable observable, final Object notification) { |
389 |
} |
390 |
|
391 |
@Override
|
392 |
public int getSelectionCount() { |
393 |
return 0; |
394 |
} |
395 |
|
396 |
@Override
|
397 |
public void addChangeListener(ActionListener listener) { |
398 |
// Do nothing
|
399 |
} |
400 |
|
401 |
|
402 |
} |