/[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 47 by mojays, Fri Apr 17 12:28:50 2009 UTC revision 58 by mojays, Fri Apr 17 15:55:33 2009 UTC
# Line 10  Line 10 
10   **/   **/
11  package skrueger.geotools;  package skrueger.geotools;
12    
13    import java.util.Iterator;
14    import java.util.Map;
15    import java.util.TreeMap;
16    import java.util.Vector;
17    
18    import org.geotools.data.DefaultQuery;
19    import org.geotools.data.FeatureSource;
20    import org.geotools.data.Query;
21    import org.geotools.data.memory.MemoryDataStore;
22    import org.geotools.feature.FeatureCollection;
23    import org.opengis.filter.Filter;
24    
25  import schmitzm.geotools.gui.FeatureCollectionTableModel;  import schmitzm.geotools.gui.FeatureCollectionTableModel;
26    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
32     * functionalities of the {@link AttributeMetaData} of
33     * {@linkplain StyledMapInterface styled objects}.
34     * <ul>
35     *   <li>column names are translated according to {@link AttributeMetaData#getTitle()}</li>
36     *   <li>columns are hidden according to {@link AttributeMetaData#isVisible()()}</li>
37     * </ul>
38     * @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 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      super(null);      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();
65        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      super(null);      this(map,Filter.INCLUDE);
74    }    }
75    
76    public String[] createColumnNames() {    /**
77      return new String[0];     * 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();
83        setFeatureCollection(map, filter);
84    }    }
85      
86    public int getRowCount() {    /**
87      return 0;     * 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;
101        if (fs != null) {
102          Query query = new DefaultQuery();
103          if (amd != null) {
104            // determine the names of the visible Attributes
105            this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(amd, true);
106            Vector<String> visibleAttrNames = new Vector<String>();
107            // Add the column with the geometry (usually "the_geom")
108            visibleAttrNames.add(fs.getSchema().getDefaultGeometry().getLocalName());
109            for (int attrIdx : visibleAMD.keySet())
110              visibleAttrNames.add(fs.getSchema().getAttributeType(attrIdx).getLocalName());
111    
112            // create a query for the visible attributes
113            String[] properties = visibleAttrNames.toArray(new String[0]);
114            query = new DefaultQuery(fs.getSchema().getTypeName(), filter, properties);
115          }
116          fc = fs.getFeatures(query);
117        }
118        setFeatureCollection(fc);
119      }
120    
121      /**
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 {
131          if (map == null)
132            setFeatureSource(null, null, null);
133          else {
134            FeatureCollection fc = map.getGeoObject();
135            String fcName = fc.getFeatureType().getTypeName();
136            FeatureSource fs = new MemoryDataStore(fc).getFeatureSource(fcName);
137            setFeatureSource(fs, map.getAttributeMetaDataMap(), filter);
138          }
139        } catch (Exception err) {
140          throw new RuntimeException(err);
141        }
142      }
143    
144      /**
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 {
153          if (map == null)
154            setFeatureSource(null, null, null);
155          else
156            setFeatureSource(map.getGeoObject(), map.getAttributeMetaDataMap(), filter);
157        } catch (Exception err) {
158          throw new RuntimeException(err);
159        }
160    }    }
161        
162    public Object getValueAt(int row, int col) {    /**
163      return "";     * 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
181      protected void reorganize(boolean fireTableStructureChanged) {
182        super.reorganize(false);
183        // translate the column names
184        if (visibleAMD != null) {
185          Iterator<Integer> keys = visibleAMD.keySet().iterator();
186          for (int i = 0; i < colNames.length && keys.hasNext(); i++) {
187            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.47  
changed lines
  Added in v.58

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26