prototype for class based patterns as entry points (IDEA-34620)

This commit is contained in:
Anna.Kozlova
2016-07-14 12:30:32 +02:00
parent e91fc68156
commit 4cbe5e7873
7 changed files with 408 additions and 8 deletions
@@ -128,10 +128,13 @@ public class UnusedDeclarationInspection extends UnusedDeclarationInspectionBase
gc.gridy++;
add(myNonJavaCheckbox, gc);
JButton configureAnnotations = EntryPointsManagerImpl.createConfigureAnnotationsButton();
final JButton configureClassPatternsButton = EntryPointsManagerImpl.createConfigureClassPatternsButton();
gc.fill = GridBagConstraints.NONE;
gc.gridy++;
gc.insets.top = 10;
add(configureClassPatternsButton, gc);
JButton configureAnnotations = EntryPointsManagerImpl.createConfigureAnnotationsButton();
gc.gridy++;
gc.weighty = 1;
add(configureAnnotations, gc);
@@ -0,0 +1,172 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection.ex;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.*;
import com.intellij.ui.table.JBTable;
import com.intellij.util.ui.ItemRemovable;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
class ConfigureClassPatternsDialog extends DialogWrapper {
private final List<EntryPointsManagerBase.ClassPattern> myModifiedPatterns;
private final List<EntryPointsManagerBase.ClassPattern> myPatterns;
private final Project myProject;
public ConfigureClassPatternsDialog(List<EntryPointsManagerBase.ClassPattern> patterns, Project project) {
super(project);
myModifiedPatterns = new ArrayList<>(patterns);
myPatterns = patterns;
myProject = project;
init();
setTitle("Configure Class Patterns");
}
@Override
protected JComponent createCenterPanel() {
final JBTable table = createTableForPatterns();
final ToolbarDecorator toolbarDecorator = ToolbarDecorator.createDecorator(table)
.setAddAction(new AnActionButtonRunnable() {
@Override
public void run(AnActionButton button) {
myModifiedPatterns.add(new EntryPointsManagerBase.ClassPattern());
AbstractTableModel model = (AbstractTableModel)table.getModel();
final int row = myModifiedPatterns.size() - 1;
model.fireTableRowsInserted(row, row);
table.setRowSelectionInterval(row, row);
table.editCellAt(row, 1);
}
}).setRemoveAction(new AnActionButtonRunnable() {
@Override
public void run(AnActionButton button) {
TableUtil.removeSelectedItems(table);
table.repaint();
}
})
.setRemoveActionUpdater(new AnActionButtonUpdater() {
@Override
public boolean isEnabled(AnActionEvent e) {
return table.getSelectedRow() >= 0;
}
})
.setButtonComparator("Add", "Remove");
JPanel panel = new JPanel(new BorderLayout());
panel.add(SeparatorFactory.createSeparator("Mark class as reachable if name matches", null), BorderLayout.NORTH);
panel.add(toolbarDecorator.createPanel(), BorderLayout.CENTER);
return panel;
}
@Override
protected void doOKAction() {
myPatterns.clear();
myPatterns.addAll(myModifiedPatterns);
DaemonCodeAnalyzer.getInstance(myProject).restart();
super.doOKAction();
}
private JBTable createTableForPatterns() {
TableModel dataModel = new MyTableModel();
final JBTable result = new JBTable(dataModel);
result.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
TableCellEditor editor = result.getDefaultEditor(String.class);
if (editor instanceof DefaultCellEditor) {
((DefaultCellEditor)editor).setClickCountToStart(1);
}
final TableColumn column = result.getTableHeader().getColumnModel().getColumn(0);
column.setResizable(false);
final int width = 15 + result.getTableHeader().getFontMetrics(result.getTableHeader().getFont()).stringWidth(result.getColumnName(0));
column.setMaxWidth(width);
column.setMinWidth(width);
return result;
}
private class MyTableModel extends AbstractTableModel implements ItemRemovable {
private final String[] myNames;
public MyTableModel() {
myNames = new String[] {"With Subclasses", "Class"};
}
public int getColumnCount() {
return 2;
}
public int getRowCount() {
return myModifiedPatterns.size();
}
@Nullable
public Object getValueAt(int row, int col) {
if (row < 0) return null;
final EntryPointsManagerBase.ClassPattern classPattern = myModifiedPatterns.get(row);
if (classPattern == null) return null;
if (col == 0) {
return classPattern.hierarchically;
}
return classPattern.pattern;
}
public String getColumnName(int column) {
return myNames[column];
}
public Class getColumnClass(int col) {
if (col == 0) {
return Boolean.class;
}
if (col == 1) {
return String.class;
}
throw new IllegalArgumentException(String.valueOf(col));
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object aValue, int row, int col) {
EntryPointsManagerBase.ClassPattern classPattern = myModifiedPatterns.get(row);
if (classPattern == null) return;
if (col == 0) {
classPattern.hierarchically = (boolean)aValue;
}
else {
classPattern.pattern = (String)aValue;
}
fireTableRowsUpdated(row, row);
}
@Override
public void removeRow(int idx) {
myModifiedPatterns.remove(idx);
}
}
}
@@ -70,6 +70,11 @@ public class EntryPointsManagerImpl extends EntryPointsManagerBase implements Pe
}.show();
}
@Override
public void configureEntryClassPatterns() {
new ConfigureClassPatternsDialog(getPatterns(), myProject).show();
}
@Override
public JButton createConfigureAnnotationsBtn() {
return createConfigureAnnotationsButton();
@@ -85,4 +90,15 @@ public class EntryPointsManagerImpl extends EntryPointsManagerBase implements Pe
});
return configureAnnotations;
}
}
public static JButton createConfigureClassPatternsButton() {
final JButton configureAnnotations = new JButton("Configure class patterns...");
configureAnnotations.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getInstance(ProjectUtil.guessCurrentProject(configureAnnotations)).configureEntryClassPatterns();
}
});
return configureAnnotations;
}
}