/[schmitzm]/trunk/src/skrueger/geotools/StyledFeatureCollectionTableModel.java
ViewVC logotype

Annotation of /trunk/src/skrueger/geotools/StyledFeatureCollectionTableModel.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (hide annotations)
Fri Apr 17 18:56:17 2009 UTC (15 years, 10 months ago) by alfonx
File size: 9668 byte(s)
* Enabled the "zoom to layer bounds" for single selected points et al.
* Inserted a stub for getColumnCLass() in FeatureCollectionTableModel.java 

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.TreeMap;
16     import java.util.Vector;
17    
18     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     import org.geotools.feature.FeatureCollection;
23     import org.opengis.filter.Filter;
24    
25 alfonx 62 import com.vividsolutions.jts.geom.Envelope;
26    
27 mojays 46 import schmitzm.geotools.gui.FeatureCollectionTableModel;
28 mojays 52 import skrueger.AttributeMetaData;
29 mojays 55 import skrueger.i8n.I8NUtil;
30     import skrueger.i8n.Translation;
31 mojays 46
32 mojays 52 /**
33 mojays 53 * This class extends the the {@link FeatureCollectionTableModel} with the
34 alfonx 56 * functionalities of the {@link AttributeMetaData} of
35     * {@linkplain StyledMapInterface styled objects}.
36 mojays 53 * <ul>
37 mojays 58 * <li>column names are translated according to {@link AttributeMetaData#getTitle()}</li>
38     * <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>
39 alfonx 56 * </ul>
40 mojays 58 * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)
41 mojays 52 */
42 mojays 58 public class StyledFeatureCollectionTableModel extends FeatureCollectionTableModel {
43     /** Holds the data source as styled map. */
44     protected StyledMapInterface map = null;
45     /** Contains only the visible elements of the {@link AttributeMetaData}-Map */
46 mojays 57 protected Map<Integer, AttributeMetaData> visibleAMD = null;
47 mojays 58 /** Holds the data source for the table as {@code FeatureSource}. */
48     protected FeatureSource featureSource = null;
49 mojays 59 /** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */
50     protected Map<Integer, AttributeMetaData> origAMD = null;
51 mojays 58 /** Holds the current filter on the table */
52     protected Filter filter = null;
53 alfonx 62 /** Holds the Bounds for all features. Only set once during the constructor **/
54     protected Envelope bounds;
55 alfonx 56
56 mojays 58 /**
57     * Creates a new table model for a styled map.
58     * @param map the styled map
59     */
60 mojays 57 public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map) {
61     this(map,Filter.INCLUDE);
62     }
63 alfonx 56
64 mojays 58 /**
65     * Creates a new table model for a styled map.
66     * @param map the styled map
67     * @param filter filter applied to the table
68     */
69 mojays 57 public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map, Filter filter) {
70     super();
71     setFeatureCollection(map, filter);
72     }
73 alfonx 56
74 mojays 58 /**
75     * Creates a new table model for a styled map.
76     * @param map the styled map
77     */
78 mojays 57 public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) {
79     this(map,Filter.INCLUDE);
80     }
81 alfonx 56
82 mojays 58 /**
83     * Creates a new table model for a styled map.
84     * @param map the styled map
85     * @param filter filter applied to the table
86     */
87 mojays 57 public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map, Filter filter) {
88     super();
89     setFeatureCollection(map, filter);
90     }
91 alfonx 56
92 mojays 58 /**
93     * Sets a new data source for the table.
94     * @param fs the feature source
95     * @param amd {@link AttributeMetaData}-Map to define the visible attributes
96     * and translation
97     */
98 mojays 57 protected void setFeatureSource(FeatureSource fs, Map<Integer, AttributeMetaData> amd, Filter filter) throws Exception {
99     if ( filter == null )
100     filter = Filter.INCLUDE;
101 alfonx 62
102 mojays 58 this.featureSource = fs;
103     this.filter = filter;
104 mojays 59 this.origAMD = amd;
105 mojays 58 this.visibleAMD = null;
106 mojays 57
107     FeatureCollection fc = null;
108     if (fs != null) {
109 alfonx 62
110     bounds = fs.getBounds();
111    
112     Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter);
113 mojays 57 if (amd != null) {
114     // determine the names of the visible Attributes
115     this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(amd, true);
116     Vector<String> visibleAttrNames = new Vector<String>();
117     // Add the column with the geometry (usually "the_geom")
118     visibleAttrNames.add(fs.getSchema().getDefaultGeometry().getLocalName());
119     for (int attrIdx : visibleAMD.keySet())
120     visibleAttrNames.add(fs.getSchema().getAttributeType(attrIdx).getLocalName());
121 alfonx 56
122 mojays 57 // create a query for the visible attributes
123     String[] properties = visibleAttrNames.toArray(new String[0]);
124 alfonx 62
125 mojays 57 query = new DefaultQuery(fs.getSchema().getTypeName(), filter, properties);
126     }
127     fc = fs.getFeatures(query);
128 alfonx 62
129     // FAILS:!!!, even with query = new DefaultQuery(fs.getSchema().getTypeName(), filter);
130     // java.lang.UnsupportedOperationException: Unknown feature
131     // attribute: PQM_MOD
132     // at
133     // schmitzm.geotools.feature.FeatureOperationTree.evaluate(FeatureOperationTree.java:93)
134     // bounds = fc.getBounds();
135     // SK, 17.4.2009
136     //
137     // System.out.println("Filter = "+filter);
138     // System.out.println("Size of FC = "+fc.size());
139     // System.out.println("anz att= "+fc.getNumberOfAttributes());
140 mojays 57 }
141     setFeatureCollection(fc);
142     }
143 alfonx 56
144 mojays 58 /**
145     * Converts the {@code StyledFeatureCollection} to a {@code FeatureSource}
146     * and sets this as the new data source for the table.
147     * @param fs the feature source
148     * @param amd {@link AttributeMetaData}-Map to define the visible attributes
149     * and translation
150     */
151 mojays 57 public void setFeatureCollection(StyledFeatureCollectionInterface map, Filter filter) {
152 mojays 58 this.map = map;
153 mojays 57 try {
154     if (map == null)
155     setFeatureSource(null, null, null);
156     else {
157     FeatureCollection fc = map.getGeoObject();
158     String fcName = fc.getFeatureType().getTypeName();
159     FeatureSource fs = new MemoryDataStore(fc).getFeatureSource(fcName);
160     setFeatureSource(fs, map.getAttributeMetaDataMap(), filter);
161     }
162     } catch (Exception err) {
163     throw new RuntimeException(err);
164     }
165     }
166 alfonx 56
167 mojays 58 /**
168     * Sets the {@code StyledFeatureCollection} as new data source for the table.
169     * @param fs the feature source
170     * @param amd {@link AttributeMetaData}-Map to define the visible attributes
171     * and translation
172     */
173 mojays 57 public void setFeatureCollection(StyledFeatureSourceInterface map, Filter filter) {
174 mojays 58 this.map = map;
175 mojays 57 try {
176     if (map == null)
177     setFeatureSource(null, null, null);
178     else
179     setFeatureSource(map.getGeoObject(), map.getAttributeMetaDataMap(), filter);
180     } catch (Exception err) {
181     throw new RuntimeException(err);
182     }
183     }
184 mojays 58
185     /**
186     * Resets the filter for the table.
187     * @param filter a filter
188     */
189     public void setFilter(Filter filter) {
190     try{
191 mojays 59 setFeatureSource(this.featureSource, this.origAMD, filter);
192 mojays 58 } catch (Exception err) {
193     throw new RuntimeException(err);
194     }
195     }
196 alfonx 62
197     /**
198     * @return <code>Filter.INCLUDE</code> or the {@link Filter} applied to the Features
199     */
200     public Filter getFilter() {
201     return this.filter;
202     }
203 alfonx 56
204 mojays 58 /**
205     * After calling {@code super.reorganize(.)} this method replaced the
206     * column descriptions with the titles of the {@code AttributeMetaData}.
207     * @param fireTableStructureChanged indicates whether a table event is
208     * initiated after reorganize
209     */
210 mojays 57 @Override
211 mojays 58 protected void reorganize(boolean fireTableStructureChanged) {
212     super.reorganize(false);
213 mojays 57 // translate the column names
214     if (visibleAMD != null) {
215     Iterator<Integer> keys = visibleAMD.keySet().iterator();
216     for (int i = 0; i < colNames.length && keys.hasNext(); i++) {
217     Translation title = visibleAMD.get(keys.next()).getTitle();
218     if (!I8NUtil.isEmpty(title)) {
219 alfonx 62 // System.out.println("set colname " + i + " to " + title.toString());
220 mojays 57 colNames[i] = title.toString();
221     }
222     }
223     }
224 mojays 58 if ( fireTableStructureChanged )
225     fireTableStructureChanged();
226 mojays 57 }
227 alfonx 62
228     /**
229     * @returns Cached bounds for the whole dataset (without applying the
230     * filter) or <code>null</code>
231     */
232     public Envelope getBounds() {
233     return bounds;
234     }
235 mojays 46 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26