/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFeatureCollectionTableModel.java
ViewVC logotype

Annotation of /branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFeatureCollectionTableModel.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 58 - (hide annotations)
Fri Apr 17 15:55:33 2009 UTC (15 years, 10 months ago) by mojays
Original Path: trunk/src/skrueger/geotools/StyledFeatureCollectionTableModel.java
File size: 8366 byte(s)
StyledFeatureCollectionTableModel stable and documented
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 mojays 46 import schmitzm.geotools.gui.FeatureCollectionTableModel;
26 mojays 52 import skrueger.AttributeMetaData;
27 mojays 55 import skrueger.i8n.I8NUtil;
28     import skrueger.i8n.Translation;
29 mojays 46
30 mojays 52 /**
31 mojays 53 * This class extends the the {@link FeatureCollectionTableModel} with the
32 alfonx 56 * functionalities of the {@link AttributeMetaData} of
33     * {@linkplain StyledMapInterface styled objects}.
34 mojays 53 * <ul>
35 mojays 58 * <li>column names are translated according to {@link AttributeMetaData#getTitle()}</li>
36     * <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>
37 alfonx 56 * </ul>
38 mojays 58 * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)
39 mojays 52 */
40 mojays 58 public class StyledFeatureCollectionTableModel extends FeatureCollectionTableModel {
41     /** Holds the data source as styled map. */
42     protected StyledMapInterface map = null;
43     /** Contains only the visible elements of the {@link AttributeMetaData}-Map */
44 mojays 57 protected Map<Integer, AttributeMetaData> visibleAMD = null;
45 mojays 58 /** Holds the data source for the table as {@code FeatureSource}. */
46     protected FeatureSource featureSource = null;
47     /** Holds the current filter on the table */
48     protected Filter filter = null;
49 alfonx 56
50 mojays 58 /**
51     * Creates a new table model for a styled map.
52     * @param map the styled map
53     */
54 mojays 57 public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map) {
55     this(map,Filter.INCLUDE);
56     }
57 alfonx 56
58 mojays 58 /**
59     * Creates a new table model for a styled map.
60     * @param map the styled map
61     * @param filter filter applied to the table
62     */
63 mojays 57 public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map, Filter filter) {
64     super();
65     setFeatureCollection(map, filter);
66     }
67 alfonx 56
68 mojays 58 /**
69     * Creates a new table model for a styled map.
70     * @param map the styled map
71     */
72 mojays 57 public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) {
73     this(map,Filter.INCLUDE);
74     }
75 alfonx 56
76 mojays 58 /**
77     * Creates a new table model for a styled map.
78     * @param map the styled map
79     * @param filter filter applied to the table
80     */
81 mojays 57 public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map, Filter filter) {
82     super();
83     setFeatureCollection(map, filter);
84     }
85 alfonx 56
86 mojays 58 /**
87     * Sets a new data source for the table.
88     * @param fs the feature source
89     * @param amd {@link AttributeMetaData}-Map to define the visible attributes
90     * and translation
91     */
92 mojays 57 protected void setFeatureSource(FeatureSource fs, Map<Integer, AttributeMetaData> amd, Filter filter) throws Exception {
93     if ( filter == null )
94     filter = Filter.INCLUDE;
95 mojays 58
96     this.featureSource = fs;
97     this.filter = filter;
98     this.visibleAMD = null;
99 mojays 57
100     FeatureCollection fc = null;
101     if (fs != null) {
102     Query query = new DefaultQuery();
103     if (amd != null) {
104     // determine the names of the visible Attributes
105     this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(amd, true);
106     Vector<String> visibleAttrNames = new Vector<String>();
107     // Add the column with the geometry (usually "the_geom")
108     visibleAttrNames.add(fs.getSchema().getDefaultGeometry().getLocalName());
109     for (int attrIdx : visibleAMD.keySet())
110     visibleAttrNames.add(fs.getSchema().getAttributeType(attrIdx).getLocalName());
111 alfonx 56
112 mojays 57 // create a query for the visible attributes
113     String[] properties = visibleAttrNames.toArray(new String[0]);
114     query = new DefaultQuery(fs.getSchema().getTypeName(), filter, properties);
115     }
116     fc = fs.getFeatures(query);
117     }
118     setFeatureCollection(fc);
119     }
120 alfonx 56
121 mojays 58 /**
122     * Converts the {@code StyledFeatureCollection} to a {@code FeatureSource}
123     * and sets this as the new data source for the table.
124     * @param fs the feature source
125     * @param amd {@link AttributeMetaData}-Map to define the visible attributes
126     * and translation
127     */
128 mojays 57 public void setFeatureCollection(StyledFeatureCollectionInterface map, Filter filter) {
129 mojays 58 this.map = map;
130 mojays 57 try {
131     if (map == null)
132     setFeatureSource(null, null, null);
133     else {
134     FeatureCollection fc = map.getGeoObject();
135     String fcName = fc.getFeatureType().getTypeName();
136     FeatureSource fs = new MemoryDataStore(fc).getFeatureSource(fcName);
137     setFeatureSource(fs, map.getAttributeMetaDataMap(), filter);
138     }
139     } catch (Exception err) {
140     throw new RuntimeException(err);
141     }
142     }
143 alfonx 56
144 mojays 58 /**
145     * Sets the {@code StyledFeatureCollection} as new data source for the table.
146     * @param fs the feature source
147     * @param amd {@link AttributeMetaData}-Map to define the visible attributes
148     * and translation
149     */
150 mojays 57 public void setFeatureCollection(StyledFeatureSourceInterface map, Filter filter) {
151 mojays 58 this.map = map;
152 mojays 57 try {
153     if (map == null)
154     setFeatureSource(null, null, null);
155     else
156     setFeatureSource(map.getGeoObject(), map.getAttributeMetaDataMap(), filter);
157     } catch (Exception err) {
158     throw new RuntimeException(err);
159     }
160     }
161 mojays 58
162     /**
163     * Resets the filter for the table.
164     * @param filter a filter
165     */
166     public void setFilter(Filter filter) {
167     try{
168     setFeatureSource(this.featureSource, this.visibleAMD, filter);
169     } catch (Exception err) {
170     throw new RuntimeException(err);
171     }
172     }
173 alfonx 56
174 mojays 58 /**
175     * After calling {@code super.reorganize(.)} this method replaced the
176     * column descriptions with the titles of the {@code AttributeMetaData}.
177     * @param fireTableStructureChanged indicates whether a table event is
178     * initiated after reorganize
179     */
180 mojays 57 @Override
181 mojays 58 protected void reorganize(boolean fireTableStructureChanged) {
182     super.reorganize(false);
183 mojays 57 // translate the column names
184     if (visibleAMD != null) {
185     Iterator<Integer> keys = visibleAMD.keySet().iterator();
186     for (int i = 0; i < colNames.length && keys.hasNext(); i++) {
187     Translation title = visibleAMD.get(keys.next()).getTitle();
188     if (!I8NUtil.isEmpty(title)) {
189     System.out.println("set colname " + i + " to " + title.toString());
190     colNames[i] = title.toString();
191     }
192     }
193     }
194 mojays 58 if ( fireTableStructureChanged )
195     fireTableStructureChanged();
196 mojays 57 }
197 mojays 46 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26