/[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

trunk/src/skrueger/geotools/StyledFeatureCollectionTableModel.java revision 256 by alfonx, Fri Jul 31 14:43:47 2009 UTC branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFeatureCollectionTableModel.java revision 464 by alfonx, Tue Oct 13 13:22:31 2009 UTC
# Line 29  Line 29 
29   ******************************************************************************/   ******************************************************************************/
30  package skrueger.geotools;  package skrueger.geotools;
31    
 import java.util.Iterator;  
 import java.util.Map;  
32  import java.util.Vector;  import java.util.Vector;
33    
34  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
35  import org.geotools.data.DefaultQuery;  import org.geotools.data.DefaultQuery;
36  import org.geotools.data.FeatureSource;  import org.geotools.data.FeatureSource;
37  import org.geotools.data.Query;  import org.geotools.data.Query;
 import org.geotools.data.memory.MemoryDataStore;  
 import org.geotools.feature.AttributeType;  
38  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
39    import org.opengis.feature.simple.SimpleFeature;
40    import org.opengis.feature.simple.SimpleFeatureType;
41    import org.opengis.feature.type.AttributeDescriptor;
42  import org.opengis.filter.Filter;  import org.opengis.filter.Filter;
43    
44  import schmitzm.geotools.gui.FeatureCollectionTableModel;  import schmitzm.geotools.gui.FeatureCollectionTableModel;
45  import skrueger.AttributeMetaData;  import skrueger.AttributeMetadata;
 import skrueger.i8n.I8NUtil;  
 import skrueger.i8n.Translation;  
46    
47  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
48    
49  /**  /**
50   * This class extends the the {@link FeatureCollectionTableModel} with the   * This class extends the the {@link FeatureCollectionTableModel} with the
51   * functionalities of the {@link AttributeMetaData} of   * functionalities of the {@link AttributeMetadata}.
  * {@linkplain StyledLayerInterface styled objects}.  
52   * <ul>   * <ul>
53   * <li>column names are translated according to   * <li>column names are translated according to
54   * {@link AttributeMetaData#getTitle()}</li>   * {@link AttributeMetadata#getTitle()}</li>
55   * <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>   * <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>
56     * <li>Any filter defined in the {@link StyledFeaturesInterface} will be applied.</li>
57   * </ul>   * </ul>
58   *   *
59   * @author <a href="mailto:[email protected]">Martin Schmitz</a>   * @author Stefan A. Krüger
  *         (University of Bonn/Germany)  
60   */   */
61  public class StyledFeatureCollectionTableModel extends  public class StyledFeatureCollectionTableModel extends
62                  FeatureCollectionTableModel {                  FeatureCollectionTableModel {
63          final static private Logger LOGGER = Logger          final static private Logger LOGGER = Logger
64                          .getLogger(StyledFeatureCollectionTableModel.class);                          .getLogger(StyledFeatureCollectionTableModel.class);
65          /** Holds the data source as styled layer. */          /** Contains the complete {@link AttributeMetadata}-Map of the styled layer. */
66          protected StyledLayerInterface<?> layer = null;          protected AttributeMetadataMap origAMD = null;
         /** Contains only the visible elements of the {@link AttributeMetaData}-Map */  
         protected Map<Integer, AttributeMetaData> visibleAMD = null;  
         /** Holds the data source for the table as {@code FeatureSource}. */  
         protected FeatureSource featureSource = null;  
         /** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */  
         protected Map<Integer, AttributeMetaData> origAMD = null;  
67          /** Holds the current filter on the table */          /** Holds the current filter on the table */
68          protected Filter filter = null;          protected Filter filter = null;
69          /** Holds the Bounds for all features. Only set once during the constructor **/          /** Holds the Bounds for all features. Only set once during the constructor **/
70          protected Envelope bounds;          protected Envelope bounds;
71    
72                    
   
         /**  
          * 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);  
         }  
   
73          /**          /**
74           * Creates a new table model for a styled layer.           * Creates a new table model for a styled layer.
75           *           *
76           * @param layer           * @param styledFeatures
77           *            the styled layer           *            the styled layer
78           * @param filter           * @param filter
79           *            filter applied to the table           *            filter applied to the table
80           */           */
81          public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer,          public StyledFeatureCollectionTableModel(StyledFeaturesInterface<?> styledFeatures) {
82                          Filter filter) {                  setStyledFeatures(styledFeatures);
                 super();  
                 setFeatureCollection(layer, filter);  
83          }          }
84    
85          /**          /**
# Line 137  public class StyledFeatureCollectionTabl Line 88  public class StyledFeatureCollectionTabl
88           * @param fs           * @param fs
89           *            the feature source           *            the feature source
90           * @param amd           * @param amd
91           *            {@link AttributeMetaData}-Map to define the visible attributes           *            {@link AttributeMetadata}-Map to define the visible attributes
92           *            and translation           *            and translation
93           */           */
94          protected void setFeatureSource(FeatureSource fs,          protected void setFeatureSource(FeatureSource<SimpleFeatureType, SimpleFeature> fs,
95                          Map<Integer, AttributeMetaData> amd, Filter filter)                          AttributeMetadataMap amd, Filter filter)
96                          throws Exception {                          throws Exception {
97                    
98                  if (filter == null)                  if (filter == null)
99                          filter = Filter.INCLUDE;                          filter = Filter.INCLUDE;
100    
101                  this.featureSource = fs;  //              this.featureSource = fs;
102                  this.filter = filter;                  this.filter = filter;
103                  this.origAMD = amd;                  this.origAMD = amd;
                 this.visibleAMD = null;  
104    
105                  FeatureCollection fc = null;                  FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null;
106                  if (fs != null) {                  if (fs != null) {
107    
108                          bounds = fs.getBounds();                          bounds = fs.getBounds();
109    
110                          Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter);                          final SimpleFeatureType schema = fs.getSchema();
111                            Query query = new DefaultQuery(schema.getTypeName(), filter);
112                          if (amd != null) {                          if (amd != null) {
                                 // determine the names of the visible Attributes  
                                 this.visibleAMD = StyledLayerUtil.getVisibleAttributeMetaData(  
                                                 amd, true);  
113                                  Vector<String> visibleAttrNames = new Vector<String>();                                  Vector<String> visibleAttrNames = new Vector<String>();
114                                  // Add the column with the geometry (usually "the_geom")                                  // Add the column with the geometry (usually "the_geom")
115                                  visibleAttrNames.add(fs.getSchema().getDefaultGeometry()                                  
116                                                  .getLocalName());                                  for (AttributeDescriptor aDesc : schema.getAttributeDescriptors()) {
117                                  for (int attrIdx : visibleAMD.keySet()) {                                          
118                                            // Always add the geometry
119                                          /**                                          if (schema.getGeometryDescriptor()
120                                           * If the user removed columns from the schema of the DBF                                                  .getName().equals(aDesc.getName())) {
121                                           * file, there might exist AttributeMetaData for columns                                                  visibleAttrNames.add(schema.getGeometryDescriptor()
122                                           * that don't exists. We check here to avoid an                                                                  .getLocalName());
123                                           * ArrayOutOfIndex.                                                  continue;
                                          */  
                                         if (attrIdx < fs.getSchema().getAttributeCount()) {  
                                                 final AttributeType attributeTypeAtIdx = fs.getSchema()  
                                                                 .getAttributeType(attrIdx);  
                                                 visibleAttrNames.add(attributeTypeAtIdx.getLocalName());  
                                         } else {  
                                                 LOGGER.warn("AttributeMetaData has been found for columnIdx="+attrIdx+", but fs.getSchema().getAttributeCount() = "+fs.getSchema().getAttributeCount()+". Ignored.");  
124                                          }                                          }
125                                            
126                                            if (amd.get(aDesc.getName()).isVisible())
127                                                    visibleAttrNames.add(aDesc.getName().getLocalPart());
128                                  }                                  }
129    
130                                  // create a query for the visible attributes                                  // create a query for the visible attributes
131                                  String[] properties = visibleAttrNames.toArray(new String[0]);                                  String[] properties = visibleAttrNames.toArray(new String[] {});
132    
133                                  LOGGER.debug("Query contains the following attributes: "                                  LOGGER.debug("Query contains the following attributes: "
134                                                  + visibleAttrNames);                                                  + visibleAttrNames);
135    
136                                  query = new DefaultQuery(fs.getSchema().getTypeName(), filter,                                  query = new DefaultQuery(schema.getTypeName(), filter,
137                                                  properties);                                                  properties);
138                          }                          }
139                          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());  
140                  }                  }
141                  setFeatureCollection(fc);                  setFeatureCollection(fc);
142          }          }
# Line 216  public class StyledFeatureCollectionTabl Line 148  public class StyledFeatureCollectionTabl
148           * @param fs           * @param fs
149           *            the feature source           *            the feature source
150           * @param amd           * @param amd
151           *            {@link AttributeMetaData}-Map to define the visible attributes           *            {@link AttributeMetadata}-Map to define the visible attributes
152           *            and translation           *            and translation
153           */           */
154          public void setFeatureCollection(StyledFeaturesInterface layer,          public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) {
                         Filter filter) {  
                 this.layer = layer;  
155                  try {                  try {
156                          if (layer == null)                          if (styledFeatures == null)
157                                  setFeatureSource(null, null, null);                                  setFeatureSource(null, null, null);
158                          else {                          else {
159                                  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);  
160                          }                          }
161                  } catch (Exception err) {                  } catch (Exception err) {
162                          throw new RuntimeException(err);                          throw new RuntimeException(err);
# Line 238  public class StyledFeatureCollectionTabl Line 164  public class StyledFeatureCollectionTabl
164          }          }
165    
166          /**          /**
          * 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;  
         }  
   
         /**  
167           * After calling {@code super.reorganize(.)} this method replaced the column           * After calling {@code super.reorganize(.)} this method replaced the column
168           * descriptions with the titles of the {@code AttributeMetaData}.           * descriptions with the titles of the {@code AttributeMetaData}.
169           *           *
# Line 293  public class StyledFeatureCollectionTabl Line 172  public class StyledFeatureCollectionTabl
172           */           */
173          @Override          @Override
174          protected void reorganize(boolean fireTableStructureChanged) {          protected void reorganize(boolean fireTableStructureChanged) {
175                    
176                  super.reorganize(false);                  super.reorganize(false);
177                    
178                  // translate the column names                  // translate the column names
179                  if (visibleAMD != null) {                  if (origAMD != null) {
180                          Iterator<Integer> keys = visibleAMD.keySet().iterator();                          for (int i = 0; i < colNames.length; i++) {
181                          for (int i = 0; i < colNames.length && keys.hasNext(); i++) {                                  colNames[i] = origAMD.get(colNames[i]).getTitle().toString();
                                 Translation title = visibleAMD.get(keys.next()).getTitle();  
                                 if (!I8NUtil.isEmpty(title)) {  
                                         // System.out.println("set colname " + i + " to " +  
                                         // title.toString());  
                                         colNames[i] = title.toString();  
                                 }  
182                          }                          }
183                            
184                  }                  }
185                  if (fireTableStructureChanged)                  if (fireTableStructureChanged)
186                          fireTableStructureChanged();                          fireTableStructureChanged();

Legend:
Removed from v.256  
changed lines
  Added in v.464

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26