/[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 55 by mojays, Fri Apr 17 14:26:14 2009 UTC revision 59 by mojays, Fri Apr 17 17:26:58 2009 UTC
# Line 27  import skrueger.AttributeMetaData; Line 27  import skrueger.AttributeMetaData;
27  import skrueger.i8n.I8NUtil;  import skrueger.i8n.I8NUtil;
28  import skrueger.i8n.Translation;  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} 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 {@link AttributeMetaData#getTitle()}</li>   *   <li>column names are translated according to {@link AttributeMetaData#getTitle()}</li>
36   *   <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 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      /** Contains the complete {@link AttributeMetaData}-Map of the styled layer. */
48      protected Map<Integer, AttributeMetaData> origAMD = null;
49      /** Holds the current filter on the table */
50      protected Filter filter = null;
51    
52      /**
53       * Creates a new table model for a styled map.
54       * @param map the styled map
55       */
56    public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map) {    public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map) {
57        this(map,Filter.INCLUDE);
58      }
59    
60      /**
61       * Creates a new table model for a styled map.
62       * @param map the styled map
63       * @param filter filter applied to the table
64       */
65      public StyledFeatureCollectionTableModel(StyledFeatureCollectionInterface map, Filter filter) {
66      super();      super();
67      setFeatureCollection(map);      setFeatureCollection(map, filter);
68    }    }
69      
70      /**
71       * Creates a new table model for a styled map.
72       * @param map the styled map
73       */
74    public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) {    public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map) {
75        this(map,Filter.INCLUDE);
76      }
77    
78      /**
79       * Creates a new table model for a styled map.
80       * @param map the styled map
81       * @param filter filter applied to the table
82       */
83      public StyledFeatureCollectionTableModel(StyledFeatureSourceInterface map, Filter filter) {
84      super();      super();
85      setFeatureCollection(map);      setFeatureCollection(map, filter);
86    }    }
87      
88    protected void setFeatureSource(FeatureSource fs, Map<Integer,AttributeMetaData> amd) throws Exception {    /**
89       * Sets a new data source for the table.
90       * @param fs     the feature source
91       * @param amd    {@link AttributeMetaData}-Map to define the visible attributes
92       *               and translation
93       */
94      protected void setFeatureSource(FeatureSource fs, Map<Integer, AttributeMetaData> amd, Filter filter) throws Exception {
95        if ( filter == null )
96          filter = Filter.INCLUDE;
97    
98        this.featureSource = fs;
99        this.filter        = filter;
100        this.origAMD       = amd;
101        this.visibleAMD    = null;
102        
103      FeatureCollection fc = null;      FeatureCollection fc = null;
104      this.visibleAMD      = null;      if (fs != null) {
     if ( fs != null ) {  
105        Query query = new DefaultQuery();        Query query = new DefaultQuery();
106        if ( amd != null ) {        if (amd != null) {
107          // determine the names of the visible Attributes          // determine the names of the visible Attributes
108          this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(amd, true);          this.visibleAMD = StyledMapUtil.getVisibleAttributeMetaData(amd, true);
109          Vector<String> visibleAttrNames = new Vector<String>();          Vector<String> visibleAttrNames = new Vector<String>();
110          for ( int attrIdx : visibleAMD.keySet() )          // Add the column with the geometry (usually "the_geom")
111            visibleAttrNames.add( fs.getSchema().getAttributeType(attrIdx).getLocalName() );          visibleAttrNames.add(fs.getSchema().getDefaultGeometry().getLocalName());
112          // create a query for the visible attributes            for (int attrIdx : visibleAMD.keySet())
113          query = new DefaultQuery(            visibleAttrNames.add(fs.getSchema().getAttributeType(attrIdx).getLocalName());
114                         fs.getSchema().getTypeName(),  
115                         Filter.INCLUDE,          // create a query for the visible attributes
116                         visibleAttrNames.toArray(new String[0])          String[] properties = visibleAttrNames.toArray(new String[0]);
117          );          query = new DefaultQuery(fs.getSchema().getTypeName(), filter, properties);
118        }        }
119        fc = fs.getFeatures(query);        fc = fs.getFeatures(query);
120      }      }
121      setFeatureCollection( fc );      setFeatureCollection(fc);
122    }    }
123      
124    public void setFeatureCollection(StyledFeatureCollectionInterface map) {    /**
125       * Converts the {@code StyledFeatureCollection} to a {@code FeatureSource}
126       * 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       */
131      public void setFeatureCollection(StyledFeatureCollectionInterface map, Filter filter) {
132        this.map = map;
133      try {      try {
134        if ( map == null )        if (map == null)
135          setFeatureSource(null,null);          setFeatureSource(null, null, null);
136        else {        else {
137          FeatureCollection fc     = map.getGeoObject();          FeatureCollection fc = map.getGeoObject();
138          String            fcName = fc.getFeatureType().getTypeName();          String fcName = fc.getFeatureType().getTypeName();
139          FeatureSource     fs     = new MemoryDataStore(fc).getFeatureSource(fcName);          FeatureSource fs = new MemoryDataStore(fc).getFeatureSource(fcName);
140          setFeatureSource(fs, map.getAttributeMetaDataMap());          setFeatureSource(fs, map.getAttributeMetaDataMap(), filter);
141        }        }
142      } catch (Exception err) {      } catch (Exception err) {
143        throw new RuntimeException(err);        throw new RuntimeException(err);
144      }      }
145    }    }
146      
147    public void setFeatureCollection(StyledFeatureSourceInterface map) {    /**
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 {      try {
156        if ( map == null )        if (map == null)
157          setFeatureSource(null,null);          setFeatureSource(null, null, null);
158        else        else
159          setFeatureSource(map.getGeoObject(), map.getAttributeMetaDataMap());          setFeatureSource(map.getGeoObject(), map.getAttributeMetaDataMap(), filter);
160      } catch (Exception err) {      } catch (Exception err) {
161        throw new RuntimeException(err);        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    @Override
184    public void reorganize() {    protected void reorganize(boolean fireTableStructureChanged) {
185      super.reorganize();      super.reorganize(false);
186      // translate the column names      // translate the column names
187      if ( visibleAMD != null ) {      if (visibleAMD != null) {
188        Iterator<Integer> keys = visibleAMD.keySet().iterator();        Iterator<Integer> keys = visibleAMD.keySet().iterator();
189        for (int i=0; i<colNames.length && keys.hasNext(); i++) {        for (int i = 0; i < colNames.length && keys.hasNext(); i++) {
190          Translation title = visibleAMD.get( keys.next() ).getTitle();          Translation title = visibleAMD.get(keys.next()).getTitle();
191          if ( !I8NUtil.isEmpty(title) )          if (!I8NUtil.isEmpty(title)) {
192              System.out.println("set colname " + i + " to " + title.toString());
193            colNames[i] = title.toString();            colNames[i] = title.toString();
194            }
195        }        }
196      }      }
197        if ( fireTableStructureChanged )
198          fireTableStructureChanged();
199    }    }
200  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26