package jetbrains.mps.console.tool;
/*Generated by MPS */
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.components.StoragePathMacros;
import jetbrains.mps.ide.tools.BaseTabbedProjectTool;
import com.intellij.openapi.components.PersistentStateComponent;
import java.util.List;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import jetbrains.mps.project.MPSProject;
import jetbrains.mps.icons.MPSIcons;
import com.intellij.openapi.wm.ToolWindowAnchor;
import java.util.Map;
import javax.swing.KeyStroke;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import java.util.HashMap;
import com.intellij.openapi.keymap.KeymapManager;
import org.jetbrains.annotations.Nullable;
import javax.swing.Icon;
import jetbrains.mps.baseLanguage.closures.runtime.Wrappers;
import org.jdom.Element;
import jetbrains.mps.persistence.PersistenceUtil;
import com.intellij.util.Base64Converter;
import jetbrains.mps.plugins.tool.IComponentDisposer;
import jetbrains.mps.util.annotation.ToRemove;
import com.intellij.util.xmlb.annotations.Tag;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.smodel.behaviour.BHReflection;
import jetbrains.mps.core.aspects.behaviour.SMethodTrimmedId;
import com.intellij.ui.content.Content;
@State(name = "ConsoleHistory", storages = @Storage(value = StoragePathMacros.WORKSPACE_FILE)
)
public class ConsoleTool extends BaseTabbedProjectTool implements PersistentStateComponent<ConsoleTool.MyState> {
private ConsoleTool.MyState loadedState;
private List<BaseConsoleTab> myTabs = ListSequence.fromList(new ArrayList<BaseConsoleTab>());
private MPSProject myProject;
public ConsoleTool(MPSProject project) {
super(project.getProject(), "Console", getKeymap(), MPSIcons.ToolWindows.OpenTerminal_13x13, ToolWindowAnchor.BOTTOM, true);
myProject = project;
}
private static Map<String, KeyStroke> getKeymap() {
return MapSequence.<String, KeyStroke>fromMapAndKeysArray(new HashMap<String, KeyStroke>(), KeymapManager.DEFAULT_IDEA_KEYMAP).withValues(KeyStroke.getKeyStroke("alt F11"));
}
@Override
protected boolean isInitiallyAvailable() {
return true;
}
private boolean pasteAsRef = true;
public boolean getPasteAsRef() {
return pasteAsRef;
}
public void runWithoutPasteAsRef(Runnable toRun) {
pasteAsRef = false;
toRun.run();
pasteAsRef = true;
}
public void clearAll() {
while (ListSequence.fromList(myTabs).isNotEmpty()) {
closeTab(ListSequence.fromList(myTabs).first());
}
loadedState = null;
initTabs();
}
public BaseConsoleTab addConsoleTab(@Nullable final ConsoleTool.TabState tabState, @Nullable Icon icon, boolean openTool) {
String title = check_xg3v07_a0a0t(tabState);
final Wrappers._T<Element> history = new Wrappers._T<Element>(check_xg3v07_a0b0t(check_xg3v07_a0a1a91(check_xg3v07_a0a0b0t(check_xg3v07_a0a0a1a91(tabState)))));
if (check_xg3v07_a0c0t(tabState) != null) {
myProject.getRepository().getModelAccess().runReadAction(new Runnable() {
public void run() {
history.value = PersistenceUtil.saveModelToXml(PersistenceUtil.loadBinaryModel(Base64Converter.decode(tabState.history.getBytes())));
}
});
}
if (icon == null) {
icon = MPSIcons.ToolWindows.OpenTerminal_13x13;
}
if (title == null) {
title = "Console";
}
BaseConsoleTab tab;
if (check_xg3v07_a6a91(tabState)) {
tab = new OutputConsoleTab(myProject, this, title, history.value);
} else {
tab = new DialogConsoleTab(myProject, this, title, history.value);
}
ListSequence.fromList(myTabs).addElement(tab);
addTab(tab, title, icon, new IComponentDisposer<BaseConsoleTab>() {
public void disposeComponent(BaseConsoleTab ct) {
ListSequence.fromList(myTabs).removeElement(ct);
}
}, openTool);
return tab;
}
@Override
protected void doRegister() {
super.doRegister();
initTabs();
}
private void initTabs() {
if (loadedState != null) {
for (ConsoleTool.TabState tabState : ListSequence.fromList(loadedState.tabs)) {
BaseConsoleTab tab = addConsoleTab(tabState, null, false);
getContentManager().getContent(tab).setPinned(true);
}
}
if (ListSequence.fromList(myTabs).count() == 0) {
BaseConsoleTab tab = addConsoleTab(null, null, false);
getContentManager().getContent(tab).setPinned(true);
}
check_xg3v07_a2a32(getContentManager().getContent(0), this);
getContentManager().setSelectedContent(getContentManager().getContent(0));
}
public void selectTab(BaseConsoleTab tab) {
getContentManager().setSelectedContent(getContentManager().getContent(tab));
}
public static class TabState {
public String title;
public boolean isHistoryTab;
@Deprecated
@ToRemove(version = 3.5)
public String history;
@Tag(value = "tab")
public Element historyXml;
}
public static class MyState {
public static final String VERSION = "3.2";
public String version = VERSION;
public ArrayList<ConsoleTool.TabState> tabs = new ArrayList<ConsoleTool.TabState>();
}
public void loadState(ConsoleTool.MyState state) {
if (eq_xg3v07_a0a0fb(state.version, ConsoleTool.MyState.VERSION)) {
loadedState = state;
}
}
public void executeCommand(final SNode command) {
final ConsoleTool.TabState tabState = new ConsoleTool.TabState();
myProject.getRepository().getModelAccess().runReadAction(new Runnable() {
public void run() {
tabState.title = ((String) BHReflection.invoke(command, SMethodTrimmedId.create("getPresentation", null, "hEwIMiw")));
}
});
tabState.isHistoryTab = true;
final BaseConsoleTab tab = addConsoleTab(tabState, null, true);
myProject.getRepository().getModelAccess().executeCommand(new Runnable() {
public void run() {
tab.execute(command, null, null);
}
});
}
public DialogConsoleTab getCurrentEditableTab() {
if (ListSequence.fromList(myTabs).getElement(this.getCurrentTabIndex()) instanceof DialogConsoleTab) {
return as_xg3v07_a0a0a0jb(ListSequence.fromList(myTabs).getElement(this.getCurrentTabIndex()), DialogConsoleTab.class);
}
return as_xg3v07_a0b0jb(ListSequence.fromList(myTabs).getElement(0), DialogConsoleTab.class);
}
@Nullable
public ConsoleTool.MyState getState() {
ConsoleTool.MyState result = new ConsoleTool.MyState();
for (BaseConsoleTab tab : ListSequence.fromList(myTabs)) {
if (!(getContentManager().getContent(tab).isPinned())) {
break;
}
ConsoleTool.TabState tabState = new ConsoleTool.TabState();
tabState.historyXml = new Element("tab").addContent(tab.saveHistory());
tabState.title = tab.getTitle();
tabState.isHistoryTab = !(tab instanceof DialogConsoleTab);
if (tabState.historyXml == null || tabState.title == null) {
break;
}
result.tabs.add(tabState);
}
return result;
}
private static String check_xg3v07_a0a0t(ConsoleTool.TabState checkedDotOperand) {
if (null != checkedDotOperand) {
return checkedDotOperand.title;
}
return null;
}
private static Element check_xg3v07_a0b0t(Element checkedDotOperand) {
if (null != checkedDotOperand) {
return checkedDotOperand.clone();
}
return null;
}
private static Element check_xg3v07_a0a1a91(List<Element> checkedDotOperand) {
if (null != checkedDotOperand) {
return checkedDotOperand.get(0);
}
return null;
}
private static List<Element> check_xg3v07_a0a0b0t(Element checkedDotOperand) {
if (null != checkedDotOperand) {
return checkedDotOperand.getChildren();
}
return null;
}
private static Element check_xg3v07_a0a0a1a91(ConsoleTool.TabState checkedDotOperand) {
if (null != checkedDotOperand) {
return checkedDotOperand.historyXml;
}
return null;
}
private static String check_xg3v07_a0c0t(ConsoleTool.TabState checkedDotOperand) {
if (null != checkedDotOperand) {
return checkedDotOperand.history;
}
return null;
}
private static boolean check_xg3v07_a6a91(ConsoleTool.TabState checkedDotOperand) {
if (null != checkedDotOperand) {
return checkedDotOperand.isHistoryTab;
}
return false;
}
private static void check_xg3v07_a2a32(Content checkedDotOperand, ConsoleTool checkedDotThisExpression) {
if (null != checkedDotOperand) {
checkedDotOperand.setCloseable(false);
}
}
private static boolean eq_xg3v07_a0a0fb(Object a, Object b) {
return (a != null ? a.equals(b) : a == b);
}
private static <T> T as_xg3v07_a0a0a0jb(Object o, Class<T> type) {
return (type.isInstance(o) ? (T) o : null);
}
private static <T> T as_xg3v07_a0b0jb(Object o, Class<T> type) {
return (type.isInstance(o) ? (T) o : null);
}
}