package jetbrains.mps.core.xml.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.nodeEditor.cells.EditorCell_Label;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModelOperations;
import org.jetbrains.mps.openapi.language.SConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SProperty;
import org.jetbrains.mps.openapi.language.SContainmentLink;

public class XmlTextValue_text extends KeyMapImpl {
  public XmlTextValue_text() {
    this.setApplicableToEveryModel(false);
    KeyMapAction action;
    action = new XmlTextValue_text_Action0();
    this.putAction("any", "\"", action);
    action = new XmlTextValue_text_Action1();
    this.putAction("any", "&", action);
  }
  public static class XmlTextValue_text_Action0 extends KeyMapActionImpl {
    public XmlTextValue_text_Action0() {
      super.setCaretPolicy(KeyMapAction.CARET_AT_INTERMEDIATE_POSITION);
      this.setShownInPopupMenu(false);
    }
    public String getDescriptionText() {
      return "insert &quot;";
    }
    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.XmlTextValue$29))) {
        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) {
      if (!(editorContext.getSelectedCell() instanceof EditorCell_Label)) {
        return false;
      }
      if (!(SNodeOperations.isInstanceOf(node, CONCEPTS.XmlValuePart$G)) || !(SNodeOperations.isInstanceOf(SNodeOperations.getParent(node), CONCEPTS.XmlAttribute$ll))) {
        return false;
      }
      return isNotEmptyString(SPropertyOperations.getString(node, PROPS.text$_LaO));
    }
    private void execute_internal(final EditorContext editorContext, final SNode node, final List<SNode> selectedNodes) {
      int index = ((EditorCell_Label) editorContext.getSelectedCell()).getCaretPosition();
      SNode attr = SNodeOperations.cast(SNodeOperations.getParent(node), CONCEPTS.XmlAttribute$ll);
      int currIndex = ListSequence.fromList(SLinkOperations.getChildren(attr, LINKS.value$1h4D)).indexOf(node);

      String currText = SPropertyOperations.getString(node, PROPS.text$_LaO);
      if (index < currText.length()) {
        SNode newText = SModelOperations.createNewNode(SNodeOperations.getModel(node), null, CONCEPTS.XmlTextValue$29);
        SPropertyOperations.assign(newText, PROPS.text$_LaO, currText.substring(index));
        SPropertyOperations.assign(node, PROPS.text$_LaO, currText.substring(0, index));
        ListSequence.fromList(SLinkOperations.getChildren(attr, LINKS.value$1h4D)).insertElement(currIndex + 1, newText);
      }
      SNode newRef = SModelOperations.createNewNode(SNodeOperations.getModel(node), null, CONCEPTS.XmlEntityRefValue$di);
      SPropertyOperations.assign(newRef, PROPS.entityName$BTnn, "quot");
      ListSequence.fromList(SLinkOperations.getChildren(attr, LINKS.value$1h4D)).insertElement(currIndex + 1, newRef);
      editorContext.selectWRTFocusPolicy(newRef);
    }
    public String getKeyStroke() {
      return " \"";
    }
    private static boolean isNotEmptyString(String str) {
      return str != null && str.length() > 0;
    }
  }
  public static class XmlTextValue_text_Action1 extends KeyMapActionImpl {
    public XmlTextValue_text_Action1() {
      super.setCaretPolicy(KeyMapAction.CARET_AT_INTERMEDIATE_POSITION);
      this.setShownInPopupMenu(false);
    }
    public String getDescriptionText() {
      return "insert entity reference";
    }
    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.XmlTextValue$29))) {
        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) {
      if (!(editorContext.getSelectedCell() instanceof EditorCell_Label)) {
        return false;
      }
      if (!(SNodeOperations.isInstanceOf(node, CONCEPTS.XmlValuePart$G)) || !(SNodeOperations.isInstanceOf(SNodeOperations.getParent(node), CONCEPTS.XmlAttribute$ll))) {
        return false;
      }
      return isNotEmptyString(SPropertyOperations.getString(node, PROPS.text$_LaO));
    }
    private void execute_internal(final EditorContext editorContext, final SNode node, final List<SNode> selectedNodes) {
      int index = ((EditorCell_Label) editorContext.getSelectedCell()).getCaretPosition();
      SNode attr = SNodeOperations.cast(SNodeOperations.getParent(node), CONCEPTS.XmlAttribute$ll);
      int currIndex = ListSequence.fromList(SLinkOperations.getChildren(attr, LINKS.value$1h4D)).indexOf(node);

      String currText = SPropertyOperations.getString(node, PROPS.text$_LaO);
      if (index < currText.length()) {
        SNode newText = SModelOperations.createNewNode(SNodeOperations.getModel(node), null, CONCEPTS.XmlTextValue$29);
        SPropertyOperations.assign(newText, PROPS.text$_LaO, currText.substring(index));
        SPropertyOperations.assign(node, PROPS.text$_LaO, currText.substring(0, index));
        ListSequence.fromList(SLinkOperations.getChildren(attr, LINKS.value$1h4D)).insertElement(currIndex + 1, newText);
      }
      SNode newRef = SModelOperations.createNewNode(SNodeOperations.getModel(node), null, CONCEPTS.XmlEntityRefValue$di);
      ListSequence.fromList(SLinkOperations.getChildren(attr, LINKS.value$1h4D)).insertElement(currIndex + 1, newRef);
      editorContext.selectWRTFocusPolicy(newRef);
    }
    public String getKeyStroke() {
      return " &";
    }
    private static boolean isNotEmptyString(String str) {
      return str != null && str.length() > 0;
    }
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept XmlTextValue$29 = MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54cfd1fL, "jetbrains.mps.core.xml.structure.XmlTextValue");
    /*package*/ static final SConcept XmlAttribute$ll = MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b8df3L, "jetbrains.mps.core.xml.structure.XmlAttribute");
    /*package*/ static final SConcept XmlValuePart$G = MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54cfd1cL, "jetbrains.mps.core.xml.structure.XmlValuePart");
    /*package*/ static final SConcept XmlEntityRefValue$di = MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54cfd21L, "jetbrains.mps.core.xml.structure.XmlEntityRefValue");
  }

  private static final class PROPS {
    /*package*/ static final SProperty text$_LaO = MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54cfd1fL, 0x5c842a42c54cfd20L, "text");
    /*package*/ static final SProperty entityName$BTnn = MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54cfd21L, 0x5c842a42c54d0258L, "entityName");
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink value$1h4D = MetaAdapterFactory.getContainmentLink(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b8df3L, 0x5c842a42c54cfd1eL, "value");
  }
}
