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

branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFeatureCollectionTableModel.java revision 490 by alfonx, Fri Oct 23 12:35:59 2009 UTC trunk/src/skrueger/geotools/StyledFeatureCollectionTableModel.java revision 1265 by alfonx, Fri Nov 12 19:17:47 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. */          /**
72          protected AttributeMetadataMap origAMD = null;           * Contains the complete {@link AttributeMetadataImpl}-Map of the styled
73             * layer.
74             */
75            protected AttributeMetadataMap<? extends AttributeMetadataInterface> amdMap = null;
76          /** Holds the current filter on the table */          /** Holds the current filter on the table */
77          protected Filter filter = null;          protected Filter filter = null;
78          /** Holds the Bounds for all features. Only set once during the constructor **/          /** Holds the Bounds for all features. Only set once during the constructor **/
79          protected Envelope bounds;          protected Envelope bounds;
80            /**
81             * Tooltips für die Spaltennamen. Wird nur beim Aufruf von
82             * {@link #reorganize} befuellt.
83             */
84            protected String[] colTooltips = null;
85    
86            /**
87             * A cache for the #sortedValuesVisibleOnly(). WHen the amd is changed
88             * externally, this object will stay the same
89             **/
90            protected List<? extends AttributeMetadataInterface> amdMapVisibleOnly = null;
91    
           
92          /**          /**
93           * Creates a new table model for a styled layer.           * Creates a new table model for a styled layer.
94           *           *
# Line 78  public class StyledFeatureCollectionTabl Line 97  public class StyledFeatureCollectionTabl
97           * @param filter           * @param filter
98           *            filter applied to the table           *            filter applied to the table
99           */           */
100          public StyledFeatureCollectionTableModel(StyledFeaturesInterface<?> styledFeatures) {          public StyledFeatureCollectionTableModel(
101                            StyledFeaturesInterface<?> styledFeatures) {
102                  setStyledFeatures(styledFeatures);                  setStyledFeatures(styledFeatures);
103          }          }
104    
105          /**          /**
106             * This overwritten method filters the values for NODATA-values defined in
107             * the {@link AttributeMetadataImpl}
108             */
109            @Override
110            public Object getValueAt(int row, int col) {
111                    Object rawValue = super.getValueAt(row, col);
112                    return amdMapVisibleOnly.get(col).fiterNodata(rawValue);
113            }
114    
115            /**
116           * Sets a new data source for the table.           * Sets a new data source for the table.
117           *           *
118           * @param fs           * @param fs
119           *            the feature source           *            the feature source
120           * @param amd           * @param amdm
121           *            {@link AttributeMetadata}-Map to define the visible attributes           *            {@link AttributeMetadataImpl}-Map to define the visible
122           *            and translation           *            attributes and translation
123           */           */
124          protected void setFeatureSource(FeatureSource<SimpleFeatureType, SimpleFeature> fs,          protected void setFeatureSource(
125                          AttributeMetadataMap amd, Filter filter)                          FeatureSource<SimpleFeatureType, SimpleFeature> fs,
126                          throws Exception {                          AttributeMetadataMap<? extends AttributeMetadataInterface> amdm,
127                                            Filter filter) throws Exception {
128    
129                  if (filter == null)                  if (filter == null)
130                          filter = Filter.INCLUDE;                          filter = Filter.INCLUDE;
131    
132  //              this.featureSource = fs;                  // this.featureSource = fs;
133                  this.filter = filter;                  this.filter = filter;
134                  this.origAMD = amd;                  this.amdMap = amdm;
135                    this.amdMapVisibleOnly = amdMap.sortedValuesVisibleOnly();
136    
137                  FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null;                  FeatureCollection<SimpleFeatureType, SimpleFeature> fc = null;
138                  if (fs != null) {                  if (fs != null) {
# Line 109  public class StyledFeatureCollectionTabl Line 141  public class StyledFeatureCollectionTabl
141    
142                          final SimpleFeatureType schema = fs.getSchema();                          final SimpleFeatureType schema = fs.getSchema();
143                          Query query = new DefaultQuery(schema.getTypeName(), filter);                          Query query = new DefaultQuery(schema.getTypeName(), filter);
144                          if (amd != null) {                          if (amdm != null) {
145                                  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());  
                                 }  
   
                                 // create a query for the visible attributes  
                                 String[] properties = visibleAttrNames.toArray(new String[] {});  
   
                                 LOGGER.debug("Query contains the following attributes: "  
                                                 + visibleAttrNames);  
   
146    
147                                  /**                                  // Add the column with the geometry (usually "the_geom") always
148                                   * I got NPEs when properties contained only [the_geom] ?!??!!??                                  visibleAttrNames.add(schema.getGeometryDescriptor()
149                                   */                                                  .getLocalName());
150                                  if (properties.length > 1) {  
151                                          query = new DefaultQuery(schema.getTypeName(), filter,                                  // Add other visible attributes as ordered by weights
152                                                          properties);                                  for (AttributeMetadataInterface a : amdMapVisibleOnly) {
153                                  } else {                                          visibleAttrNames.add(a.getLocalName());
                                         query = new DefaultQuery(schema.getTypeName(), filter);  
154                                  }                                  }
155    
156                                    // Tested with 2.6.x trunk from 2009-11-26 and it now works. So
157                                    // we only request the properties we need!
158                                    // /**
159                                    // * I got NPEs when properties contained only [the_geom]
160                                    // ?!??!!??
161                                    // */
162                                    // if (properties.length > 1) {
163                                    query = new DefaultQuery(schema.getTypeName(), filter,
164                                                    visibleAttrNames.toArray(new String[] {}));
165                                    // } else {
166                                    // query = new DefaultQuery(schema.getTypeName(), filter);
167                                    // }
168                          }                          }
169                          fc = fs.getFeatures(query);                          fc = fs.getFeatures(query);
170                  }                  }
# Line 156  public class StyledFeatureCollectionTabl Line 178  public class StyledFeatureCollectionTabl
178           * @param fs           * @param fs
179           *            the feature source           *            the feature source
180           * @param amd           * @param amd
181           *            {@link AttributeMetadata}-Map to define the visible attributes           *            {@link AttributeMetadataImpl}-Map to define the visible
182           *            and translation           *            attributes and translation
183           */           */
184          public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) {          public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) {
185                  try {                  try {
186                          if (styledFeatures == null)                          if (styledFeatures == null)
187                                  setFeatureSource(null, null, null);                                  setFeatureSource(null, null, null);
188                          else {                          else {
189                                  setFeatureSource(styledFeatures.getFeatureSource(), styledFeatures.getAttributeMetaDataMap(), styledFeatures.getFilter());                                  setFeatureSource(styledFeatures.getFeatureSource(),
190                                                    styledFeatures.getAttributeMetaDataMap(),
191                                                    styledFeatures.getFilter());
192                          }                          }
193                  } catch (Exception err) {                  } catch (Exception err) {
194                          throw new RuntimeException(err);                          throw new RuntimeException(err);
# Line 180  public class StyledFeatureCollectionTabl Line 204  public class StyledFeatureCollectionTabl
204           */           */
205          @Override          @Override
206          protected void reorganize(boolean fireTableStructureChanged) {          protected void reorganize(boolean fireTableStructureChanged) {
207                    
208                  super.reorganize(false);                  featureArray = FeatureUtil.featuresToArray(featureTable);
209                                    if (featureArray == null || featureArray.length == 0) {
210                  // translate the column names                          colNames = new String[0];
211                  if (origAMD != null) {                          colTooltips = new String[0]; // Only set and used in
212                            // StyledFeatureCollectionTableModel
213                            colClass = new Class[0];
214                    } else {
215                            // Struktur der Tabelle vom AttributeMetaDtaaMap übernehmen
216                            SimpleFeatureType schema = featureArray[0].getFeatureType();
217                            // Pruefen, welche Attribute angezeigt werden
218                            attrTypes.clear();
219                            for (AttributeMetadataInterface amd : amdMapVisibleOnly) {
220                                    Name name = amd.getName();
221                                    AttributeDescriptor type = schema.getDescriptor(name);
222                                    // if type can not be determined by complete name,
223                                    // try only the local name
224                                    if (type == null)
225                                            type = schema.getDescriptor(name.getLocalPart());
226                                    if (attrFilter == null || attrFilter.accept(type))
227                                            attrTypes.add(type);
228                            }
229                            // Namen und Attribut-Indizes der angezeigten Spalten ermitteln
230                            colNames = new String[attrTypes.size()];
231                            colTooltips = new String[attrTypes.size()]; // Only set and used in
232                            // StyledFeatureCollectionTableModel
233                            colClass = new Class[attrTypes.size()];
234                            attrIdxForCol = new int[attrTypes.size()];
235                          for (int i = 0; i < colNames.length; i++) {                          for (int i = 0; i < colNames.length; i++) {
236                                  colNames[i] = origAMD.get(colNames[i]).getTitle().toString();                                  Name name = amdMapVisibleOnly.get(i).getName();
237                                    AttributeDescriptor descriptor = schema.getDescriptor(name);
238                                    // if type can not be determined by complete name,
239                                    // try only the local name
240                                    if (descriptor == null)
241                                            descriptor = schema.getDescriptor(name.getLocalPart());
242    
243                                    // Not so nice in 26: find the index of an attribute...
244                                    int idx = schema.getAttributeDescriptors().indexOf(descriptor);
245                                    attrIdxForCol[i] = idx;
246    
247                                    String attName = schema.getAttributeDescriptors().get(idx)
248                                                    .getLocalName();
249                                    colNames[i] = amdMap.get(attName).getTitle().toString();
250                                    AttributeMetadataInterface amd = amdMap.get(attName);
251                                    colTooltips[i] = "<html>" + amd.getDesc().toString() + "<br>"
252                                                    + amd.getName() + "</html>";
253                                    colClass[i] = schema.getAttributeDescriptors().get(idx)
254                                                    .getType().getBinding();
255                          }                          }
                           
256                  }                  }
257    
258                    // store feature indexes in HashMap to optimize findFeature(.)
259                    featureIdx = new HashMap<String, Integer>();
260                    for (int i = 0; i < featureArray.length; i++)
261                            if (featureArray[i] != null)
262                                    featureIdx.put(featureArray[i].getID(), i);
263                    //
264                    // // translate the column names
265                    // if (amdMap != null) {
266                    // for (int i = 0; i < colNames.length; i++) {
267                    // colTooltips[i] = amdMap.get(colNames[i]).getDesc().toString()
268                    // + "<br>" + colNames[i];
269                    // colNames[i] = amdMap.get(colNames[i]).getTitle().toString();
270                    //
271                    // }
272                    // }
273                  if (fireTableStructureChanged)                  if (fireTableStructureChanged)
274                          fireTableStructureChanged();                          fireTableStructureChanged();
275    
276          }          }
277    
278          /**          /**

Legend:
Removed from v.490  
changed lines
  Added in v.1265

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26