/[schmitzm]/branches/2.0-GP14/src/skrueger/geotools/StyledFeatureCollectionTableModel.java
ViewVC logotype

Diff of /branches/2.0-GP14/src/skrueger/geotools/StyledFeatureCollectionTableModel.java

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

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

Legend:
Removed from v.58  
changed lines
  Added in v.168

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26