@NotNull public static List<Object[]> removeSelectedItems(@NotNull JTable table, @Nullable ItemChecker applyable) { final TableModel model = table.getModel(); if (!(model instanceof ItemRemovable)) { throw new RuntimeException("model must be instance of ItemRemovable"); } if (table.getSelectionModel().isSelectionEmpty()) { return new ArrayList<Object[]>(0); } final List<Object[]> removedItems = new SmartList<Object[]>(); final ItemRemovable itemRemovable = (ItemRemovable)model; final int columnCount = model.getColumnCount(); doRemoveSelectedItems(table, new ItemRemovable() { @Override public void removeRow(int index) { Object[] row = new Object[columnCount]; for (int column = 0; column < columnCount; column++) { row[column] = model.getValueAt(index, column); } removedItems.add(row); itemRemovable.removeRow(index); } }, applyable); return ContainerUtil.reverse(removedItems); }
public static boolean doRemoveSelectedItems(@NotNull JTable table, @NotNull ItemRemovable itemRemovable, @Nullable ItemChecker applyable) { if (table.isEditing()) { table.getCellEditor().stopCellEditing(); } ListSelectionModel selectionModel = table.getSelectionModel(); int minSelectionIndex = selectionModel.getMinSelectionIndex(); int maxSelectionIndex = selectionModel.getMaxSelectionIndex(); if (minSelectionIndex == -1 || maxSelectionIndex == -1) { return false; } TableModel model = table.getModel(); boolean removed = false; for (int index = maxSelectionIndex; index >= 0; index--) { int modelIndex = table.convertRowIndexToModel(index); if (selectionModel.isSelectedIndex(index) && (applyable == null || applyable.isOperationApplyable(model, modelIndex))) { itemRemovable.removeRow(modelIndex); removed = true; } } if (!removed) { return false; } int count = model.getRowCount(); if (count == 0) { table.clearSelection(); } else if (selectionModel.getMinSelectionIndex() == -1) { if (minSelectionIndex >= model.getRowCount()) { selectionModel.setSelectionInterval(model.getRowCount() - 1, model.getRowCount() - 1); } else { selectionModel.setSelectionInterval(minSelectionIndex, minSelectionIndex); } } return true; }
@NotNull public static List<Object[]> removeSelectedItems(@NotNull JTable table, @Nullable ItemChecker applyable) { if (table.isEditing()){ table.getCellEditor().stopCellEditing(); } TableModel model = table.getModel(); if (!(model instanceof ItemRemovable)) { throw new RuntimeException("model must be instance of ItemRemovable"); } ListSelectionModel selectionModel = table.getSelectionModel(); int minSelectionIndex = selectionModel.getMinSelectionIndex(); if (minSelectionIndex == -1) return new ArrayList<Object[]>(0); List<Object[]> removedItems = new LinkedList<Object[]>(); final int columnCount = model.getColumnCount(); for (int idx = table.getRowCount() - 1; idx >= 0; idx--) { if (selectionModel.isSelectedIndex(idx) && (applyable == null || applyable.isOperationApplyable(model, idx))) { final Object[] row = new Object[columnCount]; for(int column = 0; column < columnCount; column++){ row[column] = model.getValueAt(idx, column); } removedItems.add(0, row); ((ItemRemovable)model).removeRow(idx); } } int count = model.getRowCount(); if (count == 0) { table.clearSelection(); } else if (table.getSelectedRow() == -1) { if (minSelectionIndex >= model.getRowCount()) { selectionModel.setSelectionInterval(model.getRowCount() - 1, model.getRowCount() - 1); } else { selectionModel.setSelectionInterval(minSelectionIndex, minSelectionIndex); } } return removedItems; }
@Nonnull public static List<Object[]> removeSelectedItems(@Nonnull JTable table, @Nullable ItemChecker applyable) { final TableModel model = table.getModel(); if (!(model instanceof ItemRemovable)) { throw new RuntimeException("model must be instance of ItemRemovable"); } if (table.getSelectionModel().isSelectionEmpty()) { return new ArrayList<Object[]>(0); } final List<Object[]> removedItems = new SmartList<Object[]>(); final ItemRemovable itemRemovable = (ItemRemovable)model; final int columnCount = model.getColumnCount(); doRemoveSelectedItems(table, new ItemRemovable() { @Override public void removeRow(int index) { Object[] row = new Object[columnCount]; for (int column = 0; column < columnCount; column++) { row[column] = model.getValueAt(index, column); } removedItems.add(row); itemRemovable.removeRow(index); } }, applyable); return ContainerUtil.reverse(removedItems); }
public static boolean doRemoveSelectedItems(@Nonnull JTable table, @Nonnull ItemRemovable itemRemovable, @Nullable ItemChecker applyable) { if (table.isEditing()) { table.getCellEditor().stopCellEditing(); } ListSelectionModel selectionModel = table.getSelectionModel(); int minSelectionIndex = selectionModel.getMinSelectionIndex(); int maxSelectionIndex = selectionModel.getMaxSelectionIndex(); if (minSelectionIndex == -1 || maxSelectionIndex == -1) { return false; } TableModel model = table.getModel(); boolean removed = false; for (int index = maxSelectionIndex; index >= 0; index--) { if (selectionModel.isSelectedIndex(index) && (applyable == null || applyable.isOperationApplyable(model, index))) { itemRemovable.removeRow(index); removed = true; } } if (!removed) { return false; } int count = model.getRowCount(); if (count == 0) { table.clearSelection(); } else if (selectionModel.getMinSelectionIndex() == -1) { if (minSelectionIndex >= model.getRowCount()) { selectionModel.setSelectionInterval(model.getRowCount() - 1, model.getRowCount() - 1); } else { selectionModel.setSelectionInterval(minSelectionIndex, minSelectionIndex); } } return true; }