Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / feature / paging / FeaturePagingHelperImpl.java @ 23761

History | View | Annotate | Download (7.24 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Gobernment (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {Create a JTable TableModel for a FeatureCollection}
26
 */
27
package org.gvsig.fmap.data.feature.paging;
28

    
29
import java.util.Iterator;
30

    
31
import org.gvsig.fmap.data.exceptions.ReadException;
32
import org.gvsig.fmap.data.feature.*;
33

    
34
/**
35
 * Helper class to access the values of a FeatureCollection by position. Handles
36
 * pagination automatically to avoid filling the memory in case of big
37
 * collections.
38
 * 
39
 * TODO: read values in the background when the returned value is near the end
40
 * of the page, instead of loading a page on demand.
41
 * 
42
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
43
 */
44
public class FeaturePagingHelperImpl implements
45
        FeaturePagingHelper {
46

    
47
    private FeatureQuery query;
48

    
49
    private FeatureCollection featureCollection;
50
    
51
    private FeatureStore featureStore;
52

    
53
    private int maxPageSize;
54

    
55
    private int numPages;
56

    
57
    private int currentPage = -1;
58

    
59
    private Feature[] values;
60

    
61
    /**
62
     * Constructs a FeatureCollectionPagingHelper from a FeatureCollection.
63
     * 
64
     * @param featureCollection
65
     *            to extract data from
66
     * @throws ReadException
67
     */
68
    public FeaturePagingHelperImpl(FeatureStore featureStore)
69
            throws ReadException {
70
        this(featureStore, DEFAULT_PAGE_SIZE);
71
    }
72

    
73
    /**
74
     * Constructs a FeatureCollectionPagingHelper from a FeatureCollection.
75
     * 
76
     * @param featureCollection
77
     *            to extract data from
78
     * @param pageSize
79
     *            the number of elements per page data
80
     * @throws ReadException
81
     */
82
    public FeaturePagingHelperImpl(FeatureStore featureStore,
83
            int pageSize) throws ReadException {
84
        this(featureStore, null, pageSize);
85
    }
86

    
87
    /**
88
     * Constructs a FeatureCollectionPagingHelper from a FeatureCollection.
89
     * 
90
     * @param featureCollection
91
     *            to extract data from
92
     * @throws ReadException
93
     */
94
    public FeaturePagingHelperImpl(FeatureStore featureStore,
95
            FeatureQuery featureQuery) throws ReadException {
96
        this(featureStore, featureQuery, DEFAULT_PAGE_SIZE);
97
    }
98

    
99
    /**
100
     * Constructs a FeatureCollectionPagingHelper from a FeatureCollection.
101
     * 
102
     * @param featureCollection
103
     *            to extract data from
104
     * @param pageSize
105
     *            the number of elements per page data
106
     * @throws ReadException
107
     */
108
    public FeaturePagingHelperImpl(FeatureStore featureStore,
109
            FeatureQuery featureQuery,
110
            int pageSize) throws ReadException {
111
        this.maxPageSize = pageSize;
112
        this.featureStore = featureStore;
113
        this.query = featureQuery;
114
        loadFeatureCollection();
115
        setNumPages(calculateNumPages());
116
    }
117

    
118
    public int getMaxPageSize() {
119
        return maxPageSize;
120
    }
121

    
122
    public void setMaxPageSize(int pageSize) {
123
        this.maxPageSize = pageSize;
124
        reloadCurrentPage();
125
    }
126

    
127
    public int getNumPages() {
128
        return numPages;
129
    }
130

    
131
    /**
132
     * Sets the number of pages.
133
     */
134
    private void setNumPages(int numPages) {
135
        this.numPages = numPages;
136
    }
137

    
138
    public int getCurrentPage() {
139
        return currentPage;
140
    }
141

    
142
    public void setCurrentPage(int page) {
143
        
144
        // TODO: change this message for a logging one through SLF4J
145
        System.out.println("Current page: " + page);
146
        
147
        if (page < 0) {
148
            throw new IndexOutOfBoundsException(
149
                    "Error, unable to set helper current page to a "
150
                            + "negative value: " + page);
151
        }
152
        if (page >= getNumPages()) {
153
            throw new IndexOutOfBoundsException(
154
                    "Error, unable to set helper current page to the page num. "
155
                            + page
156
                            + ", as the Collection only has " + getNumPages()
157
                            + " pages of Features");
158
        }
159
        currentPage = page;
160
        loadCurrentPageData();
161
    }
162

    
163
    public int getTotalSize() {
164
        return getFeatureCollection().size();
165
    }
166

    
167
    public Feature getFeatureAt(int index) {
168
        // Check if we have currently loaded the viewed page data,
169
        // or we need to load a new one
170
        int pageForIndex = (int) Math.floor(index / getMaxPageSize());
171

    
172
        if (pageForIndex != getCurrentPage()) {
173
            setCurrentPage(pageForIndex);
174
        }
175
        
176
        int positionForIndex = index - (getCurrentPage() * getMaxPageSize());
177
        
178
        return values[positionForIndex];
179
    }
180
    
181
    public Feature[] getCurrentPageFeatures() {
182
        return values;
183
    }
184

    
185
    public FeatureCollection getFeatureCollection() {
186
        return featureCollection;
187
    }
188
    
189
    public void reloadCurrentPage() {
190
        setNumPages(calculateNumPages());
191
        if (currentPage > -1) {
192
            loadCurrentPageData();
193
        }
194
    }
195
    
196
    public void reload() throws ReadException {
197
        loadFeatureCollection();
198
        reloadCurrentPage();
199
    }
200

    
201
    public FeatureStore getFeatureStore() {
202
        return featureStore;
203
    }
204

    
205
    public FeatureQuery getFeatureQuery() {
206
        return query;
207
    }
208

    
209
    /**
210
     * Calculates the number of pages.
211
     */
212
    private int calculateNumPages() {
213
        return ((int) Math.floor(getTotalSize() / getMaxPageSize())) + 1;
214
    }
215

    
216
    /**
217
     * Loads all the Features of the current page.
218
     */
219
    private void loadCurrentPageData() {
220
        int currentPage = getCurrentPage();
221
        int currentPageSize;
222
        if (currentPage < (numPages - 1)) {
223
            currentPageSize = getMaxPageSize();
224
        } else {
225
            currentPageSize = getTotalSize() - (currentPage * getMaxPageSize());
226
        }
227

    
228
        values = new Feature[currentPageSize];
229

    
230
        int firstPosition = currentPage * getMaxPageSize();
231

    
232
        Iterator iter = getFeatureCollection().iterator(firstPosition);
233
        int i = 0;
234
        while (iter.hasNext() && i < currentPageSize) {
235
            values[i] = (Feature) iter.next();
236
            i++;
237
        }
238
    }
239
    
240
    private void loadFeatureCollection() throws ReadException {
241
        if (featureCollection != null) {
242
            featureCollection.dispose();
243
        }
244
        if (query == null) {
245
            featureCollection = getFeatureStore().getFeatureCollection();
246
        } else {
247
            featureCollection = getFeatureStore().getFeatureCollection(query);
248
        }
249
    }
250
}