Project: SWT
Bug 199811 Calling TableItem#setText(int,String) with sortDirection set to SWT.DOWN extermley slow
Reported: 2007-08-13 19:09:55 | Status: resolved fixed

Build ID: 3.3 I've set up a table viewer with a custom comparator. When I click the column to sort the first time, nothing happens as expected since the data is created sorted. When I click the column the second time to reverse the sort, it happens almost instantly. When I click the column the third time, the app hangs and pegs the cpu at 100%. Here's the sample code: package test.sort; import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerComparator; public class TableDataComparator extends ViewerComparator { public TableDataComparator(int column, boolean up) { this.column = column; this.up = up; } @SuppressWarnings("unchecked") @Override public int compare(Viewer viewer, Object e1, Object e2) { Comparable o1 = (Comparable) ((Object[])e1)[column]; Comparable o2 = (Comparable) ((Object[])e2)[column]; if(o1 == null && o2 == null) return 0; if(o1 == null && o2 != null) return -1; if(o1 != null && o2 == null) return 1; return up ? o1.compareTo(o2) : o2.compareTo(o1); } private int column; private boolean up; } package test.sort; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.swt.graphics.Image; public class TableLabelProvider extends LabelProvider implements ITableLabelProvider { public Image getColumnImage(Object element, int columnIndex) { return null; } public String getColumnText(Object element, int columnIndex) { Object[] data = (Object[]) element; return super.getText(data[columnIndex]); } } package test.sort; import java.util.ArrayList; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.ui.part.ViewPart; public class TestSortView extends ViewPart implements SelectionListener { private TableViewer tableViewer; private Table table; public static final String ID = "test.sort.TestSortView"; //$NON-NLS-1$ /** * Create contents of the view part * * @param parent */ @Override public void createPartControl(Composite parent) { Composite container = new Composite(parent, SWT.NONE); container.setLayout(new FillLayout()); tableViewer = new TableViewer(container, SWT.BORDER); tableViewer.setContentProvider(new ArrayContentProvider()); tableViewer.setLabelProvider(new TableLabelProvider()); table = tableViewer.getTable(); table.setLinesVisible(true); table.setHeaderVisible(true); final TableColumn col0 = new TableColumn(table, SWT.NONE); col0.setWidth(100); col0.setText("Column 0"); col0.addSelectionListener(this); final TableColumn col1 = new TableColumn(table, SWT.NONE); col1.setWidth(100); col1.setText("Colum 1"); col1.addSelectionListener(this); final TableColumn col2 = new TableColumn(table, SWT.NONE); col2.setWidth(100); col2.setText("Column 2"); col2.addSelectionListener(this); // createActions(); initializeToolBar(); initializeMenu(); tableViewer.setInput(createData()); } /** * Create the actions */ private void createActions() { // Create the actions } /** * Initialize the toolbar */ private void initializeToolBar() { IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager(); } /** * Initialize the menu */ private void initializeMenu() { IMenuManager menuManager = getViewSite().getActionBars().getMenuManager(); } @Override public void setFocus() { // Set the focus } private ArrayList<Object[]> createData() { ArrayList<Object[]> data = new ArrayList<Object[]>(); for(int i = 0; i < 7000; i++) { Integer[] row = new Integer[3]; data.add(row); for(int j = 0; j < 3; j++) row[j] = i; } return data; } public void widgetDefaultSelected(SelectionEvent e) { // TODO Auto-generated method stub } public void widgetSelected(SelectionEvent e) { final TableColumn currentColumn = (TableColumn) e.widget; final Table table = currentColumn.getParent(); final TableColumn sortColumn = table.getSortColumn(); int sortDirection = table.getSortDirection(); if (sortColumn == currentColumn) sortDirection = sortDirection == SWT.UP ? SWT.DOWN : SWT.UP; else sortDirection = SWT.UP; final boolean sortUp = sortDirection == SWT.UP; tableViewer.setComparator(new TableDataComparator(Integer.parseInt(currentColumn.getText().substring(7)), sortUp)); table.setSortColumn(currentColumn); table.setSortDirection(sortDirection); } }

Files changed in fix:
bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Table.java bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/TableItem.java