/[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/2.0-RC1/src/skrueger/geotools/StyledFeatureCollectionTableModel.java revision 604 by alfonx, Wed Dec 9 14:15:53 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.HashMap;  import java.util.HashMap;
33  import java.util.Vector;  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 40  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;  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   * <li>Any filter defined in the {@link StyledFeaturesInterface} will be
62   * applied.</li>   * 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 amdMap = 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 **/
# Line 78  public class StyledFeatureCollectionTabl Line 84  public class StyledFeatureCollectionTabl
84          protected String[] colTooltips = null;          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           *           *
95           * @param styledFeatures           * @param styledFeatures
# Line 91  public class StyledFeatureCollectionTabl Line 103  public class StyledFeatureCollectionTabl
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 amdm           * @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(          protected void setFeatureSource(
125                          FeatureSource<SimpleFeatureType, SimpleFeature> fs,                          FeatureSource<SimpleFeatureType, SimpleFeature> fs,
126                          AttributeMetadataMap amdm, Filter filter) 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;
# Line 109  public class StyledFeatureCollectionTabl Line 132  public class StyledFeatureCollectionTabl
132                  // this.featureSource = fs;                  // this.featureSource = fs;
133                  this.filter = filter;                  this.filter = filter;
134                  this.amdMap = amdm;                  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 118  public class StyledFeatureCollectionTabl Line 142  public class StyledFeatureCollectionTabl
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 (amdm != null) {                          if (amdm != null) {
145                                  Vector<String> visibleAttrNames = new Vector<String>();                                  LinkedHashSet<String> visibleAttrNames = new LinkedHashSet<String>();
146    
147                                  // Add the column with the geometry (usually "the_geom")                                  // Add the column with the geometry (usually "the_geom") always
148                                  visibleAttrNames.add(schema.getGeometryDescriptor()                                  visibleAttrNames.add(schema.getGeometryDescriptor()
149                                                  .getLocalName());                                                  .getLocalName());
150    
151                                  // Add other visible attributes as ordered by weights                                  // Add other visible attributes as ordered by weights
152                                  for (AttributeMetadata a : amdm.sortedValuesVisibleOnly()) {                                  for (AttributeMetadataInterface a : amdMapVisibleOnly) {
153                                          visibleAttrNames.add(a.getLocalName());                                          visibleAttrNames.add(a.getLocalName());
154                                  }                                  }
155    
156                                  // for (AttributeDescriptor aDesc :                                  // Tested with 2.6.x trunk from 2009-11-26 and it now works. So
157                                  // schema.getAttributeDescriptors()) {                                  // we only request the properties we need!
158                                  //                                                                        // /**
159                                  // // Always add the geometry                                  // * I got NPEs when properties contained only [the_geom]
160                                  // if (schema.getGeometryDescriptor()                                  // ?!??!!??
161                                  // .getName().equals(aDesc.getName())) {                                  // */
162                                  // visibleAttrNames.add(schema.getGeometryDescriptor()                                  // if (properties.length > 1) {
163                                  // .getLocalName());                                  query = new DefaultQuery(schema.getTypeName(), filter,
164                                  // continue;                                                  visibleAttrNames.toArray(new String[] {}));
165                                    // } else {
166                                    // query = new DefaultQuery(schema.getTypeName(), filter);
167                                  // }                                  // }
                                 //                                        
                                 // 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);  
   
                                 /**  
                                  * I got NPEs when properties contained only [the_geom] ?!??!!??  
                                  * TODO Try again one day... Not today... 20.11.2009, SK  
                                  */  
                                 if (properties.length > 1) {  
                                         query = new DefaultQuery(schema.getTypeName(), filter,  
                                                         properties);  
                                 } else {  
                                         query = new DefaultQuery(schema.getTypeName(), filter);  
                                 }  
168                          }                          }
169                          fc = fs.getFeatures(query);                          fc = fs.getFeatures(query);
170                  }                  }
# Line 173  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 {
# Line 204  public class StyledFeatureCollectionTabl Line 209  public class StyledFeatureCollectionTabl
209                  if (featureArray == null || featureArray.length == 0) {                  if (featureArray == null || featureArray.length == 0) {
210                          colNames = new String[0];                          colNames = new String[0];
211                          colTooltips = new String[0]; // Only set and used in                          colTooltips = new String[0]; // Only set and used in
212                                                                                          // StyledFeatureCollectionTableModel                          // StyledFeatureCollectionTableModel
213                          colClass = new Class[0];                          colClass = new Class[0];
214                  } else {                  } else {
215                          // Struktur der Tabelle vom AttributeMetaDtaaMap übernehmen                          // Struktur der Tabelle vom AttributeMetaDtaaMap übernehmen
216                          SimpleFeatureType schema = featureArray[0].getFeatureType();                          SimpleFeatureType schema = featureArray[0].getFeatureType();
217                          // Pruefen, welche Attribute angezeigt werden                          // Pruefen, welche Attribute angezeigt werden
218                          attrTypes.clear();                          attrTypes.clear();
219                          for (AttributeMetadata amd : amdMap.sortedValuesVisibleOnly()) {                          for (AttributeMetadataInterface amd : amdMapVisibleOnly) {
220                                  AttributeDescriptor type = schema.getDescriptor(amd.getName());                                  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))                                  if (attrFilter == null || attrFilter.accept(type))
227                                          attrTypes.add(type);                                          attrTypes.add(type);
228                          }                          }
229                          // Namen und Attribut-Indizes der angezeigten Spalten ermitteln                          // Namen und Attribut-Indizes der angezeigten Spalten ermitteln
230                          colNames = new String[attrTypes.size()];                          colNames = new String[attrTypes.size()];
231                          colTooltips = new String[attrTypes.size()]; // Only set and used in                          colTooltips = new String[attrTypes.size()]; // Only set and used in
232                                                                                                                  // StyledFeatureCollectionTableModel                          // StyledFeatureCollectionTableModel
233                          colClass = new Class[attrTypes.size()];                          colClass = new Class[attrTypes.size()];
234                          attrIdxForCol = new int[attrTypes.size()];                          attrIdxForCol = new int[attrTypes.size()];
235                          for (int i = 0; i < colNames.length; i++) {                          for (int i = 0; i < colNames.length; i++) {
236                                  AttributeDescriptor descriptor = schema.getDescriptor(amdMap.sortedValuesVisibleOnly().get(i).getName());                                  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...                                  // Not so nice in 26: find the index of an attribute...
244                                  int idx = schema.getAttributeDescriptors().indexOf(descriptor);                                  int idx = schema.getAttributeDescriptors().indexOf(descriptor);
# Line 232  public class StyledFeatureCollectionTabl Line 247  public class StyledFeatureCollectionTabl
247                                  String attName = schema.getAttributeDescriptors().get(idx)                                  String attName = schema.getAttributeDescriptors().get(idx)
248                                                  .getLocalName();                                                  .getLocalName();
249                                  colNames[i] = amdMap.get(attName).getTitle().toString();                                  colNames[i] = amdMap.get(attName).getTitle().toString();
250                                  AttributeMetadata amd = amdMap.get(attName);                                  AttributeMetadataInterface amd = amdMap.get(attName);
251                                  colTooltips[i] = "<html>"+amd.getDesc().toString()+"<br>"+amd.getName()+"</html>";                                  colTooltips[i] = "<html>" + amd.getDesc().toString() + "<br>"
252                                  colClass[i] = schema.getAttributeDescriptors().get(idx).getType()                                                  + amd.getName() + "</html>";
253                                                  .getBinding();                                  colClass[i] = schema.getAttributeDescriptors().get(idx)
254                                                    .getType().getBinding();
255                          }                          }
256                  }                  }
257    
# Line 244  public class StyledFeatureCollectionTabl Line 260  public class StyledFeatureCollectionTabl
260                  for (int i = 0; i < featureArray.length; i++)                  for (int i = 0; i < featureArray.length; i++)
261                          if (featureArray[i] != null)                          if (featureArray[i] != null)
262                                  featureIdx.put(featureArray[i].getID(), i);                                  featureIdx.put(featureArray[i].getID(), i);
263  //                  //
264  //              // translate the column names                  // // translate the column names
265  //              if (amdMap != null) {                  // if (amdMap != null) {
266  //                      for (int i = 0; i < colNames.length; i++) {                  // for (int i = 0; i < colNames.length; i++) {
267  //                              colTooltips[i] = amdMap.get(colNames[i]).getDesc().toString()                  // colTooltips[i] = amdMap.get(colNames[i]).getDesc().toString()
268  //                                              + "<br>" + colNames[i];                  // + "<br>" + colNames[i];
269  //                              colNames[i] = amdMap.get(colNames[i]).getTitle().toString();                  // colNames[i] = amdMap.get(colNames[i]).getTitle().toString();
270  //                  //
271  //                      }                  // }
272  //              }                  // }
273                  if (fireTableStructureChanged)                  if (fireTableStructureChanged)
274                          fireTableStructureChanged();                          fireTableStructureChanged();
275    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26