|
|
Re: GridTableViewer and sorting [message #62894 is a reply to message #62892] |
Tue, 24 February 2009 11:47 |
Christian Hauser Messages: 189 Registered: July 2009 |
Senior Member |
|
|
Hi all,
I managed to get my Grid to sort the columns. I had a problem in my code.
This is the way I'm doing the sorting now:
For each column I add a new ColumnViewerSorter:
new ColumnViewerSorter(myGridTableViewer, myGridViewerColumn) {
@Override
protected int doCompare (Viewer viewer, Object e1, Object e2) {
// compare the two items e1 and e2
}
};
And this is the implementation of ColumnViewerSorter:
public abstract class ColumnViewerSorter extends ViewerComparator {
public static final int ASC= 1;
public static final int NONE= 0;
public static final int DESC= -1;
private int direction= 0;
private final GridViewerColumn column;
private final ColumnViewer viewer;
public ColumnViewerSorter (ColumnViewer viewer, GridViewerColumn
column) {
this.viewer= viewer;
this.column= column;
this.column.getColumn().addSelectionListener(new
SelectionAdapter() {
@Override
public void widgetSelected (SelectionEvent e) {
if (ColumnViewerSorter.this.viewer.getComparator() !=
null) {
if (ColumnViewerSorter.this.viewer.getComparator()
== ColumnViewerSorter.this) {
int tdirection= ColumnViewerSorter.this.direction;
if (tdirection == ASC) {
setSorter(ColumnViewerSorter.this, DESC);
} else if (tdirection == DESC) {
setSorter(ColumnViewerSorter.this, NONE);
}
} else {
setSorter(ColumnViewerSorter.this, ASC);
}
} else {
setSorter(ColumnViewerSorter.this, ASC);
}
}
});
}
protected void setSorter (ColumnViewerSorter sorter, int direction) {
if (direction == NONE) {
column.getColumn().setSort(SWT.NONE);
viewer.setComparator(null);
} else {
sorter.direction= direction;
if (direction == ASC) {
column.getColumn().setSort(SWT.DOWN);
} else {
column.getColumn().setSort(SWT.UP);
}
if (viewer.getComparator() == sorter) {
viewer.refresh();
} else {
viewer.setComparator(sorter);
}
}
}
@Override
public int compare (Viewer viewer, Object e1, Object e2) {
return direction * doCompare(viewer, e1, e2);
}
protected abstract int doCompare (Viewer viewer, Object e1, Object e2);
}
|
|
|
|
Re: GridTableViewer and sorting [message #593817 is a reply to message #62892] |
Tue, 24 February 2009 11:47 |
Christian Hauser Messages: 189 Registered: July 2009 |
Senior Member |
|
|
Hi all,
I managed to get my Grid to sort the columns. I had a problem in my code.
This is the way I'm doing the sorting now:
For each column I add a new ColumnViewerSorter:
new ColumnViewerSorter(myGridTableViewer, myGridViewerColumn) {
@Override
protected int doCompare (Viewer viewer, Object e1, Object e2) {
// compare the two items e1 and e2
}
};
And this is the implementation of ColumnViewerSorter:
public abstract class ColumnViewerSorter extends ViewerComparator {
public static final int ASC= 1;
public static final int NONE= 0;
public static final int DESC= -1;
private int direction= 0;
private final GridViewerColumn column;
private final ColumnViewer viewer;
public ColumnViewerSorter (ColumnViewer viewer, GridViewerColumn
column) {
this.viewer= viewer;
this.column= column;
this.column.getColumn().addSelectionListener(new
SelectionAdapter() {
@Override
public void widgetSelected (SelectionEvent e) {
if (ColumnViewerSorter.this.viewer.getComparator() !=
null) {
if (ColumnViewerSorter.this.viewer.getComparator()
== ColumnViewerSorter.this) {
int tdirection= ColumnViewerSorter.this.direction;
if (tdirection == ASC) {
setSorter(ColumnViewerSorter.this, DESC);
} else if (tdirection == DESC) {
setSorter(ColumnViewerSorter.this, NONE);
}
} else {
setSorter(ColumnViewerSorter.this, ASC);
}
} else {
setSorter(ColumnViewerSorter.this, ASC);
}
}
});
}
protected void setSorter (ColumnViewerSorter sorter, int direction) {
if (direction == NONE) {
column.getColumn().setSort(SWT.NONE);
viewer.setComparator(null);
} else {
sorter.direction= direction;
if (direction == ASC) {
column.getColumn().setSort(SWT.DOWN);
} else {
column.getColumn().setSort(SWT.UP);
}
if (viewer.getComparator() == sorter) {
viewer.refresh();
} else {
viewer.setComparator(sorter);
}
}
}
@Override
public int compare (Viewer viewer, Object e1, Object e2) {
return direction * doCompare(viewer, e1, e2);
}
protected abstract int doCompare (Viewer viewer, Object e1, Object e2);
}
|
|
|
Powered by
FUDForum. Page generated in 0.03053 seconds