// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package com.intellij.openapi.actionSystem.ex;

import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.*;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author max
 */
public abstract class CheckboxAction extends ToggleAction implements CustomComponentAction {

  protected CheckboxAction() {}

  protected CheckboxAction(final String text) {
    super(text);
  }

  protected CheckboxAction(final String text, final String description, final Icon icon) {
    super(text, description, icon);
  }

  @NotNull
  @Override
  public JComponent createCustomComponent(@NotNull Presentation presentation, @NotNull String place) {
    JCheckBox checkBox = new JCheckBox();
    updateCustomComponent(checkBox, presentation);
    return createCheckboxComponent(checkBox, this, place);
  }

  @Override
  public void update(@NotNull final AnActionEvent e) {
    super.update(e);
    Presentation presentation = e.getPresentation();
    JComponent property = presentation.getClientProperty(COMPONENT_KEY);
    if (property instanceof JCheckBox) {
      JCheckBox checkBox = (JCheckBox)property;

      updateCustomComponent(checkBox, presentation);
    }
  }

  protected void updateCustomComponent(JCheckBox checkBox, Presentation presentation) {
    updateCheckboxPresentation(checkBox, presentation);
    checkBox.setSelected(Boolean.TRUE.equals(presentation.getClientProperty(SELECTED_PROPERTY)));
  }

  static void updateCheckboxPresentation(JCheckBox checkBox, Presentation presentation) {
    checkBox.setText(presentation.getText());
    checkBox.setToolTipText(presentation.getDescription());
    checkBox.setMnemonic(presentation.getMnemonic());
    checkBox.setDisplayedMnemonicIndex(presentation.getDisplayedMnemonicIndex());
    checkBox.setEnabled(presentation.isEnabled());
    checkBox.setVisible(presentation.isVisible());
  }

  @NotNull
  static JComponent createCheckboxComponent(@NotNull JCheckBox checkBox, @NotNull AnAction action, @NotNull String place) {
    // this component cannot be stored right in AnAction because of action system architecture:
    // one action can be shown on multiple toolbars simultaneously
    checkBox.setOpaque(false);
    checkBox.setBorder(JBUI.Borders.emptyRight(9));

    checkBox.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        JCheckBox checkBox = (JCheckBox)e.getSource();
        ActionToolbar actionToolbar = UIUtil.getParentOfType(ActionToolbar.class, checkBox);
        DataContext dataContext =
          actionToolbar != null ? actionToolbar.getToolbarDataContext() : DataManager.getInstance().getDataContext(checkBox);
        action.actionPerformed(AnActionEvent.createFromAnAction(action, null, place, dataContext));
      }
    });

    return checkBox;
  }
}

@@ -259,24 +259,70 @@
 onSystem.*;%0A
+import com.intellij.ui.components.JBCheckBox;%0A
 import com.i
@@ -997,24 +997,25 @@
 ace) %7B%0A    J
+B
 CheckBox che
@@ -1027,16 +1027,17 @@
  = new J
+B
 CheckBox
@@ -1036,24 +1036,58 @@
 CheckBox();%0A
+    checkBox.setFocusable(false);%0A
     updateCu

make non-focusable for SE and all other uses