2 * Copyright 2010-2016 Bas Leijdekkers
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.siyeh.ig.ui;
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;
36 import javax.swing.table.TableCellEditor;
38 import java.util.Collection;
40 public class UiUtils {
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));
51 public static JPanel createAddRemovePanel(final ListTable table) {
52 final JPanel panel = ToolbarDecorator.createDecorator(table)
53 .setAddAction(new AnActionButtonRunnable() {
55 public void run(AnActionButton button) {
56 final ListWrappingTableModel tableModel = table.getModel();
58 EventQueue.invokeLater(() -> {
59 final int lastRowIndex = tableModel.getRowCount() - 1;
60 editTableCell(table, lastRowIndex, 0);
64 .setRemoveAction(button -> TableUtil.removeSelectedItems(table))
65 .disableUpDownActions().createPanel();
66 panel.setPreferredSize(JBUI.size(150, 100));
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;
77 filter = new SubclassFilter(ancestorClasses);
79 final JPanel panel = ToolbarDecorator.createDecorator(table)
80 .setAddAction(new AnActionButtonRunnable() {
82 public void run(AnActionButton button) {
83 final DataContext dataContext = DataManager.getInstance().getDataContext(table);
84 final Project project = CommonDataKeys.PROJECT.getData(dataContext);
86 final ListWrappingTableModel tableModel = table.getModel();
87 if (project == null) {
89 rowIndex = tableModel.getRowCount() - 1;
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) {
100 final String qualifiedName = selectedClass.getQualifiedName();
101 final int index = tableModel.indexOf(qualifiedName, 0);
103 tableModel.addRow(qualifiedName);
104 rowIndex = tableModel.getRowCount() - 1;
110 editTableCell(table, rowIndex, table.getColumnCount() > 1 && project != null ? 1 : 0);
112 }).setRemoveAction(button -> TableUtil.removeSelectedItems(table))
113 .disableUpDownActions().createPanel();
114 panel.setPreferredSize(JBUI.size(150, 100));
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();
133 public static JPanel createTreeClassChooserList(final Collection<String> collection,
135 final String chooserTitle,
136 String... ancestorClasses) {
137 final ClassFilter filter;
138 if (ancestorClasses.length == 0) {
139 filter = ClassFilter.ALL;
142 filter = new SubclassFilter(ancestorClasses);
144 final JPanel optionsPanel = new JPanel(new BorderLayout());
145 final JBList list = new JBList(collection);
147 final JPanel panel = ToolbarDecorator.createDecorator(list)
148 .disableUpDownActions()
149 .setAddAction(new AnActionButtonRunnable() {
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) {
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) {
164 final String qualifiedName = selected.getQualifiedName();
165 final DefaultListModel model = (DefaultListModel)list.getModel();
166 final int index = model.indexOf(qualifiedName);
168 model.addElement(qualifiedName);
169 collection.add(qualifiedName);
172 list.setSelectedIndex(index);
176 .setRemoveAction(new AnActionButtonRunnable() {
178 public void run(AnActionButton anActionButton) {
179 collection.remove(list.getSelectedValue());
180 ListUtil.removeSelectedItems(list);
183 panel.setPreferredSize(JBUI.size(150, 100));
184 optionsPanel.setBorder(IdeBorderFactory.createTitledBorder(borderTitle,
185 false, JBUI.insetsTop(10)));
186 optionsPanel.add(panel);
190 private static class SubclassFilter implements ClassFilter {
192 private final String[] ancestorClasses;
194 private SubclassFilter(String[] ancestorClasses) {
195 this.ancestorClasses = ancestorClasses;
199 public boolean isAccepted(PsiClass aClass) {
200 for (String ancestorClass : ancestorClasses) {
201 if (InheritanceUtil.isInheritor(aClass, ancestorClass)) {