Class TableCellEditor

java.lang.Object
org.eclipse.nebula.widgets.nattable.edit.editor.AbstractCellEditor
org.eclipse.nebula.widgets.nattable.edit.editor.TableCellEditor
All Implemented Interfaces:
ICellEditor

public class TableCellEditor extends AbstractCellEditor
ICellEditor that uses a JFace TableViewer as editor control. In combination with the TableCellPainter it can be used to simulate a table within a cell to support editing of collections or arrays of values of an entity. This implementation is a workaround for the missing feature of nested tables in NatTable 1.x architecture. It is limited to one column editing. If a more complex nested table structure is needed, you need to extend or implement a new table cell editor.

Note: This editor is different to other default editors in NatTable in several facts:

  • The data value in the NatTable cell needs to be an array or a collection of values
  • getCanonicalValue() will directly update the underlying NatTable data model
  • Committing the value in the NatTable framework code will simply replace the list reference with itself
  • It does only support validation error styling for conversion and validation errors as well
See Also:
  • Field Details

    • cellEditorValidator

      protected org.eclipse.jface.viewers.ICellEditorValidator cellEditorValidator
      Internal ICellEditorValidator that checks if a value is valid based on conversion and validation rules applied via NatTable IDisplayConverter and IDataValidator
  • Constructor Details

    • TableCellEditor

      public TableCellEditor()
      Creates a TableCellEditor with a default sub cell height of 20 (which is the same as the default for the TableCellPainter), that moves the selection on committing the value with enter and always opens the editor of the cell that is currently selected.
    • TableCellEditor

      public TableCellEditor(int fixedSubCellHeight, boolean moveSelectionOnEnter, boolean alwaysOpenEditor)
      Creates a TableCellEditor with the given configurations.
      Parameters:
      fixedSubCellHeight - The height of the sub cells to use. Setting a value >= 0 will result in using the specified fixed sub cell heights, a negative value will result in using the OS default height based on the font. Note that because of limitations in the native table control for some OS, it is not possible to specify different row heights.
      moveSelectionOnEnter - configure whether the selection should move after a value was committed by pressing enter.
      alwaysOpenEditor - configure whether the adjacent editor on selection movements should always open the cell for editing instead of moving into the selection state of the table control.
  • Method Details

    • getEditorValue

      public Object getEditorValue()
      Description copied from interface: ICellEditor
      Returns the current value in this editor prior to conversion. For a text editor that is used to edit integer values, this would mean it returns the text value instead of the converted integer value. This method is only intended to be used internally .
      Returns:
      The current value in this editor prior to conversion.
    • setEditorValue

      public void setEditorValue(Object value)
      Description copied from interface: ICellEditor
      Sets the given value to editor control. This method is used to put the display values to the wrapped editor.
      Parameters:
      value - The display value to set to the wrapped editor control.
    • getCanonicalValue

      public Object getCanonicalValue()
      Description copied from class: AbstractCellEditor
      Converts the current value in this editor using the configured IDisplayConverter. If there is no IDisplayConverter registered for this editor, the value itself will be returned.
      Specified by:
      getCanonicalValue in interface ICellEditor
      Overrides:
      getCanonicalValue in class AbstractCellEditor
      Returns:
      The canonical value after converting the current value or the value itself if no IDisplayConverter is configured.
      See Also:
    • setCanonicalValue

      public void setCanonicalValue(Object canonicalValue)
      Description copied from interface: ICellEditor
      Sets the given canonical value to the wrapped editor control. Prior to setting the value it needs to be converted to the display value, using the configured IDisplayConverter.
      Specified by:
      setCanonicalValue in interface ICellEditor
      Overrides:
      setCanonicalValue in class AbstractCellEditor
      Parameters:
      canonicalValue - The canonical value to be set to the wrapped editor control.
    • getEditorControl

      public org.eclipse.swt.widgets.Table getEditorControl()
      Returns:
      The editor control that is wrapped by this ICellEditor.
    • createEditorControl

      public org.eclipse.swt.widgets.Table createEditorControl(org.eclipse.swt.widgets.Composite parent)
      Description copied from interface: ICellEditor
      Creates the editor control that is wrapped by this ICellEditor. Will use the style configurations in ConfigRegistry for styling the control.
      Parameters:
      parent - The Composite that will be the parent of the new editor control. Can not be null
      Returns:
      The created editor control that is wrapped by this ICellEditor.
    • activateCell

      protected org.eclipse.swt.widgets.Control activateCell(org.eclipse.swt.widgets.Composite parent, Object originalCanonicalValue)
      Description copied from class: AbstractCellEditor
      This method will be called by AbstractCellEditor.activateCell(Composite, Object, EditModeEnum, ICellEditHandler, ILayerCell, IConfigRegistry) after initializing the activation values and before adding the default listeners. In this method the underlying editor control should be created and initialized, hiding default configuration from editor implementors.
      Specified by:
      activateCell in class AbstractCellEditor
      Parameters:
      parent - The parent Composite, needed for the creation of the editor control.
      originalCanonicalValue - The value that should be put to the activated editor control.
      Returns:
      The SWT Control to be used for capturing the new cell value.
    • close

      public void close()
      Description copied from interface: ICellEditor
      Close/dispose the contained Control
      Specified by:
      close in interface ICellEditor
      Overrides:
      close in class AbstractCellEditor
    • commit

      public boolean commit(SelectionLayer.MoveDirectionEnum direction, boolean closeAfterCommit, boolean skipValidation)
      Description copied from interface: ICellEditor
      Commits the current value of this editor.
      Specified by:
      commit in interface ICellEditor
      Overrides:
      commit in class AbstractCellEditor
      Parameters:
      direction - The direction the selection within the NatTable should move after commit has finished.
      closeAfterCommit - flag to tell whether this editor needs to closed after the commit or if it should stay open.
      skipValidation - Flag to specify whether the current value in this editor should be validated or not.
      Returns:
      true if the commit operation succeeded, false if the current value could not be committed. A value might not be committed for example if the conversion or the validation failed.
    • getDataAsArray

      protected Object[] getDataAsArray(Object cellData)
      Checks if the given data object is of type Collection or Array. Will return the Collection or Array as Object[] or null if the data object is not a Collection or Array.
      Parameters:
      cellData - The cellData that should be checked for its type.
      Returns:
      The Object[] representation of the data object if it is of type Collection or Array, or null if the data object is not a Collection or Array.
    • getFixedSubCellHeight

      public int getFixedSubCellHeight()
      Note that because of limitations to native tables of the OS, it is not possible to specify different row heights.
      Returns:
      The height of the sub cells to use. A value >= 0 results in using the specified fixed sub cell heights, a negative value results in using the OS default height based on the font.
    • setFixedSubCellHeight

      public void setFixedSubCellHeight(int fixedSubCellHeight)
      Setting a value >= 0 will result in using a fixed height of the sub cells. Setting the value to a negative number will result in using the OS default height based on the font.

      Note that because of limitations to native tables of the OS, it is not possible to specify different row heights.

      Parameters:
      fixedSubCellHeight - The height of the sub cells to use.
    • getEditingSupport

      protected org.eclipse.jface.viewers.EditingSupport getEditingSupport()
      Returns:
      The EditingSupport to use to make the TableViewer editable.
    • addEditorControlListeners

      public void addEditorControlListeners()
      This method is intended to add listeners to the wrapped editor control to add context related behavior. For example, in EditModeEnum.INLINE by default this should add a FocusListener that commits the current value if the editor control loses focus.

      This method was introduced mainly because of two issues:

      1. On Mac OS calling setBounds() on a Control will cause losing focus. So listeners need to be added after this method is called by the EditController, otherwise on activating the editor it will be closed immediately after the correct size is calculated.
      2. The main concept for cell editor activation is, that the editor control is disposed on closing the editor. This way everytime the cell editor is activated, a new editor control will be created. If an editor is implemented that needs to keep the editor control after closing the editor, it needs to be ensured that the listeners are removed again. Otherwise the listeners would be added again everytime the editor is activated.
      This method will be called automatically by EditController.editCell(ILayerCell, Composite, Object, IConfigRegistry).

      Note: We need to override this to not register the FocusListener. This is necessary because on editing the TableViewer, a Text control will be created that gains focus. This would mean to close the editor and break the whole use case.

      Specified by:
      addEditorControlListeners in interface ICellEditor
      Overrides:
      addEditorControlListeners in class AbstractCellEditor
    • removeEditorControlListeners

      public void removeEditorControlListeners()
      Description copied from interface: ICellEditor
      This method is intended to remove listeners from the wrapped editor control that was added by ICellEditor.addEditorControlListeners() before to add context related behavior.

      This method was introduced to add the possibility to create an ICellEditor whose wrapped editor control should not be disposed on closing the editor.

      The main concept for cell editor activation is, that the editor control is disposed on closing the editor. This way everytime the cell editor is activated, a new editor control will be created. If an editor is implemented that needs to keep the editor control after closing the editor, it needs to be ensured that the listeners are removed again. Otherwise the listeners would be added again everytime the editor is activated.

      This method needs to be called on ICellEditor.close(). There is no automatical call by the framework if you are not using the abstract implementation of ICellEditor.
      Specified by:
      removeEditorControlListeners in interface ICellEditor
      Overrides:
      removeEditorControlListeners in class AbstractCellEditor