package jetbrains.mps.lang.script.pluginSolution.plugin;
/*Generated by MPS */
import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import jetbrains.mps.project.MPSProject;
import jetbrains.mps.ide.actions.MPSCommonDataKeys;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import java.awt.Frame;
import java.util.List;
import org.jetbrains.mps.openapi.model.SModel;
import org.jetbrains.mps.openapi.module.SModule;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.baseLanguage.closures.runtime.Wrappers;
import org.jetbrains.mps.openapi.module.SearchScope;
import jetbrains.mps.ide.script.plugin.AbstractMigrationScriptHelper;
import org.jetbrains.mps.openapi.model.SNodeReference;
import jetbrains.mps.smodel.ModelAccess;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.internal.collections.runtime.ISelector;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.ide.script.plugin.RunMigrationScriptsDialog;
import java.awt.Component;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.openapi.navigation.NavigationSupport;
public class RunMigrationScripts_Action extends BaseAction {
private static final Icon ICON = null;
private boolean global;
public RunMigrationScripts_Action(boolean global_par) {
super("All Scripts...", "", ICON);
this.global = global_par;
this.setIsAlwaysVisible(false);
this.setExecuteOutsideCommand(true);
}
@Override
public boolean isDumbAware() {
return true;
}
@Override
protected boolean collectActionData(AnActionEvent event, final Map<String, Object> _params) {
if (!(super.collectActionData(event, _params))) {
return false;
}
{
MPSProject p = event.getData(MPSCommonDataKeys.MPS_PROJECT);
MapSequence.fromMap(_params).put("mpsProject", p);
if (p == null) {
return false;
}
}
{
Frame p = event.getData(MPSCommonDataKeys.FRAME);
MapSequence.fromMap(_params).put("frame", p);
if (p == null) {
return false;
}
}
{
List<SModel> p = event.getData(MPSCommonDataKeys.MODELS);
MapSequence.fromMap(_params).put("models", p);
}
{
List<SModule> p = event.getData(MPSCommonDataKeys.MODULES);
MapSequence.fromMap(_params).put("modules", p);
}
return true;
}
@Override
public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
final Wrappers._T<SearchScope> scope = new Wrappers._T<SearchScope>();
if (RunMigrationScripts_Action.this.global) {
scope.value = AbstractMigrationScriptHelper.createMigrationScope(((List<SModule>) MapSequence.fromMap(_params).get("modules")), ((List<SModel>) MapSequence.fromMap(_params).get("models")));
} else {
scope.value = AbstractMigrationScriptHelper.createMigrationScope(((MPSProject) MapSequence.fromMap(_params).get("mpsProject")));
}
if (!(scope.value.getModels().iterator().hasNext())) {
return;
}
final Wrappers._T<List<SNodeReference>> allScripts = new Wrappers._T<List<SNodeReference>>();
ModelAccess.instance().runReadAction(new Runnable() {
public void run() {
ScriptsMenuBuilder menuBuilder = new ScriptsMenuBuilder(((MPSProject) MapSequence.fromMap(_params).get("mpsProject")), RunMigrationScripts_Action.this.global);
allScripts.value = ListSequence.fromList(menuBuilder.getAllScripts()).sort(new ISelector<SNode, String>() {
public String select(SNode it) {
return (SPropertyOperations.getString_def(it, MetaAdapterFactory.getProperty(0xeddeefac2d64437L, 0xbc2cde50fd4ce470L, 0x11225e9072dL, 0x498b4f71ee081153L, "type"), "enhancement") == null ? "" : SPropertyOperations.getString_def(it, MetaAdapterFactory.getProperty(0xeddeefac2d64437L, 0xbc2cde50fd4ce470L, 0x11225e9072dL, 0x498b4f71ee081153L, "type"), "enhancement"));
}
}, true).alsoSort(new ISelector<SNode, String>() {
public String select(SNode it) {
return (SPropertyOperations.getString(it, MetaAdapterFactory.getProperty(0xeddeefac2d64437L, 0xbc2cde50fd4ce470L, 0x11225e9072dL, 0x498b4f71ee081155L, "toBuild")) == null ? "" : SPropertyOperations.getString(it, MetaAdapterFactory.getProperty(0xeddeefac2d64437L, 0xbc2cde50fd4ce470L, 0x11225e9072dL, 0x498b4f71ee081155L, "toBuild")));
}
}, true).select(new ISelector<SNode, SNodeReference>() {
public SNodeReference select(SNode it) {
return it.getReference();
}
}).toListSequence();
}
});
final RunMigrationScriptsDialog dialog = new RunMigrationScriptsDialog(((MPSProject) MapSequence.fromMap(_params).get("mpsProject")), ((Frame) MapSequence.fromMap(_params).get("frame")), allScripts.value);
int x = ((Frame) MapSequence.fromMap(_params).get("frame")).getX() + ((Frame) MapSequence.fromMap(_params).get("frame")).getWidth() / 2 - dialog.getWidth() / 2;
int y = ((Frame) MapSequence.fromMap(_params).get("frame")).getY() + ((Frame) MapSequence.fromMap(_params).get("frame")).getHeight() / 2 - dialog.getHeight() / 2;
// cast to Component eliminates out of search scope error in Java8 vs Java6
// setLocation() has got implementation in Window class since Java7
((Component) dialog).setLocation(x, y);
dialog.setVisible(true);
((MPSProject) MapSequence.fromMap(_params).get("mpsProject")).getRepository().getModelAccess().executeCommand(new Runnable() {
public void run() {
if (dialog.isRunChecked()) {
List<SNodeReference> checked = dialog.getCheckedScripts();
AbstractMigrationScriptHelper.doRunScripts(ListSequence.fromList(checked).select(new ISelector<SNodeReference, SNode>() {
public SNode select(SNodeReference it) {
return SNodeOperations.cast(it.resolve(((MPSProject) MapSequence.fromMap(_params).get("mpsProject")).getRepository()), MetaAdapterFactory.getConcept(0xeddeefac2d64437L, 0xbc2cde50fd4ce470L, 0x11225e9072dL, "jetbrains.mps.lang.script.structure.MigrationScript"));
}
}).toListSequence(), scope.value, ((MPSProject) MapSequence.fromMap(_params).get("mpsProject")));
} else if (dialog.isOpenSelected()) {
SNode selectedScript = ListSequence.fromList(dialog.getSelectedScripts()).first().resolve(((MPSProject) MapSequence.fromMap(_params).get("mpsProject")).getRepository());
NavigationSupport.getInstance().openNode(((MPSProject) MapSequence.fromMap(_params).get("mpsProject")), selectedScript, true, true);
}
}
});
}
@NotNull
public String getActionId() {
StringBuilder res = new StringBuilder();
res.append(super.getActionId());
res.append("#");
res.append(((Object) this.global).toString());
res.append("!");
return res.toString();
}
}