package jetbrains.mps.execution.lib; /*Generated by MPS */ import jetbrains.mps.execution.api.settings.IPersistentConfiguration; import org.apache.log4j.Logger; import org.apache.log4j.LogManager; import org.jetbrains.annotations.NotNull; import jetbrains.mps.execution.api.settings.PersistentConfigurationContext; import com.intellij.execution.configurations.RuntimeConfigurationException; import org.jetbrains.mps.openapi.module.SRepository; import jetbrains.mps.smodel.ModelAccessHelper; import jetbrains.mps.util.Computable; import org.jetbrains.mps.openapi.model.SNodeReference; import org.jetbrains.mps.openapi.model.SNode; import com.intellij.execution.configurations.RuntimeConfigurationError; import org.jdom.Element; import com.intellij.openapi.util.WriteExternalException; import com.intellij.util.xmlb.XmlSerializer; import com.intellij.openapi.util.InvalidDataException; import org.jetbrains.annotations.Nullable; import org.jetbrains.mps.openapi.persistence.PersistenceFacade; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations; import jetbrains.mps.execution.lib.ui.NodeChooser; import jetbrains.mps.internal.collections.runtime.ListSequence; import jetbrains.mps.internal.collections.runtime.IWhereFilter; import org.jetbrains.mps.openapi.language.SAbstractConcept; import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes; import org.apache.log4j.Level; import java.util.List; public class NodeBySeveralConcepts_Configuration implements IPersistentConfiguration { private static final Logger LOG = LogManager.getLogger(NodeBySeveralConcepts_Configuration.class); @NotNull private NodeBySeveralConcepts_Configuration.MyState myState = new NodeBySeveralConcepts_Configuration.MyState(); public void checkConfiguration(final PersistentConfigurationContext context) throws RuntimeConfigurationException { final SRepository repo = context.getProject().getRepository(); String errorText = new ModelAccessHelper(repo).runReadAction(new Computable<String>() { public String compute() { try { final SNodeReference ptr = getNode(); if (ptr == null) { return "Node is not specified."; } SNode resolved = ptr.resolve(repo); if (resolved == null) { return "Failed to resolve node reference"; } return (isValid(resolved) ? null : "Node didn't pass validation"); } catch (IllegalArgumentException ex) { String m = "Node reference is not valid"; return (ex.getMessage() != null ? String.format("%s: %s", m, ex.getMessage()) : m); } } }); if ((errorText != null && errorText.length() > 0)) { throw new RuntimeConfigurationError(errorText); } } @Override public void writeExternal(Element element) throws WriteExternalException { element.addContent(XmlSerializer.serialize(myState)); } @Override public void readExternal(Element element) throws InvalidDataException { if (element == null) { throw new InvalidDataException("Cant read " + this + ": element is null."); } XmlSerializer.deserializeInto(myState, (Element) element.getChildren().get(0)); } public String getNodePointer() { return myState.myNodePointer; } public String getNodeText() { return myState.myNodeText; } public void setNodePointer(String value) { myState.myNodePointer = value; } public void setNodeText(String value) { myState.myNodeText = value; } @Nullable public SNodeReference getNode() { if (this.getNodePointer() == null) { return null; } return PersistenceFacade.getInstance().createNodeReference(this.getNodePointer()); } public void setNode(@Nullable SNode node) { if (node == null) { this.setNodePointer(null); this.setNodeText(null); } else { setNode(SNodeOperations.getPointer(node)); this.setNodeText(NodeChooser.getFqName(node)); } } /*package*/ void setNode(SNodeReference nodePtr) { this.setNodePointer((nodePtr == null ? null : PersistenceFacade.getInstance().asString(nodePtr))); } private boolean isValid(final SNode node) { return ListSequence.fromList(myTargets).findFirst(new IWhereFilter<NodesDescriptor>() { public boolean accept(NodesDescriptor it) { SAbstractConcept concept = it.concept(); _FunctionTypes._return_P1_E0<? extends Boolean, ? super SNode> function = it.filter(); if (SNodeOperations.isInstanceOf(node, SNodeOperations.asSConcept(concept))) { if (function != null) { return function.invoke(node); } else { return true; } } return false; } }) != null; } @Override public NodeBySeveralConcepts_Configuration clone() { NodeBySeveralConcepts_Configuration clone = null; try { clone = createCloneTemplate(); clone.myState = (NodeBySeveralConcepts_Configuration.MyState) myState.clone(); return clone; } catch (CloneNotSupportedException ex) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("", ex); } } return clone; } public class MyState { public String myNodePointer; public String myNodeText; public MyState() { } @Override public Object clone() throws CloneNotSupportedException { NodeBySeveralConcepts_Configuration.MyState state = new NodeBySeveralConcepts_Configuration.MyState(); state.myNodePointer = myNodePointer; state.myNodeText = myNodeText; return state; } } public NodeBySeveralConcepts_Configuration(List<NodesDescriptor> targets) { myTargets = targets; } private final List<NodesDescriptor> myTargets; public NodeBySeveralConcepts_Configuration createCloneTemplate() { return new NodeBySeveralConcepts_Configuration(myTargets); } public NodeBySeveralConcepts_Configuration_Editor getEditor() { return new NodeBySeveralConcepts_Configuration_Editor(myTargets); } }