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