com.intellij.util.ui.ItemRemovable Java Examples

The following examples show how to use com.intellij.util.ui.ItemRemovable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TableUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #2
Source File: TableUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
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;
}