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