/[schmitzm]/branches/2.0-RC2/src/skrueger/geotools/StyledFeatureCollectionTableModel.java
ViewVC logotype

Annotation of /branches/2.0-RC2/src/skrueger/geotools/StyledFeatureCollectionTableModel.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 224 - (hide annotations)
Tue Jul 14 15:57:19 2009 UTC (15 years, 7 months ago) by alfonx
Original Path: trunk/src/skrueger/geotools/StyledFeatureCollectionTableModel.java
File size: 10620 byte(s)
* big biG BIG refactoring: StyledMap[Interface,Util,Style] never dealt with Maps (=Orchestrations of layers), but always only with single Layers. Now it has been refactored to StyledLayer[Interface,Util,Style]. 
* Some variable names and methodNames have been corrected also, but many will still be missing.
* New JARs committed
1 mojays 47 /** SCHMITZM - This file is part of the java library of Martin O.J. Schmitz (SCHMITZM)
2    
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.
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
6    
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.
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.
10     **/
11 mojays 46 package skrueger.geotools;
12    
13 mojays 52 import java.util.Iterator;
14     import java.util.Map;
15     import java.util.Vector;
16    
17 alfonx 111 import org.apache.log4j.Logger;
18 mojays 52 import org.geotools.data.DefaultQuery;
19     import org.geotools.data.FeatureSource;
20     import org.geotools.data.Query;
21     import org.geotools.data.memory.MemoryDataStore;
22 alfonx 168 import org.geotools.feature.AttributeType;
23 mojays 52 import org.geotools.feature.FeatureCollection;
24     import org.opengis.filter.Filter;
25    
26 mojays 46 import schmitzm.geotools.gui.FeatureCollectionTableModel;
27 mojays 52 import skrueger.AttributeMetaData;
28 mojays 55 import skrueger.i8n.I8NUtil;
29     import skrueger.i8n.Translation;
30 mojays 46
31 mojays 127 import com.vividsolutions.jts.geom.Envelope;
32    
33 mojays 52 /**
34 mojays 53 * This class extends the the {@link FeatureCollectionTableModel} with the
35 alfonx 56 * functionalities of the {@link AttributeMetaData} of
36 alfonx 224 * {@linkplain StyledLayerInterface styled objects}.
37 mojays 53 * <ul>
38 alfonx 168 * <li>column names are translated according to
39     * {@link AttributeMetaData#getTitle()}</li>
40     * <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>
41 alfonx 56 * </ul>
42 alfonx 168 *
43     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
44     * (University of Bonn/Germany)
45 mojays 52 */
46 alfonx 168 public class StyledFeatureCollectionTableModel extends
47     FeatureCollectionTableModel {
48     final static private Logger LOGGER = Logger
49     .getLogger(StyledFeatureCollectionTableModel.class);
50     /** Holds the data source as styled map. */
51 alfonx 224 protected StyledLayerInterface<?> layer = null;
52 alfonx 168 /** Contains only the visible elements of the {@link AttributeMetaData}-Map */
53     protected Map<Integer, AttributeMetaData> visibleAMD = null;
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. */
57     protected Map<Integer, AttributeMetaData> origAMD = null;
58     /** Holds the current filter on the table */
59     protected Filter filter = null;
60     /** Holds the Bounds for all features. Only set once during the constructor **/
61     protected Envelope bounds;
62 alfonx 56
63 alfonx 224
64    
65 alfonx 168 /**
66     * Creates a new table model for a styled map.
67     *
68 alfonx 224 * @param layer
69     * the styled layer
70 alfonx 168 */
71     public StyledFeatureCollectionTableModel(
72 alfonx 224 StyledFeatureCollectionInterface layer) {
73     this(layer, Filter.INCLUDE);
74 alfonx 168 }
75 alfonx 56
76 alfonx 168 /**
77     * Creates a new table model for a styled map.
78     *
79 alfonx 224 * @param layer
80     * the styled layer
81 alfonx 168 * @param filter
82     * filter applied to the table
83     */
84     public StyledFeatureCollectionTableModel(
85 alfonx 224 StyledFeatureCollectionInterface layer, Filter filter) {
86 alfonx 168 super();
87 alfonx 224 setFeatureCollection(layer, filter);
88 alfonx 168 }
89 alfonx 56
90 alfonx 224
91 alfonx 168 /**
92     * Creates a new table model for a styled map.
93     *
94 alfonx 224 * @param layer
95     * the styled layer
96 alfonx 168 */
97 alfonx 224 public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer) {
98     this(layer, Filter.INCLUDE);
99 alfonx 168 }
100 alfonx 56
101 alfonx 168 /**
102     * Creates a new table model for a styled map.
103     *
104 alfonx 224 * @param layer
105     * the styled layer
106 alfonx 168 * @param filter
107     * filter applied to the table
108     */
109 alfonx 224 public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer,
110 alfonx 168 Filter filter) {
111     super();
112 alfonx 224 setFeatureCollection(layer, filter);
113 alfonx 168 }
114 alfonx 56
115 alfonx 168 /**
116     * Sets a new data source for the table.
117     *
118     * @param fs
119     * the feature source
120     * @param amd
121     * {@link AttributeMetaData}-Map to define the visible attributes
122     * and translation
123     */
124     protected void setFeatureSource(FeatureSource fs,
125     Map<Integer, AttributeMetaData> amd, Filter filter)
126     throws Exception {
127     if (filter == null)
128     filter = Filter.INCLUDE;
129 alfonx 56
130 alfonx 168 this.featureSource = fs;
131     this.filter = filter;
132     this.origAMD = amd;
133     this.visibleAMD = null;
134 alfonx 62
135 alfonx 168 FeatureCollection fc = null;
136     if (fs != null) {
137    
138     bounds = fs.getBounds();
139    
140     Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter);
141     if (amd != null) {
142     // determine the names of the visible Attributes
143 alfonx 224 this.visibleAMD = StyledLayerUtil.getVisibleAttributeMetaData(
144 alfonx 168 amd, true);
145     Vector<String> visibleAttrNames = new Vector<String>();
146     // Add the column with the geometry (usually "the_geom")
147     visibleAttrNames.add(fs.getSchema().getDefaultGeometry()
148     .getLocalName());
149     for (int attrIdx : visibleAMD.keySet()) {
150    
151     /**
152     * If the user removed columns from the schema of the DBF
153     * file, there might exist AttributeMetaData for columns
154     * that don't exists. We check here to avoid an
155     * ArrayOutOfIndex.
156     */
157     if (attrIdx < fs.getSchema().getAttributeCount()) {
158     final AttributeType attributeTypeAtIdx = fs.getSchema()
159     .getAttributeType(attrIdx);
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.");
163     }
164     }
165    
166     // create a query for the visible attributes
167     String[] properties = visibleAttrNames.toArray(new String[0]);
168    
169     LOGGER.debug("Query contains the following attributes: "
170     + visibleAttrNames);
171    
172     query = new DefaultQuery(fs.getSchema().getTypeName(), filter,
173     properties);
174     }
175     fc = fs.getFeatures(query);
176    
177     // FAILS:!!!, even with query = new
178     // DefaultQuery(fs.getSchema().getTypeName(), filter);
179 alfonx 62 // java.lang.UnsupportedOperationException: Unknown feature
180     // attribute: PQM_MOD
181     // at
182     // schmitzm.geotools.feature.FeatureOperationTree.evaluate(FeatureOperationTree.java:93)
183     // bounds = fc.getBounds();
184     // SK, 17.4.2009
185     //
186     // System.out.println("Filter = "+filter);
187     // System.out.println("Size of FC = "+fc.size());
188     // System.out.println("anz att= "+fc.getNumberOfAttributes());
189 alfonx 168 }
190     setFeatureCollection(fc);
191     }
192 alfonx 56
193 alfonx 168 /**
194     * Converts the {@code StyledFeatureCollection} to a {@code FeatureSource}
195     * and sets this as the new data source for the table.
196     *
197     * @param fs
198     * the feature source
199     * @param amd
200     * {@link AttributeMetaData}-Map to define the visible attributes
201     * and translation
202     */
203 alfonx 224 public void setFeatureCollection(StyledFeaturesInterface layer,
204 alfonx 168 Filter filter) {
205 alfonx 224 this.layer = layer;
206 alfonx 168 try {
207 alfonx 224 if (layer == null)
208 alfonx 168 setFeatureSource(null, null, null);
209     else {
210 alfonx 224 FeatureCollection fc = layer.getFeatureCollection();
211 alfonx 168 String fcName = fc.getSchema().getTypeName();
212     FeatureSource fs = new MemoryDataStore(fc)
213     .getFeatureSource(fcName);
214 alfonx 224 setFeatureSource(fs, layer.getAttributeMetaDataMap(), filter);
215 alfonx 168 }
216     } catch (Exception err) {
217     throw new RuntimeException(err);
218     }
219     }
220 alfonx 56
221 alfonx 168 /**
222     * Sets the {@code StyledFeatureCollection} as new data source for the
223     * table.
224     *
225     * @param fs
226     * the feature source
227     * @param amd
228     * {@link AttributeMetaData}-Map to define the visible attributes
229     * and translation
230     */
231 alfonx 224 public void setFeatureCollection(StyledFeatureSourceInterface layer,
232 alfonx 168 Filter filter) {
233 alfonx 224 this.layer = layer;
234 alfonx 168 try {
235 alfonx 224 if (layer == null)
236 alfonx 168 setFeatureSource(null, null, null);
237     else
238 alfonx 224 setFeatureSource(layer.getGeoObject(), layer
239 alfonx 168 .getAttributeMetaDataMap(), filter);
240     } catch (Exception err) {
241     throw new RuntimeException(err);
242     }
243     }
244 alfonx 56
245 alfonx 168 /**
246     * Resets the filter for the table.
247     *
248     * @param filter
249     * a filter
250     */
251     public void setFilter(Filter filter) {
252     try {
253     setFeatureSource(this.featureSource, this.origAMD, filter);
254     } catch (Exception err) {
255     LOGGER.error("Setting the filter of the table model", err);
256     throw new RuntimeException(err);
257     }
258     }
259 alfonx 62
260 alfonx 168 /**
261     * @return <code>Filter.INCLUDE</code> or the {@link Filter} applied to the
262     * Features
263 alfonx 62 */
264 alfonx 168 public Filter getFilter() {
265     return this.filter;
266     }
267    
268     /**
269     * After calling {@code super.reorganize(.)} this method replaced the column
270     * descriptions with the titles of the {@code AttributeMetaData}.
271     *
272     * @param fireTableStructureChanged
273     * indicates whether a table event is initiated after reorganize
274     */
275     @Override
276     protected void reorganize(boolean fireTableStructureChanged) {
277     super.reorganize(false);
278     // translate the column names
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)) {
284     // System.out.println("set colname " + i + " to " +
285     // title.toString());
286     colNames[i] = title.toString();
287     }
288     }
289     }
290     if (fireTableStructureChanged)
291     fireTableStructureChanged();
292     }
293    
294     /**
295     * @return Cached bounds for the whole dataset (without applying the filter)
296     * or <code>null</code>
297     */
298 alfonx 62 public Envelope getBounds() {
299     return bounds;
300     }
301 mojays 46 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26