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

revision 56 by alfonx, Fri Apr 17 15:19:00 2009 UTC revision 62 by alfonx, Fri Apr 17 18:56:17 2009 UTC
# Line 22  import org.geotools.data.memory.MemoryDa Line 22  import org.geotools.data.memory.MemoryDa
22  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
23  import org.opengis.filter.Filter;  import org.opengis.filter.Filter;
24    
25    import com.vividsolutions.jts.geom.Envelope;
26    
27  import schmitzm.geotools.gui.FeatureCollectionTableModel;  import schmitzm.geotools.gui.FeatureCollectionTableModel;
28  import skrueger.AttributeMetaData;  import skrueger.AttributeMetaData;
29  import skrueger.i8n.I8NUtil;  import skrueger.i8n.I8NUtil;
# Line 32  import skrueger.i8n.Translation; Line 34  import skrueger.i8n.Translation;
34   * functionalities of the {@link AttributeMetaData} of   * functionalities of the {@link AttributeMetaData} of
35   * {@linkplain StyledMapInterface styled objects}.   * {@linkplain StyledMapInterface styled objects}.
36   * <ul>   * <ul>
37   * <li>column names are translated according to   *   <li>column names are translated according to {@link AttributeMetaData#getTitle()}</li>
38   * {@link AttributeMetaData#getTitle()}</li>   *   <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>
  * <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>  
39   * </ul>   * </ul>
40   *   * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)
  * @author <a href="mailto:[email protected]">Martin Schmitz</a>  
  *         (University of Bonn/Germany)  
  *  
41   */   */
42  public class StyledFeatureCollectionTableModel extends  public class StyledFeatureCollectionTableModel extends FeatureCollectionTableModel {
43                  FeatureCollectionTableModel {    /** Holds the data source as styled map. */
44      protected StyledMapInterface map = null;
45          protected Map<Integer, AttributeMetaData> visibleAMD = null;    /** Contains only the visible elements of the {@link AttributeMetaData}-Map */
46      protected Map<Integer, AttributeMetaData> visibleAMD = null;
47          public StyledFeatureCollectionTableModel(    /** Holds the data source for the table as {@code FeatureSource}. */
48                          StyledFeatureCollectionInterface map) {    protected FeatureSource featureSource = null;
49                  super();    /** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */
50                  setFeatureCollection(map);    protected Map<Integer, AttributeMetaData> origAMD = null;
51          }    /** Holds the current filter on the table */
52      protected Filter filter = null;
53          public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) {    /** Holds the Bounds for all features. Only set once during the constructor **/
54                  super();    protected Envelope bounds;
55                  setFeatureCollection(map);  
56          }    /**
57       * Creates a new table model for a styled map.
58          protected void setFeatureSource(FeatureSource fs,     * @param map the styled map
59                          Map<Integer, AttributeMetaData> amd) throws Exception {     */
60                  FeatureCollection fc = null;    public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map) {
61                  this.visibleAMD = null;      this(map,Filter.INCLUDE);
62                  if (fs != null) {    }
63                          Query query = new DefaultQuery();  
64                          if (amd != null) {    /**
65                                  // determine the names of the visible Attributes     * Creates a new table model for a styled map.
66                                  this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(     * @param map the styled map
67                                                  amd, true);     * @param filter filter applied to the table
68                                  Vector<String> visibleAttrNames = new Vector<String>();     */
69      public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map, Filter filter) {
70                                  // Add the column with the geometry (usually "the_geom")      super();
71                                  visibleAttrNames.add(fs.getSchema().getDefaultGeometry()      setFeatureCollection(map, filter);
72                                                  .getLocalName());    }
73    
74                                  for (int attrIdx : visibleAMD.keySet())    /**
75                                          visibleAttrNames.add(fs.getSchema().getAttributeType(     * Creates a new table model for a styled map.
76                                                          attrIdx).getLocalName());     * @param map the styled map
77       */
78                                  // create a query for the visible attributes    public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) {
79                                  String[] properties = visibleAttrNames.toArray(new String[0]);      this(map,Filter.INCLUDE);
80      }
81                                  query = new DefaultQuery(fs.getSchema().getTypeName(),  
82                                                  Filter.INCLUDE, properties);    /**
83                          }     * Creates a new table model for a styled map.
84                          fc = fs.getFeatures(query);     * @param map the styled map
85                  }     * @param filter filter applied to the table
86                  setFeatureCollection(fc);     */
87          }    public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map, Filter filter) {
88        super();
89          public void setFeatureCollection(StyledFeatureCollectionInterface map) {      setFeatureCollection(map, filter);
90                  try {    }
91                          if (map == null)  
92                                  setFeatureSource(null, null);    /**
93                          else {     * Sets a new data source for the table.
94                                  FeatureCollection fc = map.getGeoObject();     * @param fs     the feature source
95                                  String fcName = fc.getFeatureType().getTypeName();     * @param amd    {@link AttributeMetaData}-Map to define the visible attributes
96                                  FeatureSource fs = new MemoryDataStore(fc)     *               and translation
97                                                  .getFeatureSource(fcName);     */
98                                  setFeatureSource(fs, map.getAttributeMetaDataMap());    protected void setFeatureSource(FeatureSource fs, Map<Integer, AttributeMetaData> amd, Filter filter) throws Exception {
99                          }      if ( filter == null )
100                  } catch (Exception err) {        filter = Filter.INCLUDE;
101                          throw new RuntimeException(err);      
102                  }      this.featureSource = fs;
103          }      this.filter        = filter;
104        this.origAMD       = amd;
105          public void setFeatureCollection(StyledFeatureSourceInterface map) {      this.visibleAMD    = null;
106                  try {      
107                          if (map == null)      FeatureCollection fc = null;
108                                  setFeatureSource(null, null);      if (fs != null) {
109                          else          
110                                  setFeatureSource(map.getGeoObject(), map         bounds = fs.getBounds();
111                                                  .getAttributeMetaDataMap());          
112                  } catch (Exception err) {        Query query = new DefaultQuery(fs.getSchema().getTypeName(), filter);
113                          throw new RuntimeException(err);        if (amd != null) {
114                  }          // determine the names of the visible Attributes
115          }          this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(amd, true);
116            Vector<String> visibleAttrNames = new Vector<String>();
117          @Override          // Add the column with the geometry (usually "the_geom")
118          public void reorganize() {          visibleAttrNames.add(fs.getSchema().getDefaultGeometry().getLocalName());
119                  super.reorganize();          for (int attrIdx : visibleAMD.keySet())
120                  // translate the column names            visibleAttrNames.add(fs.getSchema().getAttributeType(attrIdx).getLocalName());
121                  if (visibleAMD != null) {  
122                          Iterator<Integer> keys = visibleAMD.keySet().iterator();          // create a query for the visible attributes
123                          for (int i = 0; i < colNames.length && keys.hasNext(); i++) {          String[] properties = visibleAttrNames.toArray(new String[0]);
124                                  Translation title = visibleAMD.get(keys.next()).getTitle();          
125                                  if (!I8NUtil.isEmpty(title)) {          query = new DefaultQuery(fs.getSchema().getTypeName(), filter, properties);
126                                          System.out.println("set colname " + i + " to "        }
127                                                          + title.toString());        fc = fs.getFeatures(query);
128                                          colNames[i] = title.toString();  
129                                  }  // FAILS:!!!, even with  query = new DefaultQuery(fs.getSchema().getTypeName(), filter);
130                          }                          // java.lang.UnsupportedOperationException: Unknown feature
131                  }                          // attribute: PQM_MOD
132                  fireTableStructureChanged();                          // at
133                            // schmitzm.geotools.feature.FeatureOperationTree.evaluate(FeatureOperationTree.java:93)
134                            // bounds = fc.getBounds();
135                            // SK, 17.4.2009
136                            //      
137                            // System.out.println("Filter = "+filter);
138                            // System.out.println("Size of FC = "+fc.size());
139                            // System.out.println("anz att= "+fc.getNumberOfAttributes());
140        }
141        setFeatureCollection(fc);
142      }
143    
144      /**
145       * Converts the {@code StyledFeatureCollection} to a {@code FeatureSource}
146       * and sets this as the new data source for the table.
147       * @param fs     the feature source
148       * @param amd    {@link AttributeMetaData}-Map to define the visible attributes
149       *               and translation
150       */
151      public void setFeatureCollection(StyledFeatureCollectionInterface map, Filter filter) {
152        this.map = map;
153        try {
154          if (map == null)
155            setFeatureSource(null, null, null);
156          else {
157            FeatureCollection fc = map.getGeoObject();
158            String fcName = fc.getFeatureType().getTypeName();
159            FeatureSource fs = new MemoryDataStore(fc).getFeatureSource(fcName);
160            setFeatureSource(fs, map.getAttributeMetaDataMap(), filter);
161          }
162        } catch (Exception err) {
163          throw new RuntimeException(err);
164        }
165      }
166    
167      /**
168       * Sets the {@code StyledFeatureCollection} as new data source for the table.
169       * @param fs     the feature source
170       * @param amd    {@link AttributeMetaData}-Map to define the visible attributes
171       *               and translation
172       */
173      public void setFeatureCollection(StyledFeatureSourceInterface map, Filter filter) {
174        this.map = map;
175        try {
176          if (map == null)
177            setFeatureSource(null, null, null);
178          else
179            setFeatureSource(map.getGeoObject(), map.getAttributeMetaDataMap(), filter);
180        } catch (Exception err) {
181          throw new RuntimeException(err);
182        }
183      }
184      
185      /**
186       * Resets the filter for the table.
187       * @param filter a filter
188       */
189      public void setFilter(Filter filter) {
190        try{
191          setFeatureSource(this.featureSource, this.origAMD, filter);
192        } catch (Exception err) {
193          throw new RuntimeException(err);
194        }
195      }
196      
197      /**
198       * @return <code>Filter.INCLUDE</code> or the {@link Filter} applied to the Features
199       */
200      public Filter getFilter() {
201              return this.filter;
202      }
203    
204      /**
205       * After calling {@code super.reorganize(.)} this method replaced the
206       * column descriptions with the titles of the {@code AttributeMetaData}.
207       * @param fireTableStructureChanged indicates whether a table event is
208       *        initiated after reorganize
209       */
210      @Override
211      protected void reorganize(boolean fireTableStructureChanged) {
212        super.reorganize(false);
213        // translate the column names
214        if (visibleAMD != null) {
215          Iterator<Integer> keys = visibleAMD.keySet().iterator();
216          for (int i = 0; i < colNames.length && keys.hasNext(); i++) {
217            Translation title = visibleAMD.get(keys.next()).getTitle();
218            if (!I8NUtil.isEmpty(title)) {
219    //          System.out.println("set colname " + i + " to " + title.toString());
220              colNames[i] = title.toString();
221            }
222          }
223        }
224        if ( fireTableStructureChanged )
225          fireTableStructureChanged();
226      }
227    
228      /**
229             * @returns Cached bounds for the whole dataset (without applying the
230             *          filter) or <code>null</code>
231             */
232            public Envelope getBounds() {
233                    return bounds;
234          }          }
235  }  }

Legend:
Removed from v.56  
changed lines
  Added in v.62

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26