KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > examples > listexample > PagedSortableCarList


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.myfaces.examples.listexample;
18
19 import java.util.*;
20
21 public class PagedSortableCarList extends SortableList
22 {
23     private List cars = new ArrayList();
24
25     public PagedSortableCarList()
26     {
27         super("type");
28         for (int i = 100; i < 900; i++)
29         {
30             cars.add(new SimpleCar(i, "Car Type " + i, (i%2==0) ? "blue" : "green"));
31         }
32     }
33
34     public List getCars()
35     {
36         sort(getSort(), isAscending());
37         return cars;
38     }
39
40     protected boolean isDefaultAscending(String JavaDoc sortColumn)
41     {
42         return true;
43     }
44
45     protected void sort(final String JavaDoc column, final boolean ascending)
46     {
47         Comparator comparator = new Comparator()
48         {
49             public int compare(Object JavaDoc o1, Object JavaDoc o2)
50             {
51                 SimpleCar c1 = (SimpleCar)o1;
52                 SimpleCar c2 = (SimpleCar)o2;
53                 if (column == null)
54                 {
55                     return 0;
56                 }
57                 if (column.equals("type"))
58                 {
59                     return ascending ? c1.getType().compareTo(c2.getType()) : c2.getType().compareTo(c1.getType());
60                 }
61                 else if (column.equals("color"))
62                 {
63                     return ascending ? c1.getColor().compareTo(c2.getColor()) : c2.getColor().compareTo(c1.getColor());
64                 }
65                 else return 0;
66             }
67         };
68         Collections.sort(cars, comparator);
69     }
70
71 }
72
Popular Tags