Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: use the same cell reference constructor in order to ensure consistency #4634

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
245d39b
fix: use the same cell reference constructor in order to ensure consi…
ugur-vaadin Feb 2, 2023
2af83b6
Merge branch 'master' into 4588-spreadsheet-does-not-trigger-addcellv…
ugur-vaadin Feb 14, 2023
fe708df
fix: use getSheet from SpreadsheetCommand to get the sheet
ugur-vaadin Feb 14, 2023
6227ec5
fix: apply formatter
ugur-vaadin Feb 14, 2023
268ff65
fix: add cell reference without sheet name to cell value change event
ugur-vaadin Feb 14, 2023
066e1d2
Merge branch 'main' into 4588-spreadsheet-does-not-trigger-addcellval…
ugur-vaadin Feb 15, 2023
7105375
fix: add missing cell reference without sheet name to event
ugur-vaadin Feb 15, 2023
893994a
Merge branch 'main' into 4588-spreadsheet-does-not-trigger-addcellval…
ugur-vaadin Feb 15, 2023
31fa8ac
fix: update cell value change unit test
ugur-vaadin Feb 15, 2023
8f08024
Merge branch '4588-spreadsheet-does-not-trigger-addcellvaluechangelis…
ugur-vaadin Feb 15, 2023
9158159
Merge branch 'main' into 4588-spreadsheet-does-not-trigger-addcellval…
ugur-vaadin Feb 27, 2023
5476fdd
fix: add sheet name to more internal cell references
ugur-vaadin Feb 27, 2023
09928c6
Merge branch 'main' into 4588-spreadsheet-does-not-trigger-addcellval…
ugur-vaadin Jun 30, 2023
530e9e7
fix: introduce cellset to avoid selected cell duplication in event
ugur-vaadin Jul 3, 2023
27ce9d9
Merge branch 'main' into 4588-spreadsheet-does-not-trigger-addcellval…
ugur-vaadin Jul 3, 2023
f58821b
refactor: add contains checks for indexes and add javadocs
ugur-vaadin Jul 10, 2023
60dd913
Merge branch 'main' into 4588-spreadsheet-does-not-trigger-addcellval…
ugur-vaadin Jul 18, 2023
e20b0fe
refactor: rename api to minimize the need for refactoring
ugur-vaadin Jul 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public void loadFixture(final Spreadsheet spreadsheet) {
}
List<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -382,16 +393,22 @@ 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);
}

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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()));
Expand All @@ -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();
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<CellRangeAddress> i = cellRangeAddresses.iterator(); i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -399,8 +401,10 @@ public void onSelectionDecreasePainted(int r, int c) {
if (!SpreadsheetUtil.isCellInRange(selectedCellReference,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

18% of developers fix this issue

NULL_DEREFERENCE: object selectedCellReference last assigned on line 398 could be null and is dereferenced by call to isCellInRange(...) at line 401.


ℹ️ Learn about @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

newPaintedCellRange)) {
selectedCellReference = new CellReference(
spreadsheet.getActiveSheet().getSheetName(),
newPaintedCellRange.getFirstRow(),
newPaintedCellRange.getFirstColumn());
newPaintedCellRange.getFirstColumn(), false,
false);
}
getCellSelectionManager().handleCellRangeSelection(
selectedCellReference, newPaintedCellRange, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CellReference> cells;

/**
* Creates a set with the specified cells
*
* @param cells
* cells to construct the set with, not {@code null}
*/
public CellSet(Set<CellReference> 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<CellReference> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -781,8 +783,9 @@ private String getFormattedCellValue(Cell cell) {
}

private void fireCellValueChangeEvent(Cell cell) {
Set<CellReference> cells = new HashSet<CellReference>();
cells.add(new CellReference(cell));
Set<CellReference> cells = new HashSet<>();
CellReference ref = new CellReference(cell);
cells.add(ref);
spreadsheet.fireEvent(new CellValueChangeEvent(spreadsheet, cells));
}

Expand Down
Loading