KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > table > components > TablePages


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
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  * A low level Table component that renders the pages in the table.
23  * This component must be wrapped by {@link org.apache.tapestry.contrib.table.components.TableView}.
24  * <p>
25  * The component generates a list of pages in the Table centered around the
26  * current one and allows you to navigate to other pages.
27  * <p>
28  * Please see the Component Reference for details on how to use this component.
29  *
30  * [<a HREF="../../../../../../../ComponentReference/contrib.TablePages.html">Component Reference</a>]
31  *
32  * @author mindbridge
33  *
34  */

35 public abstract class TablePages extends AbstractTableViewComponent
36 {
37     // Bindings
38
public abstract int getPagesDisplayed();
39
40     // Transient
41
private int m_nDisplayPage;
42
43     /**
44      * Returns the displayPage.
45      * @return int
46      */

47     public int getDisplayPage()
48     {
49         return m_nDisplayPage;
50     }
51
52     /**
53      * Sets the displayPage.
54      * @param displayPage The displayPage to set
55      */

56     public void setDisplayPage(int displayPage)
57     {
58         m_nDisplayPage = displayPage;
59     }
60
61     public int getCurrentPage()
62     {
63         return getTableModelSource().getTableModel().getPagingState().getCurrentPage() + 1;
64     }
65
66     public int getPageCount()
67     {
68         return getTableModelSource().getTableModel().getPageCount();
69     }
70
71     public boolean getCondBack()
72     {
73         return getCurrentPage() > 1;
74     }
75
76     public boolean getCondFwd()
77     {
78         return getCurrentPage() < getPageCount();
79     }
80
81     public boolean getCondCurrent()
82     {
83         return getDisplayPage() == getCurrentPage();
84     }
85
86     public int getStartPage()
87     {
88         int nCurrent = getCurrentPage();
89         int nPagesDisplayed = getPagesDisplayed();
90
91         int nRightMargin = nPagesDisplayed / 2;
92         int nStop = nCurrent + nRightMargin;
93         int nLastPage = getPageCount();
94
95         int nLeftAddon = 0;
96         if (nStop > nLastPage)
97             nLeftAddon = nStop - nLastPage;
98
99         int nLeftMargin = (nPagesDisplayed - 1) / 2 + nLeftAddon;
100         int nStart = nCurrent - nLeftMargin;
101         int nFirstPage = 1;
102         if (nStart < nFirstPage)
103             nStart = nFirstPage;
104         return nStart;
105     }
106
107     public int getStopPage()
108     {
109         int nCurrent = getCurrentPage();
110         int nPagesDisplayed = getPagesDisplayed();
111
112         int nLeftMargin = (nPagesDisplayed - 1) / 2;
113         int nStart = nCurrent - nLeftMargin;
114         int nFirstPage = 1;
115
116         int nRightAddon = 0;
117         if (nStart < nFirstPage)
118             nRightAddon = nFirstPage - nStart;
119
120         int nRightMargin = nPagesDisplayed / 2 + nRightAddon;
121         int nStop = nCurrent + nRightMargin;
122         int nLastPage = getPageCount();
123         if (nStop > nLastPage)
124             nStop = nLastPage;
125         return nStop;
126     }
127
128     public Integer JavaDoc[] getPageList()
129     {
130         int nStart = getStartPage();
131         int nStop = getStopPage();
132
133         Integer JavaDoc[] arrPages = new Integer JavaDoc[nStop - nStart + 1];
134         for (int i = nStart; i <= nStop; i++)
135             arrPages[i - nStart] = new Integer JavaDoc(i);
136
137         return arrPages;
138     }
139
140     public Object JavaDoc[] getFirstPageContext()
141     {
142         ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
143         return new Object JavaDoc[] { objAddress, new Integer JavaDoc(1)};
144     }
145
146     public Object JavaDoc[] getLastPageContext()
147     {
148         ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
149         return new Object JavaDoc[] { objAddress, new Integer JavaDoc(getPageCount())};
150     }
151
152     public Object JavaDoc[] getBackPageContext()
153     {
154         ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
155         return new Object JavaDoc[] { objAddress, new Integer JavaDoc(getCurrentPage() - 1)};
156     }
157
158     public Object JavaDoc[] getFwdPageContext()
159     {
160         ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
161         return new Object JavaDoc[] { objAddress, new Integer JavaDoc(getCurrentPage() + 1)};
162     }
163
164     public Object JavaDoc[] getDisplayPageContext()
165     {
166         ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
167         return new Object JavaDoc[] { objAddress, new Integer JavaDoc(m_nDisplayPage)};
168     }
169
170     public void changePage(IRequestCycle objCycle)
171     {
172         Object JavaDoc[] arrParameters = objCycle.getListenerParameters();
173         if (arrParameters.length != 2
174             && !(arrParameters[0] instanceof ComponentAddress)
175             && !(arrParameters[1] instanceof Integer JavaDoc))
176         {
177             // error
178
return;
179         }
180
181         ComponentAddress objAddress = (ComponentAddress) arrParameters[0];
182         ITableModelSource objSource = (ITableModelSource) objAddress.findComponent(objCycle);
183         setCurrentPage(objSource, ((Integer JavaDoc) arrParameters[1]).intValue());
184
185         // ensure that the change is saved
186
objSource.fireObservedStateChange();
187     }
188
189     public void setCurrentPage(ITableModelSource objSource, int nPage)
190     {
191         objSource.getTableModel().getPagingState().setCurrentPage(nPage - 1);
192     }
193
194 }
195
Popular Tags