eu.webtoolkit.jwt
Class WTreeView

java.lang.Object
  extended by eu.webtoolkit.jwt.WObject
      extended by eu.webtoolkit.jwt.WWidget
          extended by eu.webtoolkit.jwt.WCompositeWidget
              extended by eu.webtoolkit.jwt.WAbstractItemView
                  extended by eu.webtoolkit.jwt.WTreeView

public class WTreeView
extends WAbstractItemView

A view class that displays a model as a tree or tree table.

The view displays data from a WAbstractItemModel in a tree or tree table. It provides incremental rendering, allowing the display of data models of any size efficiently, without excessive use of client- or serverside resources. Data of all predefined roles is displayed (see also ItemDataRole), including text, icons, checkboxes, and tooltips .

By default, all but the first columns are given a width of 150px, and the first column takes the remaining size. Note that this may have as consequence that the first column's size is reduced to 0. Column widths of all columns, including the first column, can be set through the API method setColumnWidth(), and also by the user using handles provided in the header.

Optionally, the treeview may be configured so that the first column is always visible while scrolling through the other columns, which may be convenient if you wish to display a model with many columns. Use setColumn1Fixed() to enable this behaviour.

If the model supports sorting ( WAbstractItemModel#sort()), such as the WStandardItemModel, then you can enable sorting buttons in the header, using WAbstractItemView#setSortingEnabled().

You can allow selection on row or item level (using WAbstractItemView#setSelectionBehavior()), and selection of single or multiple items (using WAbstractItemView#setSelectionMode()), and listen for changes in the selection using the WAbstractItemView#selectionChanged() signal.

You may enable drag & drop support for this view, whith awareness of the items in the model. When enabling dragging (see WAbstractItemView#setDragEnabled()), the current selection may be dragged, but only when all items in the selection indicate support for dragging (controlled by the ItemIsDragEnabled flag), and if the model indicates a mime-type (controlled by WAbstractItemModel#getMimeType()). Likewise, by enabling support for dropping (see WAbstractItemView#setDropsEnabled()), the treeview may receive a drop event on a particular item, at least if the item indicates support for drops (controlled by the ItemIsDropEnabled flag).

You may also react to mouse click events on any item, by connecting to one of the WAbstractItemView#clicked() or WAbstractItemView#doubleClicked() signals.

CSS

The treeview is styled by the current CSS theme. The look can be overridden using the Wt-treeview CSS class and the following selectors.

Selectors that apply to the header:

 .Wt-treeview .Wt-headerdiv         : header (background)
 .Wt-treeview .Wt-header .Wt-label  : header label
 .Wt-treeview .Wt-tv-rh             : column resize handle
 .Wt-treeview .Wt-tv-sh-none        : column sort handle, no sorting
 .Wt-treeview .Wt-tv-sh-up          : column sort handle, sort up
 .Wt-treeview .Wt-tv-sh-down        : column sort handle, sort down
 
Selectors that apply to the tree decoration:
 .Wt-treeview .Wt-trunk          : vertical line, trunk
 .Wt-treeview .Wt-end            : vertical line, last item
 .Wt-treeview .Wt-collapse       : collapse icon
 .Wt-treeview .Wt-expand         : expand icon
 .Wt-treeview .Wt-noexpand       : leaf icon
 
Selectors that apply to the table contents:
 .Wt-treeview .Wt-spacer         : spacer for non-loaded content
 .Wt-treeview .Wt-selected       : selected item
 .Wt-treeview .Wt-drop-site      : possible drop site
 
Selectors that apply to the table borders (which must be 1 pixel wide)
 .Wt-treeview .Wt-tv-row .Wt-tv-c      : border-right property of cells
 .Wt-treeview .Wt-header .Wt-tv-row,
   .Wt-treeview .Wt-tv-node .Wt-tv-row : border-left property of cells
 

A snapshot of the WTreeView:

WTreeView example (default)

WTreeView example (default)

WTreeView example (polished)

WTreeView example (polished)


Constructor Summary
WTreeView()
          Creates a new tree view.
WTreeView(WContainerWidget parent)
          Creates a new tree view.
 
Method Summary
 void collapse(WModelIndex index)
          Collapses a node.
 Signal1<WModelIndex> collapsed()
          Signal emitted when a node is collapsed.
protected  void enableAjax()
          Progresses to an Ajax-enabled widget.
 void expand(WModelIndex index)
          Expands a node.
 Signal1<WModelIndex> expanded()
          Signal emitted when a node is expanded.
 void expandToDepth(int depth)
          Expands all nodes to a depth.
 AlignmentFlag getColumnAlignment(int column)
          Returns the content alignment for a column.
 WLength getColumnWidth(int column)
          Returns the column width.
 AlignmentFlag getHeaderAlignment(int column)
          Returns the header alignment for a column.
 WModelIndex getRootIndex()
          Returns the root index.
 boolean hasAlternatingRowColors()
          Returns whether alternating row colors are used.
 boolean isColumn1Fixed()
          Returns whether horizontal scrolling includes the first column.
 boolean isExpanded(WModelIndex index)
          Returns whether a node is expanded.
 boolean isRootDecorated()
          Returns whether toplevel items are decorated.
 void load()
          Loads content just before the widget is used.
 void refresh()
          Refresh the widget.
 void remove()
          Destructor.
 void resize(WLength width, WLength height)
          Resizes the widget.
 void setAlternatingRowColors(boolean enable)
          Sets if alternating row colors are to be used.
 void setColumn1Fixed(boolean fixed)
          Configures whether horizontal scrolling includes the first column.
 void setColumnAlignment(int column, AlignmentFlag alignment)
          Sets the content alignment for a column.
 void setColumnBorder(WColor color)
          Sets the column border color.
 void setColumnWidth(int column, WLength width)
          Sets the column width.
 void setExpanded(WModelIndex index, boolean expanded)
          Expands or collapses a node.
 void setHeaderAlignment(int column, AlignmentFlag alignment)
          Sets the header alignment for a column.
 void setHeaderHeight(WLength height, boolean multiLine)
          Sets the header height.
 void setModel(WAbstractItemModel model)
          Sets the model.
 void setRootIndex(WModelIndex rootIndex)
          Sets the root index.
 void setRootIsDecorated(boolean show)
          Sets whether toplevel items are decorated.
 void setRowHeight(WLength rowHeight)
          Sets the row height.
 
Methods inherited from class eu.webtoolkit.jwt.WAbstractItemView
clicked, createExtraHeaderWidget, doubleClicked, dropEvent, extraHeaderWidget, getHeaderHeight, getItemDelegate, getItemDelegate, getItemDelegate, getItemDelegateForColumn, getModel, getRowHeight, getSelectedIndexes, getSelectionBehavior, getSelectionMode, getSelectionModel, isColumnResizeEnabled, isSelected, isSortingEnabled, mouseWentDown, mouseWentUp, select, select, selectionChanged, setColumnResizeEnabled, setDragEnabled, setDropsEnabled, setHeaderHeight, setItemDelegate, setItemDelegateForColumn, setSelectedIndexes, setSelectionBehavior, setSelectionMode, setSortingEnabled, setSortingEnabled, sortByColumn
 
Methods inherited from class eu.webtoolkit.jwt.WCompositeWidget
boxBorder, boxPadding, callJavaScriptMember, find, getAttributeValue, getClearSides, getDecorationStyle, getFloatSide, getHeight, getId, getJavaScriptMember, getLineHeight, getMargin, getMaximumHeight, getMaximumWidth, getMinimumHeight, getMinimumWidth, getOffset, getPositionScheme, getStyleClass, getToolTip, getVerticalAlignment, getVerticalAlignmentLength, getWidth, isDisabled, isEnabled, isHidden, isHiddenKeepsGeometry, isInline, isLoaded, isPopup, setAttributeValue, setClearSides, setDecorationStyle, setDisabled, setFloatSide, setHidden, setHiddenKeepsGeometry, setId, setImplementation, setInline, setJavaScriptMember, setLineHeight, setMargin, setMaximumSize, setMinimumSize, setOffsets, setPopup, setPositionScheme, setSelectable, setStyleClass, setToolTip, setVerticalAlignment
 
Methods inherited from class eu.webtoolkit.jwt.WWidget
acceptDrops, acceptDrops, disable, dropEvent, enable, getJsRef, getParent, hide, htmlText, isRendered, positionAt, positionAt, resize, setClearSides, setMargin, setMargin, setMargin, setMargin, setMargin, setOffsets, setOffsets, setOffsets, setOffsets, setOffsets, setVerticalAlignment, show, stopAcceptDrops, tr
 
Methods inherited from class eu.webtoolkit.jwt.WObject
getObjectName, setObjectName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WTreeView

public WTreeView(WContainerWidget parent)
Creates a new tree view.


WTreeView

public WTreeView()
Creates a new tree view.

Calls this((WContainerWidget)null)

Method Detail

remove

public void remove()
Destructor.

Overrides:
remove in class WCompositeWidget
See Also:
WContainerWidget.removeWidget(WWidget widget)

setRootIndex

public void setRootIndex(WModelIndex rootIndex)
Sets the root index.

The root index is the model index that is considered the root node. This node itself is not rendered, but all its children are the top level nodes.

The default value is WModelIndex(), corresponding to the invisible root.

See Also:
setModel(WAbstractItemModel model)

getRootIndex

public WModelIndex getRootIndex()
Returns the root index.

See Also:
setRootIndex(WModelIndex rootIndex)

setExpanded

public void setExpanded(WModelIndex index,
                        boolean expanded)
Expands or collapses a node.

See Also:
expand(WModelIndex index), collapse(WModelIndex index)

isExpanded

public boolean isExpanded(WModelIndex index)
Returns whether a node is expanded.

See Also:
setExpanded(WModelIndex index, boolean expanded)

collapse

public void collapse(WModelIndex index)
Collapses a node.

See Also:
setExpanded(WModelIndex index, boolean expanded), expand(WModelIndex index)

expand

public void expand(WModelIndex index)
Expands a node.

See Also:
setExpanded(WModelIndex index, boolean expanded), collapse(WModelIndex index)

expandToDepth

public void expandToDepth(int depth)
Expands all nodes to a depth.

Expands all nodes to the given depth. A depth of 1 corresponds to the top level nodes.

See Also:
expand(WModelIndex index)

setAlternatingRowColors

public void setAlternatingRowColors(boolean enable)
Sets if alternating row colors are to be used. Configure whether rows get an alternating background color. These are implemented by using a background image on the root node, like:.

Sample image use for alternating row colors

Sample image use for alternating row colors

The image that is used is resourcesURL"/stripes/stripe- <i>n</i>px.gif", where n is the row height. In the resource folder are images pregenerated for one color and row sizes from 10 to 30px.

The default value is false.

Specified by:
setAlternatingRowColors in class WAbstractItemView

hasAlternatingRowColors

public boolean hasAlternatingRowColors()
Description copied from class: WAbstractItemView
Returns whether alternating row colors are used.

Specified by:
hasAlternatingRowColors in class WAbstractItemView
See Also:
WAbstractItemView.setAlternatingRowColors(boolean enable)

setRootIsDecorated

public void setRootIsDecorated(boolean show)
Sets whether toplevel items are decorated.

By default, top level nodes have expand/collapse and other lines to display their linkage and offspring, like any node.

By setting show to false, you can hide these decorations for root nodes, and in this way mimic a plain list.


isRootDecorated

public boolean isRootDecorated()
Returns whether toplevel items are decorated.

See Also:
setRootIsDecorated(boolean show)

setColumn1Fixed

public void setColumn1Fixed(boolean fixed)
Configures whether horizontal scrolling includes the first column.

To display a model with many columns, this option allows you to keep the first column fixed while scrolling through the other columns of the model.

The default value is false.

Note: Currently, you must set this option before any other manipulation of a newly created treeview, and only enable = true is supported.


isColumn1Fixed

public boolean isColumn1Fixed()
Returns whether horizontal scrolling includes the first column.

See Also:
setColumn1Fixed(boolean fixed)

resize

public void resize(WLength width,
                   WLength height)
Description copied from class: WWidget
Resizes the widget.

Specify a new size for this widget, by specifying width and height. By default a widget has automatic width and height, see WLength#isAuto().

This applies to CSS-based layout, and only block widgets can be given a size reliably.

See als setJavaScriptMember() for defining a "wtResize" method which allows a widget to actively manage its size using client-side JavaScript.

Overrides:
resize in class WCompositeWidget
See Also:
WWidget.getWidth(), WWidget.getHeight()

load

public void load()
Description copied from class: WWidget
Loads content just before the widget is used.

When the widget is inserted in the widget hierarchy, this method is called. Widgets that get inserted in the widget hierarchy will be rendered. Visible widgets are rendered immediately, and invisible widgets in the back-ground (or not for a plain HTML session). This method is called when the widget is directly or indirectly inserted into the widget tree.

The default implementation simply propagates the load signal to its children. You may want to override this method to delay loading of resource-intensive contents.

Overrides:
load in class WCompositeWidget

collapsed

public Signal1<WModelIndex> collapsed()
Signal emitted when a node is collapsed.

See Also:
setExpanded(WModelIndex index, boolean expanded), expanded()

expanded

public Signal1<WModelIndex> expanded()
Signal emitted when a node is expanded.

See Also:
setExpanded(WModelIndex index, boolean expanded), collapsed()

setModel

public void setModel(WAbstractItemModel model)
Description copied from class: WAbstractItemView
Sets the model.

The view will render the data in the given model. Changes to the model are reflected in the view.

The initial model is null.

Ownership of the model is not transferred (and thus the previously set model is not deleted).

Overrides:
setModel in class WAbstractItemView

setColumnWidth

public void setColumnWidth(int column,
                           WLength width)
Sets the column width.

For a model with columnCount() == N, the initial width of columns 1.. N is set to 150 pixels, and column 0 will take all remaining space.

Note: The actual space occupied by each column is the column width augmented by 7 pixels for internal padding and a border.

Specified by:
setColumnWidth in class WAbstractItemView
See Also:
setRowHeight(WLength rowHeight)

getColumnWidth

public WLength getColumnWidth(int column)
Description copied from class: WAbstractItemView
Returns the column width.

Specified by:
getColumnWidth in class WAbstractItemView
See Also:
WAbstractItemView.setColumnWidth(int column, WLength width)

setRowHeight

public void setRowHeight(WLength rowHeight)
Description copied from class: WAbstractItemView
Sets the row height.

The view assumes that all rows are of the same height. Use this method to set the height.

The default value is 20 pixels.

Note: The height must be specified in WLength.Unit.Pixel units.

Specified by:
setRowHeight in class WAbstractItemView
See Also:
WAbstractItemView.setColumnWidth(int column, WLength width)

setHeaderHeight

public void setHeaderHeight(WLength height,
                            boolean multiLine)
Description copied from class: WAbstractItemView
Sets the header height.

Use this method to change the header height. You may also enable the use of multi-line headers. By default, the header text is a single line, that is centered vertically.

The default value is 20 pixels.

Specified by:
setHeaderHeight in class WAbstractItemView

setColumnAlignment

public void setColumnAlignment(int column,
                               AlignmentFlag alignment)
Description copied from class: WAbstractItemView
Sets the content alignment for a column.

The default value is AlignLeft.

Note: For column 0, AlignCenter is currently not supported.

Specified by:
setColumnAlignment in class WAbstractItemView
See Also:
WAbstractItemView.setHeaderAlignment(int column, AlignmentFlag alignment)

getColumnAlignment

public AlignmentFlag getColumnAlignment(int column)
Description copied from class: WAbstractItemView
Returns the content alignment for a column.

Specified by:
getColumnAlignment in class WAbstractItemView
See Also:
WAbstractItemView.setColumnAlignment(int column, AlignmentFlag alignment)

setHeaderAlignment

public void setHeaderAlignment(int column,
                               AlignmentFlag alignment)
Description copied from class: WAbstractItemView
Sets the header alignment for a column.

The default value is AlignLeft.

Specified by:
setHeaderAlignment in class WAbstractItemView
See Also:
WAbstractItemView.setColumnAlignment(int column, AlignmentFlag alignment)

getHeaderAlignment

public AlignmentFlag getHeaderAlignment(int column)
Description copied from class: WAbstractItemView
Returns the header alignment for a column.

Specified by:
getHeaderAlignment in class WAbstractItemView
See Also:
WAbstractItemView.setHeaderAlignment(int column, AlignmentFlag alignment)

setColumnBorder

public void setColumnBorder(WColor color)
Description copied from class: WAbstractItemView
Sets the column border color.

The default border color is white.

Specified by:
setColumnBorder in class WAbstractItemView

refresh

public void refresh()
Description copied from class: WWidget
Refresh the widget.

The refresh method is invoked when the locale is changed using WApplication#setLocale() or when the user hit the refresh button.

The widget must actualize its contents in response.

Overrides:
refresh in class WCompositeWidget

enableAjax

protected void enableAjax()
Description copied from class: WWidget
Progresses to an Ajax-enabled widget.

This method is called when the progressive bootstrap method is used, and support for AJAX has been detected. The default behavior will upgrade the widget's event handling to use AJAX instead of full page reloads, and propagate the call to its children.

You may want to reimplement this method if you want to make changes to widget when AJAX is enabled. You should always call the base implementation.

Overrides:
enableAjax in class WCompositeWidget
See Also:
WApplication.enableAjax()