Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » ViewerSorter problem with a Table
ViewerSorter problem with a Table [message #466066] Fri, 30 December 2005 15:42 Go to next message
Sebastian is currently offline SebastianFriend
Messages: 26
Registered: July 2009
Junior Member
Hi all.

I would like to make my SWT-Table sortable. Therefore I implemented a
ViewerSorter on my table. Now to my problem: If I select a column to sort,
the sorting only orients to the first column (whatever which table I
selected to sort), but I want that the table should be sorted for the
selected column.
With the following Code, I set a listener to my columns in the table:

column1 = new TableColumn(table, SWT.NONE);
column1.setText("Modul");
column1.setWidth(200);
column1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
((NameSorter)tableViewer.getSorter()).setCriteria(NameSorter .MODUL);
tableViewer.refresh();
}
});

This is my ViewerSorter class:

public class NameSorter extends ViewerSorter
{
// columns
public final static int MODUL = 0;
public final static int LABEL = 1;

// Criteria for NameSorter instance
private int criteria;

private boolean toggleFlag = true;

public NameSorter(int criteria)
{
super();
this.criteria = criteria;
}

public void setCriteria(int criteria)
{
if (this.criteria != criteria)
{
this.criteria = criteria;
} else
{
toggleFlag = !toggleFlag;
}
}

public void sort(final Viewer viewer, Object[] elements)
{
Arrays.sort(elements);
}

}

Can somebody help me?

Sebastian
Re: ViewerSorter problem with a Table [message #466081 is a reply to message #466066] Fri, 30 December 2005 22:00 Go to previous messageGo to next message
Luis Reyes is currently offline Luis ReyesFriend
Messages: 14
Registered: July 2009
Junior Member
I'm going to assume that your criteria is the same as the column header.
So let's say your object are player objects and there's 4 columns in your
table: name, last name, points, rebounds and assists. in your sorter
class, don't override the sort method, override the compare method. Here
is an example:


//assume that in the constructor you passed the index number of the
//column that you want to sort, in a variable called column

public int compare(Viewer viewer, Object e1, Object e2) {

int returnValue = 0;

if (e1 instanceof Player) {
Player p1 = (Player) e1;
Player p2 = (Player) e2;

// Determine which column and do the appropriate sort
switch (column) {
case PlayerConst.COLUMN_FIRST_NAME:
returnValue = collator.compare(p1.getFirstName(), p2.getFirstName());
break;
case PlayerConst.COLUMN_LAST_NAME:
returnValue = collator.compare(p1.getLastName(),
p2.getLastName());
break;
case PlayerConst.COLUMN_POINTS:
returnValue = p1.getPoints() > p2.getPoints() ? 1 : -1;
break;
case PlayerConst.COLUMN_REBOUNDS:
returnValue = p1.getRebounds() > p2.getRebounds() ? 1 : -1;
break;
case PlayerConst.COLUMN_ASSISTS:
returnValue = p1.getAssists() > p2.getAssists() ? 1 : -1;
break;
}

// If descending order, flip the direction
// introduce some kind of if statement and
// check sort direction, then you can say
// returnValue = -returnValue;

return returnValue;

}

}
Re: ViewerSorter problem with a Table [message #466087 is a reply to message #466081] Sat, 31 December 2005 10:48 Go to previous messageGo to next message
Sebastian is currently offline SebastianFriend
Messages: 26
Registered: July 2009
Junior Member
Thank you for your answer. But can you tell me, what's the content of
Object e1 and e2? Because I haven't such a class. I felt the table with
entries of a txt-file. How can I then compare? Is e1 perhaps one row of
the column and e2 another one?
Re: ViewerSorter problem with a Table [message #466128 is a reply to message #466087] Tue, 03 January 2006 15:15 Go to previous messageGo to next message
Luis Reyes is currently offline Luis ReyesFriend
Messages: 14
Registered: July 2009
Junior Member
If you are using a TableViewer, I assume you have a content provider.
Usually one uses some kind of data structure (like an ArrayList) to store
the data that the content provider uses Viewer, or whatever you set the
input to be. This are the objects passed to the compare method. The reason
why Object is used, is to make generic enough so that you can use it for
any kind of object.

You said you are readinf from a file and populating the table like that.
Maybe you can create an object (let's say FileRow), and populate it with
the data from the File. Then if you put this objects in, let's say, an
ArrayList, things will be much easier.

e.g.
le'ts say a your file looks like this.

Name LastName Address ZipCode
John Doe 12 Carl Av. 10000
Jane
Re: ViewerSorter problem with a Table (complete) [message #466130 is a reply to message #466087] Tue, 03 January 2006 15:29 Go to previous messageGo to next message
Luis Reyes is currently offline Luis ReyesFriend
Messages: 14
Registered: July 2009
Junior Member
If you are using a TableViewer, I assume you have a content provider.
Usually one uses some kind of data structure (like an ArrayList) to store
the data used by the content provider. e1 and e2 in the compare method are
the objects obtained from the content provider. The reason why Object is
used, is to make it generic enough so that you can use it for any kind of
object.

You said you are reading from a file and populating the table like that.
Maybe you can create an object (let's say Player), and populate it with
the data from the File. Then if you put this objects in, let's say, an
ArrayList, things will be much easier.

e.g.
le'ts say your file looks like this.

Name LastName Points Rebounds Assists
John Doe 24 10 14
Jane Dona 30 11 10

Then as you read this file you can populate your player object, let's say
Player Object.

//Note: this is not correct API, is just to give you an idea
String name = stringTokenizer(filereader.getLine()).getToken();
player.setName();

playersArrayList.add(player);

Then in the content provider all you need to do is return this players
arrayList as an array.

Object[] getElements(){
return playersArrayList.toArray();
}


I hope this helps.

You can find a lot of examples here:
http://www.java2s.com/ExampleCode/SWT-JFace-Eclipse/CatalogS WT-JFace-Eclipse.htm
Re: ViewerSorter problem with a Table (complete) [message #466166 is a reply to message #466130] Wed, 04 January 2006 07:41 Go to previous message
Sebastian is currently offline SebastianFriend
Messages: 26
Registered: July 2009
Junior Member
Hello,
I founded my problem and now all works fine.
Thank you very much.
Previous Topic:Is this a resource leak bug?
Next Topic:(c)TabFolder, multiple lines
Goto Forum:
  


Current Time: Sat Dec 07 02:37:22 GMT 2024

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

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

Back to the top