/[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 464 by alfonx, Tue Oct 13 13:22:31 2009 UTC branches/2.2.x/src/skrueger/geotools/StyledFeatureCollectionTableModel.java revision 1046 by mojays, Wed Sep 22 12:10:52 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.HashMap;
33    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;
# Line 39  import org.geotools.feature.FeatureColle Line 42  import org.geotools.feature.FeatureColle
42  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
43  import org.opengis.feature.simple.SimpleFeatureType;  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.AttributeMetadataInterface;
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}.   * functionalities of the {@link AttributeMetadataImpl}.
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 applied.</li>   * <li>Any filter defined in the {@link StyledFeaturesInterface} will be
63     * applied.</li>
64   * </ul>   * </ul>
65   *   *
66   * @author Stefan A. Krüger   * @author Stefan A. Tzeggai
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          /** Contains the complete {@link AttributeMetadata}-Map of the styled layer. */          /** Contains the complete {@link AttributeMetadataImpl}-Map of the styled layer. */
73          protected AttributeMetadataMap origAMD = null;          protected AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap = 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             * Tooltips für die Spaltennamen. Wird nur beim Aufruf von
80             * {@link #reorganize} befuellt.
81             */
82            protected String[] colTooltips = null;
83    
84            /** 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           *           *
# Line 78  public class StyledFeatureCollectionTabl Line 92  public class StyledFeatureCollectionTabl
92           * @param filter           * @param filter
93           *            filter applied to the table           *            filter applied to the table
94           */           */
95          public StyledFeatureCollectionTableModel(StyledFeaturesInterface<?> styledFeatures) {          public StyledFeatureCollectionTableModel(
96                            StyledFeaturesInterface<?> styledFeatures) {
97                  setStyledFeatures(styledFeatures);                  setStyledFeatures(styledFeatures);
98          }          }
99    
100          /**          /**
101             * This overwritten method filters the values for NODATA-values defined in
102             * the {@link AttributeMetadataImpl}
103             */
104            @Override
105            public Object getValueAt(int row, int col) {
106                    Object rawValue = super.getValueAt(row, col);
107                    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<SimpleFeatureType, SimpleFeature> fs,          protected void setFeatureSource(
120                          AttributeMetadataMap 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.amdMapVisibleOnly = amdMap.sortedValuesVisibleOnly();
130                    
131                  FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null;                  FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null;
132                  if (fs != null) {                  if (fs != null) {
133    
# Line 109  public class StyledFeatureCollectionTabl Line 135  public class StyledFeatureCollectionTabl
135    
136                          final SimpleFeatureType schema = fs.getSchema();                          final SimpleFeatureType schema = fs.getSchema();
137                          Query query = new DefaultQuery(schema.getTypeName(), filter);                          Query query = new DefaultQuery(schema.getTypeName(), filter);
138                          if (amd != null) {                          if (amdm != null) {
139                                  Vector<String> visibleAttrNames = new Vector<String>();                LinkedHashSet<String> visibleAttrNames = new LinkedHashSet<String>();
                                 // Add the column with the geometry (usually "the_geom")  
                                   
                                 for (AttributeDescriptor aDesc : schema.getAttributeDescriptors()) {  
                                           
                                         // Always add the geometry  
                                         if (schema.getGeometryDescriptor()  
                                                 .getName().equals(aDesc.getName())) {  
                                                 visibleAttrNames.add(schema.getGeometryDescriptor()  
                                                                 .getLocalName());  
                                                 continue;  
                                         }  
                                           
                                         if (amd.get(aDesc.getName()).isVisible())  
                                                 visibleAttrNames.add(aDesc.getName().getLocalPart());  
                                 }  
140    
141                                  // create a query for the visible attributes                                  // Add the column with the geometry (usually "the_geom") always
142                                  String[] properties = visibleAttrNames.toArray(new String[] {});                                  String geomColumnLocalName = schema.getGeometryDescriptor()
143                                                    .getLocalName();
144                                  LOGGER.debug("Query contains the following attributes: "                                  visibleAttrNames.add(geomColumnLocalName);
145                                                  + visibleAttrNames);  
146                                    // Add other visible attributes as ordered by weights
147                                    for (AttributeMetadataInterface a : amdMapVisibleOnly) {
148                                            visibleAttrNames.add(a.getLocalName());
149                                    }
150    
151                                    // Tested with 2.6.x trunk from 2009-11-26 and it now works. So
152                                    // we only request the properties we need!
153                                    // /**
154                                    // * I got NPEs when properties contained only [the_geom]
155                                    // ?!??!!??
156                                    // */
157                                    // if (properties.length > 1) {
158                                  query = new DefaultQuery(schema.getTypeName(), filter,                                  query = new DefaultQuery(schema.getTypeName(), filter,
159                                                  properties);                                                  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);
167                  }                  }
# Line 148  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 setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) {          public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) {
# Line 156  public class StyledFeatureCollectionTabl Line 183  public class StyledFeatureCollectionTabl
183                          if (styledFeatures == null)                          if (styledFeatures == null)
184                                  setFeatureSource(null, null, null);                                  setFeatureSource(null, null, null);
185                          else {                          else {
186                                  setFeatureSource(styledFeatures.getFeatureSource(), styledFeatures.getAttributeMetaDataMap(), styledFeatures.getFilter());                                  setFeatureSource(styledFeatures.getFeatureSource(),
187                                                    styledFeatures.getAttributeMetaDataMap(),
188                                                    styledFeatures.getFilter());
189                          }                          }
190                  } catch (Exception err) {                  } catch (Exception err) {
191                          throw new RuntimeException(err);                          throw new RuntimeException(err);
# Line 172  public class StyledFeatureCollectionTabl Line 201  public class StyledFeatureCollectionTabl
201           */           */
202          @Override          @Override
203          protected void reorganize(boolean fireTableStructureChanged) {          protected void reorganize(boolean fireTableStructureChanged) {
204                    
205                  super.reorganize(false);                  featureArray = FeatureUtil.featuresToArray(featureTable);
206                                    if (featureArray == null || featureArray.length == 0) {
207                  // translate the column names                          colNames = new String[0];
208                  if (origAMD != null) {                          colTooltips = new String[0]; // Only set and used in
209                            // StyledFeatureCollectionTableModel
210                            colClass = new Class[0];
211                    } else {
212                            // Struktur der Tabelle vom AttributeMetaDtaaMap übernehmen
213                            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++) {                          for (int i = 0; i < colNames.length; i++) {
233                                  colNames[i] = origAMD.get(colNames[i]).getTitle().toString();                                  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.464  
changed lines
  Added in v.1046

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26