ToolsPanel() {

    myTree =
        new CheckboxTree(
            new CheckboxTree.CheckboxTreeCellRenderer() {
              public void customizeRenderer(
                  final JTree tree,
                  final Object value,
                  final boolean selected,
                  final boolean expanded,
                  final boolean leaf,
                  final int row,
                  final boolean hasFocus) {
                if (!(value instanceof CheckedTreeNode)) return;
                Object object = ((CheckedTreeNode) value).getUserObject();

                if (object instanceof ToolsGroup) {
                  final String groupName = ((ToolsGroup) object).getName();
                  if (groupName != null) {
                    getTextRenderer()
                        .append(groupName, SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
                  } else {
                    getTextRenderer()
                        .append("[unnamed group]", SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
                  }
                } else if (object instanceof Tool) {
                  getTextRenderer()
                      .append(
                          ((Tool) object).getName(), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
                }
              }
            },
            new CheckedTreeNode(null)) {
          @Override
          protected void onDoubleClick(final CheckedTreeNode node) {
            editSelected();
          }

          @Override
          protected void onNodeStateChanged(final CheckedTreeNode node) {
            myIsModified = true;
          }
        };

    myTree.setRootVisible(false);
    myTree.getEmptyText().setText(ToolsBundle.message("tools.not.configured"));
    myTree.setSelectionModel(new DefaultTreeSelectionModel());
    myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

    setLayout(new BorderLayout());
    add(
        ToolbarDecorator.createDecorator(myTree)
            .setAddAction(
                new AnActionButtonRunnable() {
                  @Override
                  public void run(AnActionButton button) {
                    ToolEditorDialog dlg = new ToolEditorDialog(ToolsPanel.this);
                    Tool tool = new Tool();
                    tool.setUseConsole(true);
                    tool.setFilesSynchronizedAfterRun(true);
                    tool.setShownInMainMenu(true);
                    tool.setShownInEditor(true);
                    tool.setShownInProjectViews(true);
                    tool.setShownInSearchResultsPopup(true);
                    tool.setEnabled(true);
                    dlg.setData(tool, getGroups());
                    dlg.show();
                    if (dlg.isOK()) {
                      insertNewTool(dlg.getData(), true);
                    }
                    myTree.requestFocus();
                  }
                })
            .setRemoveAction(
                new AnActionButtonRunnable() {
                  @Override
                  public void run(AnActionButton button) {
                    removeSelected();
                  }
                })
            .setEditAction(
                new AnActionButtonRunnable() {
                  @Override
                  public void run(AnActionButton button) {
                    editSelected();
                    myTree.requestFocus();
                  }
                })
            .setMoveUpAction(
                new AnActionButtonRunnable() {
                  @Override
                  public void run(AnActionButton button) {
                    moveNode(Direction.UP);
                    myIsModified = true;
                  }
                })
            .setMoveDownAction(
                new AnActionButtonRunnable() {
                  @Override
                  public void run(AnActionButton button) {
                    moveNode(Direction.DOWN);
                    myIsModified = true;
                  }
                })
            .addExtraAction(
                myCopyButton =
                    new AnActionButton(
                        ToolsBundle.message("tools.copy.button"), PlatformIcons.COPY_ICON) {
                      @Override
                      public void actionPerformed(AnActionEvent e) {
                        Tool originalTool = getSelectedTool();

                        if (originalTool != null) {
                          ToolEditorDialog dlg = new ToolEditorDialog(ToolsPanel.this);
                          Tool toolCopy = new Tool();
                          toolCopy.copyFrom(originalTool);
                          dlg.setData(toolCopy, getGroups());
                          dlg.show();
                          if (dlg.isOK()) {
                            insertNewTool(dlg.getData(), true);
                          }
                          myTree.requestFocus();
                        }
                      }
                    })
            .setButtonComparator("Add", "Copy", "Edit", "Remove", "Up", "Down")
            .createPanel(),
        BorderLayout.CENTER);

    myAddButton = ToolbarDecorator.findAddButton(this);
    myEditButton = ToolbarDecorator.findEditButton(this);
    myRemoveButton = ToolbarDecorator.findRemoveButton(this);
    myMoveUpButton = ToolbarDecorator.findUpButton(this);
    myMoveDownButton = ToolbarDecorator.findDownButton(this);

    // TODO check edit and delete

    myTree
        .getSelectionModel()
        .addTreeSelectionListener(
            new TreeSelectionListener() {
              public void valueChanged(TreeSelectionEvent e) {
                update();
              }
            });
  }
  public SeverityEditorDialog(
      final JComponent parent,
      final HighlightSeverity severity,
      final SeverityRegistrar severityRegistrar) {
    super(parent, true);
    mySeverityRegistrar = severityRegistrar;
    myOptionsList.setCellRenderer(
        new DefaultListCellRenderer() {
          @Override
          public Component getListCellRendererComponent(
              JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            final Component rendererComponent =
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof SeverityBasedTextAttributes) {
              setText(((SeverityBasedTextAttributes) value).getSeverity().toString());
            }
            return rendererComponent;
          }
        });
    myOptionsList.addListSelectionListener(
        new ListSelectionListener() {
          @Override
          public void valueChanged(ListSelectionEvent e) {
            if (myCurrentSelection != null) {
              apply(myCurrentSelection);
            }
            myCurrentSelection = (SeverityBasedTextAttributes) myOptionsList.getSelectedValue();
            if (myCurrentSelection != null) {
              reset(myCurrentSelection);
              myCard.show(
                  myRightPanel,
                  mySeverityRegistrar.isDefaultSeverity(myCurrentSelection.getSeverity())
                      ? DEFAULT
                      : EDITABLE);
            }
          }
        });
    myOptionsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    JPanel leftPanel =
        ToolbarDecorator.createDecorator(myOptionsList)
            .setAddAction(
                new AnActionButtonRunnable() {
                  @Override
                  public void run(AnActionButton button) {
                    final String name =
                        Messages.showInputDialog(
                            myPanel,
                            InspectionsBundle.message(
                                "highlight.severity.create.dialog.name.label"),
                            InspectionsBundle.message("highlight.severity.create.dialog.title"),
                            Messages.getQuestionIcon(),
                            "",
                            new InputValidator() {
                              @Override
                              public boolean checkInput(final String inputString) {
                                final ListModel listModel = myOptionsList.getModel();
                                for (int i = 0; i < listModel.getSize(); i++) {
                                  final String severityName =
                                      ((SeverityBasedTextAttributes) listModel.getElementAt(i))
                                          .getSeverity()
                                          .myName;
                                  if (Comparing.strEqual(severityName, inputString)) return false;
                                }
                                return true;
                              }

                              @Override
                              public boolean canClose(final String inputString) {
                                return checkInput(inputString);
                              }
                            });
                    if (name == null) return;
                    final TextAttributes textAttributes =
                        CodeInsightColors.WARNINGS_ATTRIBUTES.getDefaultAttributes();
                    HighlightInfoType.HighlightInfoTypeImpl info =
                        new HighlightInfoType.HighlightInfoTypeImpl(
                            new HighlightSeverity(name, 50),
                            TextAttributesKey.createTextAttributesKey(name));

                    SeverityBasedTextAttributes newSeverityBasedTextAttributes =
                        new SeverityBasedTextAttributes(textAttributes.clone(), info);
                    ((DefaultListModel) myOptionsList.getModel())
                        .addElement(newSeverityBasedTextAttributes);

                    myOptionsList.clearSelection();
                    ListScrollingUtil.selectItem(myOptionsList, newSeverityBasedTextAttributes);
                  }
                })
            .setMoveUpAction(
                new AnActionButtonRunnable() {
                  @Override
                  public void run(AnActionButton button) {
                    apply(myCurrentSelection);
                    ListUtil.moveSelectedItemsUp(myOptionsList);
                  }
                })
            .setMoveDownAction(
                new AnActionButtonRunnable() {
                  @Override
                  public void run(AnActionButton button) {
                    apply(myCurrentSelection);
                    ListUtil.moveSelectedItemsDown(myOptionsList);
                  }
                })
            .createPanel();
    ToolbarDecorator.findRemoveButton(leftPanel)
        .addCustomUpdater(
            new AnActionButtonUpdater() {
              @Override
              public boolean isEnabled(AnActionEvent e) {
                return !mySeverityRegistrar.isDefaultSeverity(
                    ((SeverityBasedTextAttributes) myOptionsList.getSelectedValue()).getSeverity());
              }
            });
    ToolbarDecorator.findUpButton(leftPanel)
        .addCustomUpdater(
            new AnActionButtonUpdater() {
              @Override
              public boolean isEnabled(AnActionEvent e) {
                boolean canMove = ListUtil.canMoveSelectedItemsUp(myOptionsList);
                if (canMove) {
                  SeverityBasedTextAttributes pair =
                      (SeverityBasedTextAttributes) myOptionsList.getSelectedValue();
                  if (pair != null && mySeverityRegistrar.isDefaultSeverity(pair.getSeverity())) {
                    final int newPosition = myOptionsList.getSelectedIndex() - 1;
                    pair =
                        (SeverityBasedTextAttributes)
                            myOptionsList.getModel().getElementAt(newPosition);
                    if (mySeverityRegistrar.isDefaultSeverity(pair.getSeverity())) {
                      canMove = false;
                    }
                  }
                }

                return canMove;
              }
            });
    ToolbarDecorator.findDownButton(leftPanel)
        .addCustomUpdater(
            new AnActionButtonUpdater() {
              @Override
              public boolean isEnabled(AnActionEvent e) {
                boolean canMove = ListUtil.canMoveSelectedItemsDown(myOptionsList);
                if (canMove) {
                  SeverityBasedTextAttributes pair =
                      (SeverityBasedTextAttributes) myOptionsList.getSelectedValue();
                  if (pair != null && mySeverityRegistrar.isDefaultSeverity(pair.getSeverity())) {
                    final int newPosition = myOptionsList.getSelectedIndex() + 1;
                    pair =
                        (SeverityBasedTextAttributes)
                            myOptionsList.getModel().getElementAt(newPosition);
                    if (mySeverityRegistrar.isDefaultSeverity(pair.getSeverity())) {
                      canMove = false;
                    }
                  }
                }

                return canMove;
              }
            });

    myPanel = new JPanel(new BorderLayout());
    myPanel.add(leftPanel, BorderLayout.CENTER);
    myCard = new CardLayout();
    myRightPanel = new JPanel(myCard);
    final JPanel disabled = new JPanel(new GridBagLayout());
    final JButton button =
        new JButton(InspectionsBundle.message("severities.default.settings.message"));
    button.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            editColorsAndFonts();
          }
        });
    disabled.add(
        button,
        new GridBagConstraints(
            0,
            0,
            1,
            1,
            0,
            0,
            GridBagConstraints.CENTER,
            GridBagConstraints.NONE,
            new Insets(0, 0, 0, 0),
            0,
            0));
    myRightPanel.add(DEFAULT, disabled);
    myRightPanel.add(EDITABLE, myOptionsPanel);
    myCard.show(myRightPanel, EDITABLE);
    myPanel.add(myRightPanel, BorderLayout.EAST);
    fillList(severity);
    init();
    setTitle(InspectionsBundle.message("severities.editor.dialog.title"));
    reset((SeverityBasedTextAttributes) myOptionsList.getSelectedValue());
  }