The TreeViewer
already shows data in a sorted list, but this is not a view-imposed sort. Because the data is stored in a TreeMap
, the sort ordering is created by the TreeMap
itself, which in turn is sorting on the value of the toString
method. To use a different ordering (say, based on the timezone offset) the choices are either to modify the TreeMap
to add a Comparator
and sort the data at creation time, or add a sorter to the TreeViewer
. The first choice is applicable if the data is only used by a single view, or if the data is coming from a large external data store which can perform the sorting more efficiently (such as a relational database). For smaller data sets, the sorting can be done in the viewer itself.
JFace structured viewers allow view-specific sorting with the
ViewerComparator
. Create a new subclass calledTimeZoneViewerComparator
in thecom.packtpub.e4.clock.ui.internal
package, and implement thecompare
method as follows:public...