package jetbrains.mps.lang.messages.editor;

/*Generated by MPS */

import jetbrains.mps.editor.runtime.cells.KeyMapImpl;
import jetbrains.mps.openapi.editor.cells.KeyMapAction;
import jetbrains.mps.editor.runtime.cells.KeyMapActionImpl;
import jetbrains.mps.openapi.editor.EditorContext;
import jetbrains.mps.openapi.editor.cells.EditorCell;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import java.util.List;
import jetbrains.mps.openapi.editor.cells.EditorCell_Label;
import jetbrains.mps.editor.runtime.selection.SelectionUtil;
import jetbrains.mps.smodel.builder.SNodeBuilder;
import jetbrains.mps.openapi.editor.selection.SelectionManager;
import org.jetbrains.mps.openapi.language.SConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SProperty;

public class MacroMessageExpression_completeTextToLiteralAndPercentToMacro extends KeyMapImpl {
  public MacroMessageExpression_completeTextToLiteralAndPercentToMacro() {
    this.setApplicableToEveryModel(false);
    KeyMapAction action;
    action = new MacroMessageExpression_completeTextToLiteralAndPercentToMacro_Action0();
    this.putAction("any", "letter or digit", action);
    this.putAction("none", " ", action);
    this.putAction("none", "'", action);
    action = new MacroMessageExpression_completeTextToLiteralAndPercentToMacro_Action1();
    this.putAction("any", "%", action);
  }
  public static class MacroMessageExpression_completeTextToLiteralAndPercentToMacro_Action0 extends KeyMapActionImpl {
    public MacroMessageExpression_completeTextToLiteralAndPercentToMacro_Action0() {
      this.setShownInPopupMenu(false);
    }
    public boolean isMenuAlwaysShown() {
      return false;
    }
    public boolean canExecute(final EditorContext editorContext) {
      EditorCell contextCell = editorContext.getContextCell();
      if ((contextCell == null)) {
        return false;
      }
      SNode contextNode = contextCell.getSNode();
      if (contextNode == null) {
        return false;
      }
      if (!(SNodeOperations.isInstanceOf(contextNode, CONCEPTS.MacroMessageExpression$DF))) {
        return false;
      }
      return true;
    }
    public void execute(final EditorContext editorContext) {
      EditorCell contextCell = editorContext.getContextCell();
      this.execute_internal(editorContext, contextCell.getSNode(), this.getSelectedNodes(editorContext));
    }
    private void execute_internal(final EditorContext editorContext, final SNode node, final List<SNode> selectedNodes) {
      EditorCell_Label label = as_4ojwcw_a0a0a4b(editorContext.getSelectedCell(), EditorCell_Label.class);
      SNode newNode;
      if (label.getCaretPosition() == 0) {
        newNode = SNodeOperations.insertPrevSiblingChild(node, createLiteralMessageExpression_4ojwcw_a0a0a0c0a0());
      } else {
        newNode = SNodeOperations.insertNextSiblingChild(node, createLiteralMessageExpression_4ojwcw_a0a0a0a2a0a());
      }
      SelectionUtil.selectNode(editorContext, newNode);
    }
    public String getKeyStroke() {
      return " letter or digit";
    }
    private static SNode createLiteralMessageExpression_4ojwcw_a0a0a0c0a0() {
      SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.LiteralMessageExpression$$8);
      n0.setProperty(PROPS.message$umWY, "");
      return n0.getResult();
    }
    private static SNode createLiteralMessageExpression_4ojwcw_a0a0a0a2a0a() {
      SNodeBuilder n0 = new SNodeBuilder().init(CONCEPTS.LiteralMessageExpression$$8);
      n0.setProperty(PROPS.message$umWY, "");
      return n0.getResult();
    }
    private static <T> T as_4ojwcw_a0a0a4b(Object o, Class<T> type) {
      return (type.isInstance(o) ? (T) o : null);
    }
  }
  public static class MacroMessageExpression_completeTextToLiteralAndPercentToMacro_Action1 extends KeyMapActionImpl {
    public MacroMessageExpression_completeTextToLiteralAndPercentToMacro_Action1() {
      this.setShownInPopupMenu(false);
    }
    public boolean isMenuAlwaysShown() {
      return false;
    }
    public boolean canExecute(final EditorContext editorContext) {
      EditorCell contextCell = editorContext.getContextCell();
      if ((contextCell == null)) {
        return false;
      }
      SNode contextNode = contextCell.getSNode();
      if (contextNode == null) {
        return false;
      }
      if (!(SNodeOperations.isInstanceOf(contextNode, CONCEPTS.MacroMessageExpression$DF))) {
        return false;
      }
      return this.canExecute_internal(editorContext, contextNode, this.getSelectedNodes(editorContext));
    }
    public void execute(final EditorContext editorContext) {
      EditorCell contextCell = editorContext.getContextCell();
      this.execute_internal(editorContext, contextCell.getSNode(), this.getSelectedNodes(editorContext));
    }
    private boolean canExecute_internal(final EditorContext editorContext, final SNode node, final List<SNode> selectedNodes) {
      EditorCell_Label label = as_4ojwcw_a0a0a4c(editorContext.getSelectedCell(), EditorCell_Label.class);
      return label.getCaretPosition() == label.getText().length();
    }
    private void execute_internal(final EditorContext editorContext, final SNode node, final List<SNode> selectedNodes) {
      if ((SNodeOperations.getNextSibling(node) != null)) {
        SelectionUtil.selectLabelCellAnSetCaret(editorContext, SNodeOperations.getNextSibling(node), SelectionManager.FIRST_EDITABLE_CELL, 0);
      } else {
        SelectionUtil.selectLabelCellAnSetCaret(editorContext, node, SelectionManager.LAST_CELL, -1);
      }
    }
    public String getKeyStroke() {
      return "any %";
    }
    private static <T> T as_4ojwcw_a0a0a4c(Object o, Class<T> type) {
      return (type.isInstance(o) ? (T) o : null);
    }
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept MacroMessageExpression$DF = MetaAdapterFactory.getConcept(0xad93155d79b24759L, 0xb10c55123e763903L, 0x48f860fc0e41e4a2L, "jetbrains.mps.lang.messages.structure.MacroMessageExpression");
    /*package*/ static final SConcept LiteralMessageExpression$$8 = MetaAdapterFactory.getConcept(0xad93155d79b24759L, 0xb10c55123e763903L, 0x48f860fc0e362dc5L, "jetbrains.mps.lang.messages.structure.LiteralMessageExpression");
  }

  private static final class PROPS {
    /*package*/ static final SProperty message$umWY = MetaAdapterFactory.getProperty(0xad93155d79b24759L, 0xb10c55123e763903L, 0x48f860fc0e362dc5L, 0x48f860fc0e362dc6L, "message");
  }
}
