Skip to content

Commit 4c79c54

Browse files
author
Jeanette Winzenburg
committed
8187307: ListView, TableView, TreeView: receives editCancel event when edit is committed
Reviewed-by: mhanl, aghaisas
1 parent f01c50d commit 4c79c54

File tree

8 files changed

+129
-41
lines changed

8 files changed

+129
-41
lines changed

modules/javafx.controls/src/main/java/javafx/scene/control/ListCell.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,16 @@ public void changed(
386386
/** {@inheritDoc} */
387387
@Override public void commitEdit(T newValue) {
388388
if (! isEditing()) return;
389-
ListView<T> list = getListView();
390389

390+
// inform parent classes of the commit, so that they can switch us
391+
// out of the editing state.
392+
// This MUST come before the updateItem call below, otherwise it will
393+
// call cancelEdit(), resulting in both commit and cancel events being
394+
// fired (as identified in RT-29650)
395+
super.commitEdit(newValue);
396+
397+
ListView<T> list = getListView();
398+
// JDK-8187307: fire the commit after updating cell's editing state
391399
if (list != null) {
392400
// Inform the ListView of the edit being ready to be committed.
393401
list.fireEvent(new ListView.EditEvent<T>(list,
@@ -396,13 +404,6 @@ public void changed(
396404
list.getEditingIndex()));
397405
}
398406

399-
// inform parent classes of the commit, so that they can switch us
400-
// out of the editing state.
401-
// This MUST come before the updateItem call below, otherwise it will
402-
// call cancelEdit(), resulting in both commit and cancel events being
403-
// fired (as identified in RT-29650)
404-
super.commitEdit(newValue);
405-
406407
// update the item within this cell, so that it represents the new value
407408
updateItem(newValue, false);
408409

modules/javafx.controls/src/main/java/javafx/scene/control/TableCell.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -353,26 +353,27 @@ TablePosition<S, T> getEditingCellAtStartEdit() {
353353
@Override public void commitEdit(T newValue) {
354354
if (!isEditing()) return;
355355

356+
// inform parent classes of the commit, so that they can switch us
357+
// out of the editing state.
358+
// This MUST come before the updateItem call below, otherwise it will
359+
// call cancelEdit(), resulting in both commit and cancel events being
360+
// fired (as identified in RT-29650)
361+
super.commitEdit(newValue);
362+
356363
final TableView<S> table = getTableView();
364+
// JDK-8187307: fire the commit after updating cell's editing state
357365
if (getTableColumn() != null) {
358366
// Inform the TableColumn of the edit being ready to be committed.
359367
CellEditEvent<S, T> editEvent = new CellEditEvent<>(
360-
table,
361-
editingCellAtStartEdit,
362-
TableColumn.editCommitEvent(),
363-
newValue
364-
);
368+
table,
369+
editingCellAtStartEdit,
370+
TableColumn.editCommitEvent(),
371+
newValue
372+
);
365373

366374
Event.fireEvent(getTableColumn(), editEvent);
367375
}
368376

369-
// inform parent classes of the commit, so that they can switch us
370-
// out of the editing state.
371-
// This MUST come before the updateItem call below, otherwise it will
372-
// call cancelEdit(), resulting in both commit and cancel events being
373-
// fired (as identified in RT-29650)
374-
super.commitEdit(newValue);
375-
376377
// update the item within this cell, so that it represents the new value
377378
updateItem(newValue, false);
378379

modules/javafx.controls/src/main/java/javafx/scene/control/TreeCell.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,17 @@ public String getName() {
394394
/** {@inheritDoc} */
395395
@Override public void commitEdit(T newValue) {
396396
if (! isEditing()) return;
397+
398+
// inform parent classes of the commit, so that they can switch us
399+
// out of the editing state.
400+
// This MUST come before the updateItem call below, otherwise it will
401+
// call cancelEdit(), resulting in both commit and cancel events being
402+
// fired (as identified in RT-29650)
403+
super.commitEdit(newValue);
404+
397405
final TreeItem<T> treeItem = getTreeItem();
398406
final TreeView<T> tree = getTreeView();
407+
// JDK-8187307: fire the commit after updating cell's editing state
399408
if (tree != null) {
400409
// Inform the TreeView of the edit being ready to be committed.
401410
tree.fireEvent(new TreeView.EditEvent<T>(tree,
@@ -405,13 +414,6 @@ public String getName() {
405414
newValue));
406415
}
407416

408-
// inform parent classes of the commit, so that they can switch us
409-
// out of the editing state.
410-
// This MUST come before the updateItem call below, otherwise it will
411-
// call cancelEdit(), resulting in both commit and cancel events being
412-
// fired (as identified in RT-29650)
413-
super.commitEdit(newValue);
414-
415417
// update the item within this cell, so that it represents the new value
416418
if (treeItem != null) {
417419
treeItem.setValue(newValue);

modules/javafx.controls/src/main/java/javafx/scene/control/TreeTableCell.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -370,26 +370,27 @@ TreeTablePosition<S, T> getEditingCellAtStartEdit() {
370370
@Override public void commitEdit(T newValue) {
371371
if (!isEditing()) return;
372372

373+
// inform parent classes of the commit, so that they can switch us
374+
// out of the editing state.
375+
// This MUST come before the updateItem call below, otherwise it will
376+
// call cancelEdit(), resulting in both commit and cancel events being
377+
// fired (as identified in RT-29650)
378+
super.commitEdit(newValue);
379+
373380
final TreeTableView<S> table = getTreeTableView();
381+
// JDK-8187307: fire the commit after updating cell's editing state
374382
if (getTableColumn() != null) {
375383
// Inform the TreeTableColumn of the edit being ready to be committed.
376384
CellEditEvent<S,T> editEvent = new CellEditEvent<S,T>(
377-
table,
378-
editingCellAtStartEdit,
379-
TreeTableColumn.<S,T>editCommitEvent(),
380-
newValue
381-
);
385+
table,
386+
editingCellAtStartEdit,
387+
TreeTableColumn.<S,T>editCommitEvent(),
388+
newValue
389+
);
382390

383391
Event.fireEvent(getTableColumn(), editEvent);
384392
}
385393

386-
// inform parent classes of the commit, so that they can switch us
387-
// out of the editing state.
388-
// This MUST come before the updateItem call below, otherwise it will
389-
// call cancelEdit(), resulting in both commit and cancel events being
390-
// fired (as identified in RT-29650)
391-
super.commitEdit(newValue);
392-
393394
// update the item within this cell, so that it represents the new value
394395
updateItem(newValue, false);
395396

modules/javafx.controls/src/test/java/test/javafx/scene/control/ListCellTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,26 @@ public void testStartEditOffRangeMustNotUpdateEditingLocation() {
874874
assertEquals("list editing location must not be updated", -1, list.getEditingIndex());
875875
}
876876

877+
@Test
878+
public void testCommitEditMustNotFireCancel() {
879+
list.setEditable(true);
880+
// JDK-8187307: handler that resets control's editing state
881+
list.setOnEditCommit(e -> {
882+
int index = e.getIndex();
883+
list.getItems().set(index, e.getNewValue());
884+
list.edit(-1);
885+
});
886+
cell.updateListView(list);
887+
int editingIndex = 1;
888+
cell.updateIndex(editingIndex);
889+
list.edit(editingIndex);
890+
List<EditEvent<String>> events = new ArrayList<>();
891+
list.setOnEditCancel(events::add);
892+
String value = "edited";
893+
cell.commitEdit(value);
894+
assertEquals("sanity: value committed", value, list.getItems().get(editingIndex));
895+
assertEquals("commit must not have fired editCancel", 0, events.size());
896+
}
877897

878898
// When the list view item's change and affects a cell that is editing, then what?
879899
// When the list cell's index is changed while it is editing, then what?

modules/javafx.controls/src/test/java/test/javafx/scene/control/TableCellTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,28 @@ public void testEditStartEventAfterStartOnTable() {
600600
}
601601

602602
//------------- commitEdit
603-
// fix of JDK-8271474 changed the implementation of how the editing location is evaluated
603+
604+
@Test
605+
public void testCommitEditMustNotFireCancel() {
606+
setupForEditing();
607+
// JDK-8187307: handler that resets control's editing state
608+
editingColumn.setOnEditCommit(e -> {
609+
table.getItems().set(e.getTablePosition().getRow(), e.getNewValue());
610+
table.edit(-1, null);
611+
});
612+
int editingRow = 1;
613+
cell.updateIndex(editingRow);
614+
table.edit(editingRow, editingColumn);
615+
List<CellEditEvent<?, ?>> events = new ArrayList<>();
616+
editingColumn.setOnEditCancel(events::add);
617+
String value = "edited";
618+
cell.commitEdit(value);
619+
assertEquals("sanity: value committed", value, table.getItems().get(editingRow));
620+
assertEquals("commit must not have fired editCancel", 0, events.size());
621+
}
622+
623+
624+
// fix of JDK-8271474 changed the implementation of how the editing location is evaluated
604625

605626
@Test
606627
public void testEditCommitEvent() {

modules/javafx.controls/src/test/java/test/javafx/scene/control/TreeCellTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,27 @@ public void testStartEditOffRangeMustNotUpdateEditingLocation() {
867867
assertNull("tree editing location must not be updated", tree.getEditingItem());
868868
}
869869

870+
@Test
871+
public void testCommitEditMustNotFireCancel() {
872+
tree.setEditable(true);
873+
int editingIndex = 1;
874+
TreeItem<String> editingItem = tree.getTreeItem(editingIndex);
875+
// JDK-8187307: handler that resets control's editing state
876+
tree.setOnEditCommit(e -> {
877+
editingItem.setValue(e.getNewValue());
878+
tree.edit(null);
879+
});
880+
cell.updateTreeView(tree);
881+
cell.updateIndex(editingIndex);
882+
List<EditEvent<String>> events = new ArrayList<>();
883+
tree.setOnEditCancel(events::add);
884+
tree.edit(editingItem);
885+
String value = "edited";
886+
cell.commitEdit(value);
887+
assertEquals("sanity: value committed", value, tree.getTreeItem(editingIndex).getValue());
888+
assertEquals("commit must not have fired editCancel", 0, events.size());
889+
}
890+
870891

871892
// When the tree view item's change and affects a cell that is editing, then what?
872893
// When the tree cell's index is changed while it is editing, then what?

modules/javafx.controls/src/test/java/test/javafx/scene/control/TreeTableCellTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,28 @@ public void testEditStartEventAfterStartOnTable() {
912912
}
913913

914914
//------------- commitEdit
915-
// fix of JDK-8271474 changed the implementation of how the editing location is evaluated
915+
916+
@Test
917+
public void testCommitEditMustNotFireCancel() {
918+
setupForEditing();
919+
// JDK-8187307: handler that resets control's editing state
920+
editingColumn.setOnEditCommit(e -> {
921+
TreeItem<String> treeItem = tree.getTreeItem(e.getTreeTablePosition().getRow());
922+
treeItem.setValue(e.getNewValue());
923+
tree.edit(-1, null);
924+
});
925+
int editingRow = 1;
926+
cell.updateIndex(editingRow);
927+
tree.edit(editingRow, editingColumn);
928+
List<CellEditEvent<?, ?>> events = new ArrayList<>();
929+
editingColumn.setOnEditCancel(events::add);
930+
String value = "edited";
931+
cell.commitEdit(value);
932+
assertEquals("sanity: value committed", value, tree.getTreeItem(editingRow).getValue());
933+
assertEquals("commit must not have fired editCancel", 0, events.size());
934+
}
935+
936+
// fix of JDK-8271474 changed the implementation of how the editing location is evaluated
916937

917938
@Test
918939
public void testEditCommitEvent() {

0 commit comments

Comments
 (0)