gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.impl / src / main / java / org / gvsig / raster / swing / impl / pagedtable / Pager.java @ 1503
History | View | Annotate | Download (3.67 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 Pager(int entriesPerPage) { |
41 |
this.entriesPerPage = entriesPerPage;
|
42 |
entries = new ArrayList<Object>(); |
43 |
} |
44 |
|
45 |
public void setEntriesPerPage(int n) { |
46 |
this.entriesPerPage = n;
|
47 |
} |
48 |
|
49 |
public void increaseSelectedPage() { |
50 |
if(selectedPage < getPageCount() - 1) |
51 |
selectedPage ++; |
52 |
} |
53 |
|
54 |
public void decreaseSelectedPage() { |
55 |
if (selectedPage > 0) { |
56 |
selectedPage --; |
57 |
} |
58 |
} |
59 |
|
60 |
public void setSelectedRows(int[] rows) { |
61 |
selectedRows = rows; |
62 |
setChanged(); |
63 |
notifyObservers(); |
64 |
} |
65 |
|
66 |
public int[] getSelectedRows() { |
67 |
return selectedRows;
|
68 |
} |
69 |
|
70 |
public void setSelectedPage(int page) { |
71 |
if(page >= 0 && page < getPageCount()) { |
72 |
selectedPage = page; |
73 |
setChanged(); |
74 |
notifyObservers(); |
75 |
} |
76 |
} |
77 |
|
78 |
public List<Object> getSelectedPage() { |
79 |
return getPage(getSelectedPageNumber());
|
80 |
} |
81 |
|
82 |
public int getIndexSelectedPage() { |
83 |
return getSelectedPageNumber();
|
84 |
} |
85 |
|
86 |
public void addEntry(Object entry) { |
87 |
entries.add(entry); |
88 |
} |
89 |
|
90 |
public void removeEntry(int pos) { |
91 |
if(pos >= 0 && pos < entries.size()) |
92 |
entries.remove(pos); |
93 |
} |
94 |
|
95 |
public void swapRow(int i, int j) { |
96 |
if(i == j)
|
97 |
return;
|
98 |
if(j < i) {
|
99 |
int aux = i;
|
100 |
i = j; |
101 |
j = aux; |
102 |
} |
103 |
|
104 |
Object a = entries.get(i);
|
105 |
Object b = entries.get(j);
|
106 |
entries.add(i, b); |
107 |
entries.remove(i + 1);
|
108 |
entries.add(j, a); |
109 |
entries.remove(j + 1);
|
110 |
} |
111 |
|
112 |
public void removePage(int page) { |
113 |
if(page >= 0 && page < getPageCount()) { |
114 |
int init = entriesPerPage * page;
|
115 |
int end = init + entriesPerPage;
|
116 |
for (int i = end - 1; i >= init; i--) { |
117 |
entries.remove(i); |
118 |
} |
119 |
} |
120 |
} |
121 |
|
122 |
public int getPageCount() { |
123 |
return (int)Math.ceil(entries.size() / (double)entriesPerPage); |
124 |
} |
125 |
|
126 |
public List<Object> getPage(int page) { |
127 |
if(page >= 0 && page < getPageCount()) { |
128 |
int init = entriesPerPage * page;
|
129 |
int end = init + entriesPerPage;
|
130 |
end = end >= entries.size() ? entries.size() : end; |
131 |
return entries.subList(init, end);
|
132 |
} |
133 |
return null; |
134 |
} |
135 |
|
136 |
public int getEntriesPerPage() { |
137 |
return entriesPerPage;
|
138 |
} |
139 |
|
140 |
public int getEntriesCount() { |
141 |
return entries.size();
|
142 |
} |
143 |
|
144 |
public int getSelectedPageNumber() { |
145 |
if(selectedPage >= getPageCount()) {
|
146 |
selectedPage = getPageCount() - 1;
|
147 |
if(selectedPage < 0) |
148 |
selectedPage = 0;
|
149 |
} |
150 |
return selectedPage;
|
151 |
} |
152 |
|
153 |
public void removeAllEntries() { |
154 |
entries.clear(); |
155 |
} |
156 |
} |