View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
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.argeo.eclipse.ui;
17  
18  import org.eclipse.jface.viewers.Viewer;
19  import org.eclipse.jface.viewers.ViewerComparator;
20  
21  public abstract class GenericTableComparator extends ViewerComparator {
22  	private static final long serialVersionUID = -1175894935075325810L;
23  	protected int propertyIndex;
24  	public static final int ASCENDING = 0, DESCENDING = 1;
25  	protected int direction = DESCENDING;
26  
27  	/**
28  	 * Creates an instance of a sorter for TableViewer.
29  	 * 
30  	 * @param defaultColumnIndex
31  	 *            the default sorter column
32  	 */
33  
34  	public GenericTableComparator(int defaultColumnIndex, int direction) {
35  		propertyIndex = defaultColumnIndex;
36  		this.direction = direction;
37  	}
38  
39  	public void setColumn(int column) {
40  		if (column == this.propertyIndex) {
41  			// Same column as last sort; toggle the direction
42  			direction = 1 - direction;
43  		} else {
44  			// New column; do a descending sort
45  			this.propertyIndex = column;
46  			direction = DESCENDING;
47  		}
48  	}
49  
50  	/**
51  	 * Must be Overriden in each view.
52  	 */
53  	public abstract int compare(Viewer viewer, Object e1, Object e2);
54  }