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.event.PageBeginRenderListener;
020    import org.apache.tapestry.event.PageDetachListener;
021    import org.apache.tapestry.event.PageEvent;
022    
023    /**
024     * A low level Table component that renders the pages in the table.
025     * 
026     * This component is a variant of {@link org.apache.tapestry.contrib.table.components.TablePages}, 
027     * but is designed for operation in a form. The necessary page data is stored 
028     * in hidden fields, so that no StaleLink exceptions occur during a rewind. 
029     * The links also submit the form, which ensures that the data in the other 
030     * form fields is preserved even when the page chages.
031     *  
032     * The component must be wrapped by {@link org.apache.tapestry.contrib.table.components.TableView}.
033     * <p>
034     * The component generates a list of pages in the Table centered around the 
035     * current one and allows you to navigate to other pages.
036     * <p> 
037     * Please see the Component Reference for details on how to use this component. 
038     * 
039     *  [<a href="../../../../../../../ComponentReference/contrib.TableFormPages.html">Component Reference</a>]
040     * 
041     * @author mindbridge
042     *
043     */
044    public abstract class TableFormPages extends TablePages 
045        implements PageDetachListener, PageBeginRenderListener
046    {
047        private int m_nCurrentPage;
048        private int m_nPageCount;
049        private int m_nStartPage;
050        private int m_nStopPage;    
051    
052        public TableFormPages()
053        {
054            initialize();
055        }
056    
057        /**
058         * @see org.apache.tapestry.event.PageDetachListener#pageDetached(org.apache.tapestry.event.PageEvent)
059         */
060        public void pageDetached(PageEvent event)
061        {
062            initialize();
063        }
064        
065        /**
066         * @see org.apache.tapestry.event.PageBeginRenderListener#pageBeginRender(org.apache.tapestry.event.PageEvent)
067         */
068        public void pageBeginRender(PageEvent event)
069        {
070            // values set during rewind are removed
071            initialize();
072        }
073    
074        /**
075         * Initialize the values and return the object to operation identical
076         * to that of the super class.
077         */
078        private void initialize()
079        {
080            m_nCurrentPage = -1;
081            m_nPageCount = -1;
082            m_nStartPage = -1;
083            m_nStopPage = -1;
084        }
085    
086        // This would ideally be a delayed invocation -- called after the form rewind
087        public void changePage(IRequestCycle objCycle)
088        {
089            ITableModelSource objSource = getTableModelSource(); 
090            objSource.storeTableAction(new TableActionPageChange(getSelectedPage()));
091        }
092    
093        // defined in the JWC file
094        public abstract int getSelectedPage();
095    
096    
097        /**
098         * @return the current page
099         */
100        public int getCurrentPage()
101        {
102            if (m_nCurrentPage < 0)
103                m_nCurrentPage = super.getCurrentPage();
104            return m_nCurrentPage;
105        }
106    
107        /**
108         * @return number of all pages to display
109         */
110        public int getPageCount()
111        {
112            if (m_nPageCount < 0)
113                m_nPageCount = super.getPageCount();
114            return m_nPageCount;
115        }
116    
117        /**
118         * @return the first page to display
119         */
120        public int getStartPage()
121        {
122            if (m_nStartPage < 0)
123                m_nStartPage = super.getStartPage();
124            return m_nStartPage;
125        }
126    
127        /**
128         * @return the last page to display
129         */
130        public int getStopPage()
131        {
132            if (m_nStopPage < 0)
133                m_nStopPage = super.getStopPage();
134            return m_nStopPage;
135        }
136    
137        /**
138         * @param i the current page
139         */
140        public void setCurrentPage(int i)
141        {
142            m_nCurrentPage = i;
143        }
144    
145        /**
146         * @param i number of all pages to display
147         */
148        public void setPageCount(int i)
149        {
150            m_nPageCount = i;
151        }
152    
153        /**
154         * @param i the first page to display
155         */
156        public void setStartPage(int i)
157        {
158            m_nStartPage = i;
159        }
160    
161        /**
162         * @param i the last page to display
163         */
164        public void setStopPage(int i)
165        {
166            m_nStopPage = i;
167        }
168    
169    }