KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.myfaces.examples.listexample;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Comparator JavaDoc;
21 import java.util.List JavaDoc;
22
23 /**
24  * DOCUMENT ME!
25  * @author Thomas Spiegl (latest modification by $Author: matzew $)
26  * @version $Revision: 1.1 $ $Date: 2005/03/24 16:47:10 $
27  */

28 public class SimpleSortableCarList
29     extends SortableList
30 {
31     private List JavaDoc _cars;
32
33     public SimpleSortableCarList()
34     {
35         super("type");
36
37         _cars = new ArrayList JavaDoc();
38         _cars.add(new SimpleCar(1, "car A", "red"));
39         _cars.add(new SimpleCar(1, "car B", "blue"));
40         _cars.add(new SimpleCar(1, "car C", "green"));
41         _cars.add(new SimpleCar(1, "car D", "yellow"));
42         _cars.add(new SimpleCar(1, "car E", "orange"));
43     }
44
45     public List JavaDoc getCars()
46     {
47         sort(getSort(), isAscending());
48         return _cars;
49     }
50
51     protected boolean isDefaultAscending(String JavaDoc sortColumn)
52     {
53         return true;
54     }
55
56     protected void sort(final String JavaDoc column, final boolean ascending)
57     {
58         Comparator JavaDoc comparator = new Comparator JavaDoc()
59         {
60             public int compare(Object JavaDoc o1, Object JavaDoc o2)
61             {
62                 SimpleCar c1 = (SimpleCar)o1;
63                 SimpleCar c2 = (SimpleCar)o2;
64                 if (column == null)
65                 {
66                     return 0;
67                 }
68                 if (column.equals("type"))
69                 {
70                     return ascending ? c1.getType().compareTo(c2.getType()) : c2.getType().compareTo(c1.getType());
71                 }
72                 else if (column.equals("color"))
73                 {
74                     return ascending ? c1.getColor().compareTo(c2.getColor()) : c2.getColor().compareTo(c1.getColor());
75                 }
76                 else return 0;
77             }
78         };
79         Collections.sort(_cars, comparator);
80     }
81 }
82
Popular Tags