/[schmitzm]/branches/2.3.x/src/skrueger/geotools/StyledFeatureCollectionTableModel.java
ViewVC logotype

Annotation of /branches/2.3.x/src/skrueger/geotools/StyledFeatureCollectionTableModel.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 59 - (hide annotations)
Fri Apr 17 17:26:58 2009 UTC (15 years, 10 months ago) by mojays
Original Path: trunk/src/skrueger/geotools/StyledFeatureCollectionTableModel.java
File size: 8538 byte(s)


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 mojays 59 /** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */
48     protected Map<Integer, AttributeMetaData> origAMD = null;
49 mojays 58 /** Holds the current filter on the table */
50     protected Filter filter = null;
51 alfonx 56
52 mojays 58 /**
53     * Creates a new table model for a styled map.
54     * @param map the styled map
55     */
56 mojays 57 public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map) {
57     this(map,Filter.INCLUDE);
58     }
59 alfonx 56
60 mojays 58 /**
61     * Creates a new table model for a styled map.
62     * @param map the styled map
63     * @param filter filter applied to the table
64     */
65 mojays 57 public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map, Filter filter) {
66     super();
67     setFeatureCollection(map, filter);
68     }
69 alfonx 56
70 mojays 58 /**
71     * Creates a new table model for a styled map.
72     * @param map the styled map
73     */
74 mojays 57 public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) {
75     this(map,Filter.INCLUDE);
76     }
77 alfonx 56
78 mojays 58 /**
79     * Creates a new table model for a styled map.
80     * @param map the styled map
81     * @param filter filter applied to the table
82     */
83 mojays 57 public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map, Filter filter) {
84     super();
85     setFeatureCollection(map, filter);
86     }
87 alfonx 56
88 mojays 58 /**
89     * Sets a new data source for the table.
90     * @param fs the feature source
91     * @param amd {@link AttributeMetaData}-Map to define the visible attributes
92     * and translation
93     */
94 mojays 57 protected void setFeatureSource(FeatureSource fs, Map<Integer, AttributeMetaData> amd, Filter filter) throws Exception {
95     if ( filter == null )
96     filter = Filter.INCLUDE;
97 mojays 58
98     this.featureSource = fs;
99     this.filter = filter;
100 mojays 59 this.origAMD = amd;
101 mojays 58 this.visibleAMD = null;
102 mojays 57
103     FeatureCollection fc = null;
104     if (fs != null) {
105     Query query = new DefaultQuery();
106     if (amd != null) {
107     // determine the names of the visible Attributes
108     this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(amd, true);
109     Vector<String> visibleAttrNames = new Vector<String>();
110     // Add the column with the geometry (usually "the_geom")
111     visibleAttrNames.add(fs.getSchema().getDefaultGeometry().getLocalName());
112     for (int attrIdx : visibleAMD.keySet())
113     visibleAttrNames.add(fs.getSchema().getAttributeType(attrIdx).getLocalName());
114 alfonx 56
115 mojays 57 // create a query for the visible attributes
116     String[] properties = visibleAttrNames.toArray(new String[0]);
117     query = new DefaultQuery(fs.getSchema().getTypeName(), filter, properties);
118     }
119     fc = fs.getFeatures(query);
120     }
121     setFeatureCollection(fc);
122     }
123 alfonx 56
124 mojays 58 /**
125     * Converts the {@code StyledFeatureCollection} to a {@code FeatureSource}
126     * and sets this as the new data source for the table.
127     * @param fs the feature source
128     * @param amd {@link AttributeMetaData}-Map to define the visible attributes
129     * and translation
130     */
131 mojays 57 public void setFeatureCollection(StyledFeatureCollectionInterface map, Filter filter) {
132 mojays 58 this.map = map;
133 mojays 57 try {
134     if (map == null)
135     setFeatureSource(null, null, null);
136     else {
137     FeatureCollection fc = map.getGeoObject();
138     String fcName = fc.getFeatureType().getTypeName();
139     FeatureSource fs = new MemoryDataStore(fc).getFeatureSource(fcName);
140     setFeatureSource(fs, map.getAttributeMetaDataMap(), filter);
141     }
142     } catch (Exception err) {
143     throw new RuntimeException(err);
144     }
145     }
146 alfonx 56
147 mojays 58 /**
148     * Sets the {@code StyledFeatureCollection} as new data source for the table.
149     * @param fs the feature source
150     * @param amd {@link AttributeMetaData}-Map to define the visible attributes
151     * and translation
152     */
153 mojays 57 public void setFeatureCollection(StyledFeatureSourceInterface map, Filter filter) {
154 mojays 58 this.map = map;
155 mojays 57 try {
156     if (map == null)
157     setFeatureSource(null, null, null);
158     else
159     setFeatureSource(map.getGeoObject(), map.getAttributeMetaDataMap(), filter);
160     } catch (Exception err) {
161     throw new RuntimeException(err);
162     }
163     }
164 mojays 58
165     /**
166     * Resets the filter for the table.
167     * @param filter a filter
168     */
169     public void setFilter(Filter filter) {
170     try{
171 mojays 59 setFeatureSource(this.featureSource, this.origAMD, filter);
172 mojays 58 } catch (Exception err) {
173     throw new RuntimeException(err);
174     }
175     }
176 alfonx 56
177 mojays 58 /**
178     * After calling {@code super.reorganize(.)} this method replaced the
179     * column descriptions with the titles of the {@code AttributeMetaData}.
180     * @param fireTableStructureChanged indicates whether a table event is
181     * initiated after reorganize
182     */
183 mojays 57 @Override
184 mojays 58 protected void reorganize(boolean fireTableStructureChanged) {
185     super.reorganize(false);
186 mojays 57 // translate the column names
187     if (visibleAMD != null) {
188     Iterator<Integer> keys = visibleAMD.keySet().iterator();
189     for (int i = 0; i < colNames.length && keys.hasNext(); i++) {
190     Translation title = visibleAMD.get(keys.next()).getTitle();
191     if (!I8NUtil.isEmpty(title)) {
192     System.out.println("set colname " + i + " to " + title.toString());
193     colNames[i] = title.toString();
194     }
195     }
196     }
197 mojays 58 if ( fireTableStructureChanged )
198     fireTableStructureChanged();
199 mojays 57 }
200 mojays 46 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26