changeset 59:3a7691d29566 CellTable

Sorting of the columns in the CellTable.
author Sebastian Kruse <skruse@mpiwg-berlin.mpg.de>
date Tue, 11 Dec 2012 17:14:26 +0100
parents 39b4d5d590ba
children 6f2d2b6f969a
files src/econnect/wp3_3/client/widgets/table/DynamicStiTable.java
diffstat 1 files changed, 52 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/econnect/wp3_3/client/widgets/table/DynamicStiTable.java	Tue Dec 11 16:03:09 2012 +0100
+++ b/src/econnect/wp3_3/client/widgets/table/DynamicStiTable.java	Tue Dec 11 17:14:26 2012 +0100
@@ -1,9 +1,11 @@
 package econnect.wp3_3.client.widgets.table;
 
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.List;
 
 import com.google.gwt.user.cellview.client.CellTable;
+import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
 import com.google.gwt.user.cellview.client.SimplePager;
 import com.google.gwt.user.cellview.client.TextColumn;
 import com.google.gwt.user.client.rpc.AsyncCallback;
@@ -277,33 +279,74 @@
 	        public String getValue(DataObject object) {
 	          return object.getName();
 	        }
-	    
-	    };	    
-	    this.elementsTable.addColumn(nameColumn);
+	    };
+	    nameColumn.setSortable(true);
+	    this.elementsTable.addColumn(nameColumn, "Name");
 
 	    TextColumn<DataObject> placeColumn = new TextColumn<DataObject>() {
 	        @Override
 	        public String getValue(DataObject object) {
 	          return object.getPlace();
 	        }
-	    
-	    };	
-	    this.elementsTable.addColumn(placeColumn);
+	    };
+	    placeColumn.setSortable(true);
+	    this.elementsTable.addColumn(placeColumn, "Place");
 	    
 	    TextColumn<DataObject> descriptionColumn = new TextColumn<DataObject>() {
 	        @Override
 	        public String getValue(DataObject object) {
 	          return object.getDescription();
 	        }
-	    
-	    };	
-	    this.elementsTable.addColumn(descriptionColumn);
+	    };
+	    descriptionColumn.setSortable(true);
+	    this.elementsTable.addColumn(descriptionColumn, "Description");
 
 	    SimplePager pager = new SimplePager();
 	    pager.setDisplay(this.elementsTable);
 
 	    dataProvider.setList(this.actualObjectSet);
 	    
+	    //The actual (at this time lexicographical) sorting routine.
+	    //TODO: remove redundant code, make this more abstract
+	    ListHandler<DataObject> columnSortHandler = new ListHandler<econnect.wp3_3.client.core.DataObject>(dataProvider.getList());
+	    columnSortHandler.setComparator(nameColumn,
+	    		new Comparator<econnect.wp3_3.client.core.DataObject>() {
+	    			public int compare(econnect.wp3_3.client.core.DataObject o1, econnect.wp3_3.client.core.DataObject o2) {
+	    				if (o1 == o2)
+	    					return 0;
+
+	    				if (o1 != null)
+	    					return (o2 != null) ? o1.getName().compareTo(o2.getName()) : 1;
+	                
+	    				return -1;
+	    			}
+	    		});
+	    columnSortHandler.setComparator(placeColumn,
+	    		new Comparator<econnect.wp3_3.client.core.DataObject>() {
+	    			public int compare(econnect.wp3_3.client.core.DataObject o1, econnect.wp3_3.client.core.DataObject o2) {
+	    				if (o1 == o2)
+	    					return 0;
+
+	    				if (o1 != null)
+	    					return (o2 != null) ? o1.getPlace().compareTo(o2.getPlace()) : 1;
+	                
+	    				return -1;
+	    			}
+	    		});
+	    columnSortHandler.setComparator(descriptionColumn,
+	    		new Comparator<econnect.wp3_3.client.core.DataObject>() {
+	    			public int compare(econnect.wp3_3.client.core.DataObject o1, econnect.wp3_3.client.core.DataObject o2) {
+	    				if (o1 == o2)
+	    					return 0;
+
+	    				if (o1 != null)
+	    					return (o2 != null) ? o1.getDescription().compareTo(o2.getDescription()) : 1;
+	                
+	    				return -1;
+	    			}
+	    		});
+	    this.elementsTable.addColumnSortHandler(columnSortHandler);
+	    
 	    //Redraw row colors on page change
 	    this.elementsTable.addRangeChangeHandler(new RangeChangeEvent.Handler(){
 	    	public void onRangeChange(RangeChangeEvent event){