/[schmitzm]/branches/2.3.KECK/src/skrueger/geotools/StyledFeatureCollectionTableModel.java
ViewVC logotype

Diff of /branches/2.3.KECK/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.3.KECK/src/skrueger/geotools/StyledFeatureCollectionTableModel.java revision 1103 by alfonx, Thu Oct 14 09:45:14 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.Vector;  import java.util.HashMap;
33    import java.util.LinkedHashSet;
34    import java.util.List;
35    
36  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
37  import org.geotools.data.DefaultQuery;  import org.geotools.data.DefaultQuery;
# Line 39  import org.geotools.feature.FeatureColle Line 41  import org.geotools.feature.FeatureColle
41  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
42  import org.opengis.feature.simple.SimpleFeatureType;  import org.opengis.feature.simple.SimpleFeatureType;
43  import org.opengis.feature.type.AttributeDescriptor;  import org.opengis.feature.type.AttributeDescriptor;
44    import org.opengis.feature.type.Name;
45  import org.opengis.filter.Filter;  import org.opengis.filter.Filter;
46    
47    import schmitzm.geotools.feature.FeatureUtil;
48  import schmitzm.geotools.gui.FeatureCollectionTableModel;  import schmitzm.geotools.gui.FeatureCollectionTableModel;
49  import skrueger.AttributeMetadata;  import skrueger.AttributeMetadataImpl;
50    import skrueger.AttributeMetadataInterface;
51    
52  import com.vividsolutions.jts.geom.Envelope;  import com.vividsolutions.jts.geom.Envelope;
53    
54  /**  /**
55   * This class extends the the {@link FeatureCollectionTableModel} with the   * This class extends the the {@link FeatureCollectionTableModel} with the
56   * functionalities of the {@link AttributeMetadata}.   * functionalities of the {@link AttributeMetadataImpl}.
57   * <ul>   * <ul>
58   * <li>column names are translated according to   * <li>column names are translated according to
59   * {@link AttributeMetadata#getTitle()}</li>   * {@link AttributeMetadataImpl#getTitle()}</li>
60   * <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>   * <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>
61   * <li>Any filter defined in the {@link StyledFeaturesInterface} will be applied.</li>   * <li>Any filter defined in the {@link StyledFeaturesInterface} will be
62     * applied.</li>
63   * </ul>   * </ul>
64   *   *
65   * @author Stefan A. Krüger   * @author Stefan A. Tzeggai
66   */   */
67  public class StyledFeatureCollectionTableModel extends  public class StyledFeatureCollectionTableModel extends
68                  FeatureCollectionTableModel {                  FeatureCollectionTableModel {
69          final static private Logger LOGGER = Logger          final static private Logger LOGGER = Logger
70                          .getLogger(StyledFeatureCollectionTableModel.class);                          .getLogger(StyledFeatureCollectionTableModel.class);
71          /** Contains the complete {@link AttributeMetadata}-Map of the styled layer. */          /** Contains the complete {@link AttributeMetadataImpl}-Map of the styled layer. */
72          protected AttributeMetadataMap origAMD = null;          protected AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap = null;
73          /** Holds the current filter on the table */          /** Holds the current filter on the table */
74          protected Filter filter = null;          protected Filter filter = null;
75          /** Holds the Bounds for all features. Only set once during the constructor **/          /** Holds the Bounds for all features. Only set once during the constructor **/
76          protected Envelope bounds;          protected Envelope bounds;
77            /**
78             * Tooltips für die Spaltennamen. Wird nur beim Aufruf von
79             * {@link #reorganize} befuellt.
80             */
81            protected String[] colTooltips = null;
82    
83            /** A cache for the #sortedValuesVisibleOnly() **/
84            protected List<? extends AttributeMetadataInterface> amdMapVisibleOnly = null;
85    
           
86          /**          /**
87           * Creates a new table model for a styled layer.           * Creates a new table model for a styled layer.
88           *           *
# Line 78  public class StyledFeatureCollectionTabl Line 91  public class StyledFeatureCollectionTabl
91           * @param filter           * @param filter
92           *            filter applied to the table           *            filter applied to the table
93           */           */
94          public StyledFeatureCollectionTableModel(StyledFeaturesInterface<?> styledFeatures) {          public StyledFeatureCollectionTableModel(
95                            StyledFeaturesInterface<?> styledFeatures) {
96                  setStyledFeatures(styledFeatures);                  setStyledFeatures(styledFeatures);
97          }          }
98    
99          /**          /**
100             * This overwritten method filters the values for NODATA-values defined in
101             * the {@link AttributeMetadataImpl}
102             */
103            @Override
104            public Object getValueAt(int row, int col) {
105                    Object rawValue = super.getValueAt(row, col);
106                    return amdMap.sortedValuesVisibleOnly().get(col).fiterNodata(rawValue);
107            }
108            
109            /**
110           * Sets a new data source for the table.           * Sets a new data source for the table.
111           *           *
112           * @param fs           * @param fs
113           *            the feature source           *            the feature source
114           * @param amd           * @param amdm
115           *            {@link AttributeMetadata}-Map to define the visible attributes           *            {@link AttributeMetadataImpl}-Map to define the visible attributes
116           *            and translation           *            and translation
117           */           */
118          protected void setFeatureSource(FeatureSource<SimpleFeatureType, SimpleFeature> fs,          protected void setFeatureSource(
119                          AttributeMetadataMap amd, Filter filter)                          FeatureSource<SimpleFeatureType, SimpleFeature> fs,
120                          throws Exception {                          AttributeMetadataMap<? extends AttributeMetadataInterface> amdm, Filter filter) throws Exception {
121                    
122                  if (filter == null)                  if (filter == null)
123                          filter = Filter.INCLUDE;                          filter = Filter.INCLUDE;
124    
125  //              this.featureSource = fs;                  // this.featureSource = fs;
126                  this.filter = filter;                  this.filter = filter;
127                  this.origAMD = amd;                  this.amdMap = amdm;
128                    this.amdMapVisibleOnly = amdMap.sortedValuesVisibleOnly();
129    
130                  FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null;                  FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null;
131                  if (fs != null) {                  if (fs != null) {
# Line 109  public class StyledFeatureCollectionTabl Line 134  public class StyledFeatureCollectionTabl
134    
135                          final SimpleFeatureType schema = fs.getSchema();                          final SimpleFeatureType schema = fs.getSchema();
136                          Query query = new DefaultQuery(schema.getTypeName(), filter);                          Query query = new DefaultQuery(schema.getTypeName(), filter);
137                          if (amd != null) {                          if (amdm != null) {
138                                  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());  
                                 }  
139    
140                                  // create a query for the visible attributes                                  // Add the column with the geometry (usually "the_geom") always
141                                  String[] properties = visibleAttrNames.toArray(new String[] {});                                  visibleAttrNames.add(schema.getGeometryDescriptor()
142                                                    .getLocalName());
143                                  LOGGER.debug("Query contains the following attributes: "  
144                                                  + visibleAttrNames);                                  // Add other visible attributes as ordered by weights
145                                    for (AttributeMetadataInterface a : amdMapVisibleOnly) {
146                                            visibleAttrNames.add(a.getLocalName());
147                                    }
148    
149                                    // Tested with 2.6.x trunk from 2009-11-26 and it now works. So
150                                    // we only request the properties we need!
151                                    // /**
152                                    // * I got NPEs when properties contained only [the_geom]
153                                    // ?!??!!??
154                                    // */
155                                    // if (properties.length > 1) {
156                                  query = new DefaultQuery(schema.getTypeName(), filter,                                  query = new DefaultQuery(schema.getTypeName(), filter,
157                                                  properties);                                                  visibleAttrNames.toArray(new String[] {}));
158                                    // } else {
159                                    // query = new DefaultQuery(schema.getTypeName(), filter);
160                                    // }
161                          }                          }
162                          fc = fs.getFeatures(query);                          fc = fs.getFeatures(query);
163                  }                  }
# Line 148  public class StyledFeatureCollectionTabl Line 171  public class StyledFeatureCollectionTabl
171           * @param fs           * @param fs
172           *            the feature source           *            the feature source
173           * @param amd           * @param amd
174           *            {@link AttributeMetadata}-Map to define the visible attributes           *            {@link AttributeMetadataImpl}-Map to define the visible attributes
175           *            and translation           *            and translation
176           */           */
177          public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) {          public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) {
# Line 156  public class StyledFeatureCollectionTabl Line 179  public class StyledFeatureCollectionTabl
179                          if (styledFeatures == null)                          if (styledFeatures == null)
180                                  setFeatureSource(null, null, null);                                  setFeatureSource(null, null, null);
181                          else {                          else {
182                                  setFeatureSource(styledFeatures.getFeatureSource(), styledFeatures.getAttributeMetaDataMap(), styledFeatures.getFilter());                                  setFeatureSource(styledFeatures.getFeatureSource(),
183                                                    styledFeatures.getAttributeMetaDataMap(),
184                                                    styledFeatures.getFilter());
185                          }                          }
186                  } catch (Exception err) {                  } catch (Exception err) {
187                          throw new RuntimeException(err);                          throw new RuntimeException(err);
# Line 172  public class StyledFeatureCollectionTabl Line 197  public class StyledFeatureCollectionTabl
197           */           */
198          @Override          @Override
199          protected void reorganize(boolean fireTableStructureChanged) {          protected void reorganize(boolean fireTableStructureChanged) {
200                    
201                  super.reorganize(false);                  featureArray = FeatureUtil.featuresToArray(featureTable);
202                                    if (featureArray == null || featureArray.length == 0) {
203                  // translate the column names                          colNames = new String[0];
204                  if (origAMD != null) {                          colTooltips = new String[0]; // Only set and used in
205                            // StyledFeatureCollectionTableModel
206                            colClass = new Class[0];
207                    } else {
208                            // Struktur der Tabelle vom AttributeMetaDtaaMap übernehmen
209                            SimpleFeatureType schema = featureArray[0].getFeatureType();
210                            // Pruefen, welche Attribute angezeigt werden
211                            attrTypes.clear();
212                            for (AttributeMetadataInterface amd : amdMapVisibleOnly) {
213                    Name name = amd.getName();
214                    AttributeDescriptor type = schema.getDescriptor(name);
215                    // if type can not be determined by complete name,
216                    // try only the local name
217                    if ( type == null )
218                      type = schema.getDescriptor(name.getLocalPart());
219                                    if (attrFilter == null || attrFilter.accept(type))
220                                            attrTypes.add(type);
221                            }
222                            // Namen und Attribut-Indizes der angezeigten Spalten ermitteln
223                            colNames = new String[attrTypes.size()];
224                            colTooltips = new String[attrTypes.size()]; // Only set and used in
225                            // StyledFeatureCollectionTableModel
226                            colClass = new Class[attrTypes.size()];
227                            attrIdxForCol = new int[attrTypes.size()];
228                          for (int i = 0; i < colNames.length; i++) {                          for (int i = 0; i < colNames.length; i++) {
229                                  colNames[i] = origAMD.get(colNames[i]).getTitle().toString();                  Name name = amdMapVisibleOnly.get(i).getName();
230                    AttributeDescriptor descriptor = schema.getDescriptor(name);
231                    // if type can not be determined by complete name,
232                    // try only the local name
233                    if ( descriptor == null )
234                      descriptor = schema.getDescriptor(name.getLocalPart());
235    
236                                    // Not so nice in 26: find the index of an attribute...
237                                    int idx = schema.getAttributeDescriptors().indexOf(descriptor);
238                                    attrIdxForCol[i] = idx;
239    
240                                    String attName = schema.getAttributeDescriptors().get(idx)
241                                                    .getLocalName();
242                                    colNames[i] = amdMap.get(attName).getTitle().toString();
243                                    AttributeMetadataInterface amd = amdMap.get(attName);
244                                    colTooltips[i] = "<html>" + amd.getDesc().toString() + "<br>"
245                                                    + amd.getName() + "</html>";
246                                    colClass[i] = schema.getAttributeDescriptors().get(idx)
247                                                    .getType().getBinding();
248                          }                          }
                           
249                  }                  }
250    
251                    // store feature indexes in HashMap to optimize findFeature(.)
252                    featureIdx = new HashMap<String, Integer>();
253                    for (int i = 0; i < featureArray.length; i++)
254                            if (featureArray[i] != null)
255                                    featureIdx.put(featureArray[i].getID(), i);
256                    //
257                    // // translate the column names
258                    // if (amdMap != null) {
259                    // for (int i = 0; i < colNames.length; i++) {
260                    // colTooltips[i] = amdMap.get(colNames[i]).getDesc().toString()
261                    // + "<br>" + colNames[i];
262                    // colNames[i] = amdMap.get(colNames[i]).getTitle().toString();
263                    //
264                    // }
265                    // }
266                  if (fireTableStructureChanged)                  if (fireTableStructureChanged)
267                          fireTableStructureChanged();                          fireTableStructureChanged();
268    
269          }          }
270    
271          /**          /**

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26