1 |
/** SCHMITZM - This file is part of the java library of Martin O.J. Schmitz (SCHMITZM) |
/******************************************************************************* |
2 |
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. |
* |
4 |
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
|
* and the Geotools library. |
7 |
Diese Bibliothek ist freie Software; Sie dürfen sie unter den Bedingungen der GNU Lesser General Public License, wie von der Free Software Foundation veröffentlicht, weiterverteilen und/oder modifizieren; entweder gemäß Version 2.1 der Lizenz oder (nach Ihrer Option) jeder späteren Version. |
* |
8 |
Diese Bibliothek wird in der Hoffnung weiterverbreitet, daß sie nützlich sein wird, jedoch OHNE IRGENDEINE GARANTIE, auch ohne die implizierte Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Mehr Details finden Sie in der GNU Lesser General Public License. |
* The SCHMITZM project is hosted at: |
9 |
Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit dieser Bibliothek erhalten haben; falls nicht, schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA. |
* http://wald.intevation.org/projects/schmitzm/ |
10 |
**/ |
* |
11 |
package skrueger.geotools; |
* This program is free software; you can redistribute it and/or |
12 |
|
* modify it under the terms of the GNU Lesser General Public License |
13 |
import java.util.Iterator; |
* as published by the Free Software Foundation; either version 3 |
14 |
import java.util.Map; |
* of the License, or (at your option) any later version. |
15 |
import java.util.Vector; |
* |
16 |
|
* This program is distributed in the hope that it will be useful, |
17 |
import org.apache.log4j.Logger; |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
import org.geotools.data.DefaultQuery; |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
import org.geotools.data.FeatureSource; |
* GNU General Public License for more details. |
20 |
import org.geotools.data.Query; |
* |
21 |
import org.geotools.data.memory.MemoryDataStore; |
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
import org.geotools.feature.AttributeType; |
* along with this program; if not, write to the Free Software |
23 |
import org.geotools.feature.FeatureCollection; |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
import org.opengis.filter.Filter; |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
|
* |
26 |
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
* Contributors: |
27 |
import skrueger.AttributeMetaData; |
* Martin O. J. Schmitz - initial API and implementation |
28 |
import skrueger.i8n.I8NUtil; |
* Stefan A. Krüger - additional utility classes |
29 |
import skrueger.i8n.Translation; |
******************************************************************************/ |
30 |
|
package skrueger.geotools; |
31 |
import com.vividsolutions.jts.geom.Envelope; |
|
32 |
|
import java.util.Iterator; |
33 |
/** |
import java.util.Map; |
34 |
* This class extends the the {@link FeatureCollectionTableModel} with the |
import java.util.Vector; |
35 |
* functionalities of the {@link AttributeMetaData} of |
|
36 |
* {@linkplain StyledLayerInterface styled objects}. |
import org.apache.log4j.Logger; |
37 |
* <ul> |
import org.geotools.data.DefaultQuery; |
38 |
* <li>column names are translated according to |
import org.geotools.data.FeatureSource; |
39 |
* {@link AttributeMetaData#getTitle()}</li> |
import org.geotools.data.Query; |
40 |
* <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li> |
import org.geotools.data.memory.MemoryDataStore; |
41 |
* </ul> |
import org.geotools.feature.FeatureCollection; |
42 |
* |
import org.opengis.feature.simple.SimpleFeature; |
43 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
import org.opengis.feature.simple.SimpleFeatureType; |
44 |
* (University of Bonn/Germany) |
import org.opengis.feature.type.AttributeDescriptor; |
45 |
*/ |
import org.opengis.feature.type.FeatureType; |
46 |
public class StyledFeatureCollectionTableModel extends |
import org.opengis.filter.Filter; |
47 |
FeatureCollectionTableModel { |
|
48 |
final static private Logger LOGGER = Logger |
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
49 |
.getLogger(StyledFeatureCollectionTableModel.class); |
import skrueger.AttributeMetaData; |
50 |
/** Holds the data source as styled layer. */ |
import skrueger.i8n.I8NUtil; |
51 |
protected StyledLayerInterface<?> layer = null; |
import skrueger.i8n.Translation; |
52 |
/** Contains only the visible elements of the {@link AttributeMetaData}-Map */ |
|
53 |
protected Map<Integer, AttributeMetaData> visibleAMD = null; |
import com.vividsolutions.jts.geom.Envelope; |
54 |
/** Holds the data source for the table as {@code FeatureSource}. */ |
|
55 |
protected FeatureSource featureSource = null; |
/** |
56 |
/** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */ |
* This class extends the the {@link FeatureCollectionTableModel} with the |
57 |
protected Map<Integer, AttributeMetaData> origAMD = null; |
* functionalities of the {@link AttributeMetaData} of |
58 |
/** Holds the current filter on the table */ |
* {@linkplain StyledLayerInterface styled objects}. |
59 |
protected Filter filter = null; |
* <ul> |
60 |
/** Holds the Bounds for all features. Only set once during the constructor **/ |
* <li>column names are translated according to |
61 |
protected Envelope bounds; |
* {@link AttributeMetaData#getTitle()}</li> |
62 |
|
* <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li> |
63 |
|
* </ul> |
64 |
|
* |
65 |
/** |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
66 |
* Creates a new table model for a styled layer. |
* (University of Bonn/Germany) |
67 |
* |
*/ |
68 |
* @param layer |
public class StyledFeatureCollectionTableModel extends |
69 |
* the styled layer |
FeatureCollectionTableModel { |
70 |
*/ |
final static private Logger LOGGER = Logger |
71 |
public StyledFeatureCollectionTableModel( |
.getLogger(StyledFeatureCollectionTableModel.class); |
72 |
StyledFeatureCollectionInterface layer) { |
/** Holds the data source as styled layer. */ |
73 |
this(layer, Filter.INCLUDE); |
protected StyledLayerInterface<?> layer = null; |
74 |
} |
/** Contains only the visible elements of the {@link AttributeMetaData}-Map */ |
75 |
|
protected Map<Integer, AttributeMetaData> visibleAMD = null; |
76 |
/** |
/** Holds the data source for the table as {@code FeatureSource}. */ |
77 |
* Creates a new table model for a styled layer. |
protected FeatureSource featureSource = null; |
78 |
* |
/** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */ |
79 |
* @param layer |
protected Map<Integer, AttributeMetaData> origAMD = null; |
80 |
* the styled layer |
/** Holds the current filter on the table */ |
81 |
* @param filter |
protected Filter filter = null; |
82 |
* filter applied to the table |
/** Holds the Bounds for all features. Only set once during the constructor **/ |
83 |
*/ |
protected Envelope bounds; |
84 |
public StyledFeatureCollectionTableModel( |
|
85 |
StyledFeatureCollectionInterface layer, Filter filter) { |
|
86 |
super(); |
|
87 |
setFeatureCollection(layer, filter); |
/** |
88 |
} |
* Creates a new table model for a styled layer. |
89 |
|
* |
90 |
|
* @param layer |
91 |
/** |
* the styled layer |
92 |
* Creates a new table model for a styled layer. |
*/ |
93 |
* |
public StyledFeatureCollectionTableModel( |
94 |
* @param layer |
StyledFeatureCollectionInterface layer) { |
95 |
* the styled layer |
this(layer, Filter.INCLUDE); |
96 |
*/ |
} |
97 |
public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer) { |
|
98 |
this(layer, Filter.INCLUDE); |
/** |
99 |
} |
* Creates a new table model for a styled layer. |
100 |
|
* |
101 |
/** |
* @param layer |
102 |
* Creates a new table model for a styled layer. |
* the styled layer |
103 |
* |
* @param filter |
104 |
* @param layer |
* filter applied to the table |
105 |
* the styled layer |
*/ |
106 |
* @param filter |
public StyledFeatureCollectionTableModel( |
107 |
* filter applied to the table |
StyledFeatureCollectionInterface layer, Filter filter) { |
108 |
*/ |
super(); |
109 |
public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer, |
setFeatureCollection(layer, filter); |
110 |
Filter filter) { |
} |
111 |
super(); |
|
112 |
setFeatureCollection(layer, filter); |
|
113 |
} |
/** |
114 |
|
* Creates a new table model for a styled layer. |
115 |
/** |
* |
116 |
* Sets a new data source for the table. |
* @param layer |
117 |
* |
* the styled layer |
118 |
* @param fs |
*/ |
119 |
* the feature source |
public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer) { |
120 |
* @param amd |
this(layer, Filter.INCLUDE); |
121 |
* {@link AttributeMetaData}-Map to define the visible attributes |
} |
122 |
* and translation |
|
123 |
*/ |
/** |
124 |
protected void setFeatureSource(FeatureSource fs, |
* Creates a new table model for a styled layer. |
125 |
Map<Integer, AttributeMetaData> amd, Filter filter) |
* |
126 |
throws Exception { |
* @param layer |
127 |
if (filter == null) |
* the styled layer |
128 |
filter = Filter.INCLUDE; |
* @param filter |
129 |
|
* filter applied to the table |
130 |
this.featureSource = fs; |
*/ |
131 |
this.filter = filter; |
public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer, |
132 |
this.origAMD = amd; |
Filter filter) { |
133 |
this.visibleAMD = null; |
super(); |
134 |
|
setFeatureCollection(layer, filter); |
135 |
FeatureCollection fc = null; |
} |
136 |
if (fs != null) { |
|
137 |
|
/** |
138 |
bounds = fs.getBounds(); |
* Sets a new data source for the table. |
139 |
|
* |
140 |
Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter); |
* @param fs |
141 |
if (amd != null) { |
* the feature source |
142 |
// determine the names of the visible Attributes |
* @param amd |
143 |
this.visibleAMD = StyledLayerUtil.getVisibleAttributeMetaData( |
* {@link AttributeMetaData}-Map to define the visible attributes |
144 |
amd, true); |
* and translation |
145 |
Vector<String> visibleAttrNames = new Vector<String>(); |
*/ |
146 |
// Add the column with the geometry (usually "the_geom") |
protected void setFeatureSource(FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
147 |
visibleAttrNames.add(fs.getSchema().getDefaultGeometry() |
Map<Integer, AttributeMetaData> amd, Filter filter) |
148 |
.getLocalName()); |
throws Exception { |
149 |
for (int attrIdx : visibleAMD.keySet()) { |
if (filter == null) |
150 |
|
filter = Filter.INCLUDE; |
151 |
/** |
|
152 |
* If the user removed columns from the schema of the DBF |
this.featureSource = fs; |
153 |
* file, there might exist AttributeMetaData for columns |
this.filter = filter; |
154 |
* that don't exists. We check here to avoid an |
this.origAMD = amd; |
155 |
* ArrayOutOfIndex. |
this.visibleAMD = null; |
156 |
*/ |
|
157 |
if (attrIdx < fs.getSchema().getAttributeCount()) { |
FeatureCollection fc = null; |
158 |
final AttributeType attributeTypeAtIdx = fs.getSchema() |
if (fs != null) { |
159 |
.getAttributeType(attrIdx); |
|
160 |
visibleAttrNames.add(attributeTypeAtIdx.getLocalName()); |
bounds = fs.getBounds(); |
161 |
} else { |
|
162 |
LOGGER.warn("AttributeMetaData has been found for columnIdx="+attrIdx+", but fs.getSchema().getAttributeCount() = "+fs.getSchema().getAttributeCount()+". Ignored."); |
Query query = new DefaultQuery(fs.getSchema().getName().getLocalPart(), filter); |
163 |
} |
if (amd != null) { |
164 |
} |
// determine the names of the visible Attributes |
165 |
|
this.visibleAMD = StyledLayerUtil.getVisibleAttributeMetaData( |
166 |
// create a query for the visible attributes |
amd, true); |
167 |
String[] properties = visibleAttrNames.toArray(new String[0]); |
Vector<String> visibleAttrNames = new Vector<String>(); |
168 |
|
// Add the column with the geometry (usually "the_geom") |
169 |
LOGGER.debug("Query contains the following attributes: " |
visibleAttrNames.add(fs.getSchema().getGeometryDescriptor() |
170 |
+ visibleAttrNames); |
.getLocalName()); |
171 |
|
for (int attrIdx : visibleAMD.keySet()) { |
172 |
query = new DefaultQuery(fs.getSchema().getTypeName(), filter, |
|
173 |
properties); |
/** |
174 |
} |
* If the user removed columns from the schema of the DBF |
175 |
fc = fs.getFeatures(query); |
* file, there might exist AttributeMetaData for columns |
176 |
|
* that don't exists. We check here to avoid an |
177 |
// FAILS:!!!, even with query = new |
* ArrayOutOfIndex. |
178 |
// DefaultQuery(fs.getSchema().getTypeName(), filter); |
*/ |
179 |
// java.lang.UnsupportedOperationException: Unknown feature |
if (attrIdx < fs.getSchema().getAttributeCount()) { |
180 |
// attribute: PQM_MOD |
final AttributeDescriptor attributeTypeAtIdx = fs.getSchema() |
181 |
// at |
.getAttributeDescriptors().get(attrIdx); |
182 |
// schmitzm.geotools.feature.FeatureOperationTree.evaluate(FeatureOperationTree.java:93) |
visibleAttrNames.add(attributeTypeAtIdx.getLocalName()); |
183 |
// bounds = fc.getBounds(); |
} else { |
184 |
// SK, 17.4.2009 |
LOGGER.warn("AttributeMetaData has been found for columnIdx="+attrIdx+", but fs.getSchema().getAttributeCount() = "+fs.getSchema().getAttributeCount()+". Ignored."); |
185 |
// |
} |
186 |
// System.out.println("Filter = "+filter); |
} |
187 |
// System.out.println("Size of FC = "+fc.size()); |
|
188 |
// System.out.println("anz att= "+fc.getNumberOfAttributes()); |
// create a query for the visible attributes |
189 |
} |
String[] properties = visibleAttrNames.toArray(new String[0]); |
190 |
setFeatureCollection(fc); |
|
191 |
} |
LOGGER.debug("Query contains the following attributes: " |
192 |
|
+ visibleAttrNames); |
193 |
/** |
|
194 |
* Converts the {@code StyledFeatureCollection} to a {@code FeatureSource} |
query = new DefaultQuery(fs.getSchema().getTypeName(), filter, |
195 |
* and sets this as the new data source for the table. |
properties); |
196 |
* |
} |
197 |
* @param fs |
fc = fs.getFeatures(query); |
198 |
* the feature source |
|
199 |
* @param amd |
// FAILS:!!!, even with query = new |
200 |
* {@link AttributeMetaData}-Map to define the visible attributes |
// DefaultQuery(fs.getSchema().getTypeName(), filter); |
201 |
* and translation |
// java.lang.UnsupportedOperationException: Unknown feature |
202 |
*/ |
// attribute: PQM_MOD |
203 |
public void setFeatureCollection(StyledFeaturesInterface layer, |
// at |
204 |
Filter filter) { |
// schmitzm.geotools.feature.FeatureOperationTree.evaluate(FeatureOperationTree.java:93) |
205 |
this.layer = layer; |
// bounds = fc.getBounds(); |
206 |
try { |
// SK, 17.4.2009 |
207 |
if (layer == null) |
// |
208 |
setFeatureSource(null, null, null); |
// System.out.println("Filter = "+filter); |
209 |
else { |
// System.out.println("Size of FC = "+fc.size()); |
210 |
FeatureCollection fc = layer.getFeatureCollection(); |
// System.out.println("anz att= "+fc.getNumberOfAttributes()); |
211 |
String fcName = fc.getSchema().getTypeName(); |
} |
212 |
FeatureSource fs = new MemoryDataStore(fc) |
setFeatureCollection(fc); |
213 |
.getFeatureSource(fcName); |
} |
214 |
setFeatureSource(fs, layer.getAttributeMetaDataMap(), filter); |
|
215 |
} |
/** |
216 |
} catch (Exception err) { |
* Converts the {@code StyledFeatureCollection} to a {@code FeatureSource} |
217 |
throw new RuntimeException(err); |
* and sets this as the new data source for the table. |
218 |
} |
* |
219 |
} |
* @param fs |
220 |
|
* the feature source |
221 |
/** |
* @param amd |
222 |
* Sets the {@code StyledFeatureCollection} as new data source for the |
* {@link AttributeMetaData}-Map to define the visible attributes |
223 |
* table. |
* and translation |
224 |
* |
*/ |
225 |
* @param fs |
public void setFeatureCollection(StyledFeaturesInterface layer, |
226 |
* the feature source |
Filter filter) { |
227 |
* @param amd |
this.layer = layer; |
228 |
* {@link AttributeMetaData}-Map to define the visible attributes |
try { |
229 |
* and translation |
if (layer == null) |
230 |
*/ |
setFeatureSource(null, null, null); |
231 |
public void setFeatureCollection(StyledFeatureSourceInterface layer, |
else { |
232 |
Filter filter) { |
FeatureCollection fc = layer.getFeatureCollection(); |
233 |
this.layer = layer; |
String fcName = fc.getSchema().getName().getLocalPart(); |
234 |
try { |
FeatureSource fs = new MemoryDataStore(fc) |
235 |
if (layer == null) |
.getFeatureSource(fcName); |
236 |
setFeatureSource(null, null, null); |
setFeatureSource(fs, layer.getAttributeMetaDataMap(), filter); |
237 |
else |
} |
238 |
setFeatureSource(layer.getGeoObject(), layer |
} catch (Exception err) { |
239 |
.getAttributeMetaDataMap(), filter); |
throw new RuntimeException(err); |
240 |
} catch (Exception err) { |
} |
241 |
throw new RuntimeException(err); |
} |
242 |
} |
|
243 |
} |
/** |
244 |
|
* Sets the {@code StyledFeatureCollection} as new data source for the |
245 |
/** |
* table. |
246 |
* Resets the filter for the table. |
* |
247 |
* |
* @param fs |
248 |
* @param filter |
* the feature source |
249 |
* a filter |
* @param amd |
250 |
*/ |
* {@link AttributeMetaData}-Map to define the visible attributes |
251 |
public void setFilter(Filter filter) { |
* and translation |
252 |
try { |
*/ |
253 |
setFeatureSource(this.featureSource, this.origAMD, filter); |
public void setFeatureCollection(StyledFeatureSourceInterface layer, |
254 |
} catch (Exception err) { |
Filter filter) { |
255 |
LOGGER.error("Setting the filter of the table model", err); |
this.layer = layer; |
256 |
throw new RuntimeException(err); |
try { |
257 |
} |
if (layer == null) |
258 |
} |
setFeatureSource(null, null, null); |
259 |
|
else |
260 |
/** |
setFeatureSource(layer.getGeoObject(), layer |
261 |
* @return <code>Filter.INCLUDE</code> or the {@link Filter} applied to the |
.getAttributeMetaDataMap(), filter); |
262 |
* Features |
} catch (Exception err) { |
263 |
*/ |
throw new RuntimeException(err); |
264 |
public Filter getFilter() { |
} |
265 |
return this.filter; |
} |
266 |
} |
|
267 |
|
/** |
268 |
/** |
* Resets the filter for the table. |
269 |
* After calling {@code super.reorganize(.)} this method replaced the column |
* |
270 |
* descriptions with the titles of the {@code AttributeMetaData}. |
* @param filter |
271 |
* |
* a filter |
272 |
* @param fireTableStructureChanged |
*/ |
273 |
* indicates whether a table event is initiated after reorganize |
public void setFilter(Filter filter) { |
274 |
*/ |
try { |
275 |
@Override |
setFeatureSource(this.featureSource, this.origAMD, filter); |
276 |
protected void reorganize(boolean fireTableStructureChanged) { |
} catch (Exception err) { |
277 |
super.reorganize(false); |
LOGGER.error("Setting the filter of the table model", err); |
278 |
// translate the column names |
throw new RuntimeException(err); |
279 |
if (visibleAMD != null) { |
} |
280 |
Iterator<Integer> keys = visibleAMD.keySet().iterator(); |
} |
281 |
for (int i = 0; i < colNames.length && keys.hasNext(); i++) { |
|
282 |
Translation title = visibleAMD.get(keys.next()).getTitle(); |
/** |
283 |
if (!I8NUtil.isEmpty(title)) { |
* @return <code>Filter.INCLUDE</code> or the {@link Filter} applied to the |
284 |
// System.out.println("set colname " + i + " to " + |
* Features |
285 |
// title.toString()); |
*/ |
286 |
colNames[i] = title.toString(); |
public Filter getFilter() { |
287 |
} |
return this.filter; |
288 |
} |
} |
289 |
} |
|
290 |
if (fireTableStructureChanged) |
/** |
291 |
fireTableStructureChanged(); |
* After calling {@code super.reorganize(.)} this method replaced the column |
292 |
} |
* descriptions with the titles of the {@code AttributeMetaData}. |
293 |
|
* |
294 |
/** |
* @param fireTableStructureChanged |
295 |
* @return Cached bounds for the whole dataset (without applying the filter) |
* indicates whether a table event is initiated after reorganize |
296 |
* or <code>null</code> |
*/ |
297 |
*/ |
@Override |
298 |
public Envelope getBounds() { |
protected void reorganize(boolean fireTableStructureChanged) { |
299 |
return bounds; |
super.reorganize(false); |
300 |
} |
// translate the column names |
301 |
} |
if (visibleAMD != null) { |
302 |
|
Iterator<Integer> keys = visibleAMD.keySet().iterator(); |
303 |
|
for (int i = 0; i < colNames.length && keys.hasNext(); i++) { |
304 |
|
Translation title = visibleAMD.get(keys.next()).getTitle(); |
305 |
|
if (!I8NUtil.isEmpty(title)) { |
306 |
|
// System.out.println("set colname " + i + " to " + |
307 |
|
// title.toString()); |
308 |
|
colNames[i] = title.toString(); |
309 |
|
} |
310 |
|
} |
311 |
|
} |
312 |
|
if (fireTableStructureChanged) |
313 |
|
fireTableStructureChanged(); |
314 |
|
} |
315 |
|
|
316 |
|
/** |
317 |
|
* @return Cached bounds for the whole dataset (without applying the filter) |
318 |
|
* or <code>null</code> |
319 |
|
*/ |
320 |
|
public Envelope getBounds() { |
321 |
|
return bounds; |
322 |
|
} |
323 |
|
} |