gvsig-raster / org.gvsig.raster / tags / 2.0.0 / org.gvsig.raster.swing / org.gvsig.raster.swing.impl / src / main / java / org / gvsig / raster / swing / impl / pagedtable / Pager.java @ 1705
History | View | Annotate | Download (7.65 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.raster.swing.impl.pagedtable; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.List; |
28 |
import java.util.Observable; |
29 |
|
30 |
|
31 |
/**
|
32 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
33 |
*/
|
34 |
public class Pager extends Observable { |
35 |
private List<Object> entries = null; |
36 |
private int entriesPerPage = 0; |
37 |
private int selectedPage = 0; |
38 |
private int[] selectedRows = null; |
39 |
|
40 |
public static int EVENT_SELECTED_ROWS = 0; |
41 |
public static int EVENT_ADD_ENTRY = 1; |
42 |
public static int EVENT_REMOVE_ENTRY = 2; |
43 |
public static int EVENT_REMOVE_ALL = 3; |
44 |
public static int EVENT_SELECTED_PAGE = 4; |
45 |
|
46 |
public Pager(int entriesPerPage) { |
47 |
this.entriesPerPage = entriesPerPage;
|
48 |
entries = new ArrayList<Object>(); |
49 |
} |
50 |
|
51 |
//*************ROWS**********************
|
52 |
|
53 |
public void setSelectedRows(int[] rows) { |
54 |
selectedRows = rows; |
55 |
setChanged(); |
56 |
notifyObservers(EVENT_SELECTED_ROWS); |
57 |
} |
58 |
|
59 |
public void increaseSelectedRows() { |
60 |
boolean change = false; |
61 |
for (int i = 0; i < selectedRows.length; i++) { |
62 |
if(selectedRows[i] < getEntriesCount()) {
|
63 |
selectedRows[i] ++; |
64 |
change = true;
|
65 |
} |
66 |
} |
67 |
if(change) {
|
68 |
setChanged(); |
69 |
notifyObservers(EVENT_SELECTED_ROWS); |
70 |
} |
71 |
} |
72 |
|
73 |
public void decreaseSelectedRows() { |
74 |
boolean change = false; |
75 |
for (int i = 0; i < selectedRows.length; i++) { |
76 |
if(selectedRows[i] > 0) { |
77 |
selectedRows[i] --; |
78 |
change = true;
|
79 |
} |
80 |
} |
81 |
if(change) {
|
82 |
setChanged(); |
83 |
notifyObservers(EVENT_SELECTED_ROWS); |
84 |
} |
85 |
} |
86 |
|
87 |
public void addSelectedRow(int row) { |
88 |
int[] newSelected = new int[selectedRows.length + 1]; |
89 |
for (int i = 0; i < selectedRows.length; i++) { |
90 |
newSelected[i] = selectedRows[i]; |
91 |
} |
92 |
newSelected[newSelected.length - 1] = row;
|
93 |
selectedRows = newSelected; |
94 |
setChanged(); |
95 |
notifyObservers(EVENT_SELECTED_ROWS); |
96 |
} |
97 |
|
98 |
public int[] getSelectedRows() { |
99 |
return selectedRows;
|
100 |
} |
101 |
|
102 |
/**
|
103 |
* Adds a new entry. In the first place, if it is necessary
|
104 |
* changes the selected page and throws the event to actualize the
|
105 |
* table and controls. Next it will notify the "add entry" event
|
106 |
*
|
107 |
* @param entry
|
108 |
*/
|
109 |
public void addEntry(Object entry) { |
110 |
int newSelectedRow = entries.size();
|
111 |
|
112 |
if(!isInCurrentPage(newSelectedRow)) {
|
113 |
setSelectedPage(getPageFromRow(newSelectedRow)); |
114 |
setChanged(); |
115 |
notifyObservers(EVENT_SELECTED_PAGE); |
116 |
} |
117 |
|
118 |
entries.add(entry); |
119 |
setChanged(); |
120 |
notifyObservers(EVENT_ADD_ENTRY); |
121 |
setSelectedRows(new int[]{newSelectedRow}); |
122 |
} |
123 |
|
124 |
public Object[] getEntry(int row) { |
125 |
if(row >= 0 && row < entries.size()) |
126 |
return (Object[])entries.get(row); |
127 |
return null; |
128 |
} |
129 |
|
130 |
public void removeEntry(int pos) { |
131 |
if(pos >= 0 && pos < entries.size()) { |
132 |
int oldSelectedPageInRow = getPageFromRow(pos);
|
133 |
int oldSelectedPage = getSelectedPageNumber();
|
134 |
|
135 |
entries.remove(pos); |
136 |
|
137 |
if(isSelectedRow(pos)) {
|
138 |
//La p?gina donde est? la entrada a borrar est? en la p?gina seleccionada
|
139 |
if(oldSelectedPageInRow == oldSelectedPage) {
|
140 |
int newRowSelected = selectAlternativeRow(pos);
|
141 |
int newPageSelected = getPageFromRow(newRowSelected);
|
142 |
if(newPageSelected != oldSelectedPage) {
|
143 |
setSelectedPage(newPageSelected); |
144 |
setChanged(); |
145 |
notifyObservers(EVENT_SELECTED_PAGE); |
146 |
return;
|
147 |
} |
148 |
} |
149 |
} |
150 |
setChanged(); |
151 |
notifyObservers(EVENT_REMOVE_ENTRY); |
152 |
} |
153 |
} |
154 |
|
155 |
private int selectAlternativeRow(int row) { |
156 |
for (int i = 0; i < selectedRows.length; i++) { |
157 |
if(selectedRows[i] == row && selectedRows[i] != 0) { |
158 |
selectedRows[i] --; |
159 |
return selectedRows[i];
|
160 |
} |
161 |
} |
162 |
return row;
|
163 |
} |
164 |
|
165 |
public void swapRow(int i, int j) { |
166 |
if(i == j)
|
167 |
return;
|
168 |
if(j < i) {
|
169 |
int aux = i;
|
170 |
i = j; |
171 |
j = aux; |
172 |
} |
173 |
|
174 |
Object a = entries.get(i);
|
175 |
Object b = entries.get(j);
|
176 |
entries.add(i, b); |
177 |
entries.remove(i + 1);
|
178 |
entries.add(j, a); |
179 |
entries.remove(j + 1);
|
180 |
|
181 |
if(!isSelectedRow(j) || !isSelectedRow(i)) { //Si hay solo una seleccionada se intercambia la selecci?n |
182 |
for (int k = 0; k < selectedRows.length; k++) { |
183 |
if(selectedRows[k] == j) {
|
184 |
selectedRows[k] = i; |
185 |
setChanged(); |
186 |
notifyObservers(EVENT_SELECTED_ROWS); |
187 |
break;
|
188 |
} |
189 |
if(selectedRows[k] == i) {
|
190 |
selectedRows[k] = j; |
191 |
setChanged(); |
192 |
notifyObservers(EVENT_SELECTED_ROWS); |
193 |
break;
|
194 |
} |
195 |
} |
196 |
} |
197 |
} |
198 |
|
199 |
public boolean isSelectedRow(int row) { |
200 |
if(selectedRows == null) |
201 |
return false; |
202 |
for (int i = 0; i < selectedRows.length; i++) { |
203 |
if(selectedRows[i] == row)
|
204 |
return true; |
205 |
} |
206 |
return false; |
207 |
} |
208 |
|
209 |
public int getEntriesCount() { |
210 |
return entries.size();
|
211 |
} |
212 |
|
213 |
public Object[] getLastEntry() { |
214 |
return (Object[])entries.get(entries.size() - 1); |
215 |
} |
216 |
|
217 |
public void removeAllEntries() { |
218 |
entries.clear(); |
219 |
setChanged(); |
220 |
notifyObservers(EVENT_REMOVE_ALL); |
221 |
} |
222 |
|
223 |
//*************PAGE**********************
|
224 |
|
225 |
public void setEntriesPerPage(int n) { |
226 |
this.entriesPerPage = n;
|
227 |
setChanged(); |
228 |
notifyObservers(EVENT_SELECTED_PAGE); |
229 |
} |
230 |
|
231 |
public void increaseSelectedPage() { |
232 |
if(selectedPage < getPageCount() - 1) { |
233 |
selectedPage ++; |
234 |
setChanged(); |
235 |
notifyObservers(EVENT_SELECTED_PAGE); |
236 |
} |
237 |
} |
238 |
|
239 |
public void decreaseSelectedPage() { |
240 |
if (selectedPage > 0) { |
241 |
selectedPage --; |
242 |
setChanged(); |
243 |
notifyObservers(EVENT_SELECTED_PAGE); |
244 |
} |
245 |
} |
246 |
|
247 |
public void setSelectedPage(int page) { |
248 |
if(page >= 0 && page < getPageCount()) { |
249 |
selectedPage = page; |
250 |
setChanged(); |
251 |
notifyObservers(EVENT_SELECTED_PAGE); |
252 |
} |
253 |
} |
254 |
|
255 |
public int getPageFromRow(int row) { |
256 |
return (int)(row / getEntriesPerPage()); |
257 |
} |
258 |
|
259 |
public List<Object> getSelectedPage() { |
260 |
return getPage(getSelectedPageNumber());
|
261 |
} |
262 |
|
263 |
public void removePage(int page) { |
264 |
if(page >= 0 && page < getPageCount()) { |
265 |
int init = entriesPerPage * page;
|
266 |
int end = init + entriesPerPage;
|
267 |
for (int i = end - 1; i >= init; i--) { |
268 |
entries.remove(i); |
269 |
} |
270 |
} |
271 |
} |
272 |
|
273 |
public int getPageCount() { |
274 |
return (int)Math.ceil(entries.size() / (double)entriesPerPage); |
275 |
} |
276 |
|
277 |
public boolean isInCurrentPage(int row) { |
278 |
int init = selectedPage * getEntriesPerPage();
|
279 |
int end = init + getEntriesPerPage() - 1; |
280 |
if(row >= init && row <= end)
|
281 |
return true; |
282 |
return false; |
283 |
} |
284 |
|
285 |
public List<Object> getPage(int page) { |
286 |
if(page >= 0 && page < getPageCount()) { |
287 |
int init = entriesPerPage * page;
|
288 |
int end = init + entriesPerPage;
|
289 |
end = end >= entries.size() ? entries.size() : end; |
290 |
return entries.subList(init, end);
|
291 |
} |
292 |
return null; |
293 |
} |
294 |
|
295 |
public int getEntriesPerPage() { |
296 |
return entriesPerPage;
|
297 |
} |
298 |
|
299 |
public int getSelectedPageNumber() { |
300 |
if(selectedPage >= getPageCount()) {
|
301 |
selectedPage = getPageCount() - 1;
|
302 |
if(selectedPage < 0) |
303 |
selectedPage = 0;
|
304 |
} |
305 |
return selectedPage;
|
306 |
} |
307 |
|
308 |
} |