Revision 23789
branches/v2_0_0_prep/libraries/libFMap_mapcontrol/src/org/gvsig/fmap/data/feature/swing/table/FeatureCellRenderer.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.DefaultTableCellRenderer; |
|
30 |
|
|
31 |
import org.gvsig.fmap.data.feature.Feature; |
|
32 |
|
|
33 |
/** |
|
34 |
* Renderer for cells of type {@link Feature} . |
|
35 |
* |
|
36 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a> |
|
37 |
*/ |
|
38 |
public class FeatureCellRenderer extends DefaultTableCellRenderer { |
|
39 |
|
|
40 |
/** |
|
41 |
* Generated Serial UID. |
|
42 |
*/ |
|
43 |
private static final long serialVersionUID = 3547431185199502021L; |
|
44 |
|
|
45 |
protected void setValue(Object value) { |
|
46 |
// TODO: change the current dumb implementation with something |
|
47 |
// more useful, as a link or button to show the Feature details |
|
48 |
// or, maybe, a subRow |
|
49 |
if (value == null) { |
|
50 |
setText(""); |
|
51 |
} else { |
|
52 |
Feature feature = (Feature) value; |
|
53 |
setText(feature.toString()); |
|
54 |
} |
|
55 |
} |
|
56 |
} |
branches/v2_0_0_prep/libraries/libFMap_mapcontrol/src/org/gvsig/fmap/data/feature/swing/table/GeometryCellRenderer.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.DefaultTableCellRenderer; |
|
30 |
|
|
31 |
import org.gvsig.fmap.geom.Geometry; |
|
32 |
|
|
33 |
/** |
|
34 |
* Renderer for cells of type {@link Geometry} . |
|
35 |
* |
|
36 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a> |
|
37 |
*/ |
|
38 |
public class GeometryCellRenderer extends DefaultTableCellRenderer { |
|
39 |
|
|
40 |
/** |
|
41 |
* Generated Serial UID. |
|
42 |
*/ |
|
43 |
private static final long serialVersionUID = -2470239399517077869L; |
|
44 |
|
|
45 |
protected void setValue(Object value) { |
|
46 |
if (value == null) { |
|
47 |
setText(""); |
|
48 |
} else { |
|
49 |
Geometry geometry = (Geometry) value; |
|
50 |
setText(geometry.toString()); |
|
51 |
setToolTipText(geometry.getGeometryType().getName()); |
|
52 |
} |
|
53 |
} |
|
54 |
} |
branches/v2_0_0_prep/libraries/libFMap_mapcontrol/src/org/gvsig/fmap/data/feature/swing/table/SetFeatureValueException.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.HashMap; |
|
30 |
import java.util.Map; |
|
31 |
|
|
32 |
import org.gvsig.tools.exception.BaseRuntimeException; |
|
33 |
|
|
34 |
/** |
|
35 |
* |
|
36 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a> |
|
37 |
*/ |
|
38 |
public class SetFeatureValueException extends BaseRuntimeException { |
|
39 |
|
|
40 |
private static final String KEY = "set_feature_value_exception"; |
|
41 |
|
|
42 |
private static final String MESSAGE = "Error setting the value of the cell " |
|
43 |
+ "at row = %(rowIndex) and column = %(columnIndex) " |
|
44 |
+ "with the value = %(value)"; |
|
45 |
|
|
46 |
private static final long serialVersionUID = -414675652211552543L; |
|
47 |
|
|
48 |
private int rowIndex; |
|
49 |
|
|
50 |
private int columnIndex; |
|
51 |
|
|
52 |
private Object value; |
|
53 |
|
|
54 |
/** |
|
55 |
* Creates a new Exception when setting a Feature value. |
|
56 |
* |
|
57 |
* @param rowIndex |
|
58 |
* the position of the Feature |
|
59 |
* @param columnIndex |
|
60 |
* the position of the value into the Feature |
|
61 |
* @param value |
|
62 |
* the value to set |
|
63 |
*/ |
|
64 |
public SetFeatureValueException(int rowIndex, int columnIndex, Object value) { |
|
65 |
super(MESSAGE, KEY, serialVersionUID); |
|
66 |
this.rowIndex = rowIndex; |
|
67 |
this.columnIndex = columnIndex; |
|
68 |
this.value = value; |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* Creates a new Exception when setting a Feature value. |
|
73 |
* |
|
74 |
* @param rowIndex |
|
75 |
* the position of the Feature |
|
76 |
* @param columnIndex |
|
77 |
* the position of the value into the Feature |
|
78 |
* @param value |
|
79 |
* the value to set |
|
80 |
* @param cause |
|
81 |
* the original Throwable |
|
82 |
*/ |
|
83 |
public SetFeatureValueException(int rowIndex, int columnIndex, |
|
84 |
Object value, Throwable cause) { |
|
85 |
super(MESSAGE, cause, KEY, serialVersionUID); |
|
86 |
this.rowIndex = rowIndex; |
|
87 |
this.columnIndex = columnIndex; |
|
88 |
this.value = value; |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* @return the rowIndex |
|
93 |
*/ |
|
94 |
public int getRowIndex() { |
|
95 |
return rowIndex; |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* @return the columnIndex |
|
100 |
*/ |
|
101 |
public int getColumnIndex() { |
|
102 |
return columnIndex; |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* @return the value |
|
107 |
*/ |
|
108 |
public Object getValue() { |
|
109 |
return value; |
|
110 |
} |
|
111 |
|
|
112 |
@SuppressWarnings("unchecked") |
|
113 |
@Override |
|
114 |
protected Map values() { |
|
115 |
Map values = new HashMap(3); |
|
116 |
values.put("rowIndex", getRowIndex()); |
|
117 |
values.put("columnIndex", getColumnIndex()); |
|
118 |
values.put("value", getValue()); |
|
119 |
return values; |
|
120 |
} |
|
121 |
|
|
122 |
} |
branches/v2_0_0_prep/libraries/libFMap_mapcontrol/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.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 |
} |
|
0 | 273 |
branches/v2_0_0_prep/libraries/libFMap_mapcontrol/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} {Create a JTable TableModel for a FeatureCollection} |
|
26 |
*/ |
|
27 |
package org.gvsig.fmap.data.feature.swing.table; |
|
28 |
|
|
29 |
import java.util.*; |
|
30 |
|
|
31 |
import org.gvsig.fmap.data.exceptions.DataException; |
|
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 DataException { |
|
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 DataException { |
|
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 DataException { |
|
151 |
FeatureQueryOrder order = getHelper().getFeatureQuery().getOrder(); |
|
152 |
order.add(name, ascending); |
|
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 EditableFeature setFeatureValue(Feature feature, int columnIndex, |
|
235 |
Object value) { |
|
236 |
int realColumnIndex = getRealColumnIndex(columnIndex); |
|
237 |
return super.setFeatureValue(feature, realColumnIndex, value); |
|
238 |
} |
|
239 |
} |
|
0 | 240 |
Also available in: Unified diff