1 |
/** 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 |
package skrueger.geotools; |
12 |
|
13 |
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 |
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
26 |
import skrueger.AttributeMetaData; |
27 |
import skrueger.i8n.I8NUtil; |
28 |
import skrueger.i8n.Translation; |
29 |
|
30 |
/** |
31 |
* This class extends the the {@link FeatureCollectionTableModel} with the |
32 |
* functionalities of the {@link AttributeMetaData} of |
33 |
* {@linkplain StyledMapInterface styled objects}. |
34 |
* <ul> |
35 |
* <li>column names are translated according to {@link AttributeMetaData#getTitle()}</li> |
36 |
* <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li> |
37 |
* </ul> |
38 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany) |
39 |
*/ |
40 |
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 |
protected Map<Integer, AttributeMetaData> visibleAMD = null; |
45 |
/** Holds the data source for the table as {@code FeatureSource}. */ |
46 |
protected FeatureSource featureSource = null; |
47 |
/** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */ |
48 |
protected Map<Integer, AttributeMetaData> origAMD = null; |
49 |
/** Holds the current filter on the table */ |
50 |
protected Filter filter = null; |
51 |
|
52 |
/** |
53 |
* Creates a new table model for a styled map. |
54 |
* @param map the styled map |
55 |
*/ |
56 |
public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map) { |
57 |
this(map,Filter.INCLUDE); |
58 |
} |
59 |
|
60 |
/** |
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 |
public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map, Filter filter) { |
66 |
super(); |
67 |
setFeatureCollection(map, filter); |
68 |
} |
69 |
|
70 |
/** |
71 |
* Creates a new table model for a styled map. |
72 |
* @param map the styled map |
73 |
*/ |
74 |
public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) { |
75 |
this(map,Filter.INCLUDE); |
76 |
} |
77 |
|
78 |
/** |
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 |
public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map, Filter filter) { |
84 |
super(); |
85 |
setFeatureCollection(map, filter); |
86 |
} |
87 |
|
88 |
/** |
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 |
protected void setFeatureSource(FeatureSource fs, Map<Integer, AttributeMetaData> amd, Filter filter) throws Exception { |
95 |
if ( filter == null ) |
96 |
filter = Filter.INCLUDE; |
97 |
|
98 |
this.featureSource = fs; |
99 |
this.filter = filter; |
100 |
this.origAMD = amd; |
101 |
this.visibleAMD = null; |
102 |
|
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 |
|
115 |
// 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 |
|
124 |
/** |
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 |
public void setFeatureCollection(StyledFeatureCollectionInterface map, Filter filter) { |
132 |
this.map = map; |
133 |
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 |
|
147 |
/** |
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 |
public void setFeatureCollection(StyledFeatureSourceInterface map, Filter filter) { |
154 |
this.map = map; |
155 |
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 |
|
165 |
/** |
166 |
* Resets the filter for the table. |
167 |
* @param filter a filter |
168 |
*/ |
169 |
public void setFilter(Filter filter) { |
170 |
try{ |
171 |
setFeatureSource(this.featureSource, this.origAMD, filter); |
172 |
} catch (Exception err) { |
173 |
throw new RuntimeException(err); |
174 |
} |
175 |
} |
176 |
|
177 |
/** |
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 |
@Override |
184 |
protected void reorganize(boolean fireTableStructureChanged) { |
185 |
super.reorganize(false); |
186 |
// 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 |
if ( fireTableStructureChanged ) |
198 |
fireTableStructureChanged(); |
199 |
} |
200 |
} |