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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 59 by mojays, Fri Apr 17 17:26:58 2009 UTC revision 225 by alfonx, Wed Jul 15 08:05:22 2009 UTC
# Line 12  package skrueger.geotools; Line 12  package skrueger.geotools;
12    
13  import java.util.Iterator;  import java.util.Iterator;
14  import java.util.Map;  import java.util.Map;
 import java.util.TreeMap;  
15  import java.util.Vector;  import java.util.Vector;
16    
17    import org.apache.log4j.Logger;
18  import org.geotools.data.DefaultQuery;  import org.geotools.data.DefaultQuery;
19  import org.geotools.data.FeatureSource;  import org.geotools.data.FeatureSource;
20  import org.geotools.data.Query;  import org.geotools.data.Query;
21  import org.geotools.data.memory.MemoryDataStore;  import org.geotools.data.memory.MemoryDataStore;
22    import org.geotools.feature.AttributeType;
23  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
24  import org.opengis.filter.Filter;  import org.opengis.filter.Filter;
25    
# Line 27  import skrueger.AttributeMetaData; Line 28  import skrueger.AttributeMetaData;
28  import skrueger.i8n.I8NUtil;  import skrueger.i8n.I8NUtil;
29  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
30    
31    import com.vividsolutions.jts.geom.Envelope;
32    
33  /**  /**
34   * This class extends the the {@link FeatureCollectionTableModel} with the   * This class extends the the {@link FeatureCollectionTableModel} with the
35   * functionalities of the {@link AttributeMetaData} of   * functionalities of the {@link AttributeMetaData} of
36   * {@linkplain StyledMapInterface styled objects}.   * {@linkplain StyledLayerInterface styled objects}.
37   * <ul>   * <ul>
38   *   <li>column names are translated according to {@link AttributeMetaData#getTitle()}</li>   * <li>column names are translated according to
39   *   <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>   * {@link AttributeMetaData#getTitle()}</li>
40     * <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>
41   * </ul>   * </ul>
42   * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)   *
43     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
44     *         (University of Bonn/Germany)
45   */   */
46  public class StyledFeatureCollectionTableModel extends FeatureCollectionTableModel {  public class StyledFeatureCollectionTableModel extends
47    /** Holds the data source as styled map. */                  FeatureCollectionTableModel {
48    protected StyledMapInterface map = null;          final static private Logger LOGGER = Logger
49    /** Contains only the visible elements of the {@link AttributeMetaData}-Map */                          .getLogger(StyledFeatureCollectionTableModel.class);
50    protected Map<Integer, AttributeMetaData> visibleAMD = null;          /** Holds the data source as styled layer. */
51    /** Holds the data source for the table as {@code FeatureSource}. */          protected StyledLayerInterface<?> layer = null;
52    protected FeatureSource featureSource = null;          /** Contains only the visible elements of the {@link AttributeMetaData}-Map */
53    /** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */          protected Map<Integer, AttributeMetaData> visibleAMD = null;
54    protected Map<Integer, AttributeMetaData> origAMD = null;          /** Holds the data source for the table as {@code FeatureSource}. */
55    /** Holds the current filter on the table */          protected FeatureSource featureSource = null;
56    protected Filter filter = null;          /** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */
57            protected Map<Integer, AttributeMetaData> origAMD = null;
58    /**          /** Holds the current filter on the table */
59     * Creates a new table model for a styled map.          protected Filter filter = null;
60     * @param map the styled map          /** Holds the Bounds for all features. Only set once during the constructor **/
61     */          protected Envelope bounds;
62    public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map) {  
63      this(map,Filter.INCLUDE);          
64    }  
65            /**
66    /**           * Creates a new table model for a styled layer.
67     * Creates a new table model for a styled map.           *
68     * @param map the styled map           * @param layer
69     * @param filter filter applied to the table           *            the styled layer
70     */           */
71    public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map, Filter filter) {          public StyledFeatureCollectionTableModel(
72      super();                          StyledFeatureCollectionInterface layer) {
73      setFeatureCollection(map, filter);                  this(layer, Filter.INCLUDE);
74    }          }
75    
76    /**          /**
77     * Creates a new table model for a styled map.           * Creates a new table model for a styled layer.
78     * @param map the styled map           *
79     */           * @param layer
80    public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) {           *            the styled layer
81      this(map,Filter.INCLUDE);           * @param filter
82    }           *            filter applied to the table
83             */
84    /**          public StyledFeatureCollectionTableModel(
85     * Creates a new table model for a styled map.                          StyledFeatureCollectionInterface layer, Filter filter) {
86     * @param map the styled map                  super();
87     * @param filter filter applied to the table                  setFeatureCollection(layer, filter);
88     */          }
89    public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map, Filter filter) {  
90      super();  
91      setFeatureCollection(map, filter);          /**
92    }           * Creates a new table model for a styled layer.
93             *
94    /**           * @param layer
95     * Sets a new data source for the table.           *            the styled layer
96     * @param fs     the feature source           */
97     * @param amd    {@link AttributeMetaData}-Map to define the visible attributes          public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer) {
98     *               and translation                  this(layer, Filter.INCLUDE);
99     */          }
100    protected void setFeatureSource(FeatureSource fs, Map<Integer, AttributeMetaData> amd, Filter filter) throws Exception {  
101      if ( filter == null )          /**
102        filter = Filter.INCLUDE;           * Creates a new table model for a styled layer.
103             *
104      this.featureSource = fs;           * @param layer
105      this.filter        = filter;           *            the styled layer
106      this.origAMD       = amd;           * @param filter
107      this.visibleAMD    = null;           *            filter applied to the table
108                 */
109      FeatureCollection fc = null;          public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer,
110      if (fs != null) {                          Filter filter) {
111        Query query = new DefaultQuery();                  super();
112        if (amd != null) {                  setFeatureCollection(layer, filter);
113          // determine the names of the visible Attributes          }
114          this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(amd, true);  
115          Vector<String> visibleAttrNames = new Vector<String>();          /**
116          // Add the column with the geometry (usually "the_geom")           * Sets a new data source for the table.
117          visibleAttrNames.add(fs.getSchema().getDefaultGeometry().getLocalName());           *
118          for (int attrIdx : visibleAMD.keySet())           * @param fs
119            visibleAttrNames.add(fs.getSchema().getAttributeType(attrIdx).getLocalName());           *            the feature source
120             * @param amd
121          // create a query for the visible attributes           *            {@link AttributeMetaData}-Map to define the visible attributes
122          String[] properties = visibleAttrNames.toArray(new String[0]);           *            and translation
123          query = new DefaultQuery(fs.getSchema().getTypeName(), filter, properties);           */
124        }          protected void setFeatureSource(FeatureSource fs,
125        fc = fs.getFeatures(query);                          Map<Integer, AttributeMetaData> amd, Filter filter)
126      }                          throws Exception {
127      setFeatureCollection(fc);                  if (filter == null)
128    }                          filter = Filter.INCLUDE;
129    
130    /**                  this.featureSource = fs;
131     * Converts the {@code StyledFeatureCollection} to a {@code FeatureSource}                  this.filter = filter;
132     * and sets this as the new data source for the table.                  this.origAMD = amd;
133     * @param fs     the feature source                  this.visibleAMD = null;
134     * @param amd    {@link AttributeMetaData}-Map to define the visible attributes  
135     *               and translation                  FeatureCollection fc = null;
136     */                  if (fs != null) {
137    public void setFeatureCollection(StyledFeatureCollectionInterface map, Filter filter) {  
138      this.map = map;                          bounds = fs.getBounds();
139      try {  
140        if (map == null)                          Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter);
141          setFeatureSource(null, null, null);                          if (amd != null) {
142        else {                                  // determine the names of the visible Attributes
143          FeatureCollection fc = map.getGeoObject();                                  this.visibleAMD = StyledLayerUtil.getVisibleAttributeMetaData(
144          String fcName = fc.getFeatureType().getTypeName();                                                  amd, true);
145          FeatureSource fs = new MemoryDataStore(fc).getFeatureSource(fcName);                                  Vector<String> visibleAttrNames = new Vector<String>();
146          setFeatureSource(fs, map.getAttributeMetaDataMap(), filter);                                  // Add the column with the geometry (usually "the_geom")
147        }                                  visibleAttrNames.add(fs.getSchema().getDefaultGeometry()
148      } catch (Exception err) {                                                  .getLocalName());
149        throw new RuntimeException(err);                                  for (int attrIdx : visibleAMD.keySet()) {
150      }  
151    }                                          /**
152                                             * If the user removed columns from the schema of the DBF
153    /**                                           * file, there might exist AttributeMetaData for columns
154     * Sets the {@code StyledFeatureCollection} as new data source for the table.                                           * that don't exists. We check here to avoid an
155     * @param fs     the feature source                                           * ArrayOutOfIndex.
156     * @param amd    {@link AttributeMetaData}-Map to define the visible attributes                                           */
157     *               and translation                                          if (attrIdx < fs.getSchema().getAttributeCount()) {
158     */                                                  final AttributeType attributeTypeAtIdx = fs.getSchema()
159    public void setFeatureCollection(StyledFeatureSourceInterface map, Filter filter) {                                                                  .getAttributeType(attrIdx);
160      this.map = map;                                                  visibleAttrNames.add(attributeTypeAtIdx.getLocalName());
161      try {                                          } else {
162        if (map == null)                                                  LOGGER.warn("AttributeMetaData has been found for columnIdx="+attrIdx+", but fs.getSchema().getAttributeCount() = "+fs.getSchema().getAttributeCount()+". Ignored.");
163          setFeatureSource(null, null, null);                                          }
164        else                                  }
165          setFeatureSource(map.getGeoObject(), map.getAttributeMetaDataMap(), filter);  
166      } catch (Exception err) {                                  // create a query for the visible attributes
167        throw new RuntimeException(err);                                  String[] properties = visibleAttrNames.toArray(new String[0]);
168      }  
169    }                                  LOGGER.debug("Query contains the following attributes: "
170                                                      + visibleAttrNames);
171    /**  
172     * Resets the filter for the table.                                  query = new DefaultQuery(fs.getSchema().getTypeName(), filter,
173     * @param filter a filter                                                  properties);
174     */                          }
175    public void setFilter(Filter filter) {                          fc = fs.getFeatures(query);
176      try{  
177        setFeatureSource(this.featureSource, this.origAMD, filter);                          // FAILS:!!!, even with query = new
178      } catch (Exception err) {                          // DefaultQuery(fs.getSchema().getTypeName(), filter);
179        throw new RuntimeException(err);                          // java.lang.UnsupportedOperationException: Unknown feature
180      }                          // attribute: PQM_MOD
181    }                          // at
182                            // schmitzm.geotools.feature.FeatureOperationTree.evaluate(FeatureOperationTree.java:93)
183    /**                          // bounds = fc.getBounds();
184     * After calling {@code super.reorganize(.)} this method replaced the                          // SK, 17.4.2009
185     * column descriptions with the titles of the {@code AttributeMetaData}.                          //      
186     * @param fireTableStructureChanged indicates whether a table event is                          // System.out.println("Filter = "+filter);
187     *        initiated after reorganize                          // System.out.println("Size of FC = "+fc.size());
188     */                          // System.out.println("anz att= "+fc.getNumberOfAttributes());
189    @Override                  }
190    protected void reorganize(boolean fireTableStructureChanged) {                  setFeatureCollection(fc);
191      super.reorganize(false);          }
192      // translate the column names  
193      if (visibleAMD != null) {          /**
194        Iterator<Integer> keys = visibleAMD.keySet().iterator();           * Converts the {@code StyledFeatureCollection} to a {@code FeatureSource}
195        for (int i = 0; i < colNames.length && keys.hasNext(); i++) {           * and sets this as the new data source for the table.
196          Translation title = visibleAMD.get(keys.next()).getTitle();           *
197          if (!I8NUtil.isEmpty(title)) {           * @param fs
198            System.out.println("set colname " + i + " to " + title.toString());           *            the feature source
199            colNames[i] = title.toString();           * @param amd
200          }           *            {@link AttributeMetaData}-Map to define the visible attributes
201        }           *            and translation
202      }           */
203      if ( fireTableStructureChanged )          public void setFeatureCollection(StyledFeaturesInterface layer,
204        fireTableStructureChanged();                          Filter filter) {
205    }                  this.layer = layer;
206                    try {
207                            if (layer == null)
208                                    setFeatureSource(null, null, null);
209                            else {
210                                    FeatureCollection fc = layer.getFeatureCollection();
211                                    String fcName = fc.getSchema().getTypeName();
212                                    FeatureSource fs = new MemoryDataStore(fc)
213                                                    .getFeatureSource(fcName);
214                                    setFeatureSource(fs, layer.getAttributeMetaDataMap(), filter);
215                            }
216                    } catch (Exception err) {
217                            throw new RuntimeException(err);
218                    }
219            }
220    
221            /**
222             * Sets the {@code StyledFeatureCollection} as new data source for the
223             * table.
224             *
225             * @param fs
226             *            the feature source
227             * @param amd
228             *            {@link AttributeMetaData}-Map to define the visible attributes
229             *            and translation
230             */
231            public void setFeatureCollection(StyledFeatureSourceInterface layer,
232                            Filter filter) {
233                    this.layer = layer;
234                    try {
235                            if (layer == null)
236                                    setFeatureSource(null, null, null);
237                            else
238                                    setFeatureSource(layer.getGeoObject(), layer
239                                                    .getAttributeMetaDataMap(), filter);
240                    } catch (Exception err) {
241                            throw new RuntimeException(err);
242                    }
243            }
244    
245            /**
246             * Resets the filter for the table.
247             *
248             * @param filter
249             *            a filter
250             */
251            public void setFilter(Filter filter) {
252                    try {
253                            setFeatureSource(this.featureSource, this.origAMD, filter);
254                    } catch (Exception err) {
255                            LOGGER.error("Setting the filter of the table model", err);
256                            throw new RuntimeException(err);
257                    }
258            }
259    
260            /**
261             * @return <code>Filter.INCLUDE</code> or the {@link Filter} applied to the
262             *         Features
263             */
264            public Filter getFilter() {
265                    return this.filter;
266            }
267    
268            /**
269             * After calling {@code super.reorganize(.)} this method replaced the column
270             * descriptions with the titles of the {@code AttributeMetaData}.
271             *
272             * @param fireTableStructureChanged
273             *            indicates whether a table event is initiated after reorganize
274             */
275            @Override
276            protected void reorganize(boolean fireTableStructureChanged) {
277                    super.reorganize(false);
278                    // translate the column names
279                    if (visibleAMD != null) {
280                            Iterator<Integer> keys = visibleAMD.keySet().iterator();
281                            for (int i = 0; i < colNames.length && keys.hasNext(); i++) {
282                                    Translation title = visibleAMD.get(keys.next()).getTitle();
283                                    if (!I8NUtil.isEmpty(title)) {
284                                            // System.out.println("set colname " + i + " to " +
285                                            // title.toString());
286                                            colNames[i] = title.toString();
287                                    }
288                            }
289                    }
290                    if (fireTableStructureChanged)
291                            fireTableStructureChanged();
292            }
293    
294            /**
295             * @return Cached bounds for the whole dataset (without applying the filter)
296             *         or <code>null</code>
297             */
298            public Envelope getBounds() {
299                    return bounds;
300            }
301  }  }

Legend:
Removed from v.59  
changed lines
  Added in v.225

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26