View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.table;
21  
22  /**
23   * Default implementation of the RowComparator.
24   * 
25   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
26   * @version $Id: DefaultRowComparator.java,v 1.5 2008-04-22 12:28:40 jbobnar Exp $
27   *
28   */
29  public class DefaultRowComparator implements RowComparator {
30  
31  	int column = 0;
32  	boolean descending;
33  
34  
35  	public DefaultRowComparator(int col, boolean desc)
36  	{
37  		column = col;
38  		descending = desc;
39  	}
40  
41  	/* (non-Javadoc)
42  	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
43  	 */
44  	public int compare(TableRow o1, TableRow o2)
45  	{
46  		TableRow r1 = (TableRow)o1;
47  		TableRow r2 = (TableRow)o2;
48  
49  		if (descending) {
50  			return r2.compareTo(r1, column);
51  		}
52  
53  		return r1.compareTo(r2, column);
54  	}
55  	
56  	/*
57  	 * (non-Javadoc)
58  	 * @see com.cosylab.gui.components.table.RowComparator#setColumn(int)
59  	 */
60  	public void setColumn(int column) {
61  		this.column = column;
62  	}
63  
64  	/*
65  	 * (non-Javadoc)
66  	 * @see com.cosylab.gui.components.table.RowComparator#setDescending(boolean)
67  	 */
68  	public void setDescending(boolean descending) {
69  		this.descending = descending;
70  	}
71  
72  	/*
73  	 * (non-Javadoc)
74  	 * @see com.cosylab.gui.components.table.RowComparator#getColumn()
75  	 */
76  	public int getColumn() {
77  		return column;
78  	}
79  
80  	/*
81  	 * (non-Javadoc)
82  	 * @see com.cosylab.gui.components.table.RowComparator#getDescending()
83  	 */
84  	public boolean getDescending() {
85  		return descending;
86  	}
87  
88  }