001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package echo2example.email;
031:
032: import java.io.Serializable;
033: import java.util.EventListener;
034: import java.util.EventObject;
035:
036: import nextapp.echo2.app.Button;
037: import nextapp.echo2.app.Extent;
038: import nextapp.echo2.app.Label;
039: import nextapp.echo2.app.Row;
040: import nextapp.echo2.app.TextField;
041: import nextapp.echo2.app.event.ActionEvent;
042: import nextapp.echo2.app.event.ActionListener;
043:
044: /**
045: * A component which provides navigation between pages.
046: */
047: public class PageNavigator extends Row {
048:
049: /**
050: * An <code>EventListener</code> to provide notification of page index
051: * changes.
052: */
053: public static interface PageIndexChangeListener extends
054: EventListener, Serializable {
055:
056: /**
057: * Provides notification of a page index change.
058: *
059: * @param e the <code>PageIndexChangeEvent</code> describing the change
060: */
061: public void pageIndexChanged(PageIndexChangeEvent e);
062: }
063:
064: /**
065: * An <code>EventObject</code> describing a page index change
066: */
067: public class PageIndexChangeEvent extends EventObject {
068:
069: private int newPageIndex;
070:
071: /**
072: * Creates a new <code>PageIndexChangeEvent</code>.
073: *
074: * @param newPageIndex the new page index
075: */
076: private PageIndexChangeEvent(int newPageIndex) {
077: super (PageNavigator.this );
078: this .newPageIndex = newPageIndex;
079: }
080:
081: /**
082: * Returns the new page index.
083: *
084: * @return the new page index
085: */
086: public int getNewPageIndex() {
087: return newPageIndex;
088: }
089: }
090:
091: private Label totalPagesLabel;
092: private TextField pageField;
093: private int pageIndex, totalPages;
094:
095: /**
096: * Creates a new <code>PageNavigator</code>.
097: */
098: public PageNavigator() {
099: super ();
100: setCellSpacing(new Extent(20));
101:
102: Button previousPageButton = new Button(
103: Styles.ICON_24_LEFT_ARROW);
104: previousPageButton.setRolloverEnabled(true);
105: previousPageButton
106: .setRolloverIcon(Styles.ICON_24_LEFT_ARROW_ROLLOVER);
107: previousPageButton.addActionListener(new ActionListener() {
108: public void actionPerformed(ActionEvent e) {
109: setPageIndex(getPageIndex() - 1);
110: }
111: });
112: add(previousPageButton);
113:
114: Row entryRow = new Row();
115: entryRow.setCellSpacing(new Extent(5));
116: add(entryRow);
117:
118: Label itemLabel = new Label(Messages
119: .getString("PageNavigator.ItemLabel"));
120: entryRow.add(itemLabel);
121:
122: pageField = new TextField();
123: pageField.setStyleName("PageNavigator.PageField");
124: pageField.setWidth(new Extent(4, Extent.EX));
125: pageField.setText("1");
126: pageField.addActionListener(new ActionListener() {
127: public void actionPerformed(ActionEvent e) {
128: try {
129: setPageIndex(Integer.parseInt(pageField.getText()) - 1);
130: } catch (NumberFormatException ex) {
131: setPageIndex(getPageIndex());
132: }
133: }
134: });
135: entryRow.add(pageField);
136:
137: Label prepositionLabel = new Label(Messages
138: .getString("PageNavigator.PrepositionLabel"));
139: entryRow.add(prepositionLabel);
140:
141: totalPagesLabel = new Label("1");
142: entryRow.add(totalPagesLabel);
143:
144: Button nextPageButton = new Button(Styles.ICON_24_RIGHT_ARROW);
145: nextPageButton.setRolloverEnabled(true);
146: nextPageButton
147: .setRolloverIcon(Styles.ICON_24_RIGHT_ARROW_ROLLOVER);
148: nextPageButton.addActionListener(new ActionListener() {
149: public void actionPerformed(ActionEvent e) {
150: setPageIndex(getPageIndex() + 1);
151: }
152: });
153: add(nextPageButton);
154: }
155:
156: /**
157: * Adds a listener to be notified of page index changes.
158: *
159: * @param l the listener to add
160: */
161: public void addPageIndexChangeListener(PageIndexChangeListener l) {
162: getEventListenerList().addListener(
163: PageIndexChangeListener.class, l);
164: }
165:
166: /**
167: * Notifies <code>PageIndexChangeListener</code>s that the page index
168: * has changed.
169: */
170: private void firePageIndexChanged() {
171: EventListener[] listeners = getEventListenerList()
172: .getListeners(PageIndexChangeListener.class);
173: PageIndexChangeEvent e = new PageIndexChangeEvent(
174: getPageIndex());
175: for (int i = 0; i < listeners.length; ++i) {
176: ((PageIndexChangeListener) listeners[i])
177: .pageIndexChanged(e);
178: }
179: }
180:
181: /**
182: * Returns the current page index.
183: *
184: * @return the current page index
185: */
186: public int getPageIndex() {
187: return pageIndex;
188: }
189:
190: /**
191: * Returns the total number of pages.
192: *
193: * @return the total number of pages
194: */
195: public int getTotalPages() {
196: return totalPages;
197: }
198:
199: /**
200: * Removes a listener from being notified of page index changes.
201: *
202: * @param l the listener to remove
203: */
204: public void removePageIndexChangeListener(PageIndexChangeListener l) {
205: getEventListenerList().removeListener(
206: PageIndexChangeListener.class, l);
207: }
208:
209: /**
210: * Sets the current page index.
211: *
212: * @param pageIndex the new page index
213: */
214: public void setPageIndex(int pageIndex) {
215: if (pageIndex < 0) {
216: pageIndex = 0;
217: }
218: if (pageIndex > 0 && pageIndex > totalPages - 1) {
219: pageIndex = totalPages - 1;
220: }
221: this .pageIndex = pageIndex;
222: pageField.setText(Integer.toString(pageIndex + 1));
223: firePageIndexChanged();
224: }
225:
226: /**
227: * Sets the total number of pages.
228: *
229: * @param totalPages the total number of pages
230: */
231: public void setTotalPages(int totalPages) {
232: this .totalPages = totalPages;
233: if (totalPages == 0) {
234: totalPagesLabel.setText("1");
235: } else {
236: totalPagesLabel.setText(Integer.toString(totalPages));
237: }
238: }
239: }
|