Skip to content

Commit 3495ba6

Browse files
authored
Merge pull request #152 from SeeSharpSoft/fb_clear_cells_index_out_of_bound
[FIX] Index out of bound error on multi line clear cells
2 parents 8fb68c9 + a62d37c commit 3495ba6

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/table/CsvTableEditor.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,9 @@ public final Object[][] removeRows(int[] indices) {
392392
currentRows.sort(Collections.reverseOrder());
393393
TableDataHandler dataHandler = getDataHandler();
394394
Object[][] currentData = dataHandler.getCurrentState();
395+
int offset = getFileEditorState().getFixedHeaders() ? 1 : 0;
395396
for (int currentRow : currentRows) {
396-
currentData = ArrayUtil.remove(currentData, currentRow + (getFileEditorState().getFixedHeaders() ? 1 : 0));
397+
currentData = ArrayUtil.remove(currentData, currentRow + offset);
397398
}
398399
updateTableComponentData(dataHandler.addState(currentData));
399400
return currentData;
@@ -414,10 +415,8 @@ public final Object[][] addColumn(int focusedColumnIndex, boolean before) {
414415
public final Object[][] removeColumns(int[] indices) {
415416
List<Integer> currentColumns = Ints.asList(indices);
416417
currentColumns.sort(Collections.reverseOrder());
417-
418418
TableDataHandler dataHandler = getDataHandler();
419419
Object[][] currentData = dataHandler.getCurrentState();
420-
421420
for (int currentColumn : currentColumns) {
422421
for (int i = 0; i < currentData.length; ++i) {
423422
currentData[i] = ArrayUtil.remove(currentData[i], currentColumn);
@@ -426,4 +425,17 @@ public final Object[][] removeColumns(int[] indices) {
426425
updateTableComponentData(dataHandler.addState(currentData));
427426
return currentData;
428427
}
428+
429+
public final Object[][] clearCells(int[] columns, int[] rows) {
430+
TableDataHandler dataHandler = getDataHandler();
431+
Object[][] currentData = dataHandler.getCurrentState();
432+
int offset = getFileEditorState().getFixedHeaders() ? 1 : 0;
433+
for (int currentColumn : columns) {
434+
for (int currentRow : rows) {
435+
currentData[currentRow + offset][currentColumn] = "";
436+
}
437+
}
438+
updateTableComponentData(dataHandler.addState(currentData));
439+
return currentData;
440+
}
429441
}

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/table/swing/CsvTableEditorActions.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import net.seesharpsoft.intellij.plugins.csv.editor.CsvFileEditorProvider;
99

1010
import javax.swing.*;
11-
import javax.swing.table.DefaultTableModel;
1211
import java.awt.*;
1312
import java.awt.event.ActionEvent;
1413
import java.awt.event.ActionListener;
@@ -184,25 +183,17 @@ public void actionPerformed(ActionEvent e) {
184183
try {
185184
JBTable table = csvTableEditor.getTable();
186185
int[] selectedRows = table.getSelectedRows();
187-
if (selectedRows == null || selectedRows.length == 0) {
188-
return;
189-
}
190186
int[] selectedColumns = table.getSelectedColumns();
191-
if (selectedColumns == null || selectedColumns.length == 0) {
187+
188+
if (selectedRows == null || selectedRows.length == 0 ||
189+
selectedColumns == null || selectedColumns.length == 0) {
192190
return;
193191
}
192+
194193
int focusedRow = table.getSelectedRow();
195194
int focusedColumn = table.getSelectedColumn();
196195

197-
DefaultTableModel tableModel = csvTableEditor.getTableModel();
198-
199-
for (int row : selectedRows) {
200-
for (int column : selectedColumns) {
201-
tableModel.setValueAt("", row, column);
202-
}
203-
}
204-
205-
csvTableEditor.syncTableModelWithUI();
196+
csvTableEditor.clearCells(selectedColumns, selectedRows);
206197

207198
selectCell(table, focusedRow, focusedColumn);
208199
} finally {

0 commit comments

Comments
 (0)