2 |
* Copyright (c) 2009 Martin O. J. Schmitz. |
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
* |
* |
4 |
* This file is part of the SCHMITZM library - a collection of utility |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
* classes based on Java 1.6, focussing (not only) on Java Swing |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
* and the Geotools library. |
* and the Geotools library. |
7 |
* |
* |
8 |
* The SCHMITZM project is hosted at: |
* The SCHMITZM project is hosted at: |
37 |
import org.geotools.data.DefaultQuery; |
import org.geotools.data.DefaultQuery; |
38 |
import org.geotools.data.FeatureSource; |
import org.geotools.data.FeatureSource; |
39 |
import org.geotools.data.Query; |
import org.geotools.data.Query; |
|
import org.geotools.data.memory.MemoryDataStore; |
|
|
import org.geotools.feature.AttributeType; |
|
40 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
41 |
|
import org.opengis.feature.simple.SimpleFeature; |
42 |
|
import org.opengis.feature.simple.SimpleFeatureType; |
43 |
|
import org.opengis.feature.type.AttributeDescriptor; |
44 |
import org.opengis.filter.Filter; |
import org.opengis.filter.Filter; |
45 |
|
|
46 |
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
52 |
|
|
53 |
/** |
/** |
54 |
* This class extends the the {@link FeatureCollectionTableModel} with the |
* This class extends the the {@link FeatureCollectionTableModel} with the |
55 |
* functionalities of the {@link AttributeMetaData} of |
* functionalities of the {@link AttributeMetaData}. |
|
* {@linkplain StyledLayerInterface styled objects}. |
|
56 |
* <ul> |
* <ul> |
57 |
* <li>column names are translated according to |
* <li>column names are translated according to |
58 |
* {@link AttributeMetaData#getTitle()}</li> |
* {@link AttributeMetaData#getTitle()}</li> |
59 |
* <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li> |
* <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li> |
60 |
|
* <li>Any filter defined in the {@link StyledFeaturesInterface} will be applied.</li> |
61 |
* </ul> |
* </ul> |
62 |
* |
* |
63 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
* @author Stefan A. Krüger |
|
* (University of Bonn/Germany) |
|
64 |
*/ |
*/ |
65 |
public class StyledFeatureCollectionTableModel extends |
public class StyledFeatureCollectionTableModel extends |
66 |
FeatureCollectionTableModel { |
FeatureCollectionTableModel { |
67 |
final static private Logger LOGGER = Logger |
final static private Logger LOGGER = Logger |
68 |
.getLogger(StyledFeatureCollectionTableModel.class); |
.getLogger(StyledFeatureCollectionTableModel.class); |
|
/** Holds the data source as styled layer. */ |
|
|
protected StyledLayerInterface<?> layer = null; |
|
69 |
/** Contains only the visible elements of the {@link AttributeMetaData}-Map */ |
/** Contains only the visible elements of the {@link AttributeMetaData}-Map */ |
70 |
protected Map<Integer, AttributeMetaData> visibleAMD = null; |
protected Map<Integer, AttributeMetaData> visibleAMD = null; |
71 |
/** Holds the data source for the table as {@code FeatureSource}. */ |
// /** Holds the data source for the table as {@code FeatureSource}. */ |
72 |
protected FeatureSource featureSource = null; |
// protected FeatureSource featureSource = null; |
73 |
/** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */ |
/** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */ |
74 |
protected Map<Integer, AttributeMetaData> origAMD = null; |
protected Map<Integer, AttributeMetaData> origAMD = null; |
75 |
/** Holds the current filter on the table */ |
/** Holds the current filter on the table */ |
78 |
protected Envelope bounds; |
protected Envelope bounds; |
79 |
|
|
80 |
|
|
|
|
|
|
/** |
|
|
* Creates a new table model for a styled layer. |
|
|
* |
|
|
* @param layer |
|
|
* the styled layer |
|
|
*/ |
|
|
public StyledFeatureCollectionTableModel( |
|
|
StyledFeatureCollectionInterface layer) { |
|
|
this(layer, Filter.INCLUDE); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Creates a new table model for a styled layer. |
|
|
* |
|
|
* @param layer |
|
|
* the styled layer |
|
|
* @param filter |
|
|
* filter applied to the table |
|
|
*/ |
|
|
public StyledFeatureCollectionTableModel( |
|
|
StyledFeatureCollectionInterface layer, Filter filter) { |
|
|
super(); |
|
|
setFeatureCollection(layer, filter); |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* Creates a new table model for a styled layer. |
|
|
* |
|
|
* @param layer |
|
|
* the styled layer |
|
|
*/ |
|
|
public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer) { |
|
|
this(layer, Filter.INCLUDE); |
|
|
} |
|
|
|
|
81 |
/** |
/** |
82 |
* Creates a new table model for a styled layer. |
* Creates a new table model for a styled layer. |
83 |
* |
* |
84 |
* @param layer |
* @param styledFeatures |
85 |
* the styled layer |
* the styled layer |
86 |
* @param filter |
* @param filter |
87 |
* filter applied to the table |
* filter applied to the table |
88 |
*/ |
*/ |
89 |
public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer, |
public StyledFeatureCollectionTableModel(StyledFeaturesInterface<?> styledFeatures) { |
90 |
Filter filter) { |
setStyledFeatures(styledFeatures); |
|
super(); |
|
|
setFeatureCollection(layer, filter); |
|
91 |
} |
} |
92 |
|
|
93 |
/** |
/** |
99 |
* {@link AttributeMetaData}-Map to define the visible attributes |
* {@link AttributeMetaData}-Map to define the visible attributes |
100 |
* and translation |
* and translation |
101 |
*/ |
*/ |
102 |
protected void setFeatureSource(FeatureSource fs, |
protected void setFeatureSource(FeatureSource<SimpleFeatureType, SimpleFeature> fs, |
103 |
Map<Integer, AttributeMetaData> amd, Filter filter) |
Map<Integer, AttributeMetaData> amd, Filter filter) |
104 |
throws Exception { |
throws Exception { |
105 |
|
|
106 |
if (filter == null) |
if (filter == null) |
107 |
filter = Filter.INCLUDE; |
filter = Filter.INCLUDE; |
108 |
|
|
109 |
this.featureSource = fs; |
// this.featureSource = fs; |
110 |
this.filter = filter; |
this.filter = filter; |
111 |
this.origAMD = amd; |
this.origAMD = amd; |
112 |
this.visibleAMD = null; |
this.visibleAMD = null; |
113 |
|
|
114 |
FeatureCollection fc = null; |
FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null; |
115 |
if (fs != null) { |
if (fs != null) { |
116 |
|
|
117 |
bounds = fs.getBounds(); |
bounds = fs.getBounds(); |
118 |
|
|
119 |
Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter); |
Query query = new DefaultQuery(fs.getSchema().getName().getLocalPart(), filter); |
120 |
if (amd != null) { |
if (amd != null) { |
121 |
// determine the names of the visible Attributes |
// determine the names of the visible Attributes |
122 |
this.visibleAMD = StyledLayerUtil.getVisibleAttributeMetaData( |
this.visibleAMD = StyledLayerUtil.getVisibleAttributeMetaData( |
123 |
amd, true); |
amd, true); |
124 |
Vector<String> visibleAttrNames = new Vector<String>(); |
Vector<String> visibleAttrNames = new Vector<String>(); |
125 |
// Add the column with the geometry (usually "the_geom") |
// Add the column with the geometry (usually "the_geom") |
126 |
visibleAttrNames.add(fs.getSchema().getDefaultGeometry() |
visibleAttrNames.add(fs.getSchema().getGeometryDescriptor() |
127 |
.getLocalName()); |
.getLocalName()); |
128 |
for (int attrIdx : visibleAMD.keySet()) { |
for (int attrIdx : visibleAMD.keySet()) { |
129 |
|
|
134 |
* ArrayOutOfIndex. |
* ArrayOutOfIndex. |
135 |
*/ |
*/ |
136 |
if (attrIdx < fs.getSchema().getAttributeCount()) { |
if (attrIdx < fs.getSchema().getAttributeCount()) { |
137 |
final AttributeType attributeTypeAtIdx = fs.getSchema() |
final AttributeDescriptor attributeTypeAtIdx = fs.getSchema() |
138 |
.getAttributeType(attrIdx); |
.getAttributeDescriptors().get(attrIdx); |
139 |
visibleAttrNames.add(attributeTypeAtIdx.getLocalName()); |
visibleAttrNames.add(attributeTypeAtIdx.getLocalName()); |
140 |
} else { |
} else { |
141 |
LOGGER.warn("AttributeMetaData has been found for columnIdx="+attrIdx+", but fs.getSchema().getAttributeCount() = "+fs.getSchema().getAttributeCount()+". Ignored."); |
LOGGER.warn("AttributeMetaData has been found for columnIdx="+attrIdx+", but fs.getSchema().getAttributeCount() = "+fs.getSchema().getAttributeCount()+". Ignored."); |
152 |
properties); |
properties); |
153 |
} |
} |
154 |
fc = fs.getFeatures(query); |
fc = fs.getFeatures(query); |
|
|
|
|
// FAILS:!!!, even with query = new |
|
|
// DefaultQuery(fs.getSchema().getTypeName(), filter); |
|
|
// java.lang.UnsupportedOperationException: Unknown feature |
|
|
// attribute: PQM_MOD |
|
|
// at |
|
|
// schmitzm.geotools.feature.FeatureOperationTree.evaluate(FeatureOperationTree.java:93) |
|
|
// bounds = fc.getBounds(); |
|
|
// SK, 17.4.2009 |
|
|
// |
|
|
// System.out.println("Filter = "+filter); |
|
|
// System.out.println("Size of FC = "+fc.size()); |
|
|
// System.out.println("anz att= "+fc.getNumberOfAttributes()); |
|
155 |
} |
} |
156 |
setFeatureCollection(fc); |
setFeatureCollection(fc); |
157 |
} |
} |
166 |
* {@link AttributeMetaData}-Map to define the visible attributes |
* {@link AttributeMetaData}-Map to define the visible attributes |
167 |
* and translation |
* and translation |
168 |
*/ |
*/ |
169 |
public void setFeatureCollection(StyledFeaturesInterface layer, |
public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) { |
|
Filter filter) { |
|
|
this.layer = layer; |
|
170 |
try { |
try { |
171 |
if (layer == null) |
if (styledFeatures == null) |
172 |
setFeatureSource(null, null, null); |
setFeatureSource(null, null, null); |
173 |
else { |
else { |
174 |
FeatureCollection fc = layer.getFeatureCollection(); |
setFeatureSource(styledFeatures.getFeatureSource(), styledFeatures.getAttributeMetaDataMap(), styledFeatures.getFilter()); |
|
String fcName = fc.getSchema().getTypeName(); |
|
|
FeatureSource fs = new MemoryDataStore(fc) |
|
|
.getFeatureSource(fcName); |
|
|
setFeatureSource(fs, layer.getAttributeMetaDataMap(), filter); |
|
175 |
} |
} |
176 |
} catch (Exception err) { |
} catch (Exception err) { |
177 |
throw new RuntimeException(err); |
throw new RuntimeException(err); |
179 |
} |
} |
180 |
|
|
181 |
/** |
/** |
|
* Sets the {@code StyledFeatureCollection} as new data source for the |
|
|
* table. |
|
|
* |
|
|
* @param fs |
|
|
* the feature source |
|
|
* @param amd |
|
|
* {@link AttributeMetaData}-Map to define the visible attributes |
|
|
* and translation |
|
|
*/ |
|
|
public void setFeatureCollection(StyledFeatureSourceInterface layer, |
|
|
Filter filter) { |
|
|
this.layer = layer; |
|
|
try { |
|
|
if (layer == null) |
|
|
setFeatureSource(null, null, null); |
|
|
else |
|
|
setFeatureSource(layer.getGeoObject(), layer |
|
|
.getAttributeMetaDataMap(), filter); |
|
|
} catch (Exception err) { |
|
|
throw new RuntimeException(err); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Resets the filter for the table. |
|
|
* |
|
|
* @param filter |
|
|
* a filter |
|
|
*/ |
|
|
public void setFilter(Filter filter) { |
|
|
try { |
|
|
setFeatureSource(this.featureSource, this.origAMD, filter); |
|
|
} catch (Exception err) { |
|
|
LOGGER.error("Setting the filter of the table model", err); |
|
|
throw new RuntimeException(err); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* @return <code>Filter.INCLUDE</code> or the {@link Filter} applied to the |
|
|
* Features |
|
|
*/ |
|
|
public Filter getFilter() { |
|
|
return this.filter; |
|
|
} |
|
|
|
|
|
/** |
|
182 |
* After calling {@code super.reorganize(.)} this method replaced the column |
* After calling {@code super.reorganize(.)} this method replaced the column |
183 |
* descriptions with the titles of the {@code AttributeMetaData}. |
* descriptions with the titles of the {@code AttributeMetaData}. |
184 |
* |
* |