TableSorter.java v2.1

O

ouroborus

As part of another project, I spent some time updating TableSorter.java so that it compiles cleanly under Java 1.5. The file can be retrieved from:

http://ouroborus.org/java/

I would appreciate any constructive comments regarding the results.
 
A

Andrew McDonagh

ouroborus said:
As part of another project, I spent some time updating TableSorter.java
so that it compiles cleanly under Java 1.5. The file can be retrieved from:

http://ouroborus.org/java/

I would appreciate any constructive comments regarding the results.

Where's the JUnit unit tests for it?
 
A

Andrew McDonagh

ouroborus said:
As part of another project, I spent some time updating TableSorter.java
so that it compiles cleanly under Java 1.5. The file can be retrieved from:

http://ouroborus.org/java/

I would appreciate any constructive comments regarding the results.

Why use reflcection to search for a method 'compareTo' when you could
use instanceOf?
 
A

Andrew McDonagh

ouroborus said:
As part of another project, I spent some time updating TableSorter.java
so that it compiles cleanly under Java 1.5. The file can be retrieved from:

http://ouroborus.org/java/

I would appreciate any constructive comments regarding the results.

At my last company we tried all sorts of way of making the adapter work
in all conditions and found the best approach for this task - is to use
the same as JTable does for allowing columns to be moved by the user.

JTable & TableColumnModel keeps tract of where columns are and doesn't
have any problems with any changes to the data, even whilst the columns
are being moved.

We ended up create a derived JTable class - SortingTable.

It worked the tree and with surprisingly little code.
 
A

Andrew McDonagh

Andrew said:
At my last company we tried all sorts of way of making the adapter work
in all conditions and found the best approach for this task - is to use
the same as JTable does for allowing columns to be moved by the user.

JTable & TableColumnModel keeps tract of where columns are and doesn't
have any problems with any changes to the data, even whilst the columns
are being moved.

We ended up create a derived JTable class - SortingTable.

It worked the tree and with surprisingly little code.

see
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#convertColumnIndexToView(int)

We realized the problem with adapting the TableModel, was that we were
trying to make our Model presentable. Whereas in the MVC pattern that
JTable (tries) to use the model should remain unaware of how its viewed.
So by putting the presentation logic in the JTable itself this not only
made more sense from the design pattern point of view (JTable is a mix
of the controller and view) it also worked in a far easier manner.
 
R

Rhino

ouroborus said:
As part of another project, I spent some time updating TableSorter.java so
that it compiles cleanly under Java 1.5. The file can be retrieved from:

http://ouroborus.org/java/

I would appreciate any constructive comments regarding the results.

Well, if you really want to make my day, you could add the ability to sort
on multiple columns.

I'm thinking of something analagous to the ORDER BY clause of the SQL SELECT
statement, which lets you say something like:

ORDER BY CITY ASC, LASTNAME DESC

If you could make the sort work on any number of columns, that would be
brilliant but even if you could only do up to 4 columns, it would be
wonderful.

I've always had this in the back of mind to do myself someday but never
quite get around to it. But, if you could figure out how to do it, I think
it would be very useful.
 
O

ouroborus

Andrew said:
Where's the JUnit unit tests for it?

There aren't any. However, feel free to volunteer for this task.
Why use reflcection to search for a method 'compareTo' when you could
use instanceOf?

I'm not a programmer by profession. My knowledge in the areas relating
to reflection and related were very minimal a few days ago before I had
started working on this. Thank you for the idea though, I'll have to
look into this.
At my last company we tried all sorts of way of making the adapter work
in all conditions and found the best approach for this task - is to use
the same as JTable does for allowing columns to be moved by the user.

JTable & TableColumnModel keeps tract of where columns are and doesn't
have any problems with any changes to the data, even whilst the columns
are being moved.

We ended up create a derived JTable class - SortingTable.

It worked the tree and with surprisingly little code.
see http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#convertColumnIndexToView(int)

We realized the problem with adapting the TableModel, was that we were
trying to make our Model presentable. Whereas in the MVC pattern that
JTable (tries) to use the model should remain unaware of how its viewed.
So by putting the presentation logic in the JTable itself this not only
made more sense from the design pattern point of view (JTable is a mix
of the controller and view) it also worked in a far easier manner.

Now that you mention it, it seems that was/is my problem to. I'm glad you
said this as it presents me with a new view of this whole thing. My
initial goal was to simply make TableSorter work and compile without
warnings. Since it turned out to be reasonably presentable, I decided to
post it.
 
D

David Segall

Rhino said:
Well, if you really want to make my day, you could add the ability to sort
on multiple columns.
I haven't looked at the code so I don't know if it qualifies but the
easy way to do this is to use a sorting algorithm that maintains the
existing order on equal keys. The user can then sort on multiple
columns by requesting a sort on each key column from least to most
significant.
 
O

Oliver Wong

David Segall said:
I haven't looked at the code so I don't know if it qualifies but the
easy way to do this is to use a sorting algorithm that maintains the
existing order on equal keys. The user can then sort on multiple
columns by requesting a sort on each key column from least to most
significant.

I did this for an older project, and I vaguely remember that the basic
design idea was to have a comparator maintained a linked list of other
comparators. The class would delegate to the first element in its list, and
upon ties, go down the list.

<UntestedCode strictnessLevel="off the top of my head">
public class MultiComparator implements Compatator<T> {
private List<Compatator<? super T>> myDelegates = new
LinkedList<Compatator<? super T>>();

/*Put various methods to manipulate the "myDelegates" list here.*/

public int compareTo(T a, T b) {
for(Comparator<? super T> c : myDelegates) {
int returnValue = c.compareTo(a, b);
if (returnValue != 0) {
return returnValue;
}
}
return 0;
}
}
</UntestedCode>

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top