/[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 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    /** 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    }           * Creates a new table model for a styled map.
65             *
66    /**           * @param map
67     * Creates a new table model for a styled map.           *            the styled map
68     * @param map the styled map           */
69     * @param filter filter applied to the table          public StyledFeatureCollectionTableModel(
70     */                          StyledFeatureCollectionInterface map) {
71    public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map, Filter filter) {                  this(map, Filter.INCLUDE);
72      super();          }
73      setFeatureCollection(map, filter);  
74    }          /**
75             * Creates a new table model for a styled map.
76    /**           *
77     * Creates a new table model for a styled map.           * @param map
78     * @param map the styled map           *            the styled map
79     */           * @param filter
80    public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) {           *            filter applied to the table
81      this(map,Filter.INCLUDE);           */
82    }          public StyledFeatureCollectionTableModel(
83                            StyledFeatureCollectionInterface map, Filter filter) {
84    /**                  super();
85     * Creates a new table model for a styled map.                  setFeatureCollection(map, filter);
86     * @param map the styled map          }
87     * @param filter filter applied to the table  
88     */          /**
89    public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map, Filter filter) {           * Creates a new table model for a styled map.
90      super();           *
91      setFeatureCollection(map, filter);           * @param map
92    }           *            the styled map
93             */
94    /**          public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) {
95     * Sets a new data source for the table.                  this(map, Filter.INCLUDE);
96     * @param fs     the feature source          }
97     * @param amd    {@link AttributeMetaData}-Map to define the visible attributes  
98     *               and translation          /**
99     */           * Creates a new table model for a styled map.
100    protected void setFeatureSource(FeatureSource fs, Map<Integer, AttributeMetaData> amd, Filter filter) throws Exception {           *
101      if ( filter == null )           * @param map
102        filter = Filter.INCLUDE;           *            the styled map
103             * @param filter
104      this.featureSource = fs;           *            filter applied to the table
105      this.filter        = filter;           */
106      this.origAMD       = amd;          public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map,
107      this.visibleAMD    = null;                          Filter filter) {
108                        super();
109      FeatureCollection fc = null;                  setFeatureCollection(map, filter);
110      if (fs != null) {          }
111        Query query = new DefaultQuery();  
112        if (amd != null) {          /**
113          // determine the names of the visible Attributes           * Sets a new data source for the table.
114          this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(amd, true);           *
115          Vector<String> visibleAttrNames = new Vector<String>();           * @param fs
116          // Add the column with the geometry (usually "the_geom")           *            the feature source
117          visibleAttrNames.add(fs.getSchema().getDefaultGeometry().getLocalName());           * @param amd
118          for (int attrIdx : visibleAMD.keySet())           *            {@link AttributeMetaData}-Map to define the visible attributes
119            visibleAttrNames.add(fs.getSchema().getAttributeType(attrIdx).getLocalName());           *            and translation
120             */
121          // create a query for the visible attributes          protected void setFeatureSource(FeatureSource fs,
122          String[] properties = visibleAttrNames.toArray(new String[0]);                          Map<Integer, AttributeMetaData> amd, Filter filter)
123          query = new DefaultQuery(fs.getSchema().getTypeName(), filter, properties);                          throws Exception {
124        }                  if (filter == null)
125        fc = fs.getFeatures(query);                          filter = Filter.INCLUDE;
126      }  
127      setFeatureCollection(fc);                  this.featureSource = fs;
128    }                  this.filter = filter;
129                    this.origAMD = amd;
130    /**                  this.visibleAMD = null;
131     * Converts the {@code StyledFeatureCollection} to a {@code FeatureSource}  
132     * and sets this as the new data source for the table.                  FeatureCollection fc = null;
133     * @param fs     the feature source                  if (fs != null) {
134     * @param amd    {@link AttributeMetaData}-Map to define the visible attributes  
135     *               and translation                          bounds = fs.getBounds();
136     */  
137    public void setFeatureCollection(StyledFeatureCollectionInterface map, Filter filter) {                          Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter);
138      this.map = map;                          if (amd != null) {
139      try {                                  // determine the names of the visible Attributes
140        if (map == null)                                  this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(
141          setFeatureSource(null, null, null);                                                  amd, true);
142        else {                                  Vector<String> visibleAttrNames = new Vector<String>();
143          FeatureCollection fc = map.getGeoObject();                                  // Add the column with the geometry (usually "the_geom")
144          String fcName = fc.getFeatureType().getTypeName();                                  visibleAttrNames.add(fs.getSchema().getDefaultGeometry()
145          FeatureSource fs = new MemoryDataStore(fc).getFeatureSource(fcName);                                                  .getLocalName());
146          setFeatureSource(fs, map.getAttributeMetaDataMap(), filter);                                  for (int attrIdx : visibleAMD.keySet()) {
147        }  
148      } catch (Exception err) {                                          /**
149        throw new RuntimeException(err);                                           * If the user removed columns from the schema of the DBF
150      }                                           * file, there might exist AttributeMetaData for columns
151    }                                           * that don't exists. We check here to avoid an
152                                             * ArrayOutOfIndex.
153    /**                                           */
154     * Sets the {@code StyledFeatureCollection} as new data source for the table.                                          if (attrIdx < fs.getSchema().getAttributeCount()) {
155     * @param fs     the feature source                                                  final AttributeType attributeTypeAtIdx = fs.getSchema()
156     * @param amd    {@link AttributeMetaData}-Map to define the visible attributes                                                                  .getAttributeType(attrIdx);
157     *               and translation                                                  visibleAttrNames.add(attributeTypeAtIdx.getLocalName());
158     */                                          } else {
159    public void setFeatureCollection(StyledFeatureSourceInterface map, Filter filter) {                                                  LOGGER.warn("AttributeMetaData has been found for columnIdx="+attrIdx+", but fs.getSchema().getAttributeCount() = "+fs.getSchema().getAttributeCount()+". Ignored.");
160      this.map = map;                                          }
161      try {                                  }
162        if (map == null)  
163          setFeatureSource(null, null, null);                                  // create a query for the visible attributes
164        else                                  String[] properties = visibleAttrNames.toArray(new String[0]);
165          setFeatureSource(map.getGeoObject(), map.getAttributeMetaDataMap(), filter);  
166      } catch (Exception err) {                                  LOGGER.debug("Query contains the following attributes: "
167        throw new RuntimeException(err);                                                  + visibleAttrNames);
168      }  
169    }                                  query = new DefaultQuery(fs.getSchema().getTypeName(), filter,
170                                                      properties);
171    /**                          }
172     * Resets the filter for the table.                          fc = fs.getFeatures(query);
173     * @param filter a filter  
174     */                          // FAILS:!!!, even with query = new
175    public void setFilter(Filter filter) {                          // DefaultQuery(fs.getSchema().getTypeName(), filter);
176      try{                          // java.lang.UnsupportedOperationException: Unknown feature
177        setFeatureSource(this.featureSource, this.origAMD, filter);                          // attribute: PQM_MOD
178      } catch (Exception err) {                          // at
179        throw new RuntimeException(err);                          // schmitzm.geotools.feature.FeatureOperationTree.evaluate(FeatureOperationTree.java:93)
180      }                          // bounds = fc.getBounds();
181    }                          // SK, 17.4.2009
182                            //      
183    /**                          // System.out.println("Filter = "+filter);
184     * After calling {@code super.reorganize(.)} this method replaced the                          // System.out.println("Size of FC = "+fc.size());
185     * column descriptions with the titles of the {@code AttributeMetaData}.                          // System.out.println("anz att= "+fc.getNumberOfAttributes());
186     * @param fireTableStructureChanged indicates whether a table event is                  }
187     *        initiated after reorganize                  setFeatureCollection(fc);
188     */          }
189    @Override  
190    protected void reorganize(boolean fireTableStructureChanged) {          /**
191      super.reorganize(false);           * Converts the {@code StyledFeatureCollection} to a {@code FeatureSource}
192      // translate the column names           * and sets this as the new data source for the table.
193      if (visibleAMD != null) {           *
194        Iterator<Integer> keys = visibleAMD.keySet().iterator();           * @param fs
195        for (int i = 0; i < colNames.length && keys.hasNext(); i++) {           *            the feature source
196          Translation title = visibleAMD.get(keys.next()).getTitle();           * @param amd
197          if (!I8NUtil.isEmpty(title)) {           *            {@link AttributeMetaData}-Map to define the visible attributes
198            System.out.println("set colname " + i + " to " + title.toString());           *            and translation
199            colNames[i] = title.toString();           */
200          }          public void setFeatureCollection(StyledFeatureCollectionInterface map,
201        }                          Filter filter) {
202      }                  this.map = map;
203      if ( fireTableStructureChanged )                  try {
204        fireTableStructureChanged();                          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.59  
changed lines
  Added in v.168

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26