001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.contrib.table.components;
016
017 import org.apache.tapestry.IRequestCycle;
018 import org.apache.tapestry.contrib.table.model.ITableModelSource;
019 import org.apache.tapestry.util.ComponentAddress;
020
021 /**
022 * A low level Table component that renders the pages in the table.
023 * This component must be wrapped by {@link org.apache.tapestry.contrib.table.components.TableView}.
024 * <p>
025 * The component generates a list of pages in the Table centered around the
026 * current one and allows you to navigate to other pages.
027 * <p>
028 * Please see the Component Reference for details on how to use this component.
029 *
030 * [<a href="../../../../../../../ComponentReference/contrib.TablePages.html">Component Reference</a>]
031 *
032 * @author mindbridge
033 *
034 */
035 public abstract class TablePages extends AbstractTableViewComponent
036 {
037 // Transient
038 private int m_nDisplayPage;
039
040 // Bindings
041 public abstract int getPagesDisplayed();
042
043 /**
044 * Returns the displayPage.
045 * @return int
046 */
047 public int getDisplayPage()
048 {
049 return m_nDisplayPage;
050 }
051
052 /**
053 * Sets the displayPage.
054 * @param displayPage The displayPage to set
055 */
056 public void setDisplayPage(int displayPage)
057 {
058 m_nDisplayPage = displayPage;
059 }
060
061 public int getCurrentPage()
062 {
063 return getTableModelSource().getTableModel().getPagingState().getCurrentPage() + 1;
064 }
065
066 public int getPageCount()
067 {
068 return getTableModelSource().getTableModel().getPageCount();
069 }
070
071 public boolean getCondBack()
072 {
073 return getCurrentPage() > 1;
074 }
075
076 public boolean getCondFwd()
077 {
078 return getCurrentPage() < getPageCount();
079 }
080
081 public boolean getCondCurrent()
082 {
083 return getDisplayPage() == getCurrentPage();
084 }
085
086 public int getStartPage()
087 {
088 int nCurrent = getCurrentPage();
089 int nPagesDisplayed = getPagesDisplayed();
090
091 int nRightMargin = nPagesDisplayed / 2;
092 int nStop = nCurrent + nRightMargin;
093 int nLastPage = getPageCount();
094
095 int nLeftAddon = 0;
096 if (nStop > nLastPage)
097 nLeftAddon = nStop - nLastPage;
098
099 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[] getPageList()
129 {
130 int nStart = getStartPage();
131 int nStop = getStopPage();
132
133 Integer[] arrPages = new Integer[nStop - nStart + 1];
134 for (int i = nStart; i <= nStop; i++)
135 arrPages[i - nStart] = new Integer(i);
136
137 return arrPages;
138 }
139
140 public Object[] getFirstPageContext()
141 {
142 ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
143 return new Object[] { objAddress, new Integer(1)};
144 }
145
146 public Object[] getLastPageContext()
147 {
148 ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
149 return new Object[] { objAddress, new Integer(getPageCount())};
150 }
151
152 public Object[] getBackPageContext()
153 {
154 ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
155 return new Object[] { objAddress, new Integer(getCurrentPage() - 1)};
156 }
157
158 public Object[] getFwdPageContext()
159 {
160 ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
161 return new Object[] { objAddress, new Integer(getCurrentPage() + 1)};
162 }
163
164 public Object[] getDisplayPageContext()
165 {
166 ComponentAddress objAddress = new ComponentAddress(getTableModelSource());
167 return new Object[] { objAddress, new Integer(m_nDisplayPage)};
168 }
169
170 public void changePage(IRequestCycle objCycle)
171 {
172 Object[] arrParameters = objCycle.getListenerParameters();
173 if (arrParameters.length != 2
174 && !(arrParameters[0] instanceof ComponentAddress)
175 && !(arrParameters[1] instanceof Integer))
176 {
177 // error
178 return;
179 }
180
181 ComponentAddress objAddress = (ComponentAddress)arrParameters[0];
182 ITableModelSource objSource = (ITableModelSource)objAddress.findComponent(objCycle);
183 int page = ((Integer)arrParameters[1]).intValue();
184
185 objSource.storeTableAction(new TableActionPageChange(page));
186 }
187
188 public void setCurrentPage(ITableModelSource objSource, int nPage)
189 {
190 objSource.getTableModel().getPagingState().setCurrentPage(nPage - 1);
191 }
192
193 }