/[schmitzm]/branches/2.2.x/src/skrueger/geotools/StyledFeatureCollectionTableModel.java
ViewVC logotype

Diff of /branches/2.2.x/src/skrueger/geotools/StyledFeatureCollectionTableModel.java

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

branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFeatureCollectionTableModel.java revision 332 by alfonx, Wed Aug 26 17:15:49 2009 UTC branches/2.2.x/src/skrueger/geotools/StyledFeatureCollectionTableModel.java revision 1042 by alfonx, Wed Sep 22 11:17:48 2010 UTC
# Line 25  Line 25 
25   *   *
26   * Contributors:   * Contributors:
27   *     Martin O. J. Schmitz - initial API and implementation   *     Martin O. J. Schmitz - initial API and implementation
28   *     Stefan A. Krüger - additional utility classes   *     Stefan A. Tzeggai - additional utility classes
29   ******************************************************************************/   ******************************************************************************/
30  package skrueger.geotools;  package skrueger.geotools;
31    
32  import java.util.Iterator;  import java.util.HashMap;
33  import java.util.Map;  import java.util.LinkedHashSet;
34    import java.util.List;
35  import java.util.Vector;  import java.util.Vector;
36    
37  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
38  import org.geotools.data.DefaultQuery;  import org.geotools.data.DefaultQuery;
39  import org.geotools.data.FeatureSource;  import org.geotools.data.FeatureSource;
40  import org.geotools.data.Query;  import org.geotools.data.Query;
 import org.geotools.data.memory.MemoryDataStore;  
41  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
42    import org.opengis.feature.simple.SimpleFeature;
43    import org.opengis.feature.simple.SimpleFeatureType;
44  import org.opengis.feature.type.AttributeDescriptor;  import org.opengis.feature.type.AttributeDescriptor;
45    import org.opengis.feature.type.Name;
46  import org.opengis.filter.Filter;  import org.opengis.filter.Filter;
47    
48    import schmitzm.geotools.feature.FeatureUtil;
49  import schmitzm.geotools.gui.FeatureCollectionTableModel;  import schmitzm.geotools.gui.FeatureCollectionTableModel;
50  import skrueger.AttributeMetaData;  import skrueger.AttributeMetadataImpl;
51  import skrueger.i8n.I8NUtil;  import skrueger.AttributeMetadataInterface;
 import skrueger.i8n.Translation;  
