Use TableUtil.removeSelectedItems()
[idea/community.git] / plugins / InspectionGadgets / src / com / siyeh / ig / ui / UiUtils.java
1 /*
2  * Copyright 2010-2016 Bas Leijdekkers
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.siyeh.ig.ui;
17
18 import com.intellij.codeInspection.ui.ListTable;
19 import com.intellij.codeInspection.ui.ListWrappingTableModel;
20 import com.intellij.ide.DataManager;
21 import com.intellij.ide.util.ClassFilter;
22 import com.intellij.ide.util.TreeClassChooser;
23 import com.intellij.ide.util.TreeClassChooserFactory;
24 import com.intellij.openapi.actionSystem.CommonDataKeys;
25 import com.intellij.openapi.actionSystem.DataContext;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.psi.PsiClass;
28 import com.intellij.psi.search.GlobalSearchScope;
29 import com.intellij.psi.util.InheritanceUtil;
30 import com.intellij.ui.*;
31 import com.intellij.ui.components.JBList;
32 import com.intellij.util.ui.JBUI;
33 import org.jetbrains.annotations.NonNls;
34
35 import javax.swing.*;
36 import javax.swing.table.TableCellEditor;
37 import java.awt.*;
38 import java.util.Collection;
39
40 public class UiUtils {
41
42   private UiUtils() {
43   }
44
45   public static void setComponentSize(Component component, int rows, int columns) {
46     final FontMetrics fontMetrics = component.getFontMetrics(component.getFont());
47     final int width = fontMetrics.charWidth('m') * columns;
48     component.setPreferredSize(new Dimension(width, fontMetrics.getHeight() * rows));
49   }
50
51   public static JPanel createAddRemovePanel(final ListTable table) {
52     final JPanel panel = ToolbarDecorator.createDecorator(table)
53       .setAddAction(new AnActionButtonRunnable() {
54         @Override
55         public void run(AnActionButton button) {
56           final ListWrappingTableModel tableModel = table.getModel();
57           tableModel.addRow();
58           EventQueue.invokeLater(() -> {
59             final int lastRowIndex = tableModel.getRowCount() - 1;
60             editTableCell(table, lastRowIndex, 0);
61           });
62         }
63       })
64       .setRemoveAction(button -> TableUtil.removeSelectedItems(table))
65       .disableUpDownActions().createPanel();
66     panel.setPreferredSize(JBUI.size(150, 100));
67     return panel;
68   }
69
70   public static JPanel createAddRemoveTreeClassChooserPanel(final ListTable table, final String chooserTitle,
71                                                             @NonNls String... ancestorClasses) {
72     final ClassFilter filter;
73     if (ancestorClasses.length == 0) {
74       filter = ClassFilter.ALL;
75     }
76     else {
77       filter = new SubclassFilter(ancestorClasses);
78     }
79     final JPanel panel = ToolbarDecorator.createDecorator(table)
80       .setAddAction(new AnActionButtonRunnable() {
81         @Override
82         public void run(AnActionButton button) {
83           final DataContext dataContext = DataManager.getInstance().getDataContext(table);
84           final Project project = CommonDataKeys.PROJECT.getData(dataContext);
85           final int rowIndex;
86           final ListWrappingTableModel tableModel = table.getModel();
87           if (project == null) {
88             tableModel.addRow();
89             rowIndex = tableModel.getRowCount() - 1;
90           }
91           else {
92             final TreeClassChooserFactory chooserFactory = TreeClassChooserFactory.getInstance(project);
93             final TreeClassChooser classChooser =
94               chooserFactory.createWithInnerClassesScopeChooser(chooserTitle, GlobalSearchScope.allScope(project), filter, null);
95             classChooser.showDialog();
96             final PsiClass selectedClass = classChooser.getSelected();
97             if (selectedClass == null) {
98               return;
99             }
100             final String qualifiedName = selectedClass.getQualifiedName();
101             final int index = tableModel.indexOf(qualifiedName, 0);
102             if (index < 0) {
103               tableModel.addRow(qualifiedName);
104               rowIndex = tableModel.getRowCount() - 1;
105             }
106             else {
107               rowIndex = index;
108             }
109           }
110           editTableCell(table, rowIndex, table.getColumnCount() > 1 && project != null ? 1 : 0);
111         }
112       }).setRemoveAction(button -> TableUtil.removeSelectedItems(table))
113       .disableUpDownActions().createPanel();
114     panel.setPreferredSize(JBUI.size(150, 100));
115     return panel;
116   }
117
118   private static void editTableCell(final ListTable table, final int row, final int column) {
119     final ListSelectionModel selectionModel = table.getSelectionModel();
120     selectionModel.setSelectionInterval(row, row);
121     EventQueue.invokeLater(() -> {
122       final ListWrappingTableModel tableModel = table.getModel();
123       table.requestFocus();
124       final Rectangle rectangle = table.getCellRect(row, column, true);
125       table.scrollRectToVisible(rectangle);
126       table.editCellAt(row, column);
127       final TableCellEditor editor = table.getCellEditor();
128       final Component component = editor.getTableCellEditorComponent(table, tableModel.getValueAt(row, column), true, row, column);
129       component.requestFocus();
130     });
131   }
132
133   public static JPanel createTreeClassChooserList(final Collection<String> collection,
134                                                   String borderTitle,
135                                                   final String chooserTitle,
136                                                   String... ancestorClasses) {
137     final ClassFilter filter;
138     if (ancestorClasses.length == 0) {
139       filter = ClassFilter.ALL;
140     }
141     else {
142       filter = new SubclassFilter(ancestorClasses);
143     }
144     final JPanel optionsPanel = new JPanel(new BorderLayout());
145     final JBList list = new JBList(collection);
146
147     final JPanel panel = ToolbarDecorator.createDecorator(list)
148       .disableUpDownActions()
149       .setAddAction(new AnActionButtonRunnable() {
150         @Override
151         public void run(AnActionButton anActionButton) {
152           final DataContext dataContext = DataManager.getInstance().getDataContext(list);
153           final Project project = CommonDataKeys.PROJECT.getData(dataContext);
154           if (project == null) {
155             return;
156           }
157           final TreeClassChooser chooser = TreeClassChooserFactory.getInstance(project)
158             .createNoInnerClassesScopeChooser(chooserTitle, GlobalSearchScope.allScope(project), filter, null);
159           chooser.showDialog();
160           final PsiClass selected = chooser.getSelected();
161           if (selected == null) {
162             return;
163           }
164           final String qualifiedName = selected.getQualifiedName();
165           final DefaultListModel model = (DefaultListModel)list.getModel();
166           final int index = model.indexOf(qualifiedName);
167           if (index < 0) {
168             model.addElement(qualifiedName);
169             collection.add(qualifiedName);
170           }
171           else {
172             list.setSelectedIndex(index);
173           }
174         }
175       })
176       .setRemoveAction(new AnActionButtonRunnable() {
177         @Override
178         public void run(AnActionButton anActionButton) {
179           collection.remove(list.getSelectedValue());
180           ListUtil.removeSelectedItems(list);
181         }
182       }).createPanel();
183     panel.setPreferredSize(JBUI.size(150, 100));
184     optionsPanel.setBorder(IdeBorderFactory.createTitledBorder(borderTitle,
185                                                                false, JBUI.insetsTop(10)));
186     optionsPanel.add(panel);
187     return optionsPanel;
188   }
189
190   private static class SubclassFilter implements ClassFilter {
191
192     private final String[] ancestorClasses;
193
194     private SubclassFilter(String[] ancestorClasses) {
195       this.ancestorClasses = ancestorClasses;
196     }
197
198     @Override
199     public boolean isAccepted(PsiClass aClass) {
200       for (String ancestorClass : ancestorClasses) {
201         if (InheritanceUtil.isInheritor(aClass, ancestorClass)) {
202           return true;
203         }
204       }
205       return false;
206     }
207   }
208 }