Skip to main content



      Home
Home » Eclipse Projects » NatTable » Dynamically create new row on commit last row's last cell's CellEditor(When I manually add to the data model in commit, two new rows are created)
Dynamically create new row on commit last row's last cell's CellEditor [message #1769473] Mon, 31 July 2017 18:05 Go to next message
Eclipse UserFriend
I'm trying to achieve the following:

I have a data model with a fixed no. of columns. Whenever a user edits the last column of the last row in the NatTable, and commits the edit, a new row should be created dynamically, with some prefilled values.

I'm trying to achieve this via a custom edit config on the NatTable object, extending AbstractRegistryConfiguration, and registering a custom cell editor extending nattable.edit.editor.TextCellEditor.

In this custom cell editor's public boolean commit(MoveDirectionEnum direction, boolean closeAfterCommit, boolean skipValidation) method which overrides the super type method, I create a new object in the data model which is basically a list of objects of just one custom type, i.e., I create a new instance of CustomType and add it to the list. (code snippets below).

For some reason, however, commit() is called twice, and thus two new objects are added to the list.

I'm not quite sure *why* commit() is called twice (perhaps the AbstractCellEditor's commit() method calling doCommand(new EditSelectionCommand)?), but I'd like to be able to hook into the edit lifecycle somehow to manually add an entry to the data model.

How can I achieve this? Many thanks!
public class NewValueCreatingTextCellEditor extends TextCellEditor {
  
  private String originalCanonicalValue;

  @Override
    protected Control activateCell(final Composite parent, Object originalCanonicalValue) {
  	this.originalCanonicalValue = String.valueOf(originalCanonicalValue);
  	return super.activateCell(parent, originalCanonicalValue);
  }

  @Override
  public boolean commit(MoveDirectionEnum direction, boolean closeAfterCommit, boolean skipValidation) {
   if (getCanonicalValue() != null) {
  	if (!getCanonicalValue().equals(originalCanonicalValue)) {
      MyEditorConfiguration.this.editor.setDirty(true);
  	}
   }
   else if (originalCanonicalValue != null) {
  	MyEditorConfiguration.this.editor.setDirty(true);
   }
   int thisRowIndex = MyEditorConfiguration.this.editor.getBodyLayer().getSelectionLayer().getSelectedRowPositions().iterator().next().end;
   boolean committed =  super.commit(MoveDirectionEnum.NONE, true, true);
   MyEditorConfiguration.this.editor.getMyModel().addValue(thisRowIndex, MyFactory.createMyValue("XXX", null, "YYY", "ZZZ", "AAA", false, null));
   return committed;
  }
	
}
Re: Dynamically create new row on commit last row's last cell's CellEditor [message #1769528 is a reply to message #1769473] Tue, 01 August 2017 06:35 Go to previous messageGo to next message
Eclipse UserFriend
Commit is probably called twice because of the focus lost event.

I probably would implement a custom UpdateDataCommandHandler and perform the necessary checks there.
Re: Dynamically create new row on commit last row's last cell's CellEditor [message #1769582 is a reply to message #1769528] Tue, 01 August 2017 19:04 Go to previous message
Eclipse UserFriend
Thanks Dirk, I'll try that.

UPDATE: Works like a spell, thanks!

[Updated on: Tue, 01 August 2017 19:43] by Moderator

Previous Topic:Not able to select Row Header when only one column is present
Next Topic:Body and row headers not displaying for Big Data
Goto Forum:
  


Current Time: Sun Jul 27 09:41:09 EDT 2025

Powered by FUDForum. Page generated in 0.03795 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top