GridTableViewer and sorting [message #62890] |
Tue, 24 February 2009 05:32  |
Eclipse User |
|
|
|
Hi all
How do I sort a Grid when using a GridTableViewer? I tried it using a
ColumnViewerSorter, but that doesn't seem to work. Furthermore, Grid
doesn't seem to have API like setSortColumn() or setSortDirection().
Any help is highly appreciated.
Thanks,
Christian
|
|
|
|
Re: GridTableViewer and sorting [message #62894 is a reply to message #62892] |
Tue, 24 February 2009 06:47  |
Eclipse User |
|
|
|
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 #593805 is a reply to message #62890] |
Tue, 24 February 2009 06:34  |
Eclipse User |
|
|
|
Christian Hauser wrote:
> Hi all
>
> How do I sort a Grid when using a GridTableViewer? I tried it using a
> ColumnViewerSorter, but that doesn't seem to work. Furthermore, Grid
> doesn't seem to have API like setSortColumn() or setSortDirection().
ColumnViewerSorter is a ViewerComparator. However, I can only set a
ViewerComparator to the GridTableViewer, not to its columns. How is this
supposed to be used?
I'm still confused and would be very happy for any hint.
Christian
|
|
|
Re: GridTableViewer and sorting [message #593817 is a reply to message #62892] |
Tue, 24 February 2009 06:47  |
Eclipse User |
|
|
|
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.07478 seconds