|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objecteu.webtoolkit.jwt.WObject
eu.webtoolkit.jwt.WAbstractItemModel
eu.webtoolkit.jwt.WAbstractProxyModel
eu.webtoolkit.jwt.WSortFilterProxyModel
public class WSortFilterProxyModel
A proxy model for Wt's item models that provides filtering and/or sorting.
This proxy model does not store data itself, but presents data from a source
model, after filtering rows. It also allows sorting of the source model data,
without actually altering the source model. This may be convenient when the
source model does not support sorting (i.e. does not reimplement
WAbstractItemModel#sort()), or you do not want to reorder the underlying
model since that affects all views on the model.
To use the proxy model to filter data, you use the methods
setFilterKeyColumn(),
setFilterRegExp() and setFilterRole() to specify a filtering operation based on the values of a
single column. If this filtering mechanism is too limiting, you can provide
specialized filtering by reimplementing the
filterAcceptRow() method.
Sorting is provided by reimplementing the standard
WAbstractItemModel#sort() method. In this way, a view class such as
WTreeView may resort the model as indicated by the user. Use
setSortRole() to indicate
on what data role sorting should be done, or reimplement the
lessThan() method to provide a specialized sorting method.
By default, the proxy does not automatically refilter and resort when the
original model changes. Data changes or row additions to the source model are
not automatically reflected in the proxy model, but to maintain integrity,
row removals in the source model are always reflected in the proxy model. You
can enable the model to always refilter and resort when the underlying model
changes using
setDynamicSortFilter().
Usage example:
// model is the source model
WAbstractItemModel model = ...
// we setup a proxy to filter the source model
WSortFilterProxyModel proxy = new WSortFilterProxyModel(this);
proxy.setSourceModel(model);
proxy.setDynamicSortFilter(true);
proxy.setFilterKeyColumn(0);
proxy.setFilterRole(ItemDataRole.UserRole);
proxy.setFilterRegExp(".*");
// configure a view to use the proxy model instead of the source model
WTreeView view = new WTreeView(this);
view.setModel(proxy);
...
| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class eu.webtoolkit.jwt.WAbstractProxyModel |
|---|
WAbstractProxyModel.BaseItem |
| Constructor Summary | |
|---|---|
WSortFilterProxyModel()
Constructor. |
|
WSortFilterProxyModel(WObject parent)
Constructor. |
|
| Method Summary | |
|---|---|
protected boolean |
filterAcceptRow(int sourceRow,
WModelIndex sourceParent)
Returns whether a source row is accepted by the filter. |
int |
getColumnCount(WModelIndex parent)
Returns the number of columns. |
int |
getFilterFlags()
Returns the filter regular expression flags. |
int |
getFilterKeyColumn()
Return the column on which the filtering is applied. |
java.lang.String |
getFilterRegExp()
Return the regular expression used for filtering. |
int |
getFilterRole()
Return the data role used for filtering. |
java.lang.Object |
getHeaderData(int section,
Orientation orientation,
int role)
Returns the row or column header data. |
java.util.EnumSet<HeaderFlag> |
getHeaderFlags(int section,
Orientation orientation)
Returns the flags for a header. |
WModelIndex |
getIndex(int row,
int column,
WModelIndex parent)
Returns the child index for the given row and column. |
WModelIndex |
getParent(WModelIndex index)
Returns the parent for a model index. |
int |
getRowCount(WModelIndex parent)
Returns the number of rows. |
int |
getSortColumn()
Returns the current sort column. |
SortOrder |
getSortOrder()
Returns the current sort order. |
int |
getSortRole()
Return the data role used for sorting. |
boolean |
insertRows(int row,
int count,
WModelIndex parent)
Inserts a number rows. |
boolean |
isDynamicSortFilter()
Returns whether this proxy dynmically filters and sorts. |
protected boolean |
lessThan(WModelIndex lhs,
WModelIndex rhs)
Compares two indexes. |
WModelIndex |
mapFromSource(WModelIndex sourceIndex)
Maps a source model index to the proxy model. |
WModelIndex |
mapToSource(WModelIndex proxyIndex)
Maps a proxy model index to the source model. |
boolean |
removeRows(int row,
int count,
WModelIndex parent)
Removes a number rows. |
void |
setDynamicSortFilter(boolean enable)
Configure the proxy to dynamically track changes in the source model. |
void |
setFilterFlags(int flags)
Sets the filter regular expression flags. |
void |
setFilterKeyColumn(int column)
Specify the column on which the filtering is applied. |
void |
setFilterRegExp(java.lang.String pattern)
Specify a regular expression for filtering. |
void |
setFilterRole(int role)
Specify the data role used for filtering. |
boolean |
setHeaderData(int section,
Orientation orientation,
java.lang.Object value,
int role)
Sets header data for a column or row. |
void |
setSortRole(int role)
Specify the data role used used for sorting. |
void |
setSourceModel(WAbstractItemModel model)
Sets the source model. |
void |
sort(int column,
SortOrder order)
Sorts the model according to a particular column. |
| Methods inherited from class eu.webtoolkit.jwt.WAbstractProxyModel |
|---|
createSourceIndex, dropEvent, fromRawIndex, getAcceptDropMimeTypes, getData, getFlags, getMimeType, getSourceModel, insertColumns, removeColumns, setData, shiftModelIndexes, toRawIndex |
| Methods inherited from class eu.webtoolkit.jwt.WObject |
|---|
addChild, getId, getObjectName, remove, setObjectName, tr |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public WSortFilterProxyModel(WObject parent)
public WSortFilterProxyModel()
Calls this((WObject)null)
| Method Detail |
|---|
public WModelIndex mapFromSource(WModelIndex sourceIndex)
WAbstractProxyModel
This method returns a model index in the proxy model that corresponds to
the model index sourceIndex in the source model. This method
must only be implemented for source model indexes that are mapped and
thus are the result of
mapToSource().
mapFromSource in class WAbstractProxyModelWAbstractProxyModel.mapToSource(WModelIndex proxyIndex)public WModelIndex mapToSource(WModelIndex proxyIndex)
WAbstractProxyModel
This method returns a model index in the source model that corresponds to
the proxy model index proxyIndex.
mapToSource in class WAbstractProxyModelWAbstractProxyModel.mapFromSource(WModelIndex sourceIndex)public void setSourceModel(WAbstractItemModel model)
WAbstractProxyModelThe source model provides the actual data for the proxy model.
Ownership of the source model is not transferred.
setSourceModel in class WAbstractProxyModelpublic void setFilterKeyColumn(int column)
This configures the column on which the
getFilterRegExp() is
applied.
The default value is 0.
setFilterRegExp(String pattern),
setFilterRole(int role)public int getFilterKeyColumn()
setFilterKeyColumn(int column)public void setFilterRegExp(java.lang.String pattern)
This configures the regular expression used for filtering on
getFilterKeyColumn().
The default value is an empty expression, which disables filtering.
setFilterKeyColumn(int column),
setFilterRole(int role)public java.lang.String getFilterRegExp()
setFilterRegExp(String pattern)public void setFilterFlags(int flags)
public int getFilterFlags()
public void setFilterRole(int role)
This configures the data role used for filtering on
getFilterKeyColumn().
The default value is DisplayRole.
setFilterKeyColumn(int column),
setFilterRegExp(String pattern)public int getFilterRole()
setFilterRole(int role)public void setSortRole(int role)
This configures the data role used for sorting.
The default value is DisplayRole.
lessThan(WModelIndex lhs, WModelIndex rhs)public int getSortRole()
setSortRole(int role)public int getSortColumn()
When sort() has not been called, the model is unsorted, and this method
returns -1.
sort(int column, SortOrder order)public SortOrder getSortOrder()
sort(int column, SortOrder order)public void setDynamicSortFilter(boolean enable)
When enable is true, the proxy will re-filter
and re-sort the model when changes happen to the source model.
Note: This may be ackward when editing through the proxy model, since changing some data may rearrange the model and thus invalidate model indexes. Therefore it is usually less complicated to manipulate directly the source model instead.
lessThan(WModelIndex lhs, WModelIndex rhs)public boolean isDynamicSortFilter()
setDynamicSortFilter(boolean enable)public int getColumnCount(WModelIndex parent)
WAbstractItemModel
This returns the number of columns at index parent.
getColumnCount in class WAbstractItemModelWAbstractItemModel.getRowCount(WModelIndex parent)public int getRowCount(WModelIndex parent)
WAbstractItemModel
This returns the number of rows at index parent.
getRowCount in class WAbstractItemModelWAbstractItemModel.getColumnCount(WModelIndex parent)public WModelIndex getParent(WModelIndex index)
WAbstractItemModel
An implementation should use
createIndex() to create a model index that corresponds to the parent of
a given index.
Note that the index itself may be stale (referencing a row/column within
the parent that is outside the model geometry), but its parent
(identified by the WModelIndex#getInternalPointer()) is referencing an existing parent. A
stale index can only be used while the model geometry is being updated,
i.e. during the emission of the corresponding
[rows/columns](Being)[Removed/Inserted]() signals.
getParent in class WAbstractItemModelWAbstractItemModel.getIndex(int row, int column, WModelIndex parent)
public WModelIndex getIndex(int row,
int column,
WModelIndex parent)
WAbstractItemModel
When implementing this method, you can use
createIndex() to create an index that corresponds to the item at
row and column within parent.
If the location is invalid (out of bounds at the parent), then an invalid index must be returned.
getIndex in class WAbstractItemModelWAbstractItemModel.getParent(WModelIndex index)
public boolean setHeaderData(int section,
Orientation orientation,
java.lang.Object value,
int role)
WAbstractItemModel
Returns true if the operation was successful.
setHeaderData in class WAbstractItemModelWAbstractItemModel.getHeaderData(int section, Orientation
orientation, int role)
public java.lang.Object getHeaderData(int section,
Orientation orientation,
int role)
WAbstractItemModel
When orientation is Horizontal, section is a column number, when
orientation is Vertical,
section is a row number.
getHeaderData in class WAbstractItemModelWAbstractItemModel.getData(WModelIndex index, int role),
WAbstractItemModel.setHeaderData(int section, Orientation
orientation, Object value, int role)
public java.util.EnumSet<HeaderFlag> getHeaderFlags(int section,
Orientation orientation)
WAbstractItemModelThe default implementation returns no flags set.
getHeaderFlags in class WAbstractItemModelHeaderFlag
public boolean insertRows(int row,
int count,
WModelIndex parent)
The rows are inserted in the source model, and if successful, also in the proxy model regardless of whether they are matched by the current filter. They are inserted at the indicated row, regardless of whether this is the correct place according to the defined sorting.
As soon as you set data for the column on which the filtering is active, or which affects the sorting, the row may be filtered out or change position when dynamic sorting/filtering is enabled. Therefore, it is usually a good idea to temporarily disable the dynamic sort/filtering behaviour while inserting new row(s) of data.
insertRows in class WAbstractItemModelWAbstractItemModel.insertColumns(int column, int count, WModelIndex
parent),
WAbstractItemModel.removeRows(int row, int count, WModelIndex
parent),
WAbstractItemModel.beginInsertRows(WModelIndex parent, int first,
int last),
WAbstractItemModel.endInsertRows()
public boolean removeRows(int row,
int count,
WModelIndex parent)
The rows are removed from the source model.
removeRows in class WAbstractItemModelWAbstractItemModel.removeColumns(int column, int count, WModelIndex
parent),
WAbstractItemModel.insertRows(int row, int count, WModelIndex
parent),
WAbstractItemModel.beginRemoveRows(WModelIndex parent, int first,
int last),
WAbstractItemModel.endRemoveRows()
public void sort(int column,
SortOrder order)
WAbstractItemModel
If the model supports sorting, then it should emit the
layoutAboutToBeChanged() signal, rearrange its items, and afterwards
emit the layoutChanged()
signal.
sort in class WAbstractItemModelWAbstractItemModel.layoutAboutToBeChanged(),
WAbstractItemModel.layoutChanged()
protected boolean filterAcceptRow(int sourceRow,
WModelIndex sourceParent)
The default implementation uses
getFilterKeyColumn(),
getFilterRole() and
getFilterRegExp().
You may want to reimplement this method to provide specialized filtering.
protected boolean lessThan(WModelIndex lhs,
WModelIndex rhs)
The default implementation uses
getSortRole() and an ordering
using the operator< when the data is of the same type or compares
lexicographically otherwise.
You may want to reimplement this method to provide specialized sorting.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||