package jetbrains.mps.ide.devkit.actions;
/*Generated by MPS */
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.smodel.behaviour.BHReflection;
import jetbrains.mps.core.aspects.behaviour.SMethodTrimmedId;
import org.jetbrains.mps.openapi.model.SModel;
import org.jetbrains.mps.openapi.module.SModule;
import jetbrains.mps.smodel.Generator;
import jetbrains.mps.kernel.model.SModelUtil;
import jetbrains.mps.smodel.SModelStereotype;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.AttributeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.IAttributeDescriptor;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
public class ConceptEditorOpenHelper {
public static SNode getBaseNode(SNode node) {
SNode baseNode = null;
if (SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getConcept(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0x1103553c5ffL, "jetbrains.mps.lang.structure.structure.AbstractConceptDeclaration"))) {
return null;
}
if (SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getInterfaceConcept(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0x24614259e94f0c84L, "jetbrains.mps.lang.structure.structure.IConceptAspect"))) {
baseNode = ((SNode) BHReflection.invoke(SNodeOperations.cast(node, MetaAdapterFactory.getInterfaceConcept(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0x24614259e94f0c84L, "jetbrains.mps.lang.structure.structure.IConceptAspect")), SMethodTrimmedId.create("getBaseConcept", null, "2hxg_BDjKM8")));
}
if (baseNode == null) {
baseNode = getBaseNode2(node);
}
if (baseNode == null) {
return null;
}
// We should be sure that node and base node are inside the same module.
// Otherwise, tabbed editor for base node will be opened, but there will be no tab for "node"
// So, the user will not be able to open node by a double-click
SModel baseModelDesIcriptor = SNodeOperations.getModel(baseNode);
SModel mainModelDescriptor = SNodeOperations.getModel(node);
if (mainModelDescriptor == null) {
return null;
}
SModule baseModule = baseModelDesIcriptor.getModule();
SModule mainModule = mainModelDescriptor.getModule();
if (mainModule instanceof Generator) {
mainModule = ((Generator) mainModule).getSourceLanguage();
}
if (baseModule != mainModule) {
return null;
}
if (!(canOpen(baseNode))) {
return null;
}
return baseNode;
}
private static SNode getBaseNode2(SNode node) {
if (node == null) {
return null;
}
SNode baseNode = findBaseNodeMultiTab(node);
if ((baseNode == null) || SModelUtil.getDeclaringLanguage(baseNode) == null || (!(SModelStereotype.isGeneratorModel(SNodeOperations.getModel(node))))) {
return null;
}
return baseNode;
}
private static boolean canOpen(SNode node) {
if (!(SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getConcept(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0x1103553c5ffL, "jetbrains.mps.lang.structure.structure.AbstractConceptDeclaration")))) {
return false;
}
if (SModelUtil.getDeclaringLanguage(SNodeOperations.cast(node, MetaAdapterFactory.getConcept(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0x1103553c5ffL, "jetbrains.mps.lang.structure.structure.AbstractConceptDeclaration"))) == null) {
return false;
}
return true;
}
private static SNode findBaseNodeMultiTab(SNode node) {
SNode baseNode = null;
if (jetbrains.mps.util.SNodeOperations.isRoot(node) && SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x10802efe25aL, "jetbrains.mps.lang.core.structure.BaseConcept"))) {
SNode bc = SNodeOperations.cast(node, MetaAdapterFactory.getConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x10802efe25aL, "jetbrains.mps.lang.core.structure.BaseConcept"));
SNode annotation = AttributeOperations.getAttribute(bc, new IAttributeDescriptor.NodeAttribute(MetaAdapterFactory.getConcept(0xb401a68083254110L, 0x8fd384331ff25befL, 0x11017244494L, "jetbrains.mps.lang.generator.structure.RootTemplateAnnotation")));
if ((annotation != null) && (SLinkOperations.getTarget(annotation, MetaAdapterFactory.getReferenceLink(0xb401a68083254110L, 0x8fd384331ff25befL, 0x11017244494L, 0x11017255ccfL, "applicableConcept")) != null)) {
baseNode = SLinkOperations.getTarget(annotation, MetaAdapterFactory.getReferenceLink(0xb401a68083254110L, 0x8fd384331ff25befL, 0x11017244494L, 0x11017255ccfL, "applicableConcept"));
}
}
if ((baseNode == null)) {
return null;
}
SModule baseNodeModule = SNodeOperations.getModel(baseNode).getModule();
SModule nodeModule = SNodeOperations.getModel(node).getModule();
if (nodeModule instanceof Generator) {
nodeModule = ((Generator) nodeModule).getSourceLanguage();
}
if (baseNodeModule != nodeModule) {
return null;
}
return baseNode;
}
}