eu.webtoolkit.jwt
Class WStandardItemModel

java.lang.Object
  extended by eu.webtoolkit.jwt.WObject
      extended by eu.webtoolkit.jwt.WAbstractItemModel
          extended by eu.webtoolkit.jwt.WStandardItemModel

public class WStandardItemModel
extends WAbstractItemModel

A standard data model, which stores its data in memory.

The standard item model supports all features of WAbstractItemModel, and can thus be used to represent tables, trees and tree tables.

The data itself are organized in WStandardItem objects. There is one invisible root object (getInvisibleRootItem()) that holds the toplevel data. Most methods in this class that access or manipulate data internally operate on this root item.

If you want to use the model as a table, then you can use WStandardItemModel() to set the initial table size, and use the getItem() and setItem() methods to set data. You can change the geometry by inserting rows (insertRow()) or columns (insertColumn()) or removing rows ( WAbstractItemModel#removeRow()) or columns ( WAbstractItemModel#removeColumn()).

If you want to use the model as a tree (or tree table), then you can use the default constructor to start with an empty tree, and use the WStandardItem API on getInvisibleRootItem() to manipulate the tree root. When you are building a tree, the column count at each node is 1. When you are building a tree table, you can add additional columns of data for each internal node. Only the items in the first column have children that result in a hierarchical tree structure.

When using the model with a view class, you can use the getItemFromIndex() and indexFromItem() models to translate between model indexes (that are used by the view class) and standard items.


Constructor Summary
WStandardItemModel()
          Creates a new standard item model.
WStandardItemModel(int rows, int columns)
          Creates a new standard item model with an initial geometry.
WStandardItemModel(int rows, int columns, WObject parent)
          Creates a new standard item model with an initial geometry.
WStandardItemModel(WObject parent)
          Creates a new standard item model.
 
Method Summary
 void appendColumn(java.util.List<WStandardItem> items)
          Adds a single column of top level items.
 void appendRow(java.util.List<WStandardItem> items)
          Adds a single row of top level items.
 void appendRow(WStandardItem item)
          Appends a single row containing a single item.
protected  void beginInsertColumns(WModelIndex parent, int first, int last)
          Method to be called before inserting columns.
protected  void beginInsertRows(WModelIndex parent, int first, int last)
          Method to be called before inserting rows.
protected  void beginRemoveColumns(WModelIndex parent, int first, int last)
          Method to be called before removing columns.
protected  void beginRemoveRows(WModelIndex parent, int first, int last)
          Method to be called before removing rows.
 void clear()
          Erases all data in the model.
 WModelIndex fromRawIndex(java.lang.Object rawIndex)
          Converts a raw pointer to a model index.
 int getColumnCount(WModelIndex parent)
          Returns the number of columns.
 java.lang.Object getData(WModelIndex index, int role)
          Returns data at a specific model index.
 java.util.EnumSet<ItemFlag> getFlags(WModelIndex index)
          Returns the flags for an item.
 java.lang.Object getHeaderData(int section, Orientation orientation, int role)
          Returns the row or column header data.
 WModelIndex getIndex(int row, int column, WModelIndex parent)
          Returns the child index for the given row and column.
 WStandardItem getInvisibleRootItem()
          Returns the invisible root item.
 WStandardItem getItem(int row)
          Returns a toplevel item.
 WStandardItem getItem(int row, int column)
          Returns a toplevel item.
 WStandardItem getItemFromIndex(WModelIndex index)
          Returns the standard item that corresponds to a model index.
 WStandardItem getItemPrototype()
          Returns the item prototype.
 WModelIndex getParent(WModelIndex index)
          Returns the parent for a model index.
 int getRowCount(WModelIndex parent)
          Returns the number of rows.
 int getSortRole()
          Returns the role used to sort the model.
 WModelIndex indexFromItem(WStandardItem item)
          Returns the model index for a particular item.
 void insertColumn(int column, java.util.List<WStandardItem> items)
          Inserts a single column of top level items.
 boolean insertColumns(int column, int count, WModelIndex parent)
          Inserts one or more columns.
 void insertRow(int row, java.util.List<WStandardItem> items)
          Inserts a single row of top level items.
 void insertRow(int row, WStandardItem item)
          Inserts a single row containing a single item.
 boolean insertRows(int row, int count, WModelIndex parent)
          Inserts one or more rows.
 Signal1<WStandardItem> itemChanged()
          Signal emitted when an item is changed.
 boolean removeColumns(int column, int count, WModelIndex parent)
          Removes columns.
 boolean removeRows(int row, int count, WModelIndex parent)
          Removes rows.
 boolean setData(WModelIndex index, java.lang.Object value, int role)
          Sets data at the given model index.
 boolean setHeaderData(int section, Orientation orientation, java.lang.Object value, int role)
          Sets header data for a column or row.
 void setItem(int row, int column, WStandardItem item)
          Sets a toplevel item.
 void setItemPrototype(WStandardItem item)
          Returns the item prototype.
 void setSortRole(int role)
          Set the role used to sort the model.
 void sort(int column, SortOrder order)
          Sorts the model according to a particular column.
 java.util.List<WStandardItem> takeColumn(int column)
          Takes a column out of the model.
 WStandardItem takeItem(int row)
          Takes an item out of the model.
 WStandardItem takeItem(int row, int column)
          Takes an item out of the model.
 java.util.List<WStandardItem> takeRow(int row)
          Takes a row out of the model.
 java.lang.Object toRawIndex(WModelIndex index)
          Converts a model index to a raw pointer that remains valid while the model's layout is changed.
 
Methods inherited from class eu.webtoolkit.jwt.WAbstractItemModel
collapseColumn, columnsAboutToBeInserted, columnsAboutToBeRemoved, columnsInserted, columnsRemoved, createIndex, dataChanged, dropEvent, endInsertColumns, endInsertRows, endRemoveColumns, endRemoveRows, expandColumn, getAcceptDropMimeTypes, getColumnCount, getData, getData, getData, getData, getHeaderData, getHeaderData, getHeaderFlags, getHeaderFlags, getIndex, getItemData, getMimeType, getRowCount, hasChildren, hasIndex, hasIndex, headerDataChanged, insertColumn, insertColumn, insertColumns, insertRow, insertRow, insertRows, layoutAboutToBeChanged, layoutChanged, match, modelReset, removeColumn, removeColumn, removeColumns, removeRow, removeRow, removeRows, reset, rowsAboutToBeInserted, rowsAboutToBeRemoved, rowsInserted, rowsRemoved, setData, setData, setData, setData, setHeaderData, setHeaderData, setItemData, sort
 
Methods inherited from class eu.webtoolkit.jwt.WObject
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

WStandardItemModel

public WStandardItemModel(WObject parent)
Creates a new standard item model.


WStandardItemModel

public WStandardItemModel()
Creates a new standard item model.

Calls this((WObject)null)


WStandardItemModel

public WStandardItemModel(int rows,
                          int columns,
                          WObject parent)
Creates a new standard item model with an initial geometry.

Creates a standard item model with a geometry of rows x columns. All items are set to null.


WStandardItemModel

public WStandardItemModel(int rows,
                          int columns)
Creates a new standard item model with an initial geometry.

Calls this(rows, columns, (WObject)null)

Method Detail

clear

public void clear()
Erases all data in the model.

After clearing the model, WAbstractItemModel#getRowCount() and WAbstractItemModel#getColumnCount() are 0.


getInvisibleRootItem

public WStandardItem getInvisibleRootItem()
Returns the invisible root item.

The invisible root item is a special item that is not rendered itself, but holds the top level data.


indexFromItem

public WModelIndex indexFromItem(WStandardItem item)
Returns the model index for a particular item.

If the item is the getInvisibleRootItem(), then an invalid index is returned.

See Also:
getItemFromIndex(WModelIndex index)

getItemFromIndex

public WStandardItem getItemFromIndex(WModelIndex index)
Returns the standard item that corresponds to a model index.

If the index is an invalid index, then the getInvisibleRootItem() is returned.

See Also:
indexFromItem(WStandardItem item)

appendColumn

public void appendColumn(java.util.List<WStandardItem> items)
Adds a single column of top level items.

Appends a single column of top level items. If necessary, the row count is increased.

Equivalent to:

 insertColumn(columnCount(), items);
 

See Also:
insertColumn(int column, List items), appendRow(List items)

insertColumn

public void insertColumn(int column,
                         java.util.List<WStandardItem> items)
Inserts a single column of top level items.

Inserts a single column of top level items at column column. If necessary, the row count is increased.

Equivalent to:

 invisibleRootItem().insertColumn(column, items);
 

See Also:
WStandardItem.insertColumn(int column, List items)

appendRow

public void appendRow(java.util.List<WStandardItem> items)
Adds a single row of top level items.

Appends a single row of top level items. If necessary, the column count is increased.

Equivalent to:

 insertRow(rowCount(), items);
 

See Also:
insertRow(int row, List items), appendColumn(List items)

insertRow

public void insertRow(int row,
                      java.util.List<WStandardItem> items)
Inserts a single row of top level items.

Inserts a single row of top level items at row row. If necessary, the column count is increased.

Equivalent to:

 invisibleRootItem().insertRow(row, items);
 

See Also:
WStandardItem.insertRow(int row, List items)

appendRow

public void appendRow(WStandardItem item)
Appends a single row containing a single item.

Appends a single toplevel row, with a single item.

Equivalent to:

 insertRow(rowCount(), item);
 

See Also:
WStandardItem.insertRow(int row, WStandardItem item)

insertRow

public void insertRow(int row,
                      WStandardItem item)
Inserts a single row containing a single item.

Inserts a single toplevel row, with a single item.

Equivalent to:

 invisibleRootItem().insertRow(row, item);
 

See Also:
WStandardItem.insertRow(int row, WStandardItem item)

getItem

public WStandardItem getItem(int row,
                             int column)
Returns a toplevel item.

Returns the top level at at (row, column). This may be 0 if no item was set previously at that position, or if the indicated position is out of bounds.

Equivalent to:

 invisibleRootItem().child(row, column);
 

See Also:
WStandardItem.getChild(int row, int column)

getItem

public final WStandardItem getItem(int row)
Returns a toplevel item.

Returns getItem(row, 0)


setItem

public void setItem(int row,
                    int column,
                    WStandardItem item)
Sets a toplevel item.

Sets the top level at at (row, column). If necessary, the number of rows or columns is increased.

If an item was previously set for that position, it is deleted first.

Equivalent to:

 invisibleRootItem().setChild(row, column, item);
 

See Also:
WStandardItem.setChild(int row, int column, WStandardItem item)

getItemPrototype

public WStandardItem getItemPrototype()
Returns the item prototype.

See Also:
setItemPrototype(WStandardItem item)

setItemPrototype

public void setItemPrototype(WStandardItem item)
Returns the item prototype.

Set the item that is cloned when an item needs to be created because the model is manipulated through its WAbstractItemModel API. For example, this may be needed when a view sets data at a position for which no item was previously set and thus created.

The new item is created based on this prototype by using WStandardItem#clone().

The default prototype is WStandardItem().

See Also:
setItemPrototype(WStandardItem item)

takeColumn

public java.util.List<WStandardItem> takeColumn(int column)
Takes a column out of the model.

Removes a column from the model, and returns the items that it contained. Ownership of the items is transferred out of the model.

Equivalent to:

 invisibleRootItem().takeColumn(column);
 

See Also:
WStandardItem.takeColumn(int column), WStandardItem.takeRow(int row)

takeRow

public java.util.List<WStandardItem> takeRow(int row)
Takes a row out of the model.

Removes a row from the model, and returns the items that it contained. Ownership of the items is transferred out of the model.

Equivalent to:

 invisibleRootItem().takeRow(row);
 

See Also:
WStandardItem.takeRow(int row), takeColumn(int column)

takeItem

public WStandardItem takeItem(int row,
                              int column)
Takes an item out of the model.

Removes an item from the model, and returns it. Ownership of the item is transferred out of the model.

Equivalent to:

 invisibleRootItem().takeItem(row, column);
 

See Also:
takeItem(int row, int column), WStandardItem.takeRow(int row), WStandardItem.takeColumn(int column)

takeItem

public final WStandardItem takeItem(int row)
Takes an item out of the model.

Returns takeItem(row, 0)


getFlags

public java.util.EnumSet<ItemFlag> getFlags(WModelIndex index)
Description copied from class: WAbstractItemModel
Returns the flags for an item.

The default implementation returns ItemIsSelectable.

Overrides:
getFlags in class WAbstractItemModel
See Also:
ItemFlag

getParent

public WModelIndex getParent(WModelIndex index)
Description copied from class: WAbstractItemModel
Returns the parent for a model index.

An implementation should use createIndex() to create a model index that corresponds to the parent of a given index.

Specified by:
getParent in class WAbstractItemModel
See Also:
WAbstractItemModel.getIndex(int row, int column, WModelIndex parent)

getData

public java.lang.Object getData(WModelIndex index,
                                int role)
Description copied from class: WAbstractItemModel
Returns data at a specific model index.

Return data for a given role at a given index.

Specified by:
getData in class WAbstractItemModel
See Also:
WAbstractItemModel.getFlags(WModelIndex index), WAbstractItemModel.getHeaderData(int section, Orientation orientation, int role), WAbstractItemModel.setData(WModelIndex index, Object value, int role)

getHeaderData

public java.lang.Object getHeaderData(int section,
                                      Orientation orientation,
                                      int role)
Description copied from class: WAbstractItemModel
Returns the row or column header data.

When orientation is Horizontal, section is a column number, when orientation is Vertical, section is a row number.

Overrides:
getHeaderData in class WAbstractItemModel
See Also:
WAbstractItemModel.getData(WModelIndex index, int role), WAbstractItemModel.setHeaderData(int section, Orientation orientation, Object value, int role)

getIndex

public WModelIndex getIndex(int row,
                            int column,
                            WModelIndex parent)
Description copied from class: WAbstractItemModel
Returns the child index for the given row and column.

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.

Specified by:
getIndex in class WAbstractItemModel
See Also:
WAbstractItemModel.getParent(WModelIndex index)

getColumnCount

public int getColumnCount(WModelIndex parent)
Description copied from class: WAbstractItemModel
Returns the number of columns.

This returns the number of columns at index parent.

Specified by:
getColumnCount in class WAbstractItemModel
See Also:
WAbstractItemModel.getRowCount(WModelIndex parent)

getRowCount

public int getRowCount(WModelIndex parent)
Description copied from class: WAbstractItemModel
Returns the number of rows.

This returns the number of rows at index parent.

Specified by:
getRowCount in class WAbstractItemModel
See Also:
WAbstractItemModel.getColumnCount(WModelIndex parent)

insertColumns

public boolean insertColumns(int column,
                             int count,
                             WModelIndex parent)
Description copied from class: WAbstractItemModel
Inserts one or more columns.

In models that support column insertion, this inserts count columns, starting at column, and returns true if the operation was successful. The new columns are inserted under parent.

The default implementation returns false.

The model implementation must call beginInsertColumns() and endInsertColumns() before and after the operation whenever its geometry is changed by inserting columns. This emits signals for views to properly react to these changes.

Overrides:
insertColumns in class WAbstractItemModel
See Also:
WAbstractItemModel.insertRows(int row, int count, WModelIndex parent), WAbstractItemModel.removeColumns(int column, int count, WModelIndex parent), WAbstractItemModel.beginInsertColumns(WModelIndex parent, int first, int last), WAbstractItemModel.endInsertColumns()

insertRows

public boolean insertRows(int row,
                          int count,
                          WModelIndex parent)
Description copied from class: WAbstractItemModel
Inserts one or more rows.

In models that support row insertion, this inserts count rows, starting at row, and returns true if the operation was successful. The new rows are inserted under parent.

If parent had no children, then a single column is added with count rows.

The default implementation returns false.

The model implementation must call beginInsertRows() and endInsertRows() before and after the operation whenever its geometry is changed by inserting rows. This emits signals for views to properly react to these changes.

Overrides:
insertRows in class WAbstractItemModel
See Also:
WAbstractItemModel.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()

removeColumns

public boolean removeColumns(int column,
                             int count,
                             WModelIndex parent)
Description copied from class: WAbstractItemModel
Removes columns.

Returns true if the operation was successful.

The default implementation returns false.

The model implementation must call beginRemoveColumns() and endRemoveColumns() before and after the operation whenever its geometry is changed by removing columns. This emits signals for views to properly react to these changes.

Overrides:
removeColumns in class WAbstractItemModel
See Also:
WAbstractItemModel.removeRows(int row, int count, WModelIndex parent), WAbstractItemModel.insertColumns(int column, int count, WModelIndex parent), WAbstractItemModel.beginRemoveColumns(WModelIndex parent, int first, int last), WAbstractItemModel.endRemoveColumns()

removeRows

public boolean removeRows(int row,
                          int count,
                          WModelIndex parent)
Description copied from class: WAbstractItemModel
Removes rows.

Returns true if the operation was successful.

The default implementation returns false.

The model implementation must call beginRemoveRows() and endRemoveRows() before and after the operation whenever its geometry is changed by removing rows. This emits signals for views to properly react to these changes.

Overrides:
removeRows in class WAbstractItemModel
See Also:
WAbstractItemModel.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()

setData

public boolean setData(WModelIndex index,
                       java.lang.Object value,
                       int role)
Description copied from class: WAbstractItemModel
Sets data at the given model index.

Returns true if the operation was successful.

The default implementation returns false.

The model implementation must emit the dataChanged() signal after data was changed.

Overrides:
setData in class WAbstractItemModel
See Also:
WAbstractItemModel.getData(WModelIndex index, int role)

setHeaderData

public boolean setHeaderData(int section,
                             Orientation orientation,
                             java.lang.Object value,
                             int role)
Description copied from class: WAbstractItemModel
Sets header data for a column or row.

Returns true if the operation was successful.

Overrides:
setHeaderData in class WAbstractItemModel
See Also:
WAbstractItemModel.getHeaderData(int section, Orientation orientation, int role)

toRawIndex

public java.lang.Object toRawIndex(WModelIndex index)
Description copied from class: WAbstractItemModel
Converts a model index to a raw pointer that remains valid while the model's layout is changed.

Use this method to temporarily save model indexes while the model's layout is changed by for example a sorting operation.

The default implementation returns null, which indicates that the index cannot be converted to a raw pointer. If you reimplement this method, you also need to reimplemnt fromRawIndex().

Overrides:
toRawIndex in class WAbstractItemModel
See Also:
WAbstractItemModel.layoutAboutToBeChanged(), WAbstractItemModel.sort(int column, SortOrder order), WAbstractItemModel.fromRawIndex(Object rawIndex)

fromRawIndex

public WModelIndex fromRawIndex(java.lang.Object rawIndex)
Description copied from class: WAbstractItemModel
Converts a raw pointer to a model index.

Use this method to create model index from temporary raw pointers. It is the reciproce method of toRawIndex().

You can return an invalid modelindex if the rawIndex no longer points to a valid item because of the layout change.

Overrides:
fromRawIndex in class WAbstractItemModel
See Also:
WAbstractItemModel.toRawIndex(WModelIndex index)

setSortRole

public void setSortRole(int role)
Set the role used to sort the model.

The default role is DisplayRole.

See Also:
sort(int column, SortOrder order)

getSortRole

public int getSortRole()
Returns the role used to sort the model.

See Also:
setSortRole(int role)

sort

public void sort(int column,
                 SortOrder order)
Description copied from class: WAbstractItemModel
Sorts the model according to a particular column.

If the model supports sorting, then it should emit the layoutAboutToBeChanged() signal, rearrange its items, and afterwards emit the layoutChanged() signal.

Overrides:
sort in class WAbstractItemModel
See Also:
WAbstractItemModel.layoutAboutToBeChanged(), WAbstractItemModel.layoutChanged()

itemChanged

public Signal1<WStandardItem> itemChanged()
Signal emitted when an item is changed.

This signal is emitted whenever date of an item has changed. The item that has changed is passed as the first parameter.

See Also:
WStandardItem.setData(Object d, int role)

beginInsertColumns

protected void beginInsertColumns(WModelIndex parent,
                                  int first,
                                  int last)
Description copied from class: WAbstractItemModel
Method to be called before inserting columns.

If your model supports insertion of columns, then you should call this method before inserting one or more columns, and endInsertColumns() afterwards. These methods emit the necessary signals to allow view classes to update themselves.

Overrides:
beginInsertColumns in class WAbstractItemModel
See Also:
WAbstractItemModel.endInsertColumns(), WAbstractItemModel.insertColumns(int column, int count, WModelIndex parent), WAbstractItemModel.columnsAboutToBeInserted()

beginInsertRows

protected void beginInsertRows(WModelIndex parent,
                               int first,
                               int last)
Description copied from class: WAbstractItemModel
Method to be called before inserting rows.

If your model supports insertion of rows, then you should call this method before inserting one or more rows, and endInsertRows() afterwards. These methods emit the necessary signals to allow view classes to update themselves.

Overrides:
beginInsertRows in class WAbstractItemModel
See Also:
WAbstractItemModel.endInsertRows(), WAbstractItemModel.insertRows(int row, int count, WModelIndex parent), WAbstractItemModel.rowsAboutToBeInserted()

beginRemoveColumns

protected void beginRemoveColumns(WModelIndex parent,
                                  int first,
                                  int last)
Description copied from class: WAbstractItemModel
Method to be called before removing columns.

If your model supports removal of columns, then you should call this method before removing one or more columns, and endRemoveColumns() afterwards. These methods emit the necessary signals to allow view classes to update themselves.

Overrides:
beginRemoveColumns in class WAbstractItemModel
See Also:
WAbstractItemModel.endRemoveColumns(), WAbstractItemModel.removeColumns(int column, int count, WModelIndex parent), WAbstractItemModel.columnsAboutToBeRemoved()

beginRemoveRows

protected void beginRemoveRows(WModelIndex parent,
                               int first,
                               int last)
Description copied from class: WAbstractItemModel
Method to be called before removing rows.

If your model supports removal of rows, then you should call this method before removing one or more rows, and endRemoveRows() afterwards. These methods emit the necessary signals to allow view classes to update themselves.

Overrides:
beginRemoveRows in class WAbstractItemModel
See Also:
WAbstractItemModel.endRemoveRows(), WAbstractItemModel.removeRows(int row, int count, WModelIndex parent), WAbstractItemModel.rowsAboutToBeRemoved()