Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / dal / feature / swing / table / ConfigurableFeatureTableModel.java @ 41093

History | View | Annotate | Download (14.6 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
 * AUTHORS (In addition to CIT):
26
 * 2008 {DiSiD Technologies}  {Create a JTable TableModel for a FeatureQuery}
27
 */
28
package org.gvsig.fmap.mapcontrol.dal.feature.swing.table;
29

    
30
import java.security.InvalidParameterException;
31
import java.util.ArrayList;
32
import java.util.HashMap;
33
import java.util.Iterator;
34
import java.util.List;
35
import java.util.Map;
36

    
37
import javax.swing.event.TableModelEvent;
38

    
39
import org.gvsig.fmap.dal.DataTypes;
40
import org.gvsig.fmap.dal.feature.EditableFeature;
41
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
42
import org.gvsig.fmap.dal.feature.Feature;
43
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
44
import org.gvsig.fmap.dal.feature.FeatureQuery;
45
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.fmap.dal.feature.FeatureType;
48
import org.gvsig.tools.exception.BaseException;
49

    
50
/**
51
 * Extends the FeatureTableModel to add more configurable options, like the
52
 * visible columns, column name aliases and row order.
53
 * 
54
 * TODO: a?adir la persistencia.
55
 * 
56
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
57
 */
58
public class ConfigurableFeatureTableModel extends FeatureTableModel {
59

    
60
    private static final long serialVersionUID = -8223987814719746492L;
61

    
62
    private List<String> columnNames;
63

    
64
    private List<String> visibleColumnNames;
65

    
66
    private List<String> visibleColumnNamesOriginal;
67

    
68
    private Map<String, String> name2Alias;
69

    
70
    private Map<String, String> name2AliasOriginal;
71

    
72
    /**
73
     * @see FeatureTableModel#FeatureTableModel(FeatureStore, FeatureQuery)
74
     */
75
    public ConfigurableFeatureTableModel(FeatureStore featureStore,
76
        FeatureQuery featureQuery) throws BaseException {
77
        super(featureStore, featureQuery);
78
    }
79

    
80
    /**
81
     * @see FeatureTableModel#FeatureTableModel(FeatureStore, FeatureQuery, int)
82
     */
83
    public ConfigurableFeatureTableModel(FeatureStore featureStore,
84
        FeatureQuery featureQuery, int pageSize) throws BaseException {
85
        super(featureStore, featureQuery, pageSize);
86
    }
87

    
88
    @Override
89
    public int getColumnCount() {
90
        return visibleColumnNames.size();
91
    }
92

    
93
    public int getOriginalColumnCount() {
94
        return super.getColumnCount();
95
    }
96

    
97
    @Override
98
    public String getColumnName(int column) {
99
        int originalIndex = getOriginalColumnIndex(column);
100
        return getAliasForColumn(getOriginalColumnName(originalIndex));
101
    }
102

    
103
    @Override
104
    public Class<?> getColumnClass(int columnIndex) {
105
        int originalIndex = getOriginalColumnIndex(columnIndex);
106
        return super.getColumnClass(originalIndex);
107
    }
108

    
109
    @Override
110
    public FeatureAttributeDescriptor getDescriptorForColumn(int columnIndex) {
111
        int originalIndex = getOriginalColumnIndex(columnIndex);
112
        return super.getDescriptorForColumn(originalIndex);
113
    }
114

    
115
    /**
116
     * Returns the original name of the column, ignoring the alias.
117
     * 
118
     * @param column
119
     *            the original index of the column
120
     * @return the original column name
121
     */
122
    public String getOriginalColumnName(int column) {
123
        return super.getColumnName(column);
124
    }
125

    
126
    /**
127
     * Sets the visibility of a table column.
128
     * 
129
     * @param columnIndex
130
     *            the index of the column to update
131
     * @param visible
132
     *            if the column will be visible or not
133
     */
134
    public void setVisible(String name, boolean visible) {
135
        // If we don't have already the column as visible,
136
        // add to the list, without order, and recreate
137
        // the visible columns list in the original order
138
        if (!columnNames.contains(name)) {
139
            throw new InvalidParameterException(name); // FIXME
140
        }
141
        if (visible && !visibleColumnNames.contains(name)) {
142
            visibleColumnNames.add(name);
143
            setVisibleColumns(visibleColumnNames);
144
        } else {
145
            visibleColumnNames.remove(name);
146
            fireTableStructureChanged();
147
        }
148

    
149
    }
150

    
151
    @Override
152
    public void setFeatureType(FeatureType featureType) {
153
        // Check if there is a new column name
154
        List<String> newColumns = new ArrayList<String>();
155
        List<String> renamedColumnsNewName = new ArrayList<String>();
156
        
157
        @SuppressWarnings("unchecked")
158
        Iterator<FeatureAttributeDescriptor> attrIter = featureType.iterator();
159
        FeatureAttributeDescriptor fad = null;
160
        EditableFeatureAttributeDescriptor efad = null;
161
        
162
        String colName;
163
        while (attrIter.hasNext()) {
164
            fad = attrIter.next();
165
            colName = fad.getName();
166
            if (!columnNames.contains(colName)) {
167
                if (fad instanceof EditableFeatureAttributeDescriptor) {
168
                    efad = (EditableFeatureAttributeDescriptor) fad; 
169
                    /*
170
                     * If editable att descriptor,
171
                     * check original name
172
                     */
173
                    if (efad.getOriginalName() != null) {
174
                        if (!columnNames.contains(efad.getOriginalName())) {
175
                            /*
176
                             * Check with original name but add current name
177
                             */
178
                            newColumns.add(colName);
179
                        } else {
180
                            /*
181
                             * List of new names of renamed columns
182
                             */
183
                            renamedColumnsNewName.add(colName);
184
                        }
185
                    } else {
186
                        newColumns.add(colName);
187
                    }
188
                } else {
189
                    newColumns.add(colName);
190
                }
191
            }
192
        }
193

    
194
        // Update column names
195
        columnNames.clear();
196
        @SuppressWarnings("unchecked")
197
        Iterator<FeatureAttributeDescriptor> visibleAttrIter =
198
            featureType.iterator();
199
        while (visibleAttrIter.hasNext()) {
200
            fad = visibleAttrIter.next();
201
            colName = fad.getName();
202
            columnNames.add(colName);
203
            //If the column is added has to be visible
204
            if (!visibleColumnNames.contains(colName)) {
205

    
206
                if (((newColumns.contains(colName)
207
                    || renamedColumnsNewName.contains(colName)))
208
                    &&
209
                    fad.getType() != DataTypes.GEOMETRY) {
210
                    // Add new columns and renamed
211
                    visibleColumnNames.add(colName);
212
                    visibleColumnNamesOriginal.add(colName);
213
                }
214
                /*
215
                if (renamedColumnsNewName.contains(colName)) {
216
                    // Add renamed
217
                    insertWhereOldName(visibleColumnNames, colName, fad);
218
                    insertWhereOldName(visibleColumnNamesOriginal, colName, fad);
219
                }
220
                */
221
            }
222
        }
223

    
224
        // remove from visible columns removed columns
225
        visibleColumnNames = intersectKeepOrder(columnNames, visibleColumnNames);
226
        // instead of: visibleColumnNames.retainAll(columnNames);
227

    
228
        visibleColumnNamesOriginal = intersectKeepOrder(columnNames, visibleColumnNamesOriginal);
229
        // instead of: visibleColumnNamesOriginal.retainAll(columnNames);
230

    
231
        // remove from alias map removed columns
232
        name2Alias.keySet().retainAll(columnNames);
233
        name2AliasOriginal.keySet().retainAll(columnNames);
234

    
235
        super.setFeatureType(featureType);
236

    
237
    }
238

    
239
    /**
240
     * keeps order of first parameter
241
     * 
242
     * @param lista
243
     * @param listb
244
     * @return
245
     */
246
    private List<String> intersectKeepOrder(List<String> lista, List<String> listb) {
247
        
248
        List<String> resp = new ArrayList<String>();
249
        resp.addAll(lista);
250
        resp.retainAll(listb);
251
        return resp;
252
    }
253

    
254
    private void insertWhereOldNamee(
255
        List<String> str_list,
256
        String str,
257
        FeatureAttributeDescriptor fad) {
258
        
259
        if (fad instanceof EditableFeatureAttributeDescriptor) {
260
            EditableFeatureAttributeDescriptor efad =
261
                (EditableFeatureAttributeDescriptor) fad;
262
            if (efad.getOriginalName() != null) {
263
                int old_ind = str_list.indexOf(efad.getOriginalName());
264
                if (old_ind != -1) {
265
                    // Insert before old name
266
                    str_list.add(old_ind, str);
267
                } else {
268
                    // Insert anyway (add)
269
                    str_list.add(str);
270
                }
271
            } else {
272
                // Insert anyway (add)
273
                str_list.add(str);
274
            }
275
        } else {
276
            // Insert anyway (add)
277
            str_list.add(str);
278
        }
279
    }
280

    
281
    /**
282
     * Sets the current visible columns list, in the original order.
283
     * 
284
     * @param names
285
     *            the names of the columns to set as visible
286
     */
287
    public void setVisibleColumns(List<String> names) {
288
        // Recreate the visible column names list
289
        // to maintain the original order
290
        visibleColumnNames = new ArrayList<String>(names.size());
291
        for (int i = 0; i < columnNames.size(); i++) {
292
            String columnName = columnNames.get(i);
293
            if (names.contains(columnName)) {
294
                visibleColumnNames.add(columnName);
295
            }
296
        }
297
        fireTableStructureChanged();
298
    }
299

    
300
    /**
301
     * Changes all columns to be visible.
302
     */
303
    public void setAllVisible() {
304
        visibleColumnNames.clear();
305
        visibleColumnNames.addAll(columnNames);
306
        fireTableStructureChanged();
307
    }
308

    
309
    /**
310
     * Sets the alias for a column.
311
     * 
312
     * @param name
313
     *            the name of the column
314
     * @param alias
315
     *            the alias for the column
316
     */
317
    public void setAlias(String name, String alias) {
318
        name2Alias.put(name, alias);
319
        fireTableStructureChanged();
320
    }
321

    
322
    public void orderByColumn(String name, boolean ascending)
323
        throws BaseException {
324
        FeatureQueryOrder order = getHelper().getFeatureQuery().getOrder();
325
        if (order == null) {
326
            order = new FeatureQueryOrder();
327
            getHelper().getFeatureQuery().setOrder(order);
328
        }
329
        order.clear();
330
        order.add(name, ascending);
331
        getHelper().reload();
332
        fireTableChanged(new TableModelEvent(this, 0, getRowCount() - 1));
333
    }
334

    
335
    @Override
336
    protected void initialize() {
337
        super.initialize();
338

    
339
        initializeVisibleColumns();
340
        initializeAliases();
341
    }
342

    
343
    /**
344
     * Returns if a column is visible.
345
     * 
346
     * @param name
347
     *            the name of the column
348
     * @return if the column is visible
349
     */
350
    public boolean isVisible(String name) {
351
        return visibleColumnNames.contains(name);
352
    }
353

    
354
    /**
355
     * Initializes the table name aliases.
356
     */
357
    private void initializeAliases() {
358
        int columns = super.getColumnCount();
359
        name2Alias = new HashMap<String, String>(columns);
360
        name2AliasOriginal = new HashMap<String, String>(columns);
361
        //
362
        // for (int i = 0; i < columns; i++) {
363
        // String columnName = super.getColumnName(i);
364
        // name2Alias.put(columnName, columnName);
365
        // }
366
    }
367

    
368
    /**
369
     * Initializes the table visible columns.
370
     */
371
    protected void initializeVisibleColumns() {
372
        int columns = super.getColumnCount();
373
        columnNames = new ArrayList<String>(columns);
374
        visibleColumnNames = new ArrayList<String>(columns);
375

    
376
        for (int i = 0; i < columns; i++) {
377
            String columnName = super.getColumnName(i);
378
            columnNames.add(columnName);
379

    
380
            // By default, geometry columns will not be visible
381
            FeatureAttributeDescriptor descriptor =
382
                super.getDescriptorForColumn(i);
383
            if (descriptor.getType() != DataTypes.GEOMETRY) {
384
                visibleColumnNames.add(columnName);
385
            }
386
        }
387
        
388
        visibleColumnNamesOriginal = new ArrayList<String>(visibleColumnNames);
389
    }
390

    
391
    /**
392
     * Returns the alias for the name of a column.
393
     * 
394
     * @param name
395
     *            of the column
396
     * @return the alias
397
     */
398
    protected String getAliasForColumn(String name) {
399
        String alias = name2Alias.get(name);
400
        return alias == null ? name : alias;
401
    }
402

    
403
    /**
404
     * Returns the original position of a column.
405
     * 
406
     * @param columnIndex
407
     *            the current visible column index
408
     * @return the original column index
409
     */
410
    protected int getOriginalColumnIndex(int columnIndex) {
411
        String columnName = visibleColumnNames.get(columnIndex);
412
        return columnNames.indexOf(columnName);
413
    }
414

    
415
    @Override
416
    protected Object getFeatureValue(Feature feature, int columnIndex) {
417
        int realColumnIndex = getOriginalColumnIndex(columnIndex);
418
        return super.getFeatureValue(feature, realColumnIndex);
419
    }
420

    
421
    @Override
422
    protected EditableFeature setFeatureValue(Feature feature, int columnIndex,
423
        Object value) {
424
        int realColumnIndex = getOriginalColumnIndex(columnIndex);
425
        return super.setFeatureValue(feature, realColumnIndex, value);
426
    }
427
    
428
    /**
429
     * Make current changes in configuration (visible columns and aliases)
430
     * as definitive.
431
     */
432
    public void acceptChanges() {
433
            visibleColumnNamesOriginal = new ArrayList<String>(visibleColumnNames);
434
            name2AliasOriginal = new HashMap<String, String>(name2Alias);
435
    }
436
    
437
    /**
438
     * Cancel current changes in configuration (visible columns and aliases)
439
     * and return to previous status.
440
     */
441
    public void cancelChanges() {
442
            visibleColumnNames = new ArrayList<String>(visibleColumnNamesOriginal);
443
            name2Alias = new HashMap<String, String>(name2AliasOriginal);
444
            fireTableStructureChanged();
445
    }
446
}