1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
* |
4 |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
* and the Geotools library. |
7 |
* |
8 |
* The SCHMITZM project is hosted at: |
9 |
* http://wald.intevation.org/projects/schmitzm/ |
10 |
* |
11 |
* 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 |
* of the License, or (at your option) any later version. |
15 |
* |
16 |
* This program is distributed in the hope that it will be useful, |
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
* GNU General Public License for more details. |
20 |
* |
21 |
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
* along with this program; if not, write to the Free Software |
23 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
* |
26 |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
30 |
package skrueger.geotools; |
31 |
|
32 |
import java.util.HashMap; |
33 |
import java.util.LinkedHashSet; |
34 |
import java.util.List; |
35 |
|
36 |
import org.apache.log4j.Logger; |
37 |
import org.geotools.data.DefaultQuery; |
38 |
import org.geotools.data.FeatureSource; |
39 |
import org.geotools.data.Query; |
40 |
import org.geotools.feature.FeatureCollection; |
41 |
import org.opengis.feature.simple.SimpleFeature; |
42 |
import org.opengis.feature.simple.SimpleFeatureType; |
43 |
import org.opengis.feature.type.AttributeDescriptor; |
44 |
import org.opengis.feature.type.Name; |
45 |
import org.opengis.filter.Filter; |
46 |
|
47 |
import schmitzm.geotools.feature.FeatureUtil; |
48 |
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
49 |
import skrueger.AttributeMetadataImpl; |
50 |
import skrueger.AttributeMetadataInterface; |
51 |
|
52 |
import com.vividsolutions.jts.geom.Envelope; |
53 |
|
54 |
/** |
55 |
* This class extends the the {@link FeatureCollectionTableModel} with the |
56 |
* functionalities of the {@link AttributeMetadataImpl}. |
57 |
* <ul> |
58 |
* <li>column names are translated according to |
59 |
* {@link AttributeMetadataImpl#getTitle()}</li> |
60 |
* <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li> |
61 |
* <li>Any filter defined in the {@link StyledFeaturesInterface} will be |
62 |
* applied.</li> |
63 |
* </ul> |
64 |
* |
65 |
* @author Stefan A. Tzeggai |
66 |
*/ |
67 |
public class StyledFeatureCollectionTableModel extends |
68 |
FeatureCollectionTableModel { |
69 |
final static private Logger LOGGER = Logger |
70 |
.getLogger(StyledFeatureCollectionTableModel.class); |
71 |
/** Contains the complete {@link AttributeMetadataImpl}-Map of the styled layer. */ |
72 |
protected AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap = null; |
73 |
/** Holds the current filter on the table */ |
74 |
protected Filter filter = null; |
75 |
/** Holds the Bounds for all features. Only set once during the constructor **/ |
76 |
protected Envelope bounds; |
77 |
/** |
78 |
* Tooltips für die Spaltennamen. Wird nur beim Aufruf von |
79 |
* {@link #reorganize} befuellt. |
80 |
*/ |
81 |
protected String[] colTooltips = null; |
82 |
|
83 |
/** A cache for the #sortedValuesVisibleOnly() **/ |
84 |
protected List<? extends AttributeMetadataInterface> amdMapVisibleOnly = null; |
85 |
|
86 |
/** |
87 |
* Creates a new table model for a styled layer. |
88 |
* |
89 |
* @param styledFeatures |
90 |
* the styled layer |
91 |
* @param filter |
92 |
* filter applied to the table |
93 |
*/ |
94 |
public StyledFeatureCollectionTableModel( |
95 |
StyledFeaturesInterface<?> styledFeatures) { |
96 |
setStyledFeatures(styledFeatures); |
97 |
} |
98 |
|
99 |
/** |
100 |
* This overwritten method filters the values for NODATA-values defined in |
101 |
* the {@link AttributeMetadataImpl} |
102 |
*/ |
103 |
@Override |
104 |
public Object getValueAt(int row, int col) { |
105 |
Object rawValue = super.getValueAt(row, col); |
106 |
return amdMap.sortedValuesVisibleOnly().get(col).fiterNodata(rawValue); |
107 |
} |
108 |
|
109 |
/** |
110 |
* Sets a new data source for the table. |
111 |
* |
112 |
* @param fs |
113 |
* the feature source |
114 |
* @param amdm |
115 |
* {@link AttributeMetadataImpl}-Map to define the visible attributes |
116 |
* and translation |
117 |
*/ |
118 |
protected void setFeatureSource( |
119 |
FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
120 |
AttributeMetadataMap<? extends AttributeMetadataInterface> amdm, Filter filter) throws Exception { |
121 |
|
122 |
if (filter == null) |
123 |
filter = Filter.INCLUDE; |
124 |
|
125 |
// this.featureSource = fs; |
126 |
this.filter = filter; |
127 |
this.amdMap = amdm; |
128 |
this.amdMapVisibleOnly = amdMap.sortedValuesVisibleOnly(); |
129 |
|
130 |
FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null; |
131 |
if (fs != null) { |
132 |
|
133 |
bounds = fs.getBounds(); |
134 |
|
135 |
final SimpleFeatureType schema = fs.getSchema(); |
136 |
Query query = new DefaultQuery(schema.getTypeName(), filter); |
137 |
if (amdm != null) { |
138 |
LinkedHashSet<String> visibleAttrNames = new LinkedHashSet<String>(); |
139 |
|
140 |
// Add the column with the geometry (usually "the_geom") always |
141 |
visibleAttrNames.add(schema.getGeometryDescriptor() |
142 |
.getLocalName()); |
143 |
|
144 |
// Add other visible attributes as ordered by weights |
145 |
for (AttributeMetadataInterface a : amdMapVisibleOnly) { |
146 |
visibleAttrNames.add(a.getLocalName()); |
147 |
} |
148 |
|
149 |
// Tested with 2.6.x trunk from 2009-11-26 and it now works. So |
150 |
// we only request the properties we need! |
151 |
// /** |
152 |
// * I got NPEs when properties contained only [the_geom] |
153 |
// ?!??!!?? |
154 |
// */ |
155 |
// if (properties.length > 1) { |
156 |
query = new DefaultQuery(schema.getTypeName(), filter, |
157 |
visibleAttrNames.toArray(new String[] {})); |
158 |
// } else { |
159 |
// query = new DefaultQuery(schema.getTypeName(), filter); |
160 |
// } |
161 |
} |
162 |
fc = fs.getFeatures(query); |
163 |
} |
164 |
setFeatureCollection(fc); |
165 |
} |
166 |
|
167 |
/** |
168 |
* Converts the {@code StyledFeatureCollection} to a {@code FeatureSource} |
169 |
* and sets this as the new data source for the table. |
170 |
* |
171 |
* @param fs |
172 |
* the feature source |
173 |
* @param amd |
174 |
* {@link AttributeMetadataImpl}-Map to define the visible attributes |
175 |
* and translation |
176 |
*/ |
177 |
public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) { |
178 |
try { |
179 |
if (styledFeatures == null) |
180 |
setFeatureSource(null, null, null); |
181 |
else { |
182 |
setFeatureSource(styledFeatures.getFeatureSource(), |
183 |
styledFeatures.getAttributeMetaDataMap(), |
184 |
styledFeatures.getFilter()); |
185 |
} |
186 |
} catch (Exception err) { |
187 |
throw new RuntimeException(err); |
188 |
} |
189 |
} |
190 |
|
191 |
/** |
192 |
* After calling {@code super.reorganize(.)} this method replaced the column |
193 |
* descriptions with the titles of the {@code AttributeMetaData}. |
194 |
* |
195 |
* @param fireTableStructureChanged |
196 |
* indicates whether a table event is initiated after reorganize |
197 |
*/ |
198 |
@Override |
199 |
protected void reorganize(boolean fireTableStructureChanged) { |
200 |
|
201 |
featureArray = FeatureUtil.featuresToArray(featureTable); |
202 |
if (featureArray == null || featureArray.length == 0) { |
203 |
colNames = new String[0]; |
204 |
colTooltips = new String[0]; // Only set and used in |
205 |
// StyledFeatureCollectionTableModel |
206 |
colClass = new Class[0]; |
207 |
} else { |
208 |
// Struktur der Tabelle vom AttributeMetaDtaaMap übernehmen |
209 |
SimpleFeatureType schema = featureArray[0].getFeatureType(); |
210 |
// Pruefen, welche Attribute angezeigt werden |
211 |
attrTypes.clear(); |
212 |
for (AttributeMetadataInterface amd : amdMapVisibleOnly) { |
213 |
Name name = amd.getName(); |
214 |
AttributeDescriptor type = schema.getDescriptor(name); |
215 |
// if type can not be determined by complete name, |
216 |
// try only the local name |
217 |
if ( type == null ) |
218 |
type = schema.getDescriptor(name.getLocalPart()); |
219 |
if (attrFilter == null || attrFilter.accept(type)) |
220 |
attrTypes.add(type); |
221 |
} |
222 |
// Namen und Attribut-Indizes der angezeigten Spalten ermitteln |
223 |
colNames = new String[attrTypes.size()]; |
224 |
colTooltips = new String[attrTypes.size()]; // Only set and used in |
225 |
// StyledFeatureCollectionTableModel |
226 |
colClass = new Class[attrTypes.size()]; |
227 |
attrIdxForCol = new int[attrTypes.size()]; |
228 |
for (int i = 0; i < colNames.length; i++) { |
229 |
Name name = amdMapVisibleOnly.get(i).getName(); |
230 |
AttributeDescriptor descriptor = schema.getDescriptor(name); |
231 |
// if type can not be determined by complete name, |
232 |
// try only the local name |
233 |
if ( descriptor == null ) |
234 |
descriptor = schema.getDescriptor(name.getLocalPart()); |
235 |
|
236 |
// Not so nice in 26: find the index of an attribute... |
237 |
int idx = schema.getAttributeDescriptors().indexOf(descriptor); |
238 |
attrIdxForCol[i] = idx; |
239 |
|
240 |
String attName = schema.getAttributeDescriptors().get(idx) |
241 |
.getLocalName(); |
242 |
colNames[i] = amdMap.get(attName).getTitle().toString(); |
243 |
AttributeMetadataInterface amd = amdMap.get(attName); |
244 |
colTooltips[i] = "<html>" + amd.getDesc().toString() + "<br>" |
245 |
+ amd.getName() + "</html>"; |
246 |
colClass[i] = schema.getAttributeDescriptors().get(idx) |
247 |
.getType().getBinding(); |
248 |
} |
249 |
} |
250 |
|
251 |
// store feature indexes in HashMap to optimize findFeature(.) |
252 |
featureIdx = new HashMap<String, Integer>(); |
253 |
for (int i = 0; i < featureArray.length; i++) |
254 |
if (featureArray[i] != null) |
255 |
featureIdx.put(featureArray[i].getID(), i); |
256 |
// |
257 |
// // translate the column names |
258 |
// if (amdMap != null) { |
259 |
// for (int i = 0; i < colNames.length; i++) { |
260 |
// colTooltips[i] = amdMap.get(colNames[i]).getDesc().toString() |
261 |
// + "<br>" + colNames[i]; |
262 |
// colNames[i] = amdMap.get(colNames[i]).getTitle().toString(); |
263 |
// |
264 |
// } |
265 |
// } |
266 |
if (fireTableStructureChanged) |
267 |
fireTableStructureChanged(); |
268 |
|
269 |
} |
270 |
|
271 |
/** |
272 |
* @return Cached bounds for the whole dataset (without applying the filter) |
273 |
* or <code>null</code> |
274 |
*/ |
275 |
public Envelope getBounds() { |
276 |
return bounds; |
277 |
} |
278 |
} |