trace/traceviewer/com.nokia.trace.eventview/src/com/nokia/trace/eventview/TableColumnSorter.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     1 /*
       
     2  * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  * Table column sorter
       
    17  *
       
    18  */
       
    19 package com.nokia.trace.eventview;
       
    20 
       
    21 import org.eclipse.jface.viewers.TableViewer;
       
    22 import org.eclipse.jface.viewers.Viewer;
       
    23 import org.eclipse.jface.viewers.ViewerComparator;
       
    24 import org.eclipse.swt.SWT;
       
    25 import org.eclipse.swt.events.SelectionAdapter;
       
    26 import org.eclipse.swt.events.SelectionEvent;
       
    27 import org.eclipse.swt.widgets.TableColumn;
       
    28 
       
    29 /**
       
    30  * Table column sorter class
       
    31  * 
       
    32  */
       
    33 public abstract class TableColumnSorter extends ViewerComparator {
       
    34 
       
    35 	/**
       
    36 	 * Ascending sorting
       
    37 	 */
       
    38 	public static final int ASC = 1;
       
    39 
       
    40 	/**
       
    41 	 * No sorting
       
    42 	 */
       
    43 	public static final int NONE = 0;
       
    44 
       
    45 	/**
       
    46 	 * Descending sorting
       
    47 	 */
       
    48 	public static final int DESC = -1;
       
    49 
       
    50 	/**
       
    51 	 * Current direction
       
    52 	 */
       
    53 	private int direction = 0;
       
    54 
       
    55 	/**
       
    56 	 * Column for this sorter
       
    57 	 */
       
    58 	private TableColumn column;
       
    59 
       
    60 	/**
       
    61 	 * Table viewer for this column
       
    62 	 */
       
    63 	private TableViewer viewer;
       
    64 
       
    65 	/**
       
    66 	 * Constructor
       
    67 	 * 
       
    68 	 * @param viewer
       
    69 	 *            table viewer
       
    70 	 * @param column
       
    71 	 *            column
       
    72 	 */
       
    73 	public TableColumnSorter(final TableViewer viewer, TableColumn column) {
       
    74 		this.column = column;
       
    75 		this.viewer = viewer;
       
    76 		this.column.addSelectionListener(new SelectionAdapter() {
       
    77 
       
    78 			@Override
       
    79 			public void widgetSelected(SelectionEvent e) {
       
    80 				if (viewer.getComparator() != null) {
       
    81 					if (viewer.getComparator() == TableColumnSorter.this) {
       
    82 						int tdirection = TableColumnSorter.this.direction;
       
    83 
       
    84 						if (tdirection == ASC) {
       
    85 							setSorter(TableColumnSorter.this, DESC);
       
    86 						} else if (tdirection == DESC) {
       
    87 							setSorter(TableColumnSorter.this, NONE);
       
    88 						}
       
    89 					} else {
       
    90 						setSorter(TableColumnSorter.this, ASC);
       
    91 					}
       
    92 				} else {
       
    93 					setSorter(TableColumnSorter.this, ASC);
       
    94 				}
       
    95 			}
       
    96 		});
       
    97 	}
       
    98 
       
    99 	/**
       
   100 	 * Sets sorter
       
   101 	 * 
       
   102 	 * @param sorter
       
   103 	 *            the sorter
       
   104 	 * @param direction
       
   105 	 *            direction
       
   106 	 */
       
   107 	public void setSorter(TableColumnSorter sorter, int direction) {
       
   108 		if (direction == NONE) {
       
   109 			column.getParent().setSortColumn(null);
       
   110 			column.getParent().setSortDirection(SWT.NONE);
       
   111 			viewer.setComparator(null);
       
   112 		} else {
       
   113 			column.getParent().setSortColumn(column);
       
   114 			sorter.direction = direction;
       
   115 
       
   116 			if (direction == ASC) {
       
   117 				column.getParent().setSortDirection(SWT.DOWN);
       
   118 			} else {
       
   119 				column.getParent().setSortDirection(SWT.UP);
       
   120 			}
       
   121 
       
   122 			if (viewer.getComparator() == sorter) {
       
   123 				viewer.refresh();
       
   124 			} else {
       
   125 				viewer.setComparator(sorter);
       
   126 			}
       
   127 
       
   128 		}
       
   129 	}
       
   130 
       
   131 	/*
       
   132 	 * (non-Javadoc)
       
   133 	 * 
       
   134 	 * @see
       
   135 	 * org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.
       
   136 	 * viewers.Viewer, java.lang.Object, java.lang.Object)
       
   137 	 */
       
   138 	@Override
       
   139 	public int compare(Viewer viewer, Object o1, Object o2) {
       
   140 		return direction * doCompare(viewer, o1, o2);
       
   141 	}
       
   142 
       
   143 	/**
       
   144 	 * Compare method
       
   145 	 * 
       
   146 	 * @param TableViewer
       
   147 	 *            table viewer
       
   148 	 * @param o1
       
   149 	 *            object 1
       
   150 	 * @param o2
       
   151 	 *            object 2
       
   152 	 * @return less than 0 if o1 is smaller than o2, 0 if equal, more than 0 if
       
   153 	 *         o1 is bigger than o2
       
   154 	 */
       
   155 	protected abstract int doCompare(Viewer TableViewer, Object o1, Object o2);
       
   156 }