From 8a3e0095dfd8343a1ac7165f90dd7f9b255f6bcb Mon Sep 17 00:00:00 2001 From: Shireesh Anjal Date: Wed, 2 Nov 2011 17:37:09 +0530 Subject: Comparator for sorting contents of a table viewer --- .../console/utils/TableViewerComparator.java | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/com.gluster.storage.management.console/src/com/gluster/storage/management/console/utils/TableViewerComparator.java diff --git a/src/com.gluster.storage.management.console/src/com/gluster/storage/management/console/utils/TableViewerComparator.java b/src/com.gluster.storage.management.console/src/com/gluster/storage/management/console/utils/TableViewerComparator.java new file mode 100644 index 00000000..cd27e1ef --- /dev/null +++ b/src/com.gluster.storage.management.console/src/com/gluster/storage/management/console/utils/TableViewerComparator.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2011 Gluster, Inc. + * This file is part of Gluster Management Console. + * + * Gluster Management Console is free software; you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Gluster Management Console is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * . + *******************************************************************************/ +package com.gluster.storage.management.console.utils; + +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.viewers.ViewerSorter; +import org.eclipse.swt.SWT; + +/** + * + */ +public class TableViewerComparator extends ViewerSorter { + private int propertyIndex; + private static final int ASCENDING = 0; + private static final int DESCENDING = 1; + private int direction = DESCENDING; + + public TableViewerComparator() { + this(ASCENDING); + } + + public TableViewerComparator(int direction) { + this.propertyIndex = 0; + this.direction = direction; + } + + public int getDirection() { + return direction == DESCENDING ? SWT.DOWN : SWT.UP; + } + + public void setColumn(int column) { + if (column == this.propertyIndex) { + // Same column as last sort; toggle the direction + direction = 1 - direction; + } else { + // New column; do an ascending sort + this.propertyIndex = column; + direction = ASCENDING; + } + } + + @Override + public int compare(Viewer viewer, Object e1, Object e2) { + int result = super.compare(viewer, e1, e2); + // If descending order, flip the direction + if (direction == DESCENDING) { + result = -result; + } + return result; + } +} -- cgit