/*
 * Copyright 2000-2017 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.usages.impl;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.ToggleAction;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.usages.UsageView;
import com.intellij.usages.rules.UsageFilteringRuleProvider;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.function.Supplier;

/**
 * @author Eugene Zhuravlev
 */
abstract class RuleAction extends ToggleAction implements DumbAware {
  protected final UsageViewImpl myView;
  private boolean myState;

  RuleAction(@NotNull UsageView view, @NotNull String text, @NotNull Icon icon) {
    this(view, () -> text, icon);
  }

  RuleAction(@NotNull UsageView view, Supplier<String> text, @NotNull Icon icon) {
    super(text, icon);
    myView = (UsageViewImpl)view;
    myState = getOptionValue();
  }

  protected abstract boolean getOptionValue();

  protected abstract void setOptionValue(boolean value);

  @Override
  public boolean isSelected(@NotNull AnActionEvent e) {
    return myState;
  }

  @Override
  public void setSelected(@NotNull AnActionEvent e, boolean state) {
    setOptionValue(state);
    myState = state;

    Project project = e.getProject();
    if (project != null) {
      project.getMessageBus().syncPublisher(UsageFilteringRuleProvider.RULES_CHANGED).run();
    }

    myView.select();
  }
}

@@ -730,36 +730,8 @@
 ew;%0A
-  private boolean myState;%0A%0A
   Ru
@@ -804,24 +804,24 @@
 con icon) %7B%0A
+
     this(vie
@@ -991,40 +991,8 @@
 ew;%0A
-    myState = getOptionValue();%0A
   %7D%0A
@@ -1177,23 +1177,32 @@
  return 
-myState
+getOptionValue()
 ;%0A  %7D%0A%0A 
@@ -1277,24 +1277,24 @@
 an state) %7B%0A
+
     setOptio
@@ -1311,29 +1311,8 @@
 te);
-%0A    myState = state;
 %0A%0A  

fix drawing incorrect checkmark