Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Nebula » GridTableViewer and sorting
GridTableViewer and sorting [message #62890] Tue, 24 February 2009 10:32 Go to next message
Christian Hauser is currently offline Christian HauserFriend
Messages: 189
Registered: July 2009
Senior Member
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 #62892 is a reply to message #62890] Tue, 24 February 2009 11:34 Go to previous messageGo to next message
Christian Hauser is currently offline Christian HauserFriend
Messages: 189
Registered: July 2009
Senior Member
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 #62894 is a reply to message #62892] Tue, 24 February 2009 11:47 Go to previous message
Christian Hauser is currently offline Christian HauserFriend
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 #593805 is a reply to message #62890] Tue, 24 February 2009 11:34 Go to previous message
Christian Hauser is currently offline Christian HauserFriend
Messages: 189
Registered: July 2009
Senior Member
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 11:47 Go to previous message
Christian Hauser is currently offline Christian HauserFriend
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);
}
Previous Topic:Can we use JFace DataBinding with Grid?
Next Topic:CDateTime issues since 26 January?
Goto Forum:
  


Current Time: Thu Apr 18 08:19:17 GMT 2024

Powered by FUDForum. Page generated in 0.02316 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top