root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / fmap / data / feature / swing / table / FeatureTableModel.java @ 23715
History | View | Annotate | Download (8.81 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.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 |
} |