package jetbrains.mps.ide.devkit.actions;

/*Generated by MPS */

import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import jetbrains.mps.workbench.action.ActionAccess;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import jetbrains.mps.ide.devkit.generator.GenerationTracerViewTool;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.ide.actions.MPSCommonDataKeys;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.extapi.model.TransientSModel;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModelOperations;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;

public class ShowGenerationTraceback_Action extends BaseAction {
  private static final Icon ICON = null;

  public ShowGenerationTraceback_Action() {
    super("Show Generation Traceback", "", ICON);
    this.setIsAlwaysVisible(false);
    this.setActionAccess(ActionAccess.UNDO_PROJECT);
  }
  @Override
  public boolean isDumbAware() {
    return true;
  }
  @Override
  public void doUpdate(@NotNull AnActionEvent event, final Map<String, Object> _params) {
    GenerationTracerViewTool tool = event.getData(CommonDataKeys.PROJECT).getComponent(GenerationTracerViewTool.class);
    SNode n = event.getData(MPSCommonDataKeys.NODE);
    if ((n == null) || !(SNodeOperations.getModel(n) instanceof TransientSModel) || tool == null) {
      disable(event.getPresentation());
    } else {
      setEnabledState(event.getPresentation(), tool.hasTracebackData(SModelOperations.getPointer(SNodeOperations.getModel(n))));
    }
  }
  @Override
  protected boolean collectActionData(AnActionEvent event, final Map<String, Object> _params) {
    if (!(super.collectActionData(event, _params))) {
      return false;
    }
    {
      Project p = event.getData(CommonDataKeys.PROJECT);
      if (p == null) {
        return false;
      }
    }
    {
      SNode node = event.getData(MPSCommonDataKeys.NODE);
      if (node == null) {
        return false;
      }
    }
    return true;
  }
  @Override
  public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
    GenerationTracerViewTool tool = event.getData(CommonDataKeys.PROJECT).getComponent(GenerationTracerViewTool.class);
    if (!(tool.showTracebackData(event.getData(MPSCommonDataKeys.NODE)))) {
      JBPopup m = JBPopupFactory.getInstance().createMessage("No tracing data available");
      m.showCenteredInCurrentWindow(event.getData(CommonDataKeys.PROJECT));
    }
  }
}
