KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > components > table > DataTablePaginatorBean


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.icefaces.samples.showcase.components.table;
35
36 import com.icesoft.icefaces.samples.showcase.common.Person;
37
38 import java.util.Arrays JavaDoc;
39 import java.util.Comparator JavaDoc;
40
41 /**
42  * <p>The DataTablePaginatorBean Class is a backing bean for the
43  * dataTablePaginator showcase demonstration and is used to store, add, or
44  * remove data to the data table. </p>
45  *
46  * @since 0.3.0
47  */

48 public class DataTablePaginatorBean extends SortableList {
49
50     // table of person data
51

52
53     public static Person[] buildPersonList() {
54         Person[] personsList = new Person[]{
55                 new Person("Mary", "Smith", "555-2629",
56                            "mary.smith@icesoft.com"),
57                 new Person("James", "Johnson", "555-3318",
58                            "james.johnson@icesoft.com"),
59                 new Person("Patricia", "Williams", "555-3702",
60                            "patricia.williams@icesoft.com"),
61                 new Person("John", "Jones", "555-6589",
62                            "john.jones@icesoft.com"),
63                 new Person("Linda", "Brown", "555-4736",
64                            "linda.brown@icesoft.com"),
65                 new Person("Robert", "Davis", "555-9732",
66                            "robert.davis@icesoft.com"),
67                 new Person("Barbara", "Miller", "555-4660",
68                            "barbara.miller@icesoft.com"),
69                 new Person("Michael", "Wilson", "555-1236",
70                            "michael.wilson@icesoft.com"),
71                 new Person("Elizabeth", "Moore", "555-6653",
72                            "elizabeth.moore@icesoft.com"),
73                 new Person("William", "Taylor", "555-1481",
74                            "william.taylor@icesoft.com"),
75                 new Person("David", "Garcia", "555-1717",
76                            "david.garcia@icesoft.com"),
77                 new Person("Maria", "Jackson", "555-8414",
78                            "maria.jackson@icesoft.com"),
79                 new Person("Richard", "White", "555-1887",
80                            "richard.white@icesoft.com"),
81                 new Person("Susan", "Harris", "555-9209",
82                            "susan.harris@icesoft.com"),
83                 new Person("Charles", "Thompson", "555-2040",
84                            "charles.thompson@icesoft.com"),
85                 new Person("Margaret", "Martinez", "555-9976",
86                            "margaret.martinez@icesoft.com"),
87                 new Person("Edward", "Phillips", "555-1325",
88                            "edward.phillips@icesoft.com"),
89         };
90         return personsList;
91     }
92
93     private Person[] persons = buildPersonList();
94
95     private String JavaDoc paginatorLayout = "hor";
96
97     /**
98      *
99      */

100     public DataTablePaginatorBean() {
101         super("lastName");
102     }
103
104     /**
105      * Gets the data paginator layout.
106      *
107      * @return the data paginator layout
108      */

109     public String JavaDoc getPaginatorLayout() {
110         return paginatorLayout;
111     }
112
113     /**
114      * Sets the data paginator layout.
115      *
116      * @param paginatorLayout the data paginator layout
117      */

118     public void setPaginatorLayout(String JavaDoc paginatorLayout) {
119         this.paginatorLayout = paginatorLayout;
120     }
121
122     /**
123      * Determines if the data paginator layout is vertical.
124      *
125      * @return the status of the data paginator layout
126      */

127     public boolean isVertical() {
128         return (paginatorLayout.equalsIgnoreCase("ver"));
129     }
130
131     /**
132      * Gets the person data.
133      *
134      * @return table of person data
135      */

136     public Person[] getPersons() {
137         return persons;
138     }
139
140     /**
141      * Gets the sorted person data.
142      *
143      * @return table of sorted person data
144      */

145     public Person[] getSortedPersons() {
146         sort(getSort(), isAscending());
147         return persons;
148     }
149
150     /**
151      * Determines the sort order.
152      *
153      * @param sortColumn to sort by.
154      * @return whether sort order is ascending or descending.
155      */

156     protected boolean isDefaultAscending(String JavaDoc sortColumn) {
157         return true;
158     }
159
160     /**
161      * Sorts the list of person data.
162      */

163     protected void sort(final String JavaDoc column, final boolean ascending) {
164         Comparator JavaDoc comparator = new Comparator JavaDoc() {
165             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
166                 Person c1 = (Person) o1;
167                 Person c2 = (Person) o2;
168                 if (column == null) {
169                     return 0;
170                 }
171                 if (column.equals("firstName")) {
172                     return ascending ?
173                            c1.getFirstName().compareTo(c2.getFirstName()) :
174                            c2.getFirstName().compareTo(c1.getFirstName());
175                 } else if (column.equals("lastName")) {
176                     return ascending ?
177                            c1.getLastName().compareTo(c2.getLastName()) :
178                            c2.getLastName().compareTo(c1.getLastName());
179                 } else if (column.equals("phoneNo")) {
180                     return ascending ?
181                            c1.getPhoneNo().compareTo(c2.getPhoneNo()) :
182                            c2.getPhoneNo().compareTo(c1.getPhoneNo());
183                 } else if (column.equals("email")) {
184                     return ascending ? c1.getEmail().compareTo(c2.getEmail()) :
185                            c2.getEmail().compareTo(c1.getEmail());
186                 } else return 0;
187             }
188         };
189         Arrays.sort(persons, comparator);
190     }
191
192     /**
193      * Dynamically adds data to the table.
194      */

195     public void addContent() {
196         if (persons.length < 300) {
197             int dataLength = persons.length;
198             Person[] newData = new Person[persons.length + 5];
199             for (int i = 0, j = 0; i < newData.length; i++, j++) {
200                 if (j >= dataLength) {
201                     j = 0;
202                 }
203                 newData[i] = persons[j];
204             }
205             persons = newData;
206         }
207     }
208
209     /**
210      * Dynamically removes data from the table.
211      */

212     public void removeContent() {
213
214         if (persons.length > 5) {
215             int dataLength = persons.length;
216
217             Person[] newData = new Person[persons.length - 5];
218             // copy original data
219
System.arraycopy(persons, 0, newData, 0, dataLength - 5);
220             persons = newData;
221         }
222     }
223
224
225 }
Popular Tags