Class WItemDelegate

    • Constructor Detail

      • WItemDelegate

        public WItemDelegate()
        Create an item delegate.
    • Method Detail

      • updateModelIndex

        public void updateModelIndex​(WWidget widget,
                                     WModelIndex index)
        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 class WAbstractItemDelegate
      • setTextFormat

        public void setTextFormat​(java.lang.String format)
        Sets the text format string.

        The ItemDataRole.Display data is converted to a string using StringUtils.asString(Object), passing the given format. If the format is an empty string, this corresponds to Object.toString().

        The default value is "".

      • setEditState

        public void setEditState​(WWidget editor,
                                 WModelIndex index,
                                 java.lang.Object value)
        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 class WAbstractItemDelegate
        See Also:
        createEditor(WModelIndex index, EnumSet flags)
      • createEditor

        protected WWidget createEditor​(WModelIndex index,
                                       java.util.EnumSet<ViewItemRenderFlag> flags)
        Creates an editor for a data item.

        The default implementation returns a WLineEdit which edits the item's ItemDataRole.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(), and setModelData().

        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 reimplement updateModelIndex().

        As an example of how to provide a specialized editor, consider the default implementation, which returns a WLineEdit:

        
         protected WWidget createEditor(WModelIndex index, EnumSet&lt;ViewItemRenderFlag&gt; 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;
         }