remotecontrol/com.nokia.s60tools.remotecontrol/src/com/nokia/s60tools/remotecontrol/ftp/ui/view/TableViewerComparator.java
changeset 0 61163b28edca
equal deleted inserted replaced
-1:000000000000 0:61163b28edca
       
     1 /*
       
     2 * Copyright (c) 2008 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 */
       
    17 
       
    18 package com.nokia.s60tools.remotecontrol.ftp.ui.view;
       
    19 
       
    20 import org.eclipse.jface.viewers.Viewer;
       
    21 import org.eclipse.jface.viewers.ViewerComparator;
       
    22 import org.eclipse.swt.SWT;
       
    23 
       
    24 import com.nokia.s60tools.hticonnection.services.DriveInfo;
       
    25 
       
    26 /**
       
    27  * Comparator that is used to sort rows in File Transfer view.
       
    28  */
       
    29 public class TableViewerComparator extends ViewerComparator {
       
    30 
       
    31 	/**
       
    32 	 * Columns used in File Transfer view.
       
    33 	 */
       
    34 	public enum Columns {
       
    35 		NAME,
       
    36 		SIZE,
       
    37 		FREE
       
    38 	}
       
    39 
       
    40 	/**
       
    41 	 * Column that is used to sort data.
       
    42 	 */
       
    43 	private Columns sortColumn;
       
    44 	/**
       
    45 	 * Direction to which rows are sorted. Value should be either SWT.UP or SWT.DOWN.
       
    46 	 */
       
    47 	private final int sortDirection;
       
    48 
       
    49 	// Results result.
       
    50 	private final int COMPARE_FIRST_BIGGER = 1;
       
    51 	private final int COMPARE_FIRST_SMALLER = -1;
       
    52 	private final int COMPARE_EQUALS = 0;
       
    53 	
       
    54 	
       
    55 	/**
       
    56 	 * Constructor.
       
    57 	 * @param sortColumn Column that is used to sort data.
       
    58 	 * @param sortDirection Direction to which rows are sorted. Value should be SWT.UP or SWT.DOWN.
       
    59 	 */
       
    60 	public TableViewerComparator(Columns sortColumn, int sortDirection) {
       
    61 		this.sortColumn = sortColumn;
       
    62 		this.sortDirection = sortDirection;
       
    63 	}
       
    64 	
       
    65 	/* (non-Javadoc)
       
    66 	 * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
       
    67 	 */
       
    68 	@Override
       
    69 	public int compare(Viewer viewer, Object e1, Object e2) {
       
    70 		switch (sortColumn) {
       
    71 		case NAME:
       
    72 			return compareNames(e1, e2);
       
    73 		case SIZE:
       
    74 			return compareSizes(e1, e2);
       
    75 		case FREE:
       
    76 			return compareFreeSpace(e1, e2);
       
    77 		default:
       
    78 			return COMPARE_EQUALS;
       
    79 		}
       
    80 	}
       
    81 
       
    82 	/**
       
    83 	 * Compares objects in free space column.
       
    84 	 * @param e1 First element to be compared.
       
    85 	 * @param e2 Second element to be compared.
       
    86 	 * @return Negative number if first object should be before second object. Positive number if
       
    87 	 * first object should be after second object. Zero if order doesn't matter.
       
    88 	 */
       
    89 	private int compareFreeSpace(Object e1, Object e2) {
       
    90 		// Checking for uplink object.
       
    91 		if(e1 instanceof FtpUplinkObject) {
       
    92 			return COMPARE_FIRST_SMALLER;
       
    93 		}
       
    94 		if(e2 instanceof FtpUplinkObject) {
       
    95 			return COMPARE_FIRST_BIGGER;
       
    96 		}
       
    97 		
       
    98 		// Comparing free space for drives.
       
    99 		if(e1 instanceof FtpDriveObject && e2 instanceof FtpDriveObject) {
       
   100 			DriveInfo driveInfo1 = ((FtpDriveObject)e1).getDriveInfo();
       
   101 			DriveInfo driveInfo2 = ((FtpDriveObject)e2).getDriveInfo();
       
   102 			if(driveInfo1 != null && driveInfo2 != null) {
       
   103 				long size1 = driveInfo1.getFreeSpace();
       
   104 				long size2 = driveInfo2.getFreeSpace();
       
   105 				
       
   106 				return compareValues(size1, size2);
       
   107 			}
       
   108 		}
       
   109 		
       
   110 		return COMPARE_EQUALS;
       
   111 	}
       
   112 
       
   113 	/**
       
   114 	 * Compares objects in size column.
       
   115 	 * @param e1 First element to be compared.
       
   116 	 * @param e2 Second element to be compared.
       
   117 	 * @return Negative number if first object should be before second object. Positive number if
       
   118 	 * first object should be after second object. Zero if order doesn't matter.
       
   119 	 */
       
   120 	private int compareSizes(Object e1, Object e2) {
       
   121 		// Checking for uplink object.
       
   122 		if(e1 instanceof FtpUplinkObject) {
       
   123 			return COMPARE_FIRST_SMALLER;
       
   124 		}
       
   125 		if(e2 instanceof FtpUplinkObject) {
       
   126 			return COMPARE_FIRST_BIGGER;
       
   127 		}
       
   128 		
       
   129 		// Comparing size of drives.
       
   130 		if(e1 instanceof FtpDriveObject && e2 instanceof FtpDriveObject) {
       
   131 			DriveInfo driveInfo1 = ((FtpDriveObject)e1).getDriveInfo();
       
   132 			DriveInfo driveInfo2 = ((FtpDriveObject)e2).getDriveInfo();
       
   133 			if(driveInfo1 != null && driveInfo2 != null) {
       
   134 				long size1 = driveInfo1.getSize();
       
   135 				long size2 = driveInfo2.getSize();
       
   136 				
       
   137 				return compareValues(size1, size2);
       
   138 			}
       
   139 		}
       
   140 		
       
   141 		// Comparing size of files.
       
   142 		if(e1 instanceof FtpFileObject && e2 instanceof FtpFileObject) {
       
   143 			long size1 = ((FtpFileObject)e1).getSize();
       
   144 			long size2 = ((FtpFileObject)e2).getSize();
       
   145 			
       
   146 			return compareValues(size1, size2);
       
   147 		}
       
   148 		
       
   149 		// Checking if objects are file and folder.
       
   150 		if(e1 instanceof FtpFolderObject && e2 instanceof FtpFileObject) {
       
   151 			return (sortDirection == SWT.UP) ? COMPARE_FIRST_SMALLER : COMPARE_FIRST_BIGGER;
       
   152 		}
       
   153 		if(e1 instanceof FtpFileObject && e2 instanceof FtpFolderObject) {
       
   154 			return (sortDirection == SWT.UP) ? COMPARE_FIRST_BIGGER : COMPARE_FIRST_SMALLER;
       
   155 		}
       
   156 		
       
   157 		// Checking for folders.
       
   158 		if(e1 instanceof FtpFolderObject && e2 instanceof FtpFolderObject) {
       
   159 			return compareNames(e1, e2);
       
   160 		}
       
   161 		
       
   162 		return COMPARE_EQUALS;
       
   163 	}
       
   164 
       
   165 	/**
       
   166 	 * Compares two long numbers.
       
   167 	 * @param e1 First number to be compared.
       
   168 	 * @param e2 Second number to be compared.
       
   169 	 * @return Negative number if first number should be before second number. Positive number if
       
   170 	 * first number should be after second number. Zero if order doesn't matter.
       
   171 	 */
       
   172 	private int compareValues(long size1, long size2) {
       
   173 		if(size1 == size2) {
       
   174 			return COMPARE_EQUALS;
       
   175 		}
       
   176 		
       
   177 		if(size1 > size2) {
       
   178 			return (sortDirection == SWT.UP) ? COMPARE_FIRST_BIGGER : COMPARE_FIRST_SMALLER;
       
   179 		} else {
       
   180 			return (sortDirection == SWT.UP) ? COMPARE_FIRST_SMALLER : COMPARE_FIRST_BIGGER;
       
   181 		}
       
   182 	}
       
   183 
       
   184 	/**
       
   185 	 * Compares objects in name column.
       
   186 	 * @param e1 First element to be compared.
       
   187 	 * @param e2 Second element to be compared.
       
   188 	 * @return Negative number if first object should be before second object. Positive number if
       
   189 	 * first object should be after second object. Zero if order doesn't matter.
       
   190 	 */
       
   191 	private int compareNames(Object e1, Object e2) {
       
   192 		// Checking for uplink object.
       
   193 		if(e1 instanceof FtpUplinkObject) {
       
   194 			return COMPARE_FIRST_SMALLER;
       
   195 		}
       
   196 		if(e2 instanceof FtpUplinkObject) {
       
   197 			return COMPARE_FIRST_BIGGER;
       
   198 		}
       
   199 		
       
   200 		// Checking if objects are file and folder.
       
   201 		if(e1 instanceof FtpFolderObject && e2 instanceof FtpFileObject) {
       
   202 			return (sortDirection == SWT.UP) ? COMPARE_FIRST_SMALLER : COMPARE_FIRST_BIGGER;
       
   203 		}
       
   204 		if(e1 instanceof FtpFileObject && e2 instanceof FtpFolderObject) {
       
   205 			return (sortDirection == SWT.UP) ? COMPARE_FIRST_BIGGER : COMPARE_FIRST_SMALLER;
       
   206 		}
       
   207 		
       
   208 		// Comparing names.
       
   209 		if(e1 instanceof FtpNamedObject && e2 instanceof FtpNamedObject) {
       
   210 			String name1 = ((FtpNamedObject)e1).getName();
       
   211 			String name2 = ((FtpNamedObject)e2).getName();
       
   212 			return (sortDirection == SWT.UP) ? name1.compareToIgnoreCase(name2)
       
   213 											: name2.compareToIgnoreCase(name1);
       
   214 		}
       
   215 		
       
   216 		return COMPARE_EQUALS;
       
   217 	}
       
   218 	
       
   219 }