package jetbrains.mps.ide.actions;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import jetbrains.mps.icons.MPSIcons;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.ide.ui.tree.smodel.SModelTreeNode;
import jetbrains.mps.project.AbstractModule;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.smodel.SModelStereotype;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.project.MPSProject;
import javax.swing.tree.TreeNode;

@GeneratedClass(node = "r:00000000-0000-4000-0000-011c895904a4(jetbrains.mps.ide.actions)/1420252515663895912", model = "r:00000000-0000-4000-0000-011c895904a4(jetbrains.mps.ide.actions)")
public class NewSubModel_Action extends BaseAction {
  private static final Icon ICON = MPSIcons.Nodes.Model;

  public NewSubModel_Action() {
    super("Model", "", ICON);
    this.setIsAlwaysVisible(false);
    this.setExecuteOutsideCommand(true);
  }
  @Override
  public boolean isDumbAware() {
    return true;
  }
  @Override
  public boolean isApplicable(AnActionEvent event, final Map<String, Object> _params) {
    final SModel model = ((SModelTreeNode) event.getData(MPSCommonDataKeys.TREE_NODE)).getModel();
    if (!(model.getModule() instanceof AbstractModule)) {
      return false;
    }

    return Sequence.fromIterable(Sequence.fromArray(SModelStereotype.values)).contains(model.getName().getStereotype());
  }
  @Override
  public void doUpdate(@NotNull AnActionEvent event, final Map<String, Object> _params) {
    this.setEnabledState(event.getPresentation(), this.isApplicable(event, _params));
  }
  @Override
  protected boolean collectActionData(AnActionEvent event, final Map<String, Object> _params) {
    if (!(super.collectActionData(event, _params))) {
      return false;
    }
    {
      MPSProject p = event.getData(MPSCommonDataKeys.MPS_PROJECT);
      if (p == null) {
        return false;
      }
    }
    {
      TreeNode p = event.getData(MPSCommonDataKeys.TREE_NODE);
      if (p == null) {
        return false;
      }
      if (p != null && !(p instanceof SModelTreeNode)) {
        return false;
      }
    }
    return true;
  }
  @Override
  public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
    NewSubModel_Action.this.getExecutor(event).execute();
  }
  protected NewModelActionExecutor getExecutor(final AnActionEvent event) {
    // XXX I don't feel this protected method truly helps with what 6d50aeda claims it does - 
    //  e.g. not clear how does it access event parameters
    final SModel model = ((SModelTreeNode) event.getData(MPSCommonDataKeys.TREE_NODE)).getModel();
    return new NewModelActionExecutor(event.getData(MPSCommonDataKeys.MPS_PROJECT), model.getModule(), model);
  }
}