52    
53  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
54    
55  /**  /**
56   * This class extends the the {@link FeatureCollectionTableModel} with the   * This class extends the the {@link FeatureCollectionTableModel} with the
57   * functionalities of the {@link AttributeMetaData} of   * functionalities of the {@link AttributeMetadataImpl}.
  * {@linkplain StyledLayerInterface styled objects}.  
58   * <ul>   * <ul>
59   * <li>column names are translated according to   * <li>column names are translated according to
60   * {@link AttributeMetaData#getTitle()}</li>   * {@link AttributeMetadataImpl#getTitle()}</li>
61   * <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>   * <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>
62     * <li>Any filter defined in the {@link StyledFeaturesInterface} will be
63     * applied.</li>
64   * </ul>   * </ul>
65   *   *
66   * @author <a href="mailto:[email protected]">Martin Schmitz</a>   * @author Stefan A. Tzeggai
  *         (University of Bonn/Germany)  
67   */   */
68  public class StyledFeatureCollectionTableModel extends  public class StyledFeatureCollectionTableModel extends
69                  FeatureCollectionTableModel {                  FeatureCollectionTableModel {
70          final static private Logger LOGGER = Logger          final static private Logger LOGGER = Logger
71                          .getLogger(StyledFeatureCollectionTableModel.class);                          .getLogger(StyledFeatureCollectionTableModel.class);
72          /** Holds the data source as styled layer. */          /** Contains the complete {@link AttributeMetadataImpl}-Map of the styled layer. */
73          protected StyledLayerInterface<?> layer = null;          protected AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap = 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;  
74          /** Holds the current filter on the table */          /** Holds the current filter on the table */
75          protected Filter filter = null;          protected Filter filter = null;
76          /** Holds the Bounds for all features. Only set once during the constructor **/          /** Holds the Bounds for all features. Only set once during the constructor **/
77          protected Envelope bounds;          protected Envelope bounds;
   
           
   
78          /**          /**
79           * Creates a new table model for a styled layer.           * Tooltips für die Spaltennamen. Wird nur beim Aufruf von
80           *           * {@link #reorganize} befuellt.
          * @param layer  
          *            the styled layer  
81           */           */
82          public StyledFeatureCollectionTableModel(          protected String[] colTooltips = null;
83                          StyledFeatureCollectionInterface layer) {  
84                  this(layer, Filter.INCLUDE);          /** A cache for the #sortedValuesVisibleOnly() **/
85          }          protected List<? extends AttributeMetadataInterface> amdMapVisibleOnly = null;
86    
87          /**          /**
88           * Creates a new table model for a styled layer.           * Creates a new table model for a styled layer.
89           *           *
90           * @param layer           * @param styledFeatures
91           *            the styled layer           *            the styled layer
92           * @param filter           * @param filter
93           *            filter applied to the table           *            filter applied to the table
94           */           */
95          public StyledFeatureCollectionTableModel(          public StyledFeatureCollectionTableModel(
96                          StyledFeatureCollectionInterface layer, Filter filter) {                          StyledFeaturesInterface<?> styledFeatures) {
97                  super();                  setStyledFeatures(styledFeatures);
                 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);  
98          }          }
99    
100          /**          /**
101           * Creates a new table model for a styled layer.           * This overwritten method filters the values for NODATA-values defined in
102           *           * the {@link AttributeMetadataImpl}
          * @param layer  
          *            the styled layer  
          * @param filter  
          *            filter applied to the table  
103           */           */
104          public StyledFeatureCollectionTableModel(StyledFeaturesInterface layer,          @Override
105                          Filter filter) {          public Object getValueAt(int row, int col) {
106                  super();                  Object rawValue = super.getValueAt(row, col);
107                  setFeatureCollection(layer, filter);                  return amdMap.sortedValuesVisibleOnly().get(col).fiterNodata(rawValue);
108          }          }
109            
110          /**          /**
111           * Sets a new data source for the table.           * Sets a new data source for the table.
112           *           *
113           * @param fs           * @param fs
114           *            the feature source           *            the feature source
115           * @param amd           * @param amdm
116           *            {@link AttributeMetaData}-Map to define the visible attributes           *            {@link AttributeMetadataImpl}-Map to define the visible attributes
117           *            and translation           *            and translation
118           */           */
119          protected void setFeatureSource(FeatureSource fs,          protected void setFeatureSource(
120                          Map<Integer, AttributeMetaData> amd, Filter filter)                          FeatureSource<SimpleFeatureType, SimpleFeature> fs,
121                          throws Exception {                          AttributeMetadataMap<? extends AttributeMetadataInterface> amdm, Filter filter) throws Exception {
122    
123                  if (filter == null)                  if (filter == null)
124                          filter = Filter.INCLUDE;                          filter = Filter.INCLUDE;
125    
126                  this.featureSource = fs;                  // this.featureSource = fs;
127                  this.filter = filter;                  this.filter = filter;
128                  this.origAMD = amd;                  this.amdMap = amdm;
129                  this.visibleAMD = null;                  this.amdMapVisibleOnly = amdMap.sortedValuesVisibleOnly();
130    
131                  FeatureCollection fc = null;                  FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null;
132                  if (fs != null) {                  if (fs != null) {
133    
134                          bounds = fs.getBounds();                          bounds = fs.getBounds();
135    
136                          Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter);                          final SimpleFeatureType schema = fs.getSchema();
137                          if (amd != null) {                          Query query = new DefaultQuery(schema.getTypeName(), filter);
138                                  // determine the names of the visible Attributes                          if (amdm != null) {
139                                  this.visibleAMD = StyledLayerUtil.getVisibleAttributeMetaData(                                  LinkedHashSet<String> visibleAttrNames = new LinkedHashSet<String>();
140                                                  amd, true);  
141                                  Vector<String> visibleAttrNames = new Vector<String>();                                  // Add the column with the geometry (usually "the_geom") always
142                                  // Add the column with the geometry (usually "the_geom")                                  String geomColumnLocalName = schema.getGeometryDescriptor()
143                                  visibleAttrNames.add(fs.getSchema().getDefaultGeometry()                                                  .getLocalName();
144                                                  .getLocalName());                                  visibleAttrNames.add(geomColumnLocalName);
145                                  for (int attrIdx : visibleAMD.keySet()) {  
146                                    // Add other visible attributes as ordered by weights
147                                          /**                                  for (AttributeMetadataInterface a : amdMapVisibleOnly) {
148                                           * If the user removed columns from the schema of the DBF                                          visibleAttrNames.add(a.getLocalName());
                                          * file, there might exist AttributeMetaData for columns  
                                          * that don't exists. We check here to avoid an  
                                          * ArrayOutOfIndex.  
                                          */  
                                         if (attrIdx < fs.getSchema().getAttributeCount()) {  
                                                 final AttributeDescriptor 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.");  
                                         }  
149                                  }                                  }
150    
151                                  // create a query for the visible attributes                                  // Tested with 2.6.x trunk from 2009-11-26 and it now works. So
152                                  String[] properties = visibleAttrNames.toArray(new String[0]);                                  // we only request the properties we need!
153                                    // /**
154                                  LOGGER.debug("Query contains the following attributes: "                                  // * I got NPEs when properties contained only [the_geom]
155                                                  + visibleAttrNames);                                  // ?!??!!??
156                                    // */
157                                  query = new DefaultQuery(fs.getSchema().getTypeName(), filter,                                  // if (properties.length > 1) {
158                                                  properties);                                  query = new DefaultQuery(schema.getTypeName(), filter,
159                                                    visibleAttrNames.toArray(new String[] {}));
160                                    // } else {
161                                    // query = new DefaultQuery(schema.getTypeName(), filter);
162                                    // }
163                                    
164                                    System.out.println(query.getPropertyNames());
165                          }                          }
166                          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());  
167                  }                  }
168                  setFeatureCollection(fc);                  setFeatureCollection(fc);
169          }          }
# Line 216  public class StyledFeatureCollectionTabl Line 175  public class StyledFeatureCollectionTabl
175           * @param fs           * @param fs
176           *            the feature source           *            the feature source
177           * @param amd           * @param amd
178           *            {@link AttributeMetaData}-Map to define the visible attributes           *            {@link AttributeMetadataImpl}-Map to define the visible attributes
179           *            and translation           *            and translation
180           */           */
181          public void setFeatureCollection(StyledFeaturesInterface layer,          public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) {
                         Filter filter) {  
                 this.layer = layer;  
182                  try {                  try {
183                          if (layer == null)                          if (styledFeatures == null)
184                                  setFeatureSource(null, null, null);                                  setFeatureSource(null, null, null);
185                          else {                          else {
186                                  FeatureCollection fc = layer.getFeatureCollection();                                  setFeatureSource(styledFeatures.getFeatureSource(),
187                                  String fcName = fc.getSchema().getTypeName();                                                  styledFeatures.getAttributeMetaDataMap(),
188                                  FeatureSource fs = new MemoryDataStore(fc)                                                  styledFeatures.getFilter());
                                                 .getFeatureSource(fcName);  
                                 setFeatureSource(fs, layer.getAttributeMetaDataMap(), filter);  
189                          }                          }
190                  } catch (Exception err) {                  } catch (Exception err) {
191                          throw new RuntimeException(err);                          throw new RuntimeException(err);
# Line 238  public class StyledFeatureCollectionTabl Line 193  public class StyledFeatureCollectionTabl
193          }          }
194    
195          /**          /**
          * 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;  
         }  
   
         /**  
196           * After calling {@code super.reorganize(.)} this method replaced the column           * After calling {@code super.reorganize(.)} this method replaced the column
197           * descriptions with the titles of the {@code AttributeMetaData}.           * descriptions with the titles of the {@code AttributeMetaData}.
198           *           *
# Line 293  public class StyledFeatureCollectionTabl Line 201  public class StyledFeatureCollectionTabl
201           */           */
202          @Override          @Override
203          protected void reorganize(boolean fireTableStructureChanged) {          protected void reorganize(boolean fireTableStructureChanged) {
204                  super.reorganize(false);  
205                  // translate the column names                  featureArray = FeatureUtil.featuresToArray(featureTable);
206                  if (visibleAMD != null) {                  if (featureArray == null || featureArray.length == 0) {
207                          Iterator<Integer> keys = visibleAMD.keySet().iterator();                          colNames = new String[0];
208                          for (int i = 0; i < colNames.length && keys.hasNext(); i++) {                          colTooltips = new String[0]; // Only set and used in
209                                  Translation title = visibleAMD.get(keys.next()).getTitle();                          // StyledFeatureCollectionTableModel
210                                  if (!I8NUtil.isEmpty(title)) {                          colClass = new Class[0];
211                                          // System.out.println("set colname " + i + " to " +                  } else {
212                                          // title.toString());                          // Struktur der Tabelle vom AttributeMetaDtaaMap übernehmen
213                                          colNames[i] = title.toString();                          SimpleFeatureType schema = featureArray[0].getFeatureType();
214                                  }                          // Pruefen, welche Attribute angezeigt werden
215                            attrTypes.clear();
216                            for (AttributeMetadataInterface amd : amdMapVisibleOnly) {
217                                    Name name = amd.getName();
218                    AttributeDescriptor type = schema.getDescriptor(name);
219                    // if type can not be determined by complete name,
220                    // try only the local name
221                    if ( type == null )
222                      type = schema.getDescriptor(name.getLocalPart());
223                                    if (attrFilter == null || attrFilter.accept(type))
224                                            attrTypes.add(type);
225                            }
226                            // Namen und Attribut-Indizes der angezeigten Spalten ermitteln
227                            colNames = new String[attrTypes.size()];
228                            colTooltips = new String[attrTypes.size()]; // Only set and used in
229                            // StyledFeatureCollectionTableModel
230                            colClass = new Class[attrTypes.size()];
231                            attrIdxForCol = new int[attrTypes.size()];
232                            for (int i = 0; i < colNames.length; i++) {
233                                    Name name = amdMapVisibleOnly.get(i).getName();
234                    AttributeDescriptor descriptor = schema.getDescriptor(name);
235                    // if type can not be determined by complete name,
236                    // try only the local name
237                    if ( descriptor == null )
238                      descriptor = schema.getDescriptor(name.getLocalPart());
239    
240                                    // Not so nice in 26: find the index of an attribute...
241                                    int idx = schema.getAttributeDescriptors().indexOf(descriptor);
242                                    attrIdxForCol[i] = idx;
243    
244                                    String attName = schema.getAttributeDescriptors().get(idx)
245                                                    .getLocalName();
246                                    colNames[i] = amdMap.get(attName).getTitle().toString();
247                                    AttributeMetadataInterface amd = amdMap.get(attName);
248                                    colTooltips[i] = "<html>" + amd.getDesc().toString() + "<br>"
249                                                    + amd.getName() + "</html>";
250                                    colClass[i] = schema.getAttributeDescriptors().get(idx)
251                                                    .getType().getBinding();
252                          }                          }
253                  }                  }
254    
255                    // store feature indexes in HashMap to optimize findFeature(.)
256                    featureIdx = new HashMap<String, Integer>();
257                    for (int i = 0; i < featureArray.length; i++)
258                            if (featureArray[i] != null)
259                                    featureIdx.put(featureArray[i].getID(), i);
260                    //
261                    // // translate the column names
262                    // if (amdMap != null) {
263                    // for (int i = 0; i < colNames.length; i++) {
264                    // colTooltips[i] = amdMap.get(colNames[i]).getDesc().toString()
265                    // + "<br>" + colNames[i];
266                    // colNames[i] = amdMap.get(colNames[i]).getTitle().toString();
267                    //
268                    // }
269                    // }
270                  if (fireTableStructureChanged)                  if (fireTableStructureChanged)
271                          fireTableStructureChanged();                          fireTableStructureChanged();
272    
273          }          }
274    
275          /**          /**

Legend:
Removed from v.332  
changed lines
  Added in v.1042

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26