Skip to content

Commit 98e7b94

Browse files
committed
fix: introduce cellset to avoid selected cell duplication in event
1 parent 2327868 commit 98e7b94

File tree

7 files changed

+63
-43
lines changed

7 files changed

+63
-43
lines changed

vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow-integration-tests/src/main/java/com/vaadin/flow/component/spreadsheet/tests/fixtures/PopupButtonFixture.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class PopupButtonFixture implements SpreadsheetFixture {
3030
@Override
3131
public void loadFixture(final Spreadsheet spreadsheet) {
3232
spreadsheet.addSelectionChangeListener(event -> {
33-
if (event.getAllSelectedCells().size() != 1) {
33+
if (event.getAllSelectedCells().getCellCount() != 1) {
3434
return;
3535
}
3636
List<String> values = new ArrayList<>(VALUES);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.vaadin.flow.component.spreadsheet;
2+
3+
import org.apache.poi.ss.util.CellReference;
4+
5+
import java.util.Collections;
6+
import java.util.Set;
7+
8+
public class CellSet {
9+
10+
private final Set<CellReference> cells;
11+
12+
public CellSet(Set<CellReference> cells) {
13+
this.cells = cells;
14+
}
15+
16+
public Set<CellReference> getCells() {
17+
return Collections.unmodifiableSet(cells);
18+
}
19+
20+
public int getCellCount() {
21+
return cells.size();
22+
}
23+
24+
public boolean containsCell(CellReference cell) {
25+
if (cells.isEmpty()) {
26+
return false;
27+
}
28+
if (cell.getSheetName() == null) {
29+
CellReference cellWithSheetName = new CellReference(
30+
cells.iterator().next().getSheetName(), cell.getRow(),
31+
cell.getCol(), cell.isRowAbsolute(), cell.isColAbsolute());
32+
return cells.contains(cellWithSheetName);
33+
}
34+
return cells.contains(cell);
35+
}
36+
}

vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellValueManager.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -783,14 +783,9 @@ private String getFormattedCellValue(Cell cell) {
783783
}
784784

785785
private void fireCellValueChangeEvent(Cell cell) {
786-
Set<CellReference> cells = new HashSet<CellReference>();
786+
Set<CellReference> cells = new HashSet<>();
787787
CellReference ref = new CellReference(cell);
788788
cells.add(ref);
789-
if (ref.getSheetName() != null) {
790-
CellReference refWithoutSheetName = new CellReference(ref.getRow(),
791-
ref.getCol());
792-
cells.add(refWithoutSheetName);
793-
}
794789
spreadsheet.fireEvent(new CellValueChangeEvent(spreadsheet, cells));
795790
}
796791

@@ -800,13 +795,6 @@ private void fireFormulaValueChangeEvent(Set<CellReference> changedCells) {
800795
}
801796

802797
private void fireCellValueChangeEvent(Set<CellReference> changedCells) {
803-
List<CellReference> cellRefsWithSheetName = changedCells.stream()
804-
.filter(ref -> ref.getSheetName() != null).toList();
805-
cellRefsWithSheetName.forEach(ref -> {
806-
CellReference refWithoutSheetName = new CellReference(ref.getRow(),
807-
ref.getCol());
808-
changedCells.add(refWithoutSheetName);
809-
});
810798
spreadsheet
811799
.fireEvent(new CellValueChangeEvent(spreadsheet, changedCells));
812800
}

vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/Spreadsheet.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,16 +2857,14 @@ public void shiftRows(int startRow, int endRow, int n,
28572857
getHiddenRowIndexes());
28582858
if (row == null) {
28592859
valueManager.updateDeletedRowsInClientCache(rowIndex, rowIndex);
2860-
if (_hiddenRowIndexes.contains(rowIndex)) {
2861-
_hiddenRowIndexes.remove(rowIndex);
2862-
}
2860+
_hiddenRowIndexes.remove(rowIndex);
28632861
for (int c = 0; c < getCols(); c++) {
28642862
styler.clearCellStyle(r, c);
28652863
}
28662864
} else {
28672865
if (row.getZeroHeight()) {
28682866
_hiddenRowIndexes.add(rowIndex);
2869-
} else if (_hiddenRowIndexes.contains(rowIndex)) {
2867+
} else {
28702868
_hiddenRowIndexes.remove(rowIndex);
28712869
}
28722870
for (int c = 0; c < getCols(); c++) {
@@ -5242,15 +5240,15 @@ public void setRowColHeadingsVisible(boolean visible) {
52425240
*/
52435241
public abstract static class ValueChangeEvent
52445242
extends ComponentEvent<Component> {
5245-
private final Set<CellReference> changedCells;
5243+
private final CellSet changedCells;
52465244

52475245
public ValueChangeEvent(Component source,
52485246
Set<CellReference> changedCells) {
52495247
super(source, false);
5250-
this.changedCells = changedCells;
5248+
this.changedCells = new CellSet(changedCells);
52515249
}
52525250

5253-
public Set<CellReference> getChangedCells() {
5251+
public CellSet getChangedCells() {
52545252
return changedCells;
52555253
}
52565254
}
@@ -5374,21 +5372,18 @@ public List<CellRangeAddress> getCellRangeAddresses() {
53745372
* @return A combination of all selected cells, regardless of selection
53755373
* mode. Doesn't contain duplicates.
53765374
*/
5377-
public Set<CellReference> getAllSelectedCells() {
5378-
return Spreadsheet.getAllSelectedCells(selectedCellReference,
5379-
individualSelectedCells, cellRangeAddresses);
5380-
5375+
public CellSet getAllSelectedCells() {
5376+
return new CellSet(
5377+
Spreadsheet.getAllSelectedCells(selectedCellReference,
5378+
individualSelectedCells, cellRangeAddresses));
53815379
}
53825380
}
53835381

53845382
private static Set<CellReference> getAllSelectedCells(
53855383
CellReference selectedCellReference,
53865384
List<CellReference> individualSelectedCells,
53875385
List<CellRangeAddress> cellRangeAddresses) {
5388-
Set<CellReference> cells = new HashSet<CellReference>();
5389-
for (CellReference r : individualSelectedCells) {
5390-
cells.add(r);
5391-
}
5386+
Set<CellReference> cells = new HashSet<>(individualSelectedCells);
53925387
cells.add(selectedCellReference);
53935388

53945389
if (cellRangeAddresses != null) {
@@ -5594,10 +5589,9 @@ public CellReference getSelectedCellReference() {
55945589
public Set<CellReference> getSelectedCellReferences() {
55955590
SelectionChangeEvent event = selectionManager.getLatestSelectionEvent();
55965591
if (event == null) {
5597-
return new HashSet<CellReference>();
5598-
} else {
5599-
return event.getAllSelectedCells();
5592+
return new HashSet<>();
56005593
}
5594+
return event.getAllSelectedCells().getCells();
56015595
}
56025596

56035597
/**

vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/charts/converter/xssfreader/AbstractSeriesReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ void onValueChange(final List<CellReference> referencedCells,
293293
return;
294294
}
295295

296-
for (CellReference changedCell : event.getChangedCells()) {
296+
for (CellReference changedCell : event.getChangedCells().getCells()) {
297297
// getChangedCell erroneously provides relative cell refs
298298
// if this gets fixed, this conversion method should be
299299
// removed

vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/test/java/com/vaadin/flow/component/spreadsheet/tests/CellValueChangeEventOnFormulaChangeTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import static org.junit.Assert.assertEquals;
1212
import static org.junit.Assert.assertTrue;
1313

14-
import java.util.LinkedList;
15-
import java.util.List;
14+
import java.util.concurrent.atomic.AtomicReference;
1615

16+
import com.vaadin.flow.component.spreadsheet.CellSet;
1717
import org.apache.poi.ss.usermodel.Row;
1818
import org.apache.poi.ss.usermodel.Sheet;
1919
import org.apache.poi.ss.usermodel.Workbook;
@@ -51,20 +51,22 @@ public void setup() {
5151
*/
5252
@Test
5353
public void formulaChangeResultingInSameValue() {
54-
List<CellReference> changedCells = new LinkedList<>();
54+
AtomicReference<CellSet> changedCells = new AtomicReference<>();
5555

5656
spreadsheet.addCellValueChangeListener(
57-
event -> changedCells.addAll(event.getChangedCells()));
57+
event -> changedCells.set(event.getChangedCells()));
5858

5959
spreadsheet.setSelection("C1");
6060
// B1 is 0, so the result doesn't change
6161
spreadsheet.getCellValueManager().onCellValueChange(3, 1, "=A1+2*B1");
6262

63-
assertEquals("There should be 2 changed cells", 2, changedCells.size());
63+
assertEquals("There should be one changed cell", 1,
64+
changedCells.get().getCellCount());
6465
assertTrue("The changed cells should include C1 with sheet name",
65-
changedCells.contains(new CellReference("Sheet0!C1")));
66+
changedCells.get()
67+
.containsCell(new CellReference("Sheet0!C1")));
6668
assertTrue("The changed cells should include C1 without sheet name",
67-
changedCells.contains(new CellReference("C1")));
69+
changedCells.get().containsCell(new CellReference("C1")));
6870
}
6971

7072
}

vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/test/java/com/vaadin/flow/component/spreadsheet/tests/FormulasTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public void formulaValueChangeListener_invokedOnFormulaValueChange() {
8383
spreadsheet.refreshCells(A1, A2);
8484

8585
// Check that the event was fired with the correct values
86-
Assert.assertEquals(event.get().getChangedCells().size(), 1);
87-
Assert.assertEquals(event.get().getChangedCells().iterator().next()
88-
.formatAsString(), "Sheet1!A1");
86+
Assert.assertEquals(event.get().getChangedCells().getCellCount(), 1);
87+
Assert.assertEquals(event.get().getChangedCells().getCells().iterator()
88+
.next().formatAsString(), "Sheet1!A1");
8989
// Sanity check for the forumula cell effective value
9090
Assert.assertEquals(2.0, A1.getNumericCellValue(), 0.0);
9191
}

0 commit comments

Comments
 (0)