getSelectedObjects — use the same approach as in TableUtil to collect selected items

This commit is contained in:
Vladimir Krivosheev
2014-01-21 20:21:13 +01:00
parent 9d412b01df
commit 47b6662290
2 changed files with 15 additions and 10 deletions
@@ -199,17 +199,21 @@ public class TableView<Item> extends BaseTableView implements ItemsProvider, Sel
@NotNull
public List<Item> getSelectedObjects() {
final int[] selectedRows = getSelectedRows();
if (selectedRows == null || selectedRows.length == 0) {
ListSelectionModel selectionModel = getSelectionModel();
int minSelectionIndex = selectionModel.getMinSelectionIndex();
int maxSelectionIndex = selectionModel.getMaxSelectionIndex();
if (minSelectionIndex == -1 || maxSelectionIndex == -1) {
return Collections.emptyList();
}
List<Item> result = new SmartList<Item>();
ListTableModel<Item> model = getListTableModel();
for (int selectedRow : selectedRows) {
int modelIndex = convertRowIndexToModel(selectedRow);
if (modelIndex >= 0 && modelIndex < model.getRowCount()) {
result.add(model.getRowValue(modelIndex));
for (int i = minSelectionIndex; i <= maxSelectionIndex; i++) {
if (selectionModel.isSelectedIndex(i)) {
int modelIndex = convertRowIndexToModel(i);
if (modelIndex >= 0 && modelIndex < model.getRowCount()) {
result.add(model.getRowValue(modelIndex));
}
}
}
return result;
@@ -105,14 +105,15 @@ public class TableUtil {
}
ListSelectionModel selectionModel = table.getSelectionModel();
if (selectionModel.isSelectionEmpty()) {
int minSelectionIndex = selectionModel.getMinSelectionIndex();
int maxSelectionIndex = selectionModel.getMaxSelectionIndex();
if (minSelectionIndex == -1 || maxSelectionIndex == -1) {
return false;
}
int minSelectionIndex = selectionModel.getMinSelectionIndex();
TableModel model = table.getModel();
boolean removed = false;
for (int index = table.getRowCount() - 1; index >= 0; index--) {
for (int index = maxSelectionIndex; index >= 0; index--) {
if (selectionModel.isSelectedIndex(index) && (applyable == null || applyable.isOperationApplyable(model, index))) {
itemRemovable.removeRow(index);
removed = true;
@@ -127,7 +128,7 @@ public class TableUtil {
if (count == 0) {
table.clearSelection();
}
else if (table.getSelectedRow() == -1) {
else if (selectionModel.getMinSelectionIndex() == -1) {
if (minSelectionIndex >= model.getRowCount()) {
selectionModel.setSelectionInterval(model.getRowCount() - 1, model.getRowCount() - 1);
}