Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / fmap / data / feature / swing / table / ConfigurableFeatureTableModel.java @ 23715

History | View | Annotate | Download (7.24 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Gobernment (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}  {{Task}}
26
 */
27
package org.gvsig.fmap.data.feature.swing.table;
28

    
29
import java.util.*;
30

    
31
import org.gvsig.fmap.data.ReadException;
32
import org.gvsig.fmap.data.feature.*;
33
import org.gvsig.fmap.data.feature.paging.FeaturePagingHelper;
34

    
35
/**
36
 * Extends the FeatureTableModel to add more configurable options, like the
37
 * visible columns, column name aliases and row order.
38
 * 
39
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
40
 */
41
public class ConfigurableFeatureTableModel extends FeatureTableModel {
42

    
43
    private static final long serialVersionUID = -8223987814719746492L;
44

    
45
    private List<String> columnNames;
46

    
47
    private List<String> visibleColumnNames;
48
    
49
    private Map<String, String> name2Alias;
50

    
51
    /**
52
     * @see FeatureTableModel#FeatureTableModel(FeatureStore, FeatureQuery)
53
     */
54
    public ConfigurableFeatureTableModel(FeatureStore featureStore,
55
            FeatureQuery featureQuery) throws ReadException {
56
        super(featureStore, featureQuery);
57
    }
58

    
59
    /**
60
     * @see FeatureTableModel#FeatureTableModel(FeatureStore, FeatureQuery, int)
61
     */
62
    public ConfigurableFeatureTableModel(FeatureStore featureStore,
63
            FeatureQuery featureQuery, int pageSize) throws ReadException {
64
        super(featureStore, featureQuery, pageSize);
65
    }
66

    
67
    /**
68
     * @see FeatureTableModel#FeatureTableModel(FeaturePagingHelper)
69
     */
70
    public ConfigurableFeatureTableModel(FeaturePagingHelper helper) {
71
        super(helper);
72
    }
73

    
74
    @Override
75
    public int getColumnCount() {
76
        return visibleColumnNames.size();
77
    }
78

    
79
    @Override
80
    public String getColumnName(int column) {
81
        int realIndex = getRealColumnIndex(column);
82
        String realName = super.getColumnName(realIndex);
83
        return getAliasForColumn(realName);
84
    }
85

    
86
    /**
87
     * Sets the visibility of a table column.
88
     * 
89
     * @param columnIndex
90
     *            the index of the column to update
91
     * @param visible
92
     *            if the column will be visible or not
93
     */
94
    public void setVisible(String name, boolean visible) {
95
        // If we don't have already the column as visible,
96
        // add to the list, without order, and recreate
97
        // the visible columns list in the original order
98
        if (visible && !visibleColumnNames.contains(name)) {
99
            visibleColumnNames.add(name);
100
            setVisibleColumns(visibleColumnNames);
101
        } else {
102
            visibleColumnNames.remove(name);
103
            fireTableStructureChanged();
104
        }
105
        
106
    }
107

    
108
    /**
109
     * Sets the current visible columns list, in the original order.
110
     * 
111
     * @param names
112
     *            the names of the columns to set as visible
113
     */
114
    public void setVisibleColumns(List<String> names) {
115
        // Recreate the visible column names list
116
        // to maintain the original order
117
        visibleColumnNames = new ArrayList<String>(names.size());
118
        for (int i = 0; i < columnNames.size(); i++) {
119
            String columnName = columnNames.get(i);
120
            if (names.contains(columnName)) {
121
                visibleColumnNames.add(columnName);
122
            }
123
        }
124
        fireTableStructureChanged();
125
    }
126

    
127
    /**
128
     * Changes all columns to be visible.
129
     */
130
    public void setAllVisible() {
131
        visibleColumnNames.clear();
132
        visibleColumnNames.addAll(columnNames);
133
        fireTableStructureChanged();
134
    }
135
    
136
    /**
137
     * Sets the alias for a column.
138
     * 
139
     * @param name
140
     *            the name of the column
141
     * @param alias
142
     *            the alias for the column
143
     */
144
    public void setAlias(String name, String alias) {
145
        name2Alias.put(name, alias);
146
        fireTableStructureChanged();
147
    }
148
    
149
    public void orderByColumn(String name, boolean ascending)
150
            throws ReadException {
151
        String order = ascending ? name.concat(" ASC") : name.concat(" DESC"); 
152
        getHelper().getFeatureQuery().setOrder(order);
153
        getHelper().reload();
154
        fireTableDataChanged();
155
    }
156

    
157
    @Override
158
    protected void initialize() {
159
        super.initialize();
160

    
161
        initializeVisibleColumns();
162
        initializeAliases();
163
    }
164

    
165
    /**
166
     * Initializes the table name aliases.
167
     */
168
    private void initializeAliases() {
169
        int columns = super.getColumnCount();
170
        name2Alias = new HashMap<String, String>(columns);
171
        
172
        for (int i = 0; i < columns; i++) {
173
            String columnName = super.getColumnName(i);
174
            name2Alias.put(columnName, columnName);
175
        }
176
    }
177

    
178
    /**
179
     * Initializes the table visible columns.
180
     */
181
    protected void initializeVisibleColumns() {
182
        int columns = super.getColumnCount();
183
        columnNames = new ArrayList<String>(columns);
184
        visibleColumnNames = new ArrayList<String>(columns);
185

    
186
        for (int i = 0; i < columns; i++) {
187
            String columnName = super.getColumnName(i);
188
            columnNames.add(columnName);
189
            visibleColumnNames.add(columnName);
190
        }
191
    }
192
    
193
    /**
194
     * Returns the alias for the name of a column.
195
     * 
196
     * @param name
197
     *            of the column
198
     * @return the alias
199
     */
200
    protected String getAliasForColumn(String name) {
201
        return name2Alias.get(name);
202
    }
203

    
204
    /**
205
     * Returns if a column is visible.
206
     * 
207
     * @param name
208
     *            the name of the column
209
     * @return if the column is visible
210
     */
211
    protected boolean isVisible(String name) {
212
        return visibleColumnNames.contains(name);
213
    }
214

    
215
    /**
216
     * Returns the real position of a column.
217
     * 
218
     * @param columnIndex
219
     *            the current visible column index
220
     * @return the real column index
221
     */
222
    protected int getRealColumnIndex(int columnIndex) {
223
        String columnName = visibleColumnNames.get(columnIndex);
224
        return columnNames.indexOf(columnName);
225
    }
226

    
227
    @Override
228
    protected Object getFeatureValue(Feature feature, int columnIndex) {
229
        int realColumnIndex = getRealColumnIndex(columnIndex);
230
        return super.getFeatureValue(feature, realColumnIndex);
231
    }
232

    
233
    @Override
234
    protected void setFeatureValue(Feature feature, int columnIndex,
235
            Object value) throws IsNotFeatureSettingException {
236
        int realColumnIndex = getRealColumnIndex(columnIndex);
237
        super.setFeatureValue(feature, realColumnIndex, value);
238
    }
239
}