KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > table > RowComparator


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.table;
14
15 import java.util.Comparator JavaDoc;
16
17 /**
18  * compares two TableRow objects for sorting
19  */

20
21 class RowComparator implements Comparator JavaDoc {
22   TableColumn column;
23
24   public RowComparator() {
25     column = new TableColumn(0);
26   }
27
28   public RowComparator(TableColumn column) {
29     this.column = column;
30   }
31
32   public int compare(Object JavaDoc o1, Object JavaDoc o2) {
33     TableRow tr1 = (TableRow)o1;
34     TableRow tr2 = (TableRow)o2;
35     int columnIndex = column.getColumnIndex();
36     Object JavaDoc v1 = tr1.getValue(columnIndex);
37     Object JavaDoc v2 = tr2.getValue(columnIndex);
38     Comparator JavaDoc comp = column.getComparator();
39     int res;
40     if (v1 == null && v2 == null)
41       res = 0;
42     else if (v1 == null)
43       res = 1;
44     else if (v2 == null)
45       res = -1;
46     else
47       res = comp.compare(v1, v2);
48     if (column.isDescending())
49       return -res;
50     return res;
51   }
52
53   public TableColumn getColumn() {
54     return column;
55   }
56
57   public void setColumn(TableColumn column) {
58     this.column = column;
59   }
60
61   public int getColumnIndex() {
62     return column.getColumnIndex();
63   }
64
65 }
Popular Tags