ColumnViewerSorter.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.viewer.scope;
00002
00003 import org.eclipse.jface.viewers.TreeViewer;
00004 import org.eclipse.jface.viewers.Viewer;
00005 import org.eclipse.jface.viewers.ViewerComparator;
00006 import org.eclipse.swt.SWT;
00007 import org.eclipse.swt.events.SelectionAdapter;
00008 import org.eclipse.swt.events.SelectionEvent;
00009 import org.eclipse.swt.widgets.TreeColumn;
00010 import org.eclipse.swt.widgets.TreeItem;
00011 import org.eclipse.swt.graphics.Image;
00012
00013
00014
00015 import edu.rice.cs.hpc.data.experiment.metric.BaseMetric;
00016 import edu.rice.cs.hpc.data.experiment.metric.MetricValue;
00017 import edu.rice.cs.hpc.data.experiment.scope.Scope;
00018 import edu.rice.cs.hpc.data.experiment.scope.LineScope;
00019 import edu.rice.cs.hpc.data.experiment.scope.CallSiteScope;
00020 import edu.rice.cs.hpc.viewer.util.Utilities;
00021
00022
00023
00024 public class ColumnViewerSorter extends ViewerComparator {
00025
00026 public static final int ASC = 1;
00027 public static final int NONE = 0;
00028 public static final int DESC = -1;
00029 private int direction = 0;
00030
00031 private TreeColumn column;
00032 private TreeViewer viewer;
00033 private int iColNumber;
00034 private BaseMetric metric;
00035
00040 public void setMetric(BaseMetric newMetric) {
00041 this.metric = newMetric;
00042 }
00050 public ColumnViewerSorter(TreeViewer viewer, TreeColumn column, BaseMetric newMetric, int colNum) {
00051 this.column = column;
00052 this.iColNumber = colNum;
00053 this.viewer = viewer;
00054 this.metric = newMetric;
00055
00056
00057 this.column.addSelectionListener(new SelectionAdapter() {
00058 public void widgetSelected(SelectionEvent e) {
00059
00060
00061 TreeItem item = ColumnViewerSorter.this.viewer.getTree().getItem(0);
00062 Image imgItem = item.getImage(0);
00063 String []sText = Utilities.getTopRowItems(ColumnViewerSorter.this.viewer);
00064 if( ColumnViewerSorter.this.viewer.getComparator() != null ) {
00065 if( ColumnViewerSorter.this.viewer.getComparator() == ColumnViewerSorter.this ) {
00066 int tdirection = ColumnViewerSorter.this.direction;
00067
00068 if( tdirection == ASC ) {
00069 setSorter(ColumnViewerSorter.this, DESC);
00070 } else if( tdirection == DESC ) {
00071 setSorter(ColumnViewerSorter.this, ASC);
00072 }
00073 } else {
00074 setSorter(ColumnViewerSorter.this, ASC);
00075 }
00076 } else {
00077 setSorter(ColumnViewerSorter.this, ASC);
00078 }
00079
00080 if(sText != null) {
00081 Utilities.insertTopRow(ColumnViewerSorter.this.viewer, imgItem, sText);
00082 }
00083 }
00084
00085 }
00086 );
00087
00088
00089 }
00090
00096 public void setSorter(ColumnViewerSorter sorter, int direction) {
00097
00098
00099
00100 this.viewer.getTree().setRedraw(false);
00101 if( direction == NONE ) {
00102 column.getParent().setSortColumn(null);
00103 column.getParent().setSortDirection(SWT.NONE);
00104 viewer.setComparator(null);
00105 } else {
00106 column.getParent().setSortColumn(column);
00107 sorter.direction = direction;
00108
00109 if( direction == ASC ) {
00110 column.getParent().setSortDirection(SWT.DOWN);
00111 } else {
00112 column.getParent().setSortDirection(SWT.UP);
00113 }
00114
00115 if( viewer.getComparator() == sorter ) {
00116
00117 } else {
00118 viewer.setComparator(sorter);
00119
00120 }
00121
00122 }
00123
00124
00125
00126 this.viewer.refresh();
00127 this.viewer.getTree().setRedraw(true);
00128 }
00129
00133 public int compare(Viewer viewer, Object e1, Object e2) {
00134 return direction * doCompare(viewer, e1, e2);
00135 }
00136
00145 private int doCompare(Scope node1, Scope node2) {
00146 if (node1 instanceof CallSiteScope &&
00147 node2 instanceof CallSiteScope) {
00148 CallSiteScope cs1 = (CallSiteScope) node1;
00149 CallSiteScope cs2 = (CallSiteScope) node2;
00150 LineScope ls1 = cs1.getLineScope();
00151 LineScope ls2 = cs2.getLineScope();
00152 if (ls1 != null && ls2 != null)
00153 return ls1.getLineNumber() - ls2.getLineNumber();
00154 }
00155 String text1 = node1.getName();
00156 String text2 = node2.getName();
00157 return text1.compareTo(text2);
00158 }
00159
00164 protected int doCompare(Viewer viewer, Object e1, Object e2) {
00165 if(e1 instanceof Scope && e2 instanceof Scope) {
00166 Scope node1 = (Scope) e1;
00167 Scope node2 = (Scope) e2;
00168
00169
00170
00171
00172 if(this.iColNumber==0) {
00173 return this.doCompare(node1, node2);
00174 } else {
00175 MetricValue mv1 = this.metric.getValue(node1);
00176 MetricValue mv2 = this.metric.getValue(node2);
00177 int iRet = MetricValue.compareTo(mv2, mv1);
00178 if(iRet != 0)
00179 return iRet;
00180
00181
00182 return this.doCompare(node1, node2);
00183 }
00184 }
00185 return 0;
00186 }
00187
00188 public void sort(Viewer viewer,
00189 Object[] elements) {
00190 super.sort(viewer, elements);
00191 }
00192
00193
00194 }