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

Legend:
Removed from v.112  
changed lines
  Added in v.224

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26