KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echo2example > email > PageNavigator


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package echo2example.email;
31
32 import java.io.Serializable JavaDoc;
33 import java.util.EventListener JavaDoc;
34 import java.util.EventObject JavaDoc;
35
36 import nextapp.echo2.app.Button;
37 import nextapp.echo2.app.Extent;
38 import nextapp.echo2.app.Label;
39 import nextapp.echo2.app.Row;
40 import nextapp.echo2.app.TextField;
41 import nextapp.echo2.app.event.ActionEvent;
42 import nextapp.echo2.app.event.ActionListener;
43
44 /**
45  * A component which provides navigation between pages.
46  */

47 public class PageNavigator extends Row {
48
49     /**
50      * An <code>EventListener</code> to provide notification of page index
51      * changes.
52      */

53     public static interface PageIndexChangeListener extends EventListener JavaDoc, Serializable JavaDoc {
54         
55         /**
56          * Provides notification of a page index change.
57          *
58          * @param e the <code>PageIndexChangeEvent</code> describing the change
59          */

60         public void pageIndexChanged(PageIndexChangeEvent e);
61     }
62     
63     /**
64      * An <code>EventObject</code> describing a page index change
65      */

66     public class PageIndexChangeEvent extends EventObject JavaDoc {
67         
68         private int newPageIndex;
69         
70         /**
71          * Creates a new <code>PageIndexChangeEvent</code>.
72          *
73          * @param newPageIndex the new page index
74          */

75         private PageIndexChangeEvent(int newPageIndex) {
76             super(PageNavigator.this);
77             this.newPageIndex = newPageIndex;
78         }
79         
80         /**
81          * Returns the new page index.
82          *
83          * @return the new page index
84          */

85         public int getNewPageIndex() {
86             return newPageIndex;
87         }
88     }
89
90     private Label totalPagesLabel;
91     private TextField pageField;
92     private int pageIndex, totalPages;
93     
94     /**
95      * Creates a new <code>PageNavigator</code>.
96      */

97     public PageNavigator() {
98         super();
99         setCellSpacing(new Extent(20));
100         
101         Button previousPageButton = new Button(Styles.ICON_24_LEFT_ARROW);
102         previousPageButton.setRolloverEnabled(true);
103         previousPageButton.setRolloverIcon(Styles.ICON_24_LEFT_ARROW_ROLLOVER);
104         previousPageButton.addActionListener(new ActionListener() {
105             public void actionPerformed(ActionEvent e) {
106                 setPageIndex(getPageIndex() - 1);
107             }
108         });
109         add(previousPageButton);
110         
111         Row entryRow = new Row();
112         entryRow.setCellSpacing(new Extent(5));
113         add(entryRow);
114         
115         Label itemLabel = new Label(Messages.getString("PageNavigator.ItemLabel"));
116         entryRow.add(itemLabel);
117         
118         pageField = new TextField();
119         pageField.setStyleName("PageNavigator.PageField");
120         pageField.setWidth(new Extent(4, Extent.EX));
121         pageField.setText("1");
122         pageField.addActionListener(new ActionListener() {
123             public void actionPerformed(ActionEvent e) {
124                 try {
125                     setPageIndex(Integer.parseInt(pageField.getText()) - 1);
126                 } catch (NumberFormatException JavaDoc ex) {
127                     setPageIndex(getPageIndex());
128                 }
129             }
130         });
131         entryRow.add(pageField);
132         
133         Label prepositionLabel = new Label(Messages.getString("PageNavigator.PrepositionLabel"));
134         entryRow.add(prepositionLabel);
135         
136         totalPagesLabel = new Label("1");
137         entryRow.add(totalPagesLabel);
138         
139         Button nextPageButton = new Button(Styles.ICON_24_RIGHT_ARROW);
140         nextPageButton.setRolloverEnabled(true);
141         nextPageButton.setRolloverIcon(Styles.ICON_24_RIGHT_ARROW_ROLLOVER);
142         nextPageButton.addActionListener(new ActionListener() {
143             public void actionPerformed(ActionEvent e) {
144                 setPageIndex(getPageIndex() + 1);
145             }
146         });
147         add(nextPageButton);
148     }
149     
150     /**
151      * Adds a listener to be notified of page index changes.
152      *
153      * @param l the listener to add
154      */

155     public void addPageIndexChangeListener(PageIndexChangeListener l) {
156         getEventListenerList().addListener(PageIndexChangeListener.class, l);
157     }
158     
159     /**
160      * Notifies <code>PageIndexChangeListener</code>s that the page index
161      * has changed.
162      */

163     private void firePageIndexChanged() {
164         EventListener JavaDoc[] listeners = getEventListenerList().getListeners(PageIndexChangeListener.class);
165         PageIndexChangeEvent e = new PageIndexChangeEvent(getPageIndex());
166         for (int i = 0; i < listeners.length; ++i) {
167             ((PageIndexChangeListener) listeners[i]).pageIndexChanged(e);
168         }
169     }
170
171     /**
172      * Returns the current page index.
173      *
174      * @return the current page index
175      */

176     public int getPageIndex() {
177         return pageIndex;
178     }
179     
180     /**
181      * Returns the total number of pages.
182      *
183      * @return the total number of pages
184      */

185     public int getTotalPages() {
186         return totalPages;
187     }
188
189     /**
190      * Removes a listener from being notified of page index changes.
191      *
192      * @param l the listener to remove
193      */

194     public void removePageIndexChangeListener(PageIndexChangeListener l) {
195         getEventListenerList().removeListener(PageIndexChangeListener.class, l);
196     }
197
198     /**
199      * Sets the current page index.
200      *
201      * @param pageIndex the new page index
202      */

203     public void setPageIndex(int pageIndex) {
204         if (pageIndex < 0) {
205             pageIndex = 0;
206         }
207         if (pageIndex > 0 && pageIndex > totalPages - 1) {
208             pageIndex = totalPages - 1;
209         }
210         this.pageIndex = pageIndex;
211         pageField.setText(Integer.toString(pageIndex + 1));
212         firePageIndexChanged();
213     }
214     
215     /**
216      * Sets the total number of pages.
217      *
218      * @param totalPages the total number of pages
219      */

220     public void setTotalPages(int totalPages) {
221         this.totalPages = totalPages;
222         if (totalPages == 0) {
223             totalPagesLabel.setText("1");
224         } else {
225             totalPagesLabel.setText(Integer.toString(totalPages));
226         }
227     }
228 }
229
Popular Tags