Class WItemDelegate
This class provides the standard implementation for rendering an item (as in a WAbstractItemView
), and renders data provided by the standard data roles (see ItemDataRole
). It also provides default editing support using a line edit.
You may provide special editing support for an item by specializing the widget and reimplement
createEditor()
, setModelData()
, getEditState()
, and setEditState()
.
-
Nested Class Summary
Nested classes/interfaces inherited from class eu.webtoolkit.jwt.WObject
WObject.FormData
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected final WWidget
createEditor
(WModelIndex index, ViewItemRenderFlag flag, ViewItemRenderFlag... flags) Creates an editor for a data item.protected WWidget
createEditor
(WModelIndex index, EnumSet<ViewItemRenderFlag> flags) Creates an editor for a data item.getEditState
(WWidget editor, WModelIndex index) Returns the current edit state.Returns the text format string.void
setEditState
(WWidget editor, WModelIndex index, Object value) Sets the editor data from the editor state.void
setModelData
(Object editState, WAbstractItemModel model, WModelIndex index) Saves the edited data to the model.void
setTextFormat
(String format) Sets the text format string.update
(WWidget widget, WModelIndex index, EnumSet<ViewItemRenderFlag> flags) Creates or updates a widget that renders an item.void
updateModelIndex
(WWidget widget, WModelIndex index) Updates the model index of a widget.Methods inherited from class eu.webtoolkit.jwt.WAbstractItemDelegate
closeEditor, update, validate
Methods inherited from class eu.webtoolkit.jwt.WObject
getId, getObjectName, remove, setFormData, setObjectName, tr
-
Constructor Details
-
WItemDelegate
public WItemDelegate()Create an item delegate.
-
-
Method Details
-
update
Creates or updates a widget that renders an item.The following properties of an item are rendered:
- text using the
ItemDataRole.Display
data, with the format specified bysetTextFormat()
- a check box depending on the
ItemFlag.UserCheckable
flag andItemDataRole.Checked
data - an anchor depending on the value of
ItemDataRole.Link
- an icon depending on the value of
ItemDataRole.Decoration
- a tooltip depending on the value of
ItemDataRole.ToolTip
- a custom style class depending on the value of
ItemDataRole.StyleClass
When the flags indicates
ViewItemRenderFlag.Editing
, thencreateEditor()
is called to create a suitable editor for editing the item.- Specified by:
update
in classWAbstractItemDelegate
- text using the
-
updateModelIndex
Description copied from class:WAbstractItemDelegate
Updates the model index of a widget.This method is invoked by the view when due to row/column insertions or removals, the index has shifted.
You should reimplement this method only if you are storing the model index in the
widget
, to update the stored model index.The default implementation does nothing.
- Overrides:
updateModelIndex
in classWAbstractItemDelegate
-
setTextFormat
Sets the text format string.The
ItemDataRole.Display
data is converted to a string usingStringUtils.asString(Object)
, passing the given format. If the format is an empty string, this corresponds toObject.toString()
.The default value is "".
-
getTextFormat
Returns the text format string.- See Also:
-
setModelData
Saves the edited data to the model.The default implementation saves the current edit value to the model. You will need to reimplement this method for a custom editor.
As an example of how to deal with a specialized editor, consider the default implementation:
public void setModelData(Object editState, WAbstractItemModel model, WModelIndex index) { model.setData(index, editState, ItemDataRole.ItemDataRole::Edit); }
- Overrides:
setModelData
in classWAbstractItemDelegate
- See Also:
-
getEditState
Returns the current edit state.The default implementation returns the current text in the line edit. You will need to reimplement this method for a custom editor.
As an example of how to deal with a specialized editor, consider the default implementation:
public Object getEditState(WWidget editor) { WContainerWidget w = (WContainerWidget) editor; WLineEdit lineEdit = (WLineEdit) w.getWidget(0); return lineEdit.getText(); }
-
setEditState
Sets the editor data from the editor state.The default implementation resets the text in the line edit. You will need to reimplement this method if for a custom editor.
As an example of how to deal with a specialized editor, consider the default implementation:
public void setEditState(WWidget editor, WModelIndex index, Object value) { WContainerWidget w = (WContainerWidget) editor; WLineEdit lineEdit = (WLineEdit) w.getWidget(0); lineEdit.setText((String) value); }
- Overrides:
setEditState
in classWAbstractItemDelegate
- See Also:
-
createEditor
Creates an editor for a data item.The default implementation returns a
WLineEdit
which edits the item'sItemDataRole.Edit
value.You may reimplement this method to provide a suitable editor, or to attach a custom validator. In that case, you will probably also want to reimplement
getEditState()
,setEditState()
, andsetModelData()
.The editor should not keep a reference to the model index (it does not need to since
setModelData()
will provide the proper model index to save the data to the model). Otherwise, because model indexes may shift because of row or column insertions, you should reimplementupdateModelIndex()
.As an example of how to provide a specialized editor, consider the default implementation, which returns a
WLineEdit
:protected WWidget createEditor(WModelIndex index, EnumSet<ViewItemRenderFlag> flags) { final WContainerWidget result = new WContainerWidget(); result.setSelectable(true); WLineEdit lineEdit = new WLineEdit(); lineEdit.setText(StringUtils.asString(index.getData(ItemDataRole.ItemDataRole::Edit), this.textFormat_).toString()); lineEdit.enterPressed().addListener(this, new Signal.Listener() { public void trigger() { WItemDelegate.this.closeEditor().trigger(result, true); } }); lineEdit.escapePressed().addListener(this, new Signal.Listener() { public void trigger() { WItemDelegate.this.closeEditor().trigger(result, false); } }); if (flags.contains(ViewItemRenderFlag.ViewItemRenderFlag::Focused)) lineEdit.setFocus(); result.setLayout(new WHBoxLayout()); result.getLayout().setContentsMargins(1, 1, 1, 1); result.getLayout().addWidget(lineEdit); return result; }
-
createEditor
protected final WWidget createEditor(WModelIndex index, ViewItemRenderFlag flag, ViewItemRenderFlag... flags) Creates an editor for a data item.
-