7ea93bb922c240338e556cf51ec21551a6cb9dc2
[idea/community.git] / platform / lang-api / src / com / intellij / codeInspection / ui / ListEditForm.java
1 /*
2  * Copyright 2000-2010 JetBrains s.r.o.
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.intellij.codeInspection.ui;
17
18 import com.intellij.ui.AnActionButton;
19 import com.intellij.ui.AnActionButtonRunnable;
20 import com.intellij.ui.ToolbarDecorator;
21
22 import javax.swing.*;
23 import javax.swing.table.TableCellEditor;
24 import java.awt.*;
25 import java.util.List;
26
27 public class ListEditForm {
28   JPanel contentPanel;
29   ListTable table;
30
31   public ListEditForm(String title, List<String> stringList) {
32     table = new ListTable(new ListWrappingTableModel(stringList, title));
33
34     contentPanel = ToolbarDecorator.createDecorator(table)
35       .setAddAction(new AnActionButtonRunnable() {
36         @Override
37         public void run(AnActionButton button) {
38           final ListWrappingTableModel tableModel = table.getModel();
39           tableModel.addRow();
40           EventQueue.invokeLater(() -> {
41             final int lastRowIndex = tableModel.getRowCount() - 1;
42             final Rectangle rectangle =
43               table.getCellRect(lastRowIndex, 0, true);
44             table.scrollRectToVisible(rectangle);
45             table.editCellAt(lastRowIndex, 0);
46             final ListSelectionModel selectionModel =
47               table.getSelectionModel();
48             selectionModel.setSelectionInterval(lastRowIndex, lastRowIndex);
49             final TableCellEditor editor = table.getCellEditor();
50             final Component component =
51               editor.getTableCellEditorComponent(table,
52                                                  null, true, lastRowIndex, 0);
53             component.requestFocus();
54           });
55         }
56       }).setRemoveAction(new AnActionButtonRunnable() {
57         @Override
58         public void run(AnActionButton button) {
59           final ListSelectionModel selectionModel = table.getSelectionModel();
60           final int minIndex = selectionModel.getMinSelectionIndex();
61           final int maxIndex = selectionModel.getMaxSelectionIndex();
62           if (minIndex == -1 || maxIndex == -1) {
63             return;
64           }
65           final ListWrappingTableModel tableModel = table.getModel();
66           for (int i = minIndex; i <= maxIndex; i++) {
67             if (selectionModel.isSelectedIndex(i)) {
68               tableModel.removeRow(i);
69             }
70           }
71           final int count = tableModel.getRowCount();
72           if (count <= minIndex) {
73             selectionModel.setSelectionInterval(count - 1,
74                                                 count - 1);
75           }
76           else if (minIndex <= 0) {
77             if (count > 0) {
78               selectionModel.setSelectionInterval(0, 0);
79             }
80           }
81           else {
82             selectionModel.setSelectionInterval(minIndex - 1,
83                                                 minIndex - 1);
84           }
85         }
86       }).disableUpDownActions().createPanel();
87   }
88
89   public JComponent getContentPanel() {
90     return contentPanel;
91   }
92 }