java-gnome version 4.0.7

org.gnome.gtk
Class TreeModelSort

java.lang.Object
  extended by org.freedesktop.bindings.Proxy
      extended by org.gnome.glib.Object
          extended by org.gnome.gtk.TreeModel
              extended by org.gnome.gtk.TreeModelSort
All Implemented Interfaces:
TreeDragSource, TreeSortable

public class TreeModelSort
extends TreeModel
implements TreeDragSource, TreeSortable

Takes an existing model and creates a new model of it sorted as specified. The data is not copied, but the TreeModel that results from creating a TreeModelSort can be used independently. It listens to and reacts to the signals emitted by the underlying base TreeModel. The end result is that of allowing you to have two different TreeViews with their own sort of the same underlying data set. This is potentially useful if you have a common TreeModel backing a number of different presentations, although you should be cognisant that a point will be reached where it is more efficient to simply have separate models.

A TreeIter pointing into this TreeModelSort is not valid in the underlying "child" TreeModel. If you need to change data in the base model, use convertIterToChildIter().

You need to be careful to use the correct TreeModel for TreeIter pointers you receive in callbacks. The scenario that arises more often is this:

 ListStore model;
 TreeModelSort sorted;
 TreeView view;
 TreeSelection selection;
 
 // usual TreeModel setup
 model = new ListStore(...);
 
 // then create the sorted one, and use it
 sorted = new TreeModelSort(model);
 view = new TreeView(sorted);
 ...
 
 // then, later
 selection.connect(new TreeSelection.CHANGED() {
     public void onChanged(TreeSelection source) {
         final TreeIter row;
         final String str;
 
         row = selection.getSelected();
         if (row == null) {
             return;
         }
         str = model.getValue(row, column);
     }
 }
 
the problem that arises is that the retrieved TreeIter is not valid in model. It's a TreeIter in sorted. Your program will crash if you get this wrong. The fix is simple; change it to use the correct TreeModel:
 ...
         str = sorted.getValue(row, column);
 ...
 
and things will work fine.

You don't normally have need of this class. Both ListStore and TreeStore implement TreeSortable already, and there are various sorting tools built into the view side of the TreeView/TreeModel MVC framework, notably TreeViewColumn's setSortColumn(). If, however, you are using a TreeModelFilter, you will need to wrap it in one of these to make sorting work normally again.

Since:
4.0.6
Author:
Andrew Cowie

Nested Class Summary
 
Nested classes/interfaces inherited from class org.gnome.gtk.TreeModel
TreeModel.ROW_CHANGED
 
Constructor Summary
TreeModelSort(TreeModel base)
          Create a new TreeModelSort, wrapping an existing model.
 
Method Summary
 TreeIter convertIterToChildIter(TreeIter row)
          Convert a TreeIter pointing into this TreeModelSort into a TreeIter valid in the underlying base TreeModel that is being proxied.
 void setSortColumn(DataColumn column, SortType ordering)
          Specify the column from your (underlying) data model which will be used for sorting this TreeModelSort.
 
Methods inherited from class org.gnome.gtk.TreeModel
connect, getIter, getIterFirst, getPath, getValue, getValue, getValue, getValue, getValue, getValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue
 
Methods inherited from class org.freedesktop.bindings.Proxy
toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TreeModelSort

public TreeModelSort(TreeModel base)
Create a new TreeModelSort, wrapping an existing model.

Since:
4.0.6
Method Detail

convertIterToChildIter

public TreeIter convertIterToChildIter(TreeIter row)
Convert a TreeIter pointing into this TreeModelSort into a TreeIter valid in the underlying base TreeModel that is being proxied.

TODO We need to test the limitations of this, as several people have actually been getting away with not worrying about converting at all, so clearly something isn't quite as expected.

Since:
Unstable

setSortColumn

public void setSortColumn(DataColumn column,
                          SortType ordering)
Description copied from interface: TreeSortable
Specify the column from your (underlying) data model which will be used for sorting this TreeModelSort. Specify ASCENDING or DESCENDING order via the ordering parameter.

Specified by:
setSortColumn in interface TreeSortable


java-gnome