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 org.apache.log4j.Level;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
public class NodeByConcept_Configuration implements IPersistentConfiguration {
private static final Logger LOG = LogManager.getLogger(NodeByConcept_Configuration.class);
@NotNull
private NodeByConcept_Configuration.MyState myState = new NodeByConcept_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 = getNodeRef();
if (ptr == null) {
return "Node is not specified.";
}
SNode resolved = ptr.resolve(repo);
if (resolved == null) {
return "Failed to resolve node reference";
}
return (myIsValid.invoke(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 getNodeRef() {
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(@Nullable SNodeReference nodePtr) {
this.setNodePointer((nodePtr == null ? null : PersistenceFacade.getInstance().asString(nodePtr)));
}
@Override
public NodeByConcept_Configuration clone() {
NodeByConcept_Configuration clone = null;
try {
clone = createCloneTemplate();
clone.myState = (NodeByConcept_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 {
NodeByConcept_Configuration.MyState state = new NodeByConcept_Configuration.MyState();
state.myNodePointer = myNodePointer;
state.myNodeText = myNodeText;
return state;
}
}
public NodeByConcept_Configuration(SAbstractConcept concept, _FunctionTypes._return_P1_E0<? extends Boolean, ? super SNode> isValid) {
myConcept = concept;
myIsValid = isValid;
}
private final SAbstractConcept myConcept;
private final _FunctionTypes._return_P1_E0<? extends Boolean, ? super SNode> myIsValid;
public NodeByConcept_Configuration createCloneTemplate() {
return new NodeByConcept_Configuration(myConcept, myIsValid);
}
public NodeByConcept_Configuration_Editor getEditor() {
return new NodeByConcept_Configuration_Editor(myConcept, myIsValid);
}
}