Revision 23715

View differences:

branches/v2_0_0_prep/applications/appgvSIG/src/org/gvsig/fmap/data/feature/swing/table/FeatureTableModel.java
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}  {Create a JTable TableModel for a FeatureCollection}
26
 */
27
package org.gvsig.fmap.data.feature.swing.table;
28

  
29
import javax.swing.table.AbstractTableModel;
30

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

  
37
/**
38
 * TableModel to access data of a FeatureCollection.
39
 * 
40
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
41
 */
42
public class FeatureTableModel extends AbstractTableModel {
43

  
44
    private static final long serialVersionUID = -2488157521902851301L;
45

  
46
    private FeaturePagingHelper helper;
47

  
48
    /**
49
     * Constructs a TableModel from a FeatureCollection, with the default page
50
     * size.
51
     * 
52
     * @param featureCollection
53
     *            to extract data from
54
     * @throws ReadException
55
     *             if there is an error reading data from the FeatureStore
56
     */
57
    public FeatureTableModel(FeatureStore featureStore,
58
            FeatureQuery featureQuery) throws ReadException {
59
        this(featureStore, featureQuery, FeaturePagingHelper.DEFAULT_PAGE_SIZE);
60
    }
61

  
62
    /**
63
     * Constructs a TableModel from a FeatureCollection, and a given page size.
64
     * 
65
     * @param featureCollection
66
     *            to extract data from
67
     * @param pageSize
68
     *            the number of elements per page data
69
     * @throws ReadException
70
     *             if there is an error reading data from the FeatureStore
71
     */
72
    public FeatureTableModel(FeatureStore featureStore,
73
            FeatureQuery featureQuery, int pageSize) throws ReadException {
74
        this(new FeaturePagingHelperImpl(featureStore, featureQuery, pageSize));
75
    }
76

  
77
    /**
78
     * Constructs a TableModel from a FeatureCollection and a Paging helper.
79
     * 
80
     * @param featureCollection
81
     *            to extract data from
82
     * @param helper
83
     *            the paging helper
84
     * @throws ReadException
85
     *             if there is an error reading data from the FeatureStore
86
     */
87
    public FeatureTableModel(FeaturePagingHelper helper) {
88
        this.helper = helper;
89
        initialize();
90
    }
91

  
92
    public int getColumnCount() {
93
        // Return the number of fields of the Features
94
        FeatureType featureType = getFeatureType();
95
        return featureType.getFields().length;
96
    }
97

  
98
    public int getRowCount() {
99
        // Return the total size of the collection
100
        return getHelper().getTotalSize();
101
    }
102

  
103
    public Object getValueAt(int rowIndex, int columnIndex) {
104
        // Get the Feature at row "rowIndex", and return the value of the
105
        // attribute at "columnIndex"
106
        Feature feature = getFeatureAt(rowIndex);
107
        return getFeatureValue(feature, columnIndex);
108
    }
109

  
110
    @SuppressWarnings("unchecked")
111
    public Class getColumnClass(int columnIndex) {
112
        // Return the class of the FeatureAttributeDescriptor for the value
113
        FeatureAttributeDescriptor attributeDesc = getAttributeDescriptor(columnIndex);
114
        Class clazz = attributeDesc.getObjectClass();
115
        return (clazz == null ? super.getColumnClass(columnIndex) : clazz);
116
    }
117

  
118
    public String getColumnName(int column) {
119
        // Return the Feature attribute name
120
        FeatureAttributeDescriptor attributeDesc = getAttributeDescriptor(column);
121
        return attributeDesc.getName();
122
    }
123

  
124
    @Override
125
    public boolean isCellEditable(int rowIndex, int columnIndex) {
126
        return getFeatureStore().isEditing();
127
    }
128

  
129
    /**
130
     * TODO: the current implementation does not work, update to the last API
131
     * changes for Feature edition.
132
     */
133
    @Override
134
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
135
        // Get the feature at rowIndex
136
        Feature feature = getFeatureAt(rowIndex);
137
        // Only set the value if the feature exists
138
        if (feature != null) {
139
            // We only need to update if the value to set is not equal to the
140
            // current value
141
            Object currentValue = getFeatureValue(feature, columnIndex);
142
            if (value != currentValue
143
                    || (value != null && !value.equals(currentValue))) {
144
                try {
145
                    // getFeatureStore().startEditing();
146
                    //                    
147
                    // EditableFeature editable = feature.getEditableFeature();
148
                    // editable.set(columnIndex, value);
149
                    //                    
150
                    // getFeatureStore().update(editable);
151
                    // getFeatureStore().finishEditing();
152

  
153
                    getFeatureStore().startEditing();
154
                    feature.editing();
155
                    setFeatureValue(feature, columnIndex, value);
156
                    getFeatureStore().update(feature);
157
                    // getHelper().reload();
158
                    // TODO: REVISAR
159
                    // getHelper().setFeatureCollection(
160
                    // (FeatureCollection) getFeatureStore()
161
                    // .getDataCollection(getFeatureQuery()));
162

  
163
                    fireTableCellUpdated(rowIndex, columnIndex);
164
                } catch (DataException ex) {
165
                    throw new SetFeatureValueException(rowIndex, columnIndex,
166
                            value, ex);
167
                }
168
            }
169
        }
170
    }
171

  
172
    /**
173
     * Returns a reference to the Paging Helper used to load the data from the
174
     * DataStore.
175
     * 
176
     * @return the paging helper
177
     */
178
    public FeaturePagingHelper getHelper() {
179
        return helper;
180
    }
181

  
182
    /**
183
     * Initialize the TableModel
184
     */
185
    protected void initialize() {
186
        // Nothing to do
187
    }
188

  
189
    /**
190
     * Returns the value for a row position.
191
     * 
192
     * @param rowIndex
193
     *            the row position
194
     * @return the Feature
195
     */
196
    protected Feature getFeatureAt(int rowIndex) {
197
        return getHelper().getFeatureAt(rowIndex);
198
    }
199

  
200
    /**
201
     * Returns the value of a Feature attribute, at the given position.
202
     * 
203
     * @param feature
204
     *            the feature to get the value from
205
     * @param columnIndex
206
     *            the Feature attribute position
207
     * @return the value
208
     */
209
    protected Object getFeatureValue(Feature feature, int columnIndex) {
210
        return feature.get(columnIndex);
211
    }
212

  
213
    /**
214
     * Sets the value of an Feature attribute at the given position.
215
     * 
216
     * @param feature
217
     *            the feature to update
218
     * @param columnIndex
219
     *            the attribute position
220
     * @param value
221
     *            the value to set
222
     * @throws IsNotFeatureSettingException
223
     *             if there is an error setting the value
224
     */
225
    protected void setFeatureValue(Feature feature, int columnIndex,
226
            Object value) throws IsNotFeatureSettingException {
227
        feature.set(columnIndex, value);
228
    }
229

  
230
    /**
231
     * Returns the FeatureCollection used to get the data.
232
     * 
233
     * @return the FeatureCollection
234
     */
235
    protected FeatureCollection getFeatureCollection() {
236
        return getHelper().getFeatureCollection();
237
    }
238

  
239
    /**
240
     * Returns the FeatureStore of the Collection.
241
     * 
242
     * @return the FeatureStore
243
     */
244
    protected FeatureStore getFeatureStore() {
245
        return getHelper().getFeatureStore();
246
    }
247

  
248
    /**
249
     * Returns the FeatureQuery used to get the Features.
250
     * 
251
     * @return the FeatureQuery
252
     */
253
    protected FeatureQuery getFeatureQuery() {
254
        return getHelper().getFeatureQuery();
255
    }
256
    
257
    /**
258
     * Returns the type of the features.
259
     */
260
    private FeatureType getFeatureType() {
261
        return getFeatureCollection().getFeatureType();
262
    }
263

  
264
    /**
265
     * Returns the descriptor of a Feature attribute.
266
     */
267
    private FeatureAttributeDescriptor getAttributeDescriptor(int columnIndex) {
268
        return getFeatureType().getByOrder(columnIndex);
269
    }
270
}
0 271

  
branches/v2_0_0_prep/applications/appgvSIG/src/org/gvsig/fmap/data/feature/swing/table/ConfigurableFeatureTableModel.java
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
}

Also available in: Unified diff