Use TableUtil.removeSelectedItems()
authorBas Leijdekkers <basleijdekkers@gmail.com>
Tue, 13 Sep 2016 10:12:37 +0000 (12:12 +0200)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Tue, 13 Sep 2016 10:13:26 +0000 (12:13 +0200)
platform/lang-api/src/com/intellij/codeInspection/ui/ListEditForm.java
platform/lang-api/src/com/intellij/codeInspection/ui/ListWrappingTableModel.java
plugins/InspectionGadgets/src/com/siyeh/ig/ui/UiUtils.java

index 7ea93bb922c240338e556cf51ec21551a6cb9dc2..389751cf6d9dcd3db03d4a9a00267ce6e08e9317 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright 2000-2010 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package com.intellij.codeInspection.ui;
 
 import com.intellij.ui.AnActionButton;
 import com.intellij.ui.AnActionButtonRunnable;
 
 import com.intellij.ui.AnActionButton;
 import com.intellij.ui.AnActionButtonRunnable;
+import com.intellij.ui.TableUtil;
 import com.intellij.ui.ToolbarDecorator;
 
 import javax.swing.*;
 import com.intellij.ui.ToolbarDecorator;
 
 import javax.swing.*;
@@ -53,37 +54,9 @@ public class ListEditForm {
             component.requestFocus();
           });
         }
             component.requestFocus();
           });
         }
-      }).setRemoveAction(new AnActionButtonRunnable() {
-        @Override
-        public void run(AnActionButton button) {
-          final ListSelectionModel selectionModel = table.getSelectionModel();
-          final int minIndex = selectionModel.getMinSelectionIndex();
-          final int maxIndex = selectionModel.getMaxSelectionIndex();
-          if (minIndex == -1 || maxIndex == -1) {
-            return;
-          }
-          final ListWrappingTableModel tableModel = table.getModel();
-          for (int i = minIndex; i <= maxIndex; i++) {
-            if (selectionModel.isSelectedIndex(i)) {
-              tableModel.removeRow(i);
-            }
-          }
-          final int count = tableModel.getRowCount();
-          if (count <= minIndex) {
-            selectionModel.setSelectionInterval(count - 1,
-                                                count - 1);
-          }
-          else if (minIndex <= 0) {
-            if (count > 0) {
-              selectionModel.setSelectionInterval(0, 0);
-            }
-          }
-          else {
-            selectionModel.setSelectionInterval(minIndex - 1,
-                                                minIndex - 1);
-          }
-        }
-      }).disableUpDownActions().createPanel();
+      })
+      .setRemoveAction(button -> TableUtil.removeSelectedItems(table))
+      .disableUpDownActions().createPanel();
   }
 
   public JComponent getContentPanel() {
   }
 
   public JComponent getContentPanel() {
index cae75d9fd121b2f126f296a83d003b320ee62929..c00773e25785f922dceb967871b749a08041bd10 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright 2007-2013 Bas Leijdekkers
+ * Copyright 2007-2016 Bas Leijdekkers
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
 package com.intellij.codeInspection.ui;
 
 import com.intellij.util.containers.ContainerUtil;
 package com.intellij.codeInspection.ui;
 
 import com.intellij.util.containers.ContainerUtil;
+import com.intellij.util.ui.ItemRemovable;
 import org.jetbrains.annotations.NotNull;
 
 import javax.swing.table.AbstractTableModel;
 import org.jetbrains.annotations.NotNull;
 
 import javax.swing.table.AbstractTableModel;
@@ -23,7 +24,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
 import java.util.Arrays;
 import java.util.List;
 
-public class ListWrappingTableModel extends AbstractTableModel {
+public class ListWrappingTableModel extends AbstractTableModel implements ItemRemovable {
 
   private final List<List<String>> list;
   private final List<String> columnNames = new ArrayList<>();
 
   private final List<List<String>> list;
   private final List<String> columnNames = new ArrayList<>();
@@ -112,6 +113,7 @@ public class ListWrappingTableModel extends AbstractTableModel {
     return true;
   }
 
     return true;
   }
 
+  @Override
   public void removeRow(int rowIndex) {
     for (List<String> column : list) {
       column.remove(rowIndex);
   public void removeRow(int rowIndex) {
     for (List<String> column : list) {
       column.remove(rowIndex);
index 7901b4205f0c497047eb6153a9280e7a96434704..446f427fe3acd778462b811fd15500d311e13c2f 100644 (file)
@@ -60,7 +60,8 @@ public class UiUtils {
             editTableCell(table, lastRowIndex, 0);
           });
         }
             editTableCell(table, lastRowIndex, 0);
           });
         }
-      }).setRemoveAction(new RemoveAction(table))
+      })
+      .setRemoveAction(button -> TableUtil.removeSelectedItems(table))
       .disableUpDownActions().createPanel();
     panel.setPreferredSize(JBUI.size(150, 100));
     return panel;
       .disableUpDownActions().createPanel();
     panel.setPreferredSize(JBUI.size(150, 100));
     return panel;
@@ -108,7 +109,7 @@ public class UiUtils {
           }
           editTableCell(table, rowIndex, table.getColumnCount() > 1 && project != null ? 1 : 0);
         }
           }
           editTableCell(table, rowIndex, table.getColumnCount() > 1 && project != null ? 1 : 0);
         }
-      }).setRemoveAction(new RemoveAction(table))
+      }).setRemoveAction(button -> TableUtil.removeSelectedItems(table))
       .disableUpDownActions().createPanel();
     panel.setPreferredSize(JBUI.size(150, 100));
     return panel;
       .disableUpDownActions().createPanel();
     panel.setPreferredSize(JBUI.size(150, 100));
     return panel;
@@ -186,47 +187,6 @@ public class UiUtils {
     return optionsPanel;
   }
 
     return optionsPanel;
   }
 
-  private static class RemoveAction implements AnActionButtonRunnable {
-
-    private final ListTable table;
-
-    public RemoveAction(ListTable table) {
-      this.table = table;
-    }
-
-    @Override
-    public void run(AnActionButton button) {
-      EventQueue.invokeLater(() -> {
-        final TableCellEditor editor = table.getCellEditor();
-        if (editor != null) {
-          editor.stopCellEditing();
-        }
-        final ListSelectionModel selectionModel = table.getSelectionModel();
-        final int minIndex = selectionModel.getMinSelectionIndex();
-        final int maxIndex = selectionModel.getMaxSelectionIndex();
-        if (minIndex == -1 || maxIndex == -1) {
-          return;
-        }
-        final ListWrappingTableModel tableModel = table.getModel();
-        for (int i = minIndex; i <= maxIndex; i++) {
-          tableModel.removeRow(minIndex);
-        }
-        final int count = tableModel.getRowCount();
-        if (count <= minIndex) {
-          selectionModel.setSelectionInterval(count - 1, count - 1);
-        }
-        else if (minIndex <= 0) {
-          if (count > 0) {
-            selectionModel.setSelectionInterval(0, 0);
-          }
-        }
-        else {
-          selectionModel.setSelectionInterval(minIndex - 1, minIndex - 1);
-        }
-      });
-    }
-  }
-
   private static class SubclassFilter implements ClassFilter {
 
     private final String[] ancestorClasses;
   private static class SubclassFilter implements ClassFilter {
 
     private final String[] ancestorClasses;