Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_mapcontrol / src / org / gvsig / fmap / data / feature / swing / table / FeatureTableModel.java @ 23789

History | View | Annotate | Download (8.74 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}  {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.exceptions.DataException;
32
import org.gvsig.fmap.data.exceptions.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 DataException {
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 DataException {
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.size();
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
    @Override
130
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
131
        // Get the feature at rowIndex
132
        Feature feature = getFeatureAt(rowIndex);
133
        // Only set the value if the feature exists
134
        if (feature != null) {
135
            // We only need to update if the value to set is not equal to the
136
            // current value
137
            Object currentValue = getFeatureValue(feature, columnIndex);
138
            if (value != currentValue
139
                    || (value != null && !value.equals(currentValue))) {
140
                try {
141
                    FeatureStore store = getFeatureStore();
142
                    store.edit();
143
                    store.update(setFeatureValue(feature, columnIndex, value));
144
                    store.finishEditing();
145

    
146
                    getHelper().reloadCurrentPage();
147
                    fireTableCellUpdated(rowIndex, columnIndex);
148
                } catch (DataException ex) {
149
                    throw new SetFeatureValueException(rowIndex, columnIndex,
150
                            value, ex);
151
                }
152
            }
153
        }
154
    }
155

    
156
    /**
157
     * Returns a reference to the Paging Helper used to load the data from the
158
     * DataStore.
159
     * 
160
     * @return the paging helper
161
     */
162
    public FeaturePagingHelper getHelper() {
163
        return helper;
164
    }
165

    
166
    /**
167
     * Sets the FeatureType to show in the table. Used for FeatureStores with
168
     * many simultaneous FeatureTypes supported. Will cause a reload of the
169
     * current data.
170
     * 
171
     * @param featureType
172
     *            the FeatureType of the Features
173
     * @throws DataException
174
     *             if there is an error loading the data
175
     */
176
    public void setFeatureType(FeatureType featureType) throws DataException {
177
        getFeatureQuery().setFeatureType(featureType);
178
        getHelper().reload();
179
        fireTableStructureChanged();
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 EditableFeature setFeatureValue(Feature feature, int columnIndex,
226
            Object value) {
227
        EditableFeature editableFeature = feature.getEditable();
228
        editableFeature.set(columnIndex, value);
229
        return editableFeature;
230
    }
231

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

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

    
250
    /**
251
     * Returns the FeatureQuery used to get the Features.
252
     * 
253
     * @return the FeatureQuery
254
     */
255
    protected FeatureQuery getFeatureQuery() {
256
        return getHelper().getFeatureQuery();
257
    }
258

    
259
    /**
260
     * Returns the type of the features.
261
     */
262
    private FeatureType getFeatureType() {
263
        return getFeatureQuery().getFeatureType();
264
    }
265

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