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. Tzeggai - additional utility classes |
29 |
import skrueger.i8n.Translation; |
******************************************************************************/ |
30 |
|
package skrueger.geotools; |
31 |
import com.vividsolutions.jts.geom.Envelope; |
|
32 |
|
import java.util.HashMap; |
33 |
/** |
import java.util.LinkedHashSet; |
34 |
* This class extends the the {@link FeatureCollectionTableModel} with the |
import java.util.List; |
35 |
* functionalities of the {@link AttributeMetaData} of |
|
36 |
* {@linkplain StyledMapInterface 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.feature.FeatureCollection; |
41 |
* </ul> |
import org.opengis.feature.simple.SimpleFeature; |
42 |
* |
import org.opengis.feature.simple.SimpleFeatureType; |
43 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
import org.opengis.feature.type.AttributeDescriptor; |
44 |
* (University of Bonn/Germany) |
import org.opengis.feature.type.Name; |
45 |
*/ |
import org.opengis.filter.Filter; |
46 |
public class StyledFeatureCollectionTableModel extends |
|
47 |
FeatureCollectionTableModel { |
import schmitzm.geotools.feature.FeatureUtil; |
48 |
final static private Logger LOGGER = Logger |
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
49 |
.getLogger(StyledFeatureCollectionTableModel.class); |
import skrueger.AttributeMetadataImpl; |
50 |
/** Holds the data source as styled map. */ |
import skrueger.AttributeMetadataInterface; |
51 |
protected StyledMapInterface<?> map = null; |
|
52 |
/** Contains only the visible elements of the {@link AttributeMetaData}-Map */ |
import com.vividsolutions.jts.geom.Envelope; |
53 |
protected Map<Integer, AttributeMetaData> visibleAMD = null; |
|
54 |
/** Holds the data source for the table as {@code FeatureSource}. */ |
/** |
55 |
protected FeatureSource featureSource = null; |
* This class extends the the {@link FeatureCollectionTableModel} with the |
56 |
/** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */ |
* functionalities of the {@link AttributeMetadataImpl}. |
57 |
protected Map<Integer, AttributeMetaData> origAMD = null; |
* <ul> |
58 |
/** Holds the current filter on the table */ |
* <li>column names are translated according to |
59 |
protected Filter filter = null; |
* {@link AttributeMetadataImpl#getTitle()}</li> |
60 |
/** Holds the Bounds for all features. Only set once during the constructor **/ |
* <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li> |
61 |
protected Envelope bounds; |
* <li>Any filter defined in the {@link StyledFeaturesInterface} will be |
62 |
|
* applied.</li> |
63 |
/** |
* </ul> |
64 |
* Creates a new table model for a styled map. |
* |
65 |
* |
* @author Stefan A. Tzeggai |
66 |
* @param map |
*/ |
67 |
* the styled map |
public class StyledFeatureCollectionTableModel extends |
68 |
*/ |
FeatureCollectionTableModel { |
69 |
public StyledFeatureCollectionTableModel( |
final static private Logger LOGGER = Logger |
70 |
StyledFeatureCollectionInterface map) { |
.getLogger(StyledFeatureCollectionTableModel.class); |
71 |
this(map, Filter.INCLUDE); |
/** |
72 |
} |
* Contains the complete {@link AttributeMetadataImpl}-Map of the styled |
73 |
|
* layer. |
74 |
/** |
*/ |
75 |
* Creates a new table model for a styled map. |
protected AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap = null; |
76 |
* |
/** Holds the current filter on the table */ |
77 |
* @param map |
protected Filter filter = null; |
78 |
* the styled map |
/** Holds the Bounds for all features. Only set once during the constructor **/ |
79 |
* @param filter |
protected Envelope bounds; |
80 |
* filter applied to the table |
/** |
81 |
*/ |
* Tooltips für die Spaltennamen. Wird nur beim Aufruf von |
82 |
public StyledFeatureCollectionTableModel( |
* {@link #reorganize} befuellt. |
83 |
StyledFeatureCollectionInterface map, Filter filter) { |
*/ |
84 |
super(); |
protected String[] colTooltips = null; |
85 |
setFeatureCollection(map, filter); |
|
86 |
} |
/** |
87 |
|
* A cache for the #sortedValuesVisibleOnly(). WHen the amd is changed |
88 |
/** |
* externally, this object will stay the same |
89 |
* Creates a new table model for a styled map. |
**/ |
90 |
* |
protected List<? extends AttributeMetadataInterface> amdMapVisibleOnly = null; |
91 |
* @param map |
|
92 |
* the styled map |
/** |
93 |
*/ |
* Creates a new table model for a styled layer. |
94 |
public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) { |
* |
95 |
this(map, Filter.INCLUDE); |
* @param styledFeatures |
96 |
} |
* the styled layer |
97 |
|
* @param filter |
98 |
/** |
* filter applied to the table |
99 |
* Creates a new table model for a styled map. |
*/ |
100 |
* |
public StyledFeatureCollectionTableModel( |
101 |
* @param map |
StyledFeaturesInterface<?> styledFeatures) { |
102 |
* the styled map |
setStyledFeatures(styledFeatures); |
103 |
* @param filter |
} |
104 |
* filter applied to the table |
|
105 |
*/ |
/** |
106 |
public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map, |
* This overwritten method filters the values for NODATA-values defined in |
107 |
Filter filter) { |
* the {@link AttributeMetadataImpl} |
108 |
super(); |
*/ |
109 |
setFeatureCollection(map, filter); |
@Override |
110 |
} |
public Object getValueAt(int row, int col) { |
111 |
|
Object rawValue = super.getValueAt(row, col); |
112 |
/** |
return amdMapVisibleOnly.get(col).fiterNodata(rawValue); |
113 |
* Sets a new data source for the table. |
} |
114 |
* |
|
115 |
* @param fs |
/** |
116 |
* the feature source |
* Sets a new data source for the table. |
117 |
* @param amd |
* |
118 |
* {@link AttributeMetaData}-Map to define the visible attributes |
* @param fs |
119 |
* and translation |
* the feature source |
120 |
*/ |
* @param amdm |
121 |
protected void setFeatureSource(FeatureSource fs, |
* {@link AttributeMetadataImpl}-Map to define the visible |
122 |
Map<Integer, AttributeMetaData> amd, Filter filter) |
* attributes and translation |
123 |
throws Exception { |
*/ |
124 |
if (filter == null) |
protected void setFeatureSource( |
125 |
filter = Filter.INCLUDE; |
FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
126 |
|
AttributeMetadataMap<? extends AttributeMetadataInterface> amdm, |
127 |
this.featureSource = fs; |
Filter filter) throws Exception { |
128 |
this.filter = filter; |
|
129 |
this.origAMD = amd; |
if (filter == null) |
130 |
this.visibleAMD = null; |
filter = Filter.INCLUDE; |
131 |
|
|
132 |
FeatureCollection fc = null; |
// this.featureSource = fs; |
133 |
if (fs != null) { |
this.filter = filter; |
134 |
|
this.amdMap = amdm; |
135 |
bounds = fs.getBounds(); |
this.amdMapVisibleOnly = amdMap.sortedValuesVisibleOnly(); |
136 |
|
|
137 |
Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter); |
FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null; |
138 |
if (amd != null) { |
if (fs != null) { |
139 |
// determine the names of the visible Attributes |
|
140 |
this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData( |
bounds = fs.getBounds(); |
141 |
amd, true); |
|
142 |
Vector<String> visibleAttrNames = new Vector<String>(); |
final SimpleFeatureType schema = fs.getSchema(); |
143 |
// Add the column with the geometry (usually "the_geom") |
Query query = new DefaultQuery(schema.getTypeName(), filter); |
144 |
visibleAttrNames.add(fs.getSchema().getDefaultGeometry() |
if (amdm != null) { |
145 |
.getLocalName()); |
LinkedHashSet<String> visibleAttrNames = new LinkedHashSet<String>(); |
146 |
for (int attrIdx : visibleAMD.keySet()) { |
|
147 |
|
// Add the column with the geometry (usually "the_geom") always |
148 |
/** |
visibleAttrNames.add(schema.getGeometryDescriptor() |
149 |
* If the user removed columns from the schema of the DBF |
.getLocalName()); |
150 |
* file, there might exist AttributeMetaData for columns |
|
151 |
* that don't exists. We check here to avoid an |
// Add other visible attributes as ordered by weights |
152 |
* ArrayOutOfIndex. |
for (AttributeMetadataInterface a : amdMapVisibleOnly) { |
153 |
*/ |
visibleAttrNames.add(a.getLocalName()); |
154 |
if (attrIdx < fs.getSchema().getAttributeCount()) { |
} |
155 |
final AttributeType attributeTypeAtIdx = fs.getSchema() |
|
156 |
.getAttributeType(attrIdx); |
// Tested with 2.6.x trunk from 2009-11-26 and it now works. So |
157 |
visibleAttrNames.add(attributeTypeAtIdx.getLocalName()); |
// we only request the properties we need! |
158 |
} else { |
// /** |
159 |
LOGGER.warn("AttributeMetaData has been found for columnIdx="+attrIdx+", but fs.getSchema().getAttributeCount() = "+fs.getSchema().getAttributeCount()+". Ignored."); |
// * I got NPEs when properties contained only [the_geom] |
160 |
} |
// ?!??!!?? |
161 |
} |
// */ |
162 |
|
// if (properties.length > 1) { |
163 |
// create a query for the visible attributes |
query = new DefaultQuery(schema.getTypeName(), filter, |
164 |
String[] properties = visibleAttrNames.toArray(new String[0]); |
visibleAttrNames.toArray(new String[] {})); |
165 |
|
// } else { |
166 |
LOGGER.debug("Query contains the following attributes: " |
// query = new DefaultQuery(schema.getTypeName(), filter); |
167 |
+ visibleAttrNames); |
// } |
168 |
|
} |
169 |
query = new DefaultQuery(fs.getSchema().getTypeName(), filter, |
fc = fs.getFeatures(query); |
170 |
properties); |
} |
171 |
} |
setFeatureCollection(fc); |
172 |
fc = fs.getFeatures(query); |
} |
173 |
|
|
174 |
// FAILS:!!!, even with query = new |
/** |
175 |
// DefaultQuery(fs.getSchema().getTypeName(), filter); |
* Converts the {@code StyledFeatureCollection} to a {@code FeatureSource} |
176 |
// java.lang.UnsupportedOperationException: Unknown feature |
* and sets this as the new data source for the table. |
177 |
// attribute: PQM_MOD |
* |
178 |
// at |
* @param fs |
179 |
// schmitzm.geotools.feature.FeatureOperationTree.evaluate(FeatureOperationTree.java:93) |
* the feature source |
180 |
// bounds = fc.getBounds(); |
* @param amd |
181 |
// SK, 17.4.2009 |
* {@link AttributeMetadataImpl}-Map to define the visible |
182 |
// |
* attributes and translation |
183 |
// System.out.println("Filter = "+filter); |
*/ |
184 |
// System.out.println("Size of FC = "+fc.size()); |
public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) { |
185 |
// System.out.println("anz att= "+fc.getNumberOfAttributes()); |
try { |
186 |
} |
if (styledFeatures == null) |
187 |
setFeatureCollection(fc); |
setFeatureSource(null, null, null); |
188 |
} |
else { |
189 |
|
setFeatureSource(styledFeatures.getFeatureSource(), |
190 |
/** |
styledFeatures.getAttributeMetaDataMap(), |
191 |
* Converts the {@code StyledFeatureCollection} to a {@code FeatureSource} |
styledFeatures.getFilter()); |
192 |
* and sets this as the new data source for the table. |
} |
193 |
* |
} catch (Exception err) { |
194 |
* @param fs |
throw new RuntimeException(err); |
195 |
* the feature source |
} |
196 |
* @param amd |
} |
197 |
* {@link AttributeMetaData}-Map to define the visible attributes |
|
198 |
* and translation |
/** |
199 |
*/ |
* After calling {@code super.reorganize(.)} this method replaced the column |
200 |
public void setFeatureCollection(StyledFeatureCollectionInterface map, |
* descriptions with the titles of the {@code AttributeMetaData}. |
201 |
Filter filter) { |
* |
202 |
this.map = map; |
* @param fireTableStructureChanged |
203 |
try { |
* indicates whether a table event is initiated after reorganize |
204 |
if (map == null) |
*/ |
205 |
setFeatureSource(null, null, null); |
@Override |
206 |
else { |
protected void reorganize(boolean fireTableStructureChanged) { |
207 |
FeatureCollection fc = map.getGeoObject(); |
|
208 |
String fcName = fc.getSchema().getTypeName(); |
featureArray = FeatureUtil.featuresToArray(featureTable); |
209 |
FeatureSource fs = new MemoryDataStore(fc) |
if (featureArray == null || featureArray.length == 0) { |
210 |
.getFeatureSource(fcName); |
colNames = new String[0]; |
211 |
setFeatureSource(fs, map.getAttributeMetaDataMap(), filter); |
colTooltips = new String[0]; // Only set and used in |
212 |
} |
// StyledFeatureCollectionTableModel |
213 |
} catch (Exception err) { |
colClass = new Class[0]; |
214 |
throw new RuntimeException(err); |
} else { |
215 |
} |
// Struktur der Tabelle vom AttributeMetaDtaaMap übernehmen |
216 |
} |
SimpleFeatureType schema = featureArray[0].getFeatureType(); |
217 |
|
// Pruefen, welche Attribute angezeigt werden |
218 |
/** |
attrTypes.clear(); |
219 |
* Sets the {@code StyledFeatureCollection} as new data source for the |
for (AttributeMetadataInterface amd : amdMapVisibleOnly) { |
220 |
* table. |
Name name = amd.getName(); |
221 |
* |
AttributeDescriptor type = schema.getDescriptor(name); |
222 |
* @param fs |
// if type can not be determined by complete name, |
223 |
* the feature source |
// try only the local name |
224 |
* @param amd |
if (type == null) |
225 |
* {@link AttributeMetaData}-Map to define the visible attributes |
type = schema.getDescriptor(name.getLocalPart()); |
226 |
* and translation |
if (attrFilter == null || attrFilter.accept(type)) |
227 |
*/ |
attrTypes.add(type); |
228 |
public void setFeatureCollection(StyledFeatureSourceInterface map, |
} |
229 |
Filter filter) { |
// Namen und Attribut-Indizes der angezeigten Spalten ermitteln |
230 |
this.map = map; |
colNames = new String[attrTypes.size()]; |
231 |
try { |
colTooltips = new String[attrTypes.size()]; // Only set and used in |
232 |
if (map == null) |
// StyledFeatureCollectionTableModel |
233 |
setFeatureSource(null, null, null); |
colClass = new Class[attrTypes.size()]; |
234 |
else |
attrIdxForCol = new int[attrTypes.size()]; |
235 |
setFeatureSource(map.getGeoObject(), map |
for (int i = 0; i < colNames.length; i++) { |
236 |
.getAttributeMetaDataMap(), filter); |
Name name = amdMapVisibleOnly.get(i).getName(); |
237 |
} catch (Exception err) { |
AttributeDescriptor descriptor = schema.getDescriptor(name); |
238 |
throw new RuntimeException(err); |
// if type can not be determined by complete name, |
239 |
} |
// try only the local name |
240 |
} |
if (descriptor == null) |
241 |
|
descriptor = schema.getDescriptor(name.getLocalPart()); |
242 |
/** |
|
243 |
* Resets the filter for the table. |
// Not so nice in 26: find the index of an attribute... |
244 |
* |
int idx = schema.getAttributeDescriptors().indexOf(descriptor); |
245 |
* @param filter |
attrIdxForCol[i] = idx; |
246 |
* a filter |
|
247 |
*/ |
String attName = schema.getAttributeDescriptors().get(idx) |
248 |
public void setFilter(Filter filter) { |
.getLocalName(); |
249 |
try { |
colNames[i] = amdMap.get(attName).getTitle().toString(); |
250 |
setFeatureSource(this.featureSource, this.origAMD, filter); |
AttributeMetadataInterface amd = amdMap.get(attName); |
251 |
} catch (Exception err) { |
colTooltips[i] = "<html>" + amd.getDesc().toString() + "<br>" |
252 |
LOGGER.error("Setting the filter of the table model", err); |
+ amd.getName() + "</html>"; |
253 |
throw new RuntimeException(err); |
colClass[i] = schema.getAttributeDescriptors().get(idx) |
254 |
} |
.getType().getBinding(); |
255 |
} |
} |
256 |
|
} |
257 |
/** |
|
258 |
* @return <code>Filter.INCLUDE</code> or the {@link Filter} applied to the |
// store feature indexes in HashMap to optimize findFeature(.) |
259 |
* Features |
featureIdx = new HashMap<String, Integer>(); |
260 |
*/ |
for (int i = 0; i < featureArray.length; i++) |
261 |
public Filter getFilter() { |
if (featureArray[i] != null) |
262 |
return this.filter; |
featureIdx.put(featureArray[i].getID(), i); |
263 |
} |
// |
264 |
|
// // translate the column names |
265 |
/** |
// if (amdMap != null) { |
266 |
* After calling {@code super.reorganize(.)} this method replaced the column |
// for (int i = 0; i < colNames.length; i++) { |
267 |
* descriptions with the titles of the {@code AttributeMetaData}. |
// colTooltips[i] = amdMap.get(colNames[i]).getDesc().toString() |
268 |
* |
// + "<br>" + colNames[i]; |
269 |
* @param fireTableStructureChanged |
// colNames[i] = amdMap.get(colNames[i]).getTitle().toString(); |
270 |
* indicates whether a table event is initiated after reorganize |
// |
271 |
*/ |
// } |
272 |
@Override |
// } |
273 |
protected void reorganize(boolean fireTableStructureChanged) { |
if (fireTableStructureChanged) |
274 |
super.reorganize(false); |
fireTableStructureChanged(); |
275 |
// translate the column names |
|
276 |
if (visibleAMD != null) { |
} |
277 |
Iterator<Integer> keys = visibleAMD.keySet().iterator(); |
|
278 |
for (int i = 0; i < colNames.length && keys.hasNext(); i++) { |
/** |
279 |
Translation title = visibleAMD.get(keys.next()).getTitle(); |
* @return Cached bounds for the whole dataset (without applying the filter) |
280 |
if (!I8NUtil.isEmpty(title)) { |
* or <code>null</code> |
281 |
// System.out.println("set colname " + i + " to " + |
*/ |
282 |
// title.toString()); |
public Envelope getBounds() { |
283 |
colNames[i] = title.toString(); |
return bounds; |
284 |
} |
} |
285 |
} |
} |
|
} |
|
|
if (fireTableStructureChanged) |
|
|
fireTableStructureChanged(); |
|
|
} |
|
|
|
|
|
/** |
|
|
* @return Cached bounds for the whole dataset (without applying the filter) |
|
|
* or <code>null</code> |
|
|
*/ |
|
|
public Envelope getBounds() { |
|
|
return bounds; |
|
|
} |
|
|
} |
|