diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow-integration-tests/src/main/java/com/vaadin/flow/component/spreadsheet/tests/fixtures/PopupButtonFixture.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow-integration-tests/src/main/java/com/vaadin/flow/component/spreadsheet/tests/fixtures/PopupButtonFixture.java index a78afcbecec..a3985d65a56 100644 --- a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow-integration-tests/src/main/java/com/vaadin/flow/component/spreadsheet/tests/fixtures/PopupButtonFixture.java +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow-integration-tests/src/main/java/com/vaadin/flow/component/spreadsheet/tests/fixtures/PopupButtonFixture.java @@ -26,8 +26,9 @@ public void loadFixture(final Spreadsheet spreadsheet) { } List values = new ArrayList<>(VALUES); CellReference ref = event.getSelectedCellReference(); - CellReference newRef = new CellReference(ref.getRow(), - ref.getCol()); + CellReference newRef = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), ref.getRow(), + ref.getCol(), false, false); DataValidationButton popupButton = new DataValidationButton( spreadsheet, values); popupButton.setUp(); diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellSelectionManager.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellSelectionManager.java index 1a62df278d2..f2ba4fff3d1 100644 --- a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellSelectionManager.java +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellSelectionManager.java @@ -116,7 +116,9 @@ public SelectionChangeEvent getLatestSelectionEvent() { } boolean isCellInsideSelection(int row, int column) { - CellReference cellReference = new CellReference(row - 1, column - 1); + CellReference cellReference = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), row - 1, + column - 1, false, false); boolean inside = cellReference.equals(selectedCellReference) || individualSelectedCells.contains(cellReference); if (!inside) { @@ -173,7 +175,9 @@ protected void reloadCurrentSelection() { */ protected void onCellSelected(int row, int column, boolean discardOldRangeSelection) { - CellReference cellReference = new CellReference(row - 1, column - 1); + CellReference cellReference = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), row - 1, + column - 1, false, false); CellReference previousCellReference = selectedCellReference; if (!cellReference.equals(previousCellReference) || discardOldRangeSelection && (!cellRangeAddresses.isEmpty() @@ -217,8 +221,9 @@ protected void onSheetAddressChanged(String value, region.col1 - 1, region.col2 - 1); } handleCellRangeSelection(cra); - selectedCellReference = new CellReference(cra.getFirstRow(), - cra.getFirstColumn()); + selectedCellReference = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), + cra.getFirstRow(), cra.getFirstColumn(), false, false); paintedCellRange = cra; cellRangeAddresses.clear(); cellRangeAddresses.add(cra); @@ -234,8 +239,10 @@ protected void onSheetAddressChanged(String value, .createCorrectCellRangeAddress(region.row1, region.col1, region.row2, region.col2); handleCellRangeSelection(cra); - selectedCellReference = new CellReference(cra.getFirstRow(), - cra.getFirstColumn()); + selectedCellReference = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), + cra.getFirstRow(), cra.getFirstColumn(), false, + false); paintedCellRange = cra; cellRangeAddresses.clear(); cellRangeAddresses.add(cra); @@ -248,7 +255,11 @@ protected void onSheetAddressChanged(String value, cellReference.getCol() + 1, cellReference.getRow() + 1, cellReference.getCol() + 1); - selectedCellReference = cellReference; + final CellReference cellReferenceWithSheetName = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), + cellReference.getRow(), cellReference.getCol(), + false, false); + selectedCellReference = cellReferenceWithSheetName; cellRangeAddresses.clear(); } } @@ -382,8 +393,9 @@ protected void handleCellRangeSelection(CellRangeAddress cra) { protected void handleCellRangeSelection(String name, CellRangeAddress cra) { - final CellReference firstCell = new CellReference(cra.getFirstRow(), - cra.getFirstColumn()); + final CellReference firstCell = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), cra.getFirstRow(), + cra.getFirstColumn(), false, false); handleCellRangeSelection(name, firstCell, cra, true); } @@ -391,7 +403,12 @@ protected void handleCellRangeSelection(String name, CellRangeAddress cra) { protected void handleCellRangeSelection(CellReference startingPoint, CellRangeAddress cellsToSelect, boolean scroll) { - handleCellRangeSelection(null, startingPoint, cellsToSelect, scroll); + final CellReference startingPointWithSheetName = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), + startingPoint.getRow(), startingPoint.getCol(), false, false); + + handleCellRangeSelection(null, startingPointWithSheetName, + cellsToSelect, scroll); } private void handleCellRangeSelection(String name, @@ -463,8 +480,9 @@ protected void onCellRangePainted(int selectedCellRow, cellRangeAddresses.clear(); individualSelectedCells.clear(); - selectedCellReference = new CellReference(selectedCellRow - 1, - selectedCellColumn - 1); + selectedCellReference = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), + selectedCellRow - 1, selectedCellColumn - 1, false, false); CellRangeAddress cra = spreadsheet.createCorrectCellRangeAddress(row1, col1, row2, col2); @@ -507,7 +525,9 @@ protected void onCellAddToSelectionAndSelected(int row, int column) { individualSelectedCells.add(selectedCellReference); } handleCellSelection(row, column); - selectedCellReference = new CellReference(row - 1, column - 1); + selectedCellReference = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), row - 1, + column - 1, false, false); spreadsheet.loadCustomEditorOnSelectedCell(); if (individualSelectedCells.contains(selectedCellReference)) { individualSelectedCells.remove( @@ -558,8 +578,9 @@ protected void onCellsAddedToRangeSelection(int row1, int col1, int row2, */ protected void onRowSelected(int row, int firstColumnIndex) { handleCellSelection(row, firstColumnIndex); - selectedCellReference = new CellReference(row - 1, - firstColumnIndex - 1); + selectedCellReference = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), row - 1, + firstColumnIndex - 1, false, false); spreadsheet.loadCustomEditorOnSelectedCell(); cellRangeAddresses.clear(); individualSelectedCells.clear(); @@ -592,8 +613,9 @@ protected void onRowAddedToRangeSelection(int row, int firstColumnIndex) { individualSelectedCells.add(selectedCellReference); } handleCellSelection(row, firstColumnIndex); - selectedCellReference = new CellReference(row - 1, - firstColumnIndex - 1); + selectedCellReference = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), row - 1, + firstColumnIndex - 1, false, false); spreadsheet.loadCustomEditorOnSelectedCell(); cellRangeAddresses.add(spreadsheet.createCorrectCellRangeAddress(row, 1, row, spreadsheet.getColumns())); @@ -612,8 +634,9 @@ protected void onRowAddedToRangeSelection(int row, int firstColumnIndex) { */ protected void onColumnSelected(int firstRowIndex, int column) { handleCellSelection(firstRowIndex, column); - selectedCellReference = new CellReference(firstRowIndex - 1, - column - 1); + selectedCellReference = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), firstRowIndex - 1, + column - 1, false, false); spreadsheet.loadCustomEditorOnSelectedCell(); cellRangeAddresses.clear(); individualSelectedCells.clear(); @@ -646,8 +669,9 @@ protected void onColumnAddedToSelection(int firstRowIndex, int column) { individualSelectedCells.add(selectedCellReference); } handleCellSelection(firstRowIndex, column); - selectedCellReference = new CellReference(firstRowIndex - 1, - column - 1); + selectedCellReference = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), firstRowIndex - 1, + column - 1, false, false); spreadsheet.loadCustomEditorOnSelectedCell(); cellRangeAddresses.add(spreadsheet.createCorrectCellRangeAddress(1, column, spreadsheet.getRows(), column)); @@ -676,8 +700,10 @@ protected void mergedRegionAdded(CellRangeAddress region) { handleCellAddressChange(region.getFirstRow() + 1, region.getFirstColumn() + 1, false); } - selectedCellReference = new CellReference(region.getFirstRow(), - region.getFirstColumn()); + selectedCellReference = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), + region.getFirstRow(), region.getFirstColumn(), false, + false); fire = true; } for (Iterator i = cellRangeAddresses.iterator(); i diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellSelectionShifter.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellSelectionShifter.java index 1da9d750c24..e191c38dfaf 100644 --- a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellSelectionShifter.java +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellSelectionShifter.java @@ -141,7 +141,9 @@ private void fireCellValueChangeEvent(CellRangeAddress region) { for (int x = region.getFirstColumn(); x <= region .getLastColumn(); x++) { for (int y = region.getFirstRow(); y <= region.getLastRow(); y++) { - cells.add(new CellReference(y, x)); + cells.add(new CellReference( + spreadsheet.getActiveSheet().getSheetName(), y, x, + false, false)); } } spreadsheet.fireEvent(new CellValueChangeEvent(spreadsheet, cells)); @@ -399,8 +401,10 @@ public void onSelectionDecreasePainted(int r, int c) { if (!SpreadsheetUtil.isCellInRange(selectedCellReference, newPaintedCellRange)) { selectedCellReference = new CellReference( + spreadsheet.getActiveSheet().getSheetName(), newPaintedCellRange.getFirstRow(), - newPaintedCellRange.getFirstColumn()); + newPaintedCellRange.getFirstColumn(), false, + false); } getCellSelectionManager().handleCellRangeSelection( selectedCellReference, newPaintedCellRange, false); diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellSet.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellSet.java new file mode 100644 index 00000000000..baf1474eb53 --- /dev/null +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellSet.java @@ -0,0 +1,117 @@ +/* + * Copyright 2023 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.component.spreadsheet; + +import org.apache.poi.ss.util.CellReference; + +import java.util.Collections; +import java.util.Objects; +import java.util.Set; + +/** + * CellSet is a set of cells that also provides utilities regarding the contents + * of the set. + */ +public class CellSet { + + private final Set cells; + + /** + * Creates a set with the specified cells + * + * @param cells + * cells to construct the set with, not {@code null} + */ + public CellSet(Set cells) { + Objects.requireNonNull(cells, "Cells cannot be null"); + this.cells = cells; + } + + /** + * Gets an unmodifiable set of the cells + * + * @return an unmodifiable set of the cells + */ + public Set getCells() { + return Collections.unmodifiableSet(cells); + } + + /** + * Gets the number of the cells + * + * @return number of cells + */ + public int size() { + return cells.size(); + } + + /** + * Whether the set contains the specified cell. Does not take the sheet + * names of the cells in set into account if the sheet name of the cell + * reference is {@code null}. + * + * @param cellReference + * cell to be checked whether it exists in the set + * @return {@code true} if set contains the specified cell, {@code false} + * otherwise + */ + public boolean contains(CellReference cellReference) { + if (cells.isEmpty()) { + return false; + } + if (cellReference.getSheetName() == null) { + CellReference cellWithSheetName = new CellReference( + cells.iterator().next().getSheetName(), + cellReference.getRow(), cellReference.getCol(), + cellReference.isRowAbsolute(), + cellReference.isColAbsolute()); + return cells.contains(cellWithSheetName); + } + return cells.contains(cellReference); + } + + /** + * Whether the set contains the specified cell. Does not take the sheet + * names of the cells in set into account. + * + * @param row + * row index of the cell, 0-based + * @param col + * col index of the cell, 0-based + * @return {@code true} if set contains the specified cell, {@code false} + * otherwise + */ + public boolean contains(int row, int col) { + return contains(new CellReference(row, col)); + } + + /** + * Whether the set contains the specified cell + * + * @param row + * row index of the cell, 0-based + * @param col + * col index of the cell, 0-based + * @param sheetName + * sheet name of the cell, not {@code null} + * @return {@code true} if set contains the specified cell, {@code false} + * otherwise + */ + public boolean contains(int row, int col, String sheetName) { + Objects.requireNonNull(sheetName, "The sheet name cannot be null"); + return contains(new CellReference(sheetName, row, col, false, false)); + } +} diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellValueManager.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellValueManager.java index a0824104c57..d49b9e125df 100644 --- a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellValueManager.java +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/CellValueManager.java @@ -602,7 +602,9 @@ public void onCellValueChange(int col, int row, String value) { // capture cell value to history CellValueCommand command = new CellValueCommand(spreadsheet); - command.captureCellValues(new CellReference(row - 1, col - 1)); + command.captureCellValues( + new CellReference(spreadsheet.getActiveSheet().getSheetName(), + row - 1, col - 1, false, false)); spreadsheet.getSpreadsheetHistoryManager().addCommand(command); boolean updateHyperlinks = false; boolean formulaChanged = false; @@ -781,8 +783,9 @@ private String getFormattedCellValue(Cell cell) { } private void fireCellValueChangeEvent(Cell cell) { - Set cells = new HashSet(); - cells.add(new CellReference(cell)); + Set cells = new HashSet<>(); + CellReference ref = new CellReference(cell); + cells.add(ref); spreadsheet.fireEvent(new CellValueChangeEvent(spreadsheet, cells)); } diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/Spreadsheet.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/Spreadsheet.java index 4167e3cd459..c980ad7c75f 100644 --- a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/Spreadsheet.java +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/Spreadsheet.java @@ -648,7 +648,8 @@ void setNamedRanges(List namedRanges) { void onPopupButtonClick(int row, int column) { PopupButton popup = sheetPopupButtons .get(SpreadsheetUtil.relativeToAbsolute(this, - new CellReference(row - 1, column - 1))); + new CellReference(getActiveSheet().getSheetName(), + row - 1, column - 1, false, false))); if (popup != null) { popup.openPopup(); } @@ -657,7 +658,8 @@ void onPopupButtonClick(int row, int column) { void onPopupClose(int row, int column) { PopupButton popup = sheetPopupButtons .get(SpreadsheetUtil.relativeToAbsolute(this, - new CellReference(row - 1, column - 1))); + new CellReference(getActiveSheet().getSheetName(), + row - 1, column - 1, false, false))); if (popup != null) { popup.closePopup(); @@ -2799,16 +2801,14 @@ public void shiftRows(int startRow, int endRow, int n, getHiddenRowIndexes()); if (row == null) { valueManager.updateDeletedRowsInClientCache(rowIndex, rowIndex); - if (_hiddenRowIndexes.contains(rowIndex)) { - _hiddenRowIndexes.remove(rowIndex); - } + _hiddenRowIndexes.remove(rowIndex); for (int c = 0; c < getCols(); c++) { styler.clearCellStyle(r, c); } } else { if (row.getZeroHeight()) { _hiddenRowIndexes.add(rowIndex); - } else if (_hiddenRowIndexes.contains(rowIndex)) { + } else { _hiddenRowIndexes.remove(rowIndex); } for (int c = 0; c < getCols(); c++) { @@ -2927,7 +2927,8 @@ private void rowsMoved(int first, int last, int n) { } else if (numberOfRowsAboveWasChanged(row, last, first)) { int newRow = cell.getRow() + n; int col = cell.getCol(); - CellReference newCell = new CellReference(newRow, col, true, + CellReference newCell = new CellReference( + getActiveSheet().getSheetName(), newRow, col, true, true); pbutton.setCellReference(newCell); updated.put(newCell, pbutton); @@ -4765,7 +4766,8 @@ public void setPopup(String cellAddress, PopupButton popupButton) { * removes the pop-up button for the target cell. */ public void setPopup(int row, int col, PopupButton popupButton) { - setPopup(new CellReference(row, col), popupButton); + setPopup(new CellReference(getActiveSheet().getSheetName(), row, col, + false, false), popupButton); } /** @@ -5035,15 +5037,15 @@ public void setRowColHeadingsVisible(boolean visible) { */ public abstract static class ValueChangeEvent extends ComponentEvent { - private final Set changedCells; + private final CellSet changedCells; public ValueChangeEvent(Component source, Set changedCells) { super(source, false); - this.changedCells = changedCells; + this.changedCells = new CellSet(changedCells); } - public Set getChangedCells() { + public CellSet getChangedCells() { return changedCells; } } @@ -5167,10 +5169,10 @@ public List getCellRangeAddresses() { * @return A combination of all selected cells, regardless of selection * mode. Doesn't contain duplicates. */ - public Set getAllSelectedCells() { - return Spreadsheet.getAllSelectedCells(selectedCellReference, - individualSelectedCells, cellRangeAddresses); - + public CellSet getAllSelectedCells() { + return new CellSet( + Spreadsheet.getAllSelectedCells(selectedCellReference, + individualSelectedCells, cellRangeAddresses)); } } @@ -5178,10 +5180,7 @@ private static Set getAllSelectedCells( CellReference selectedCellReference, List individualSelectedCells, List cellRangeAddresses) { - Set cells = new HashSet(); - for (CellReference r : individualSelectedCells) { - cells.add(r); - } + Set cells = new HashSet<>(individualSelectedCells); cells.add(selectedCellReference); if (cellRangeAddresses != null) { @@ -5189,7 +5188,9 @@ private static Set getAllSelectedCells( for (int x = a.getFirstColumn(); x <= a.getLastColumn(); x++) { for (int y = a.getFirstRow(); y <= a.getLastRow(); y++) { - cells.add(new CellReference(y, x)); + cells.add(new CellReference( + selectedCellReference.getSheetName(), y, x, + false, false)); } } } @@ -5385,10 +5386,9 @@ public CellReference getSelectedCellReference() { public Set getSelectedCellReferences() { SelectionChangeEvent event = selectionManager.getLatestSelectionEvent(); if (event == null) { - return new HashSet(); - } else { - return event.getAllSelectedCells(); + return new HashSet<>(); } + return event.getAllSelectedCells().getCells(); } /** diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/SpreadsheetHandlerImpl.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/SpreadsheetHandlerImpl.java index 91e77ba7ca5..3062ccf7eef 100644 --- a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/SpreadsheetHandlerImpl.java +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/SpreadsheetHandlerImpl.java @@ -334,7 +334,9 @@ private void fireCellValueChangeEvent(CellRangeAddress region) { for (int x = region.getFirstColumn(); x <= region .getLastColumn(); x++) { for (int y = region.getFirstRow(); y <= region.getLastRow(); y++) { - cells.add(new CellReference(y, x)); + cells.add(new CellReference( + spreadsheet.getActiveSheet().getSheetName(), y, x, + false, false)); } } fireCellValueChangeEvent(cells); diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/charts/converter/xssfreader/AbstractSeriesReader.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/charts/converter/xssfreader/AbstractSeriesReader.java index 430b31b0e26..248a2b0024a 100644 --- a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/charts/converter/xssfreader/AbstractSeriesReader.java +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/charts/converter/xssfreader/AbstractSeriesReader.java @@ -293,7 +293,7 @@ void onValueChange(final List referencedCells, return; } - for (CellReference changedCell : event.getChangedCells()) { + for (CellReference changedCell : event.getChangedCells().getCells()) { // getChangedCell erroneously provides relative cell refs // if this gets fixed, this conversion method should be // removed diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/command/CellShiftValuesCommand.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/command/CellShiftValuesCommand.java index 2fac89c4192..85f9efa6bd6 100644 --- a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/command/CellShiftValuesCommand.java +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/command/CellShiftValuesCommand.java @@ -52,8 +52,9 @@ public CellReference getSelectedCellReference() { .isCellInRange(selectedCellReference, paintedCellRange)) { return selectedCellReference; } else { - return new CellReference(paintedCellRange.getFirstRow(), - paintedCellRange.getFirstColumn()); + return new CellReference(getSheet().getSheetName(), + paintedCellRange.getFirstRow(), + paintedCellRange.getFirstColumn(), false, false); } } diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/command/CellValueCommand.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/command/CellValueCommand.java index 95fa46e858d..c9c03e3ff5c 100644 --- a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/command/CellValueCommand.java +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/command/CellValueCommand.java @@ -148,7 +148,8 @@ public void captureCellRangeValues(CellRangeAddress... cellRanges) { @Override public CellReference getSelectedCellReference() { - return new CellReference(selectedCellRow, selectedcellCol); + return new CellReference(getSheet().getSheetName(), selectedCellRow, + selectedcellCol, false, false); } @Override @@ -339,13 +340,14 @@ public Set getChangedCells() { for (Object o : values) { if (o instanceof CellValue) { CellValue cellValue = (CellValue) o; - changedCells - .add(new CellReference(cellValue.row, cellValue.col)); + changedCells.add(new CellReference(getSheet().getSheetName(), + cellValue.row, cellValue.col, false, false)); } else { CellRangeValue cellRangeValue = (CellRangeValue) o; for (int r = cellRangeValue.row1; r <= cellRangeValue.row2; r++) { for (int c = cellRangeValue.col1; c <= cellRangeValue.col2; c++) { - changedCells.add(new CellReference(r, c)); + changedCells.add(new CellReference( + getSheet().getSheetName(), r, c, false, false)); } } } diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/command/RowInsertOrDeleteCommand.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/command/RowInsertOrDeleteCommand.java index 07af0d092ac..55534d53238 100644 --- a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/command/RowInsertOrDeleteCommand.java +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/main/java/com/vaadin/flow/component/spreadsheet/command/RowInsertOrDeleteCommand.java @@ -44,7 +44,8 @@ public void execute() { @Override public CellReference getSelectedCellReference() { - return new CellReference(row, 0); + return new CellReference(getSheet().getSheetName(), row, 0, false, + false); } @Override diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/test/java/com/vaadin/flow/component/spreadsheet/tests/CellValueChangeEventOnFormulaChangeTest.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/test/java/com/vaadin/flow/component/spreadsheet/tests/CellValueChangeEventOnFormulaChangeTest.java index c17fd85adb0..b7fa2aeed10 100644 --- a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/test/java/com/vaadin/flow/component/spreadsheet/tests/CellValueChangeEventOnFormulaChangeTest.java +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/test/java/com/vaadin/flow/component/spreadsheet/tests/CellValueChangeEventOnFormulaChangeTest.java @@ -1,10 +1,12 @@ package com.vaadin.flow.component.spreadsheet.tests; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; -import java.util.LinkedList; -import java.util.List; +import java.util.concurrent.atomic.AtomicReference; +import com.vaadin.flow.component.spreadsheet.CellSet; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -42,18 +44,34 @@ public void setup() { */ @Test public void formulaChangeResultingInSameValue() { - List changedCells = new LinkedList<>(); + AtomicReference changedCells = new AtomicReference<>(); spreadsheet.addCellValueChangeListener( - event -> changedCells.addAll(event.getChangedCells())); + event -> changedCells.set(event.getChangedCells())); spreadsheet.setSelection("C1"); // B1 is 0, so the result doesn't change spreadsheet.getCellValueManager().onCellValueChange(3, 1, "=A1+2*B1"); - assertEquals("There should be 1 changed cell", 1, changedCells.size()); - assertEquals("The changed cell should be C1", - new CellReference("Sheet0!C1"), changedCells.get(0)); + assertEquals("There should be one changed cell", 1, + changedCells.get().size()); + assertTrue("The changed cells should include C1 with sheet name", + changedCells.get().contains(new CellReference("Sheet0!C1"))); + assertFalse( + "The changed cells should not include C1 with a wrong sheet name", + changedCells.get().contains(new CellReference("Sheet1!C1"))); + assertTrue("The changed cells should include C1 without sheet name", + changedCells.get().contains(new CellReference("C1"))); + assertTrue( + "The changed cells should include a cell with correct indexes without a sheet name", + changedCells.get().contains(0, 2)); + assertTrue( + "The changed cells should include a cell with correct indexes and sheet name", + changedCells.get().contains(0, 2, "Sheet0")); + assertFalse( + "The changed cells should not include a cell with correct indexes and a wrong sheet name", + changedCells.get().contains(0, 2, "Sheet1")); + } } diff --git a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/test/java/com/vaadin/flow/component/spreadsheet/tests/FormulasTest.java b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/test/java/com/vaadin/flow/component/spreadsheet/tests/FormulasTest.java index c8f999bb2a9..29d9c5b2d7e 100644 --- a/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/test/java/com/vaadin/flow/component/spreadsheet/tests/FormulasTest.java +++ b/vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow/src/test/java/com/vaadin/flow/component/spreadsheet/tests/FormulasTest.java @@ -65,8 +65,8 @@ public void formulaValueChangeListener_invokedOnFormulaValueChange() { // Check that the event was fired with the correct values Assert.assertEquals(event.get().getChangedCells().size(), 1); - Assert.assertEquals(event.get().getChangedCells().iterator().next() - .formatAsString(), "Sheet1!A1"); + Assert.assertEquals(event.get().getChangedCells().getCells().iterator() + .next().formatAsString(), "Sheet1!A1"); // Sanity check for the forumula cell effective value Assert.assertEquals(2.0, A1.getNumericCellValue(), 0.0); }