|
| 1 | +/** |
| 2 | + * Copyright 2000-2025 Vaadin Ltd. |
| 3 | + * |
| 4 | + * This program is available under Vaadin Commercial License and Service Terms. |
| 5 | + * |
| 6 | + * See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full |
| 7 | + * license. |
| 8 | + */ |
| 9 | +package com.vaadin.flow.component.spreadsheet.tests.fixtures; |
| 10 | + |
| 11 | +import org.apache.poi.ss.usermodel.Cell; |
| 12 | +import org.apache.poi.ss.usermodel.Sheet; |
| 13 | + |
| 14 | +import com.vaadin.flow.component.Component; |
| 15 | +import com.vaadin.flow.component.HasValue; |
| 16 | +import com.vaadin.flow.component.spreadsheet.Spreadsheet; |
| 17 | +import com.vaadin.flow.component.spreadsheet.SpreadsheetComponentFactory; |
| 18 | +import com.vaadin.flow.component.textfield.TextField; |
| 19 | + |
| 20 | +public class AdjacentCustomEditorsFixture implements SpreadsheetFixture { |
| 21 | + |
| 22 | + @Override |
| 23 | + public void loadFixture(final Spreadsheet spreadsheet) { |
| 24 | + spreadsheet.setShowCustomEditorOnFocus(true); |
| 25 | + spreadsheet.setSpreadsheetComponentFactory(new CustomEditorFactory()); |
| 26 | + } |
| 27 | + |
| 28 | + private static class CustomEditorFactory |
| 29 | + implements SpreadsheetComponentFactory { |
| 30 | + |
| 31 | + @Override |
| 32 | + public Component getCustomComponentForCell(Cell cell, int rowIndex, |
| 33 | + int columnIndex, Spreadsheet spreadsheet, Sheet sheet) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public Component getCustomEditorForCell(Cell cell, final int rowIndex, |
| 39 | + final int columnIndex, final Spreadsheet spreadsheet, |
| 40 | + Sheet sheet) { |
| 41 | + if (rowIndex != 1 || columnIndex < 1 || columnIndex > 5) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + var textField = new TextField(); |
| 45 | + textField.addValueChangeListener( |
| 46 | + e -> spreadsheet.refreshCells(spreadsheet |
| 47 | + .createCell(rowIndex, columnIndex, e.getValue()))); |
| 48 | + return textField; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onCustomEditorDisplayed(Cell cell, int rowIndex, |
| 53 | + int columnIndex, Spreadsheet spreadsheet, Sheet sheet, |
| 54 | + Component customEditor) { |
| 55 | + if (cell == null) { |
| 56 | + return; |
| 57 | + } |
| 58 | + String cellValue = cell.getStringCellValue(); |
| 59 | + ((HasValue) customEditor).setValue(cellValue); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments