1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.contrib.table.components; |
16 |
| |
17 |
| import org.apache.tapestry.IRequestCycle; |
18 |
| import org.apache.tapestry.contrib.table.model.ITableModelSource; |
19 |
| import org.apache.tapestry.util.ComponentAddress; |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public abstract class TablePages extends AbstractTableViewComponent |
36 |
| { |
37 |
| |
38 |
| public abstract int getPagesDisplayed(); |
39 |
| |
40 |
| |
41 |
| private int m_nDisplayPage; |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
0
| public int getDisplayPage()
|
48 |
| { |
49 |
0
| return m_nDisplayPage;
|
50 |
| } |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
0
| public void setDisplayPage(int displayPage)
|
57 |
| { |
58 |
0
| m_nDisplayPage = displayPage;
|
59 |
| } |
60 |
| |
61 |
0
| public int getCurrentPage()
|
62 |
| { |
63 |
0
| return getTableModelSource().getTableModel().getPagingState().getCurrentPage() + 1;
|
64 |
| } |
65 |
| |
66 |
0
| public int getPageCount()
|
67 |
| { |
68 |
0
| return getTableModelSource().getTableModel().getPageCount();
|
69 |
| } |
70 |
| |
71 |
0
| public boolean getCondBack()
|
72 |
| { |
73 |
0
| return getCurrentPage() > 1;
|
74 |
| } |
75 |
| |
76 |
0
| public boolean getCondFwd()
|
77 |
| { |
78 |
0
| return getCurrentPage() < getPageCount();
|
79 |
| } |
80 |
| |
81 |
0
| public boolean getCondCurrent()
|
82 |
| { |
83 |
0
| return getDisplayPage() == getCurrentPage();
|
84 |
| } |
85 |
| |
86 |
0
| public int getStartPage()
|
87 |
| { |
88 |
0
| int nCurrent = getCurrentPage();
|
89 |
0
| int nPagesDisplayed = getPagesDisplayed();
|
90 |
| |
91 |
0
| int nRightMargin = nPagesDisplayed / 2;
|
92 |
0
| int nStop = nCurrent + nRightMargin;
|
93 |
0
| int nLastPage = getPageCount();
|
94 |
| |
95 |
0
| int nLeftAddon = 0;
|
96 |
0
| if (nStop > nLastPage)
|
97 |
0
| nLeftAddon = nStop - nLastPage;
|
98 |
| |
99 |
0
| int nLeftMargin = (nPagesDisplayed - 1) / 2 + nLeftAddon;
|
100 |
0
| int nStart = nCurrent - nLeftMargin;
|
101 |
0
| int nFirstPage = 1;
|
102 |
0
| if (nStart < nFirstPage)
|
103 |
0
| nStart = nFirstPage;
|
104 |
0
| return nStart;
|
105 |
| } |
106 |
| |
107 |
0
| public int getStopPage()
|
108 |
| { |
109 |
0
| int nCurrent = getCurrentPage();
|
110 |
0
| int nPagesDisplayed = getPagesDisplayed();
|
111 |
| |
112 |
0
| int nLeftMargin = (nPagesDisplayed - 1) / 2;
|
113 |
0
| int nStart = nCurrent - nLeftMargin;
|
114 |
0
| int nFirstPage = 1;
|
115 |
| |
116 |
0
| int nRightAddon = 0;
|
117 |
0
| if (nStart < nFirstPage)
|
118 |
0
| nRightAddon = nFirstPage - nStart;
|
119 |
| |
120 |
0
| int nRightMargin = nPagesDisplayed / 2 + nRightAddon;
|
121 |
0
| int nStop = nCurrent + nRightMargin;
|
122 |
0
| int nLastPage = getPageCount();
|
123 |
0
| if (nStop > nLastPage)
|
124 |
0
| nStop = nLastPage;
|
125 |
0
| return nStop;
|
126 |
| } |
127 |
| |
128 |
0
| public Integer[] getPageList()
|
129 |
| { |
130 |
0
| int nStart = getStartPage();
|
131 |
0
| int nStop = getStopPage();
|
132 |
| |
133 |
0
| Integer[] arrPages = new Integer[nStop - nStart + 1];
|
134 |
0
| for (int i = nStart; i <= nStop; i++)
|
135 |
0
| arrPages[i - nStart] = new Integer(i);
|
136 |
| |
137 |
0
| return arrPages;
|
138 |
| } |
139 |
| |
140 |
0
| public Object[] getFirstPageContext()
|
141 |
| { |
142 |
0
| ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
|
143 |
0
| return new Object[] { objAddress, new Integer(1)};
|
144 |
| } |
145 |
| |
146 |
0
| public Object[] getLastPageContext()
|
147 |
| { |
148 |
0
| ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
|
149 |
0
| return new Object[] { objAddress, new Integer(getPageCount())};
|
150 |
| } |
151 |
| |
152 |
0
| public Object[] getBackPageContext()
|
153 |
| { |
154 |
0
| ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
|
155 |
0
| return new Object[] { objAddress, new Integer(getCurrentPage() - 1)};
|
156 |
| } |
157 |
| |
158 |
0
| public Object[] getFwdPageContext()
|
159 |
| { |
160 |
0
| ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
|
161 |
0
| return new Object[] { objAddress, new Integer(getCurrentPage() + 1)};
|
162 |
| } |
163 |
| |
164 |
0
| public Object[] getDisplayPageContext()
|
165 |
| { |
166 |
0
| ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
|
167 |
0
| return new Object[] { objAddress, new Integer(m_nDisplayPage)};
|
168 |
| } |
169 |
| |
170 |
0
| public void changePage(IRequestCycle objCycle)
|
171 |
| { |
172 |
0
| Object[] arrParameters = objCycle.getListenerParameters();
|
173 |
0
| if (arrParameters.length != 2
|
174 |
| && !(arrParameters[0] instanceof ComponentAddress) |
175 |
| && !(arrParameters[1] instanceof Integer)) |
176 |
| { |
177 |
| |
178 |
0
| return;
|
179 |
| } |
180 |
| |
181 |
0
| ComponentAddress objAddress = (ComponentAddress) arrParameters[0];
|
182 |
0
| ITableModelSource objSource = (ITableModelSource) objAddress.findComponent(objCycle);
|
183 |
0
| setCurrentPage(objSource, ((Integer) arrParameters[1]).intValue());
|
184 |
| |
185 |
| |
186 |
0
| objSource.fireObservedStateChange();
|
187 |
| } |
188 |
| |
189 |
0
| public void setCurrentPage(ITableModelSource objSource, int nPage)
|
190 |
| { |
191 |
0
| objSource.getTableModel().getPagingState().setCurrentPage(nPage - 1);
|
192 |
| } |
193 |
| |
194 |
| } |