TODO: reimplement filter dialog

- use validation from dialog wrapper
- use checkboxList instead of custom table with checkboxes
This commit is contained in:
Alexander Zolotov
2016-06-18 18:51:23 +03:00
parent ef75dc083b
commit 9d1898eef2
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 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.
@@ -16,36 +16,31 @@
package com.intellij.ide.todo.configurable;
import com.intellij.CommonBundle;
import com.intellij.ide.IdeBundle;
import com.intellij.ide.todo.TodoFilter;
import com.intellij.openapi.help.HelpManager;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.psi.search.TodoPattern;
import com.intellij.ui.CheckBoxList;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.TableUtil;
import com.intellij.util.ui.Table;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.FormBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.util.List;
/**
* @author Vladimir Kondratyev
*/
class FilterDialog extends DialogWrapper {
private final TodoFilter myFilter;
private final int myFilterIndex;
private final List<TodoPattern> myPatterns;
private final List<TodoFilter> myFilters;
private final JTextField myNameField;
private final Table myTable;
private final JScrollPane myPatternsScrollPane;
/**
* @param parent parent component.
@@ -55,56 +50,60 @@ class FilterDialog extends DialogWrapper {
* @param filters all already configured filters. This parameter is used to
* @param patterns all patterns available in this filter.
*/
public FilterDialog(Component parent,
TodoFilter filter,
int filterIndex,
List<TodoFilter> filters,
List<TodoPattern> patterns) {
public FilterDialog(Component parent, TodoFilter filter, int filterIndex, List<TodoFilter> filters, List<TodoPattern> patterns) {
super(parent, true);
myFilter = filter;
myFilterIndex = filterIndex;
myPatterns = patterns;
myFilters = filters;
myNameField = new JTextField(filter.getName());
MyModel model = new MyModel();
myTable = new Table(model);
myNameField = new JBTextField(filter.getName());
CheckBoxList<TodoPattern> patternsList = new CheckBoxList<TodoPattern>();
patternsList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
patternsList.setCheckBoxListListener((int index, boolean value) -> {
if (value) {
myFilter.addTodoPattern(patternsList.getItemAt(index));
}
else {
myFilter.removeTodoPattern(patternsList.getItemAt(index));
}
});
for (TodoPattern pattern : patterns) {
patternsList.addItem(pattern, pattern.getPatternString(), myFilter.contains(pattern));
}
if (patternsList.getItemsCount() > 0) {
patternsList.setSelectedIndex(0);
}
myPatternsScrollPane = ScrollPaneFactory.createScrollPane(patternsList);
myPatternsScrollPane.setMinimumSize(new Dimension(300, -1));
init();
}
@Nullable
@Override
protected ValidationInfo doValidate() {
String filterName = getNewFilterName();
if (filterName.isEmpty()) {
return new ValidationInfo(IdeBundle.message("error.filter.name.should.be.specified"), myNameField);
}
for (int i = 0; i < myFilters.size(); i++) {
TodoFilter filter = myFilters.get(i);
if (myFilterIndex != i && filterName.equals(filter.getName())) {
return new ValidationInfo(IdeBundle.message("error.filter.with.the.same.name.already.exists"), myNameField);
}
}
if (myFilter.isEmpty()) {
return new ValidationInfo(IdeBundle.message("error.filter.should.contain.at.least.one.pattern"), myPatternsScrollPane);
}
return super.doValidate();
}
@NotNull
private String getNewFilterName() {
return myNameField.getText().trim();
}
@Override
protected void doOKAction() {
// Validate filter name
myFilter.setName(myNameField.getText().trim());
if (myFilter.getName().length() == 0) {
Messages.showMessageDialog(myTable,
IdeBundle.message("error.filter.name.should.be.specified"),
CommonBundle.getErrorTitle(),
Messages.getErrorIcon());
return;
}
for (int i = 0; i < myFilters.size(); i++) {
TodoFilter filter = myFilters.get(i);
if (myFilterIndex != i && myFilter.getName().equals(filter.getName())) {
Messages.showMessageDialog(myTable,
IdeBundle.message("error.filter.with.the.same.name.already.exists"),
CommonBundle.getErrorTitle(),
Messages.getErrorIcon());
return;
}
}
// Validate that at least one pettern is selected
if (myFilter.isEmpty()) {
Messages.showMessageDialog(myTable,
IdeBundle.message("error.filter.should.contain.at.least.one.pattern"),
CommonBundle.getErrorTitle(),
Messages.getErrorIcon());
return;
}
myFilter.setName(getNewFilterName());
super.doOKAction();
}
@@ -126,93 +125,11 @@ class FilterDialog extends DialogWrapper {
@Override
protected JComponent createCenterPanel() {
JPanel panel = new JPanel(new GridBagLayout());
JLabel nameLabel = new JLabel(IdeBundle.message("label.todo.filter.name"));
panel.add(nameLabel,
new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 10), 0, 0));
panel.add(myNameField,
new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
JPanel patternsPanel = new JPanel(new GridBagLayout());
Border border = IdeBorderFactory.createTitledBorder(IdeBundle.message("group.todo.filter.patterns"), false);
patternsPanel.setBorder(border);
myTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myTable);
scrollPane.setPreferredSize(new Dimension(550, myTable.getRowHeight() * 10));
patternsPanel.add(scrollPane,
new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
// Column "Available"
TableUtil.setupCheckboxColumn(myTable, 0);
//
panel.add(patternsPanel,
new GridBagConstraints(0, 1, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
return panel;
}
private final class MyModel extends AbstractTableModel {
private final String[] ourColumnNames = new String[]{" ", IdeBundle.message("column.todo.filter.pattern"), };
private final Class[] ourColumnClasses = new Class[]{Boolean.class, String.class};
@Override
public String getColumnName(int column) {
return ourColumnNames[column];
}
@Override
public Class getColumnClass(int column) {
return ourColumnClasses[column];
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public int getRowCount() {
return myPatterns.size();
}
@Override
public Object getValueAt(int row, int column) {
TodoPattern pattern = myPatterns.get(row);
switch (column) {
case 0:
// "Available" column
return myFilter.contains(pattern);
case 1:
// "Pattern" column
return pattern.getPatternString();
default:
throw new IllegalArgumentException();
}
}
@Override
public void setValueAt(Object value, int row, int column) {
switch (column) {
case 0:
TodoPattern pattern = myPatterns.get(row);
if (((Boolean)value).booleanValue()) {
if (!myFilter.contains(pattern)) {
myFilter.addTodoPattern(pattern);
}
}
else {
myFilter.removeTodoPattern(pattern);
}
break;
default:
throw new IllegalArgumentException();
}
}
@Override
public boolean isCellEditable(int row, int column) {
return column == 0;
}
JPanel patternListPanel = new JPanel(new BorderLayout());
patternListPanel.setBorder(IdeBorderFactory.createTitledBorder(IdeBundle.message("group.todo.filter.patterns")));
patternListPanel.add(myPatternsScrollPane, BorderLayout.CENTER);
return FormBuilder.createFormBuilder()
.addLabeledComponent(IdeBundle.message("label.todo.filter.name"), myNameField)
.addComponentFillVertically(patternListPanel, 0).getPanel();
}
}