private void removeSelected() {
   CheckedTreeNode node = getSelectedToolNode();
   if (node != null) {
     int result =
         Messages.showYesNoDialog(
             this,
             ToolsBundle.message("tools.delete.confirmation"),
             CommonBundle.getWarningTitle(),
             Messages.getWarningIcon());
     if (result != 0) {
       return;
     }
     myIsModified = true;
     if (node.getUserObject() instanceof Tool) {
       Tool tool = (Tool) node.getUserObject();
       CheckedTreeNode parentNode = (CheckedTreeNode) node.getParent();
       ((ToolsGroup) parentNode.getUserObject()).removeElement(tool);
       removeNodeFromParent(node);
       if (parentNode.getChildCount() == 0) {
         removeNodeFromParent(parentNode);
       }
     } else if (node.getUserObject() instanceof ToolsGroup) {
       removeNodeFromParent(node);
     }
     update();
     myTree.requestFocus();
   }
 }
  private void editSelected() {
    CheckedTreeNode node = getSelectedToolNode();
    if (node != null && node.getUserObject() instanceof Tool) {
      Tool selected = (Tool) node.getUserObject();
      if (selected != null) {
        String oldGroupName = selected.getGroup();
        ToolEditorDialog dlg = createToolEditorDialog(ToolsBundle.message("tools.edit.title"));
        dlg.setData(selected, getGroups());
        dlg.show();
        if (dlg.isOK()) {
          selected.copyFrom(dlg.getData());
          String newGroupName = selected.getGroup();
          if (!Comparing.equal(oldGroupName, newGroupName)) {
            CheckedTreeNode oldGroupNode = (CheckedTreeNode) node.getParent();
            removeNodeFromParent(node);
            ((ToolsGroup) oldGroupNode.getUserObject()).removeElement(selected);
            if (oldGroupNode.getChildCount() == 0) {
              removeNodeFromParent(oldGroupNode);
            }

            insertNewTool(selected, true);
          } else {
            (getModel()).nodeChanged(node);
          }
          myIsModified = true;
          update();
        }
      }
    }
  }
  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();
              }
            });
  }