/[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/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 1043 by alfonx, Wed Sep 22 11:19:06 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.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 40  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;  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   * <li>Any filter defined in the {@link StyledFeaturesInterface} will be
63   * applied.</li>   * 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 amdMap = 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 **/
# Line 77  public class StyledFeatureCollectionTabl Line 81  public class StyledFeatureCollectionTabl
81           */           */
82          protected String[] colTooltips = null;          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 91  public class StyledFeatureCollectionTabl Line 98  public class StyledFeatureCollectionTabl
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 amdm           * @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(          protected void setFeatureSource(
120                          FeatureSource<SimpleFeatureType, SimpleFeature> fs,                          FeatureSource<SimpleFeatureType, SimpleFeature> fs,
121                          AttributeMetadataMap amdm, Filter filter) 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;
# Line 109  public class StyledFeatureCollectionTabl Line 126  public class StyledFeatureCollectionTabl
126                  // this.featureSource = fs;                  // this.featureSource = fs;
127                  this.filter = filter;                  this.filter = filter;
128                  this.amdMap = amdm;                  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) {
# Line 118  public class StyledFeatureCollectionTabl Line 136  public class StyledFeatureCollectionTabl
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 (amdm != null) {                          if (amdm != null) {
139                                  Vector<String> visibleAttrNames = new Vector<String>();                                  LinkedHashSet<String> visibleAttrNames = new LinkedHashSet<String>();
140    
141                                  // Add the column with the geometry (usually "the_geom")                                  // Add the column with the geometry (usually "the_geom") always
142                                  visibleAttrNames.add(schema.getGeometryDescriptor()                                  visibleAttrNames.add(schema.getGeometryDescriptor()
143                                                  .getLocalName());                                                  .getLocalName());
144    
145                                  // Add other visible attributes as ordered by weights                                  // Add other visible attributes as ordered by weights
146                                  for (AttributeMetadata a : amdm.sortedValuesVisibleOnly()) {                                  for (AttributeMetadataInterface a : amdMapVisibleOnly) {
147                                          visibleAttrNames.add(a.getLocalName());                                          visibleAttrNames.add(a.getLocalName());
148                                  }                                  }
149    
150                                  // for (AttributeDescriptor aDesc :                                  // Tested with 2.6.x trunk from 2009-11-26 and it now works. So
151                                  // schema.getAttributeDescriptors()) {                                  // we only request the properties we need!
152                                  //                                                                        // /**
153                                  // // Always add the geometry                                  // * I got NPEs when properties contained only [the_geom]
154                                  // if (schema.getGeometryDescriptor()                                  // ?!??!!??
155                                  // .getName().equals(aDesc.getName())) {                                  // */
156                                  // visibleAttrNames.add(schema.getGeometryDescriptor()                                  // if (properties.length > 1) {
157                                  // .getLocalName());                                  query = new DefaultQuery(schema.getTypeName(), filter,
158                                  // continue;                                                  visibleAttrNames.toArray(new String[] {}));
159                                  // }                                  // } else {
160                                  //                                                                        // query = new DefaultQuery(schema.getTypeName(), filter);
                                 // if (amd.get(aDesc.getName()).isVisible())  
                                 // visibleAttrNames.add(aDesc.getName().getLocalPart());  
161                                  // }                                  // }
                                 //  
                                 // // 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);  
                                 }  
162                          }                          }
163                          fc = fs.getFeatures(query);                          fc = fs.getFeatures(query);
164                  }                  }
# Line 173  public class StyledFeatureCollectionTabl Line 172  public class StyledFeatureCollectionTabl
172           * @param fs           * @param fs
173           *            the feature source           *            the feature source
174           * @param amd           * @param amd
175           *            {@link AttributeMetadata}-Map to define the visible attributes           *            {@link AttributeMetadataImpl}-Map to define the visible attributes
176           *            and translation           *            and translation
177           */           */
178          public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) {          public void setStyledFeatures(StyledFeaturesInterface<?> styledFeatures) {
# Line 204  public class StyledFeatureCollectionTabl Line 203  public class StyledFeatureCollectionTabl
203                  if (featureArray == null || featureArray.length == 0) {                  if (featureArray == null || featureArray.length == 0) {
204                          colNames = new String[0];                          colNames = new String[0];
205                          colTooltips = new String[0]; // Only set and used in                          colTooltips = new String[0]; // Only set and used in
206                                                                                          // StyledFeatureCollectionTableModel                          // StyledFeatureCollectionTableModel
207                          colClass = new Class[0];                          colClass = new Class[0];
208                  } else {                  } else {
209                          // Struktur der Tabelle vom AttributeMetaDtaaMap übernehmen                          // Struktur der Tabelle vom AttributeMetaDtaaMap übernehmen
210                          SimpleFeatureType schema = featureArray[0].getFeatureType();                          SimpleFeatureType schema = featureArray[0].getFeatureType();
211                          // Pruefen, welche Attribute angezeigt werden                          // Pruefen, welche Attribute angezeigt werden
212                          attrTypes.clear();                          attrTypes.clear();
213                          for (AttributeMetadata amd : amdMap.sortedValuesVisibleOnly()) {                          for (AttributeMetadataInterface amd : amdMapVisibleOnly) {
214                                  AttributeDescriptor type = schema.getDescriptor(amd.getName());                  Name name = amd.getName();
215                    AttributeDescriptor type = schema.getDescriptor(name);
216                    // if type can not be determined by complete name,
217                    // try only the local name
218                    if ( type == null )
219                      type = schema.getDescriptor(name.getLocalPart());
220                                  if (attrFilter == null || attrFilter.accept(type))                                  if (attrFilter == null || attrFilter.accept(type))
221                                          attrTypes.add(type);                                          attrTypes.add(type);
222                          }                          }
223                          // Namen und Attribut-Indizes der angezeigten Spalten ermitteln                          // Namen und Attribut-Indizes der angezeigten Spalten ermitteln
224                          colNames = new String[attrTypes.size()];                          colNames = new String[attrTypes.size()];
225                          colTooltips = new String[attrTypes.size()]; // Only set and used in                          colTooltips = new String[attrTypes.size()]; // Only set and used in
226                                                                                                                  // StyledFeatureCollectionTableModel                          // StyledFeatureCollectionTableModel
227                          colClass = new Class[attrTypes.size()];                          colClass = new Class[attrTypes.size()];
228                          attrIdxForCol = new int[attrTypes.size()];                          attrIdxForCol = new int[attrTypes.size()];
229                          for (int i = 0; i < colNames.length; i++) {                          for (int i = 0; i < colNames.length; i++) {
230                                  AttributeDescriptor descriptor = schema.getDescriptor(amdMap.sortedValuesVisibleOnly().get(i).getName());                  Name name = amdMapVisibleOnly.get(i).getName();
231                    AttributeDescriptor descriptor = schema.getDescriptor(name);
232                    // if type can not be determined by complete name,
233                    // try only the local name
234                    if ( descriptor == null )
235                      descriptor = schema.getDescriptor(name.getLocalPart());
236    
237                                  // Not so nice in 26: find the index of an attribute...                                  // Not so nice in 26: find the index of an attribute...
238                                  int idx = schema.getAttributeDescriptors().indexOf(descriptor);                                  int idx = schema.getAttributeDescriptors().indexOf(descriptor);
# Line 232  public class StyledFeatureCollectionTabl Line 241  public class StyledFeatureCollectionTabl
241                                  String attName = schema.getAttributeDescriptors().get(idx)                                  String attName = schema.getAttributeDescriptors().get(idx)
242                                                  .getLocalName();                                                  .getLocalName();
243                                  colNames[i] = amdMap.get(attName).getTitle().toString();                                  colNames[i] = amdMap.get(attName).getTitle().toString();
244                                  AttributeMetadata amd = amdMap.get(attName);                                  AttributeMetadataInterface amd = amdMap.get(attName);
245                                  colTooltips[i] = "<html>"+amd.getDesc().toString()+"<br>"+amd.getName()+"</html>";                                  colTooltips[i] = "<html>" + amd.getDesc().toString() + "<br>"
246                                  colClass[i] = schema.getAttributeDescriptors().get(idx).getType()                                                  + amd.getName() + "</html>";
247                                                  .getBinding();                                  colClass[i] = schema.getAttributeDescriptors().get(idx)
248                                                    .getType().getBinding();
249                          }                          }
250                  }                  }
251    
# Line 244  public class StyledFeatureCollectionTabl Line 254  public class StyledFeatureCollectionTabl
254                  for (int i = 0; i < featureArray.length; i++)                  for (int i = 0; i < featureArray.length; i++)
255                          if (featureArray[i] != null)                          if (featureArray[i] != null)
256                                  featureIdx.put(featureArray[i].getID(), i);                                  featureIdx.put(featureArray[i].getID(), i);
257  //                  //
258  //              // translate the column names                  // // translate the column names
259  //              if (amdMap != null) {                  // if (amdMap != null) {
260  //                      for (int i = 0; i < colNames.length; i++) {                  // for (int i = 0; i < colNames.length; i++) {
261  //                              colTooltips[i] = amdMap.get(colNames[i]).getDesc().toString()                  // colTooltips[i] = amdMap.get(colNames[i]).getDesc().toString()
262  //                                              + "<br>" + colNames[i];                  // + "<br>" + colNames[i];
263  //                              colNames[i] = amdMap.get(colNames[i]).getTitle().toString();                  // colNames[i] = amdMap.get(colNames[i]).getTitle().toString();
264  //                  //
265  //                      }                  // }
266  //              }                  // }
267                  if (fireTableStructureChanged)                  if (fireTableStructureChanged)
268                          fireTableStructureChanged();                          fireTableStructureChanged();
269    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26