Commits

Joachim Hofer  committed 90b0cd9

completed flexible view sorting feature, yay!

  • Participants
  • Parent commits 384fb0a

Comments (0)

Files changed (2)

File ecobertura.ui/src/main/scala/ecobertura/ui/views/session/CoverageSessionView.scala

     addTreeColumn(BranchesPercentage, "%", SWT.RIGHT, 1).setLabelProvider(new BranchesPercentageLabelProvider)
   
     viewer.setContentProvider(CoverageSessionModel.get)
-    viewer.setSorter(new CoverageViewSorter)
+    viewer.setSorter(viewSorter)
     viewer.setInput(CoverageSessionRoot)
   
     viewer.addDoubleClickListener { 
       swtColumn.setAlignment(alignment)
       treeColumnLayout.setColumnData(swtColumn, new ColumnWeightData(weight))
       swtColumn.addSelectionListener((event: SelectionEvent) => {
-        viewSorter.columnType = columnType
+        viewSorter.setColumnType(columnType)
         val treeTable = viewer.getTree
 
         val direction = if (treeTable.getSortColumn == swtColumn) 
           
         treeTable.setSortDirection(direction);
         treeTable.setSortColumn(swtColumn);
-        viewer.refresh();
+        viewer.refresh()
       })
       
       column

File ecobertura.ui/src/main/scala/ecobertura/ui/views/session/CoverageViewSorter.scala

 
 import org.eclipse.jface.viewers._
 
-// TODO handle sort oder and column type changes...
 class CoverageViewSorter extends ViewerSorter {
   import ColumnType._
   
-  var columnType = Name
+  object SortDirection extends Enumeration {
+    type SortDirection = Value
+    val Ascending, Descending = Value
+  }
+  import SortDirection._
+
+  private var columnType = Name
+  private var sortDirection = Descending
   
+  def setColumnType(columnType: ColumnType) = {
+    if (this.columnType == columnType) sortDirection = reversedSortDirection
+    else this.columnType = columnType
+  }
+  
+  private def reversedSortDirection = sortDirection match {
+      case Ascending => Descending
+      case Descending => Ascending
+  }
+
   override def compare(viewer: Viewer, first: Any, second: Any) = {
     (first, second) match {
       case (firstNode: CoverageSessionTreeNode, secondNode: CoverageSessionTreeNode) => {
-        firstNode.name.compareTo(secondNode.name)
+        compareTreeNodes(firstNode, secondNode)
       }
       case _ => first.toString.compareTo(second.toString)
     }
   }
+  
+  private def compareTreeNodes(
+      firstNode: CoverageSessionTreeNode, secondNode: CoverageSessionTreeNode) = {
+    val firstCoverage = firstNode.coverageData
+    val secondCoverage = secondNode.coverageData
+    
+    val comparisonValue = columnType match {
+      case Name => firstNode.name.compareTo(secondNode.name)
+      case CoveredLines => intWrapper(firstCoverage.linesCovered).compareTo(secondCoverage.linesCovered) 
+      case TotalLines => intWrapper(firstCoverage.linesTotal).compareTo(secondCoverage.linesTotal)
+      case LinesPercentage => intWrapper(firstCoverage.linesCovered).compareTo(secondCoverage.linesCovered)
+      case CoveredBranches => intWrapper(firstCoverage.branchesCovered).compareTo(secondCoverage.branchesCovered) 
+      case TotalBranches => intWrapper(firstCoverage.branchesTotal).compareTo(secondCoverage.branchesTotal)
+      case BranchesPercentage => intWrapper(firstCoverage.branchesCovered).compareTo(secondCoverage.branchesCovered)
+    }
+   
+    if (sortDirection == Descending) comparisonValue else -comparisonValue
+  }
 }
-