/[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 59 by mojays, Fri Apr 17 17:26:58 2009 UTC
# Line 32  import skrueger.i8n.Translation; Line 32  import skrueger.i8n.Translation;
32   * functionalities of the {@link AttributeMetaData} of   * functionalities of the {@link AttributeMetaData} of
33   * {@linkplain StyledMapInterface styled objects}.   * {@linkplain StyledMapInterface styled objects}.
34   * <ul>   * <ul>
35   * <li>column names are translated according to   *   <li>column names are translated according to {@link AttributeMetaData#getTitle()}</li>
36   * {@link AttributeMetaData#getTitle()}</li>   *   <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>
  * <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>  
37   * </ul>   * </ul>
38   *   * @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)  
  *  
39   */   */
40  public class StyledFeatureCollectionTableModel extends  public class StyledFeatureCollectionTableModel extends FeatureCollectionTableModel {
41                  FeatureCollectionTableModel {    /** Holds the data source as styled map. */
42      protected StyledMapInterface map = null;
43          protected Map<Integer, AttributeMetaData> visibleAMD = null;    /** Contains only the visible elements of the {@link AttributeMetaData}-Map */
44      protected Map<Integer, AttributeMetaData> visibleAMD = null;
45          public StyledFeatureCollectionTableModel(    /** Holds the data source for the table as {@code FeatureSource}. */
46                          StyledFeatureCollectionInterface map) {    protected FeatureSource featureSource = null;
47                  super();    /** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */
48                  setFeatureCollection(map);    protected Map<Integer, AttributeMetaData> origAMD = null;
49          }    /** Holds the current filter on the table */
50      protected Filter filter = null;
51          public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) {  
52                  super();    /**
53                  setFeatureCollection(map);     * Creates a new table model for a styled map.
54          }     * @param map the styled map
55       */
56          protected void setFeatureSource(FeatureSource fs,    public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map) {
57                          Map<Integer, AttributeMetaData> amd) throws Exception {      this(map,Filter.INCLUDE);
58                  FeatureCollection fc = null;    }
59                  this.visibleAMD = null;  
60                  if (fs != null) {    /**
61                          Query query = new DefaultQuery();     * Creates a new table model for a styled map.
62                          if (amd != null) {     * @param map the styled map
63                                  // determine the names of the visible Attributes     * @param filter filter applied to the table
64                                  this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(     */
65                                                  amd, true);    public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map, Filter filter) {
66                                  Vector<String> visibleAttrNames = new Vector<String>();      super();
67        setFeatureCollection(map, filter);
68                                  // Add the column with the geometry (usually "the_geom")    }
69                                  visibleAttrNames.add(fs.getSchema().getDefaultGeometry()  
70                                                  .getLocalName());    /**
71       * Creates a new table model for a styled map.
72                                  for (int attrIdx : visibleAMD.keySet())     * @param map the styled map
73                                          visibleAttrNames.add(fs.getSchema().getAttributeType(     */
74                                                          attrIdx).getLocalName());    public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) {
75        this(map,Filter.INCLUDE);
76                                  // create a query for the visible attributes    }
77                                  String[] properties = visibleAttrNames.toArray(new String[0]);  
78      /**
79                                  query = new DefaultQuery(fs.getSchema().getTypeName(),     * Creates a new table model for a styled map.
80                                                  Filter.INCLUDE, properties);     * @param map the styled map
81                          }     * @param filter filter applied to the table
82                          fc = fs.getFeatures(query);     */
83                  }    public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map, Filter filter) {
84                  setFeatureCollection(fc);      super();
85          }      setFeatureCollection(map, filter);
86      }
87          public void setFeatureCollection(StyledFeatureCollectionInterface map) {  
88                  try {    /**
89                          if (map == null)     * Sets a new data source for the table.
90                                  setFeatureSource(null, null);     * @param fs     the feature source
91                          else {     * @param amd    {@link AttributeMetaData}-Map to define the visible attributes
92                                  FeatureCollection fc = map.getGeoObject();     *               and translation
93                                  String fcName = fc.getFeatureType().getTypeName();     */
94                                  FeatureSource fs = new MemoryDataStore(fc)    protected void setFeatureSource(FeatureSource fs, Map<Integer, AttributeMetaData> amd, Filter filter) throws Exception {
95                                                  .getFeatureSource(fcName);      if ( filter == null )
96                                  setFeatureSource(fs, map.getAttributeMetaDataMap());        filter = Filter.INCLUDE;
97                          }  
98                  } catch (Exception err) {      this.featureSource = fs;
99                          throw new RuntimeException(err);      this.filter        = filter;
100                  }      this.origAMD       = amd;
101          }      this.visibleAMD    = null;
102        
103          public void setFeatureCollection(StyledFeatureSourceInterface map) {      FeatureCollection fc = null;
104                  try {      if (fs != null) {
105                          if (map == null)        Query query = new DefaultQuery();
106                                  setFeatureSource(null, null);        if (amd != null) {
107                          else          // determine the names of the visible Attributes
108                                  setFeatureSource(map.getGeoObject(), map          this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(amd, true);
109                                                  .getAttributeMetaDataMap());          Vector<String> visibleAttrNames = new Vector<String>();
110                  } catch (Exception err) {          // Add the column with the geometry (usually "the_geom")
111                          throw new RuntimeException(err);          visibleAttrNames.add(fs.getSchema().getDefaultGeometry().getLocalName());
112                  }          for (int attrIdx : visibleAMD.keySet())
113          }            visibleAttrNames.add(fs.getSchema().getAttributeType(attrIdx).getLocalName());
114    
115          @Override          // create a query for the visible attributes
116          public void reorganize() {          String[] properties = visibleAttrNames.toArray(new String[0]);
117                  super.reorganize();          query = new DefaultQuery(fs.getSchema().getTypeName(), filter, properties);
118                  // translate the column names        }
119                  if (visibleAMD != null) {        fc = fs.getFeatures(query);
120                          Iterator<Integer> keys = visibleAMD.keySet().iterator();      }
121                          for (int i = 0; i < colNames.length && keys.hasNext(); i++) {      setFeatureCollection(fc);
122                                  Translation title = visibleAMD.get(keys.next()).getTitle();    }
123                                  if (!I8NUtil.isEmpty(title)) {  
124                                          System.out.println("set colname " + i + " to "    /**
125                                                          + title.toString());     * Converts the {@code StyledFeatureCollection} to a {@code FeatureSource}
126                                          colNames[i] = title.toString();     * and sets this as the new data source for the table.
127                                  }     * @param fs     the feature source
128                          }     * @param amd    {@link AttributeMetaData}-Map to define the visible attributes
129                  }     *               and translation
130                  fireTableStructureChanged();     */
131          }    public void setFeatureCollection(StyledFeatureCollectionInterface map, Filter filter) {
132        this.map = map;
133        try {
134          if (map == null)
135            setFeatureSource(null, null, null);
136          else {
137            FeatureCollection fc = map.getGeoObject();
138            String fcName = fc.getFeatureType().getTypeName();
139            FeatureSource fs = new MemoryDataStore(fc).getFeatureSource(fcName);
140            setFeatureSource(fs, map.getAttributeMetaDataMap(), filter);
141          }
142        } catch (Exception err) {
143          throw new RuntimeException(err);
144        }
145      }
146    
147      /**
148       * Sets the {@code StyledFeatureCollection} as new data source for the table.
149       * @param fs     the feature source
150       * @param amd    {@link AttributeMetaData}-Map to define the visible attributes
151       *               and translation
152       */
153      public void setFeatureCollection(StyledFeatureSourceInterface map, Filter filter) {
154        this.map = map;
155        try {
156          if (map == null)
157            setFeatureSource(null, null, null);
158          else
159            setFeatureSource(map.getGeoObject(), map.getAttributeMetaDataMap(), filter);
160        } catch (Exception err) {
161          throw new RuntimeException(err);
162        }
163      }
164      
165      /**
166       * Resets the filter for the table.
167       * @param filter a filter
168       */
169      public void setFilter(Filter filter) {
170        try{
171          setFeatureSource(this.featureSource, this.origAMD, filter);
172        } catch (Exception err) {
173          throw new RuntimeException(err);
174        }
175      }
176    
177      /**
178       * After calling {@code super.reorganize(.)} this method replaced the
179       * column descriptions with the titles of the {@code AttributeMetaData}.
180       * @param fireTableStructureChanged indicates whether a table event is
181       *        initiated after reorganize
182       */
183      @Override
184      protected void reorganize(boolean fireTableStructureChanged) {
185        super.reorganize(false);
186        // translate the column names
187        if (visibleAMD != null) {
188          Iterator<Integer> keys = visibleAMD.keySet().iterator();
189          for (int i = 0; i < colNames.length && keys.hasNext(); i++) {
190            Translation title = visibleAMD.get(keys.next()).getTitle();
191            if (!I8NUtil.isEmpty(title)) {
192              System.out.println("set colname " + i + " to " + title.toString());
193              colNames[i] = title.toString();
194            }
195          }
196        }
197        if ( fireTableStructureChanged )
198          fireTableStructureChanged();
199      }
200  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26