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

Legend:
Removed from v.53  
changed lines
  Added in v.58

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26