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
015package org.apache.tapestry.contrib.table.components;
016
017import org.apache.tapestry.IRequestCycle;
018import org.apache.tapestry.contrib.table.model.ITableModelSource;
019import org.apache.tapestry.event.PageBeginRenderListener;
020import org.apache.tapestry.event.PageDetachListener;
021import 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 */
044public 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        setCurrentPage(objSource, getSelectedPage());
091
092        // ensure that the change is saved
093        objSource.fireObservedStateChange();
094    }
095
096    // defined in the JWC file
097    public abstract int getSelectedPage();
098
099
100    /**
101     * @return the current page
102     */
103    public int getCurrentPage()
104    {
105        if (m_nCurrentPage < 0)
106            m_nCurrentPage = super.getCurrentPage();
107        return m_nCurrentPage;
108    }
109
110    /**
111     * @return number of all pages to display
112     */
113    public int getPageCount()
114    {
115        if (m_nPageCount < 0)
116            m_nPageCount = super.getPageCount();
117        return m_nPageCount;
118    }
119
120    /**
121     * @return the first page to display
122     */
123    public int getStartPage()
124    {
125        if (m_nStartPage < 0)
126            m_nStartPage = super.getStartPage();
127        return m_nStartPage;
128    }
129
130    /**
131     * @return the last page to display
132     */
133    public int getStopPage()
134    {
135        if (m_nStopPage < 0)
136            m_nStopPage = super.getStopPage();
137        return m_nStopPage;
138    }
139
140    /**
141     * @param i the current page
142     */
143    public void setCurrentPage(int i)
144    {
145        m_nCurrentPage = i;
146    }
147
148    /**
149     * @param i number of all pages to display
150     */
151    public void setPageCount(int i)
152    {
153        m_nPageCount = i;
154    }
155
156    /**
157     * @param i the first page to display
158     */
159    public void setStartPage(int i)
160    {
161        m_nStartPage = i;
162    }
163
164    /**
165     * @param i the last page to display
166     */
167    public void setStopPage(int i)
168    {
169        m_nStopPage = i;
170    }
171
172}