package jetbrains.mps.ide.migration.wizard;
/*Generated by MPS */
import java.util.Map;
import jetbrains.mps.migration.global.ProjectMigrationWithOptions;
import javax.swing.JComponent;
import java.util.HashMap;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.Box;
import com.intellij.ui.IdeBorderFactory;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.project.Project;
import javax.swing.tree.DefaultMutableTreeNode;
import jetbrains.mps.ide.migration.MigrationManager;
import javax.swing.Icon;
import com.intellij.openapi.application.ApplicationManager;
import jetbrains.mps.ide.icons.GlobalIconManager;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.internal.collections.runtime.IVisitor;
import jetbrains.mps.migration.global.ProjectMigration;
import jetbrains.mps.migration.global.CleanupProjectMigration;
import java.util.List;
import jetbrains.mps.ide.migration.ScriptApplied;
import jetbrains.mps.lang.migration.runtime.base.MigrationModuleUtil;
import jetbrains.mps.lang.migration.runtime.base.BaseScriptReference;
import jetbrains.mps.internal.collections.runtime.ISelector;
import org.jetbrains.mps.openapi.language.SLanguage;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import jetbrains.mps.lang.migration.runtime.base.MigrationScriptReference;
import jetbrains.mps.util.NameUtil;
import jetbrains.mps.icons.MPSIcons;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
import org.jetbrains.mps.openapi.module.SModule;
import jetbrains.mps.lang.migration.runtime.base.RefactoringScriptReference;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.Component;
import jetbrains.mps.ide.icons.IdeIcons;
import com.intellij.ui.components.JBScrollPane;
import java.awt.BorderLayout;
import com.intellij.ide.wizard.AbstractWizardStepEx;
import com.intellij.ide.wizard.CommitStepException;
import jetbrains.mps.internal.collections.runtime.SetSequence;
import jetbrains.mps.lang.migration.runtime.base.MigrationScript;
import jetbrains.mps.lang.migration.runtime.base.RefactoringScript;
public class InitialStep extends BaseStep {
public static final String ID = "initial";
private MigrationSession mySession;
private Map<ProjectMigrationWithOptions.Option, JComponent> myComponents = new HashMap<ProjectMigrationWithOptions.Option, JComponent>();
public InitialStep(MigrationSession session) {
super("Migration Required", ID);
mySession = session;
}
@Override
protected void doCreateComponent(JComponent mainPanel) {
// Set preferred size to avoid trim of Help button (if no icon presented)
mainPanel.setPreferredSize(new Dimension(400, 200));
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.add(createInfoPanel("<html>This project should be migrated.<br><br>" + "Migrations to be applied:<br></html>"));
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.X_AXIS));
infoPanel.add(Box.createRigidArea(new Dimension()));
infoPanel.add(createMigrationsInfo());
infoPanel.add(Box.createRigidArea(new Dimension()));
mainPanel.add(infoPanel);
if (!(myComponents.isEmpty())) {
JPanel settingsPanel = new JPanel();
settingsPanel.setLayout(new BoxLayout(settingsPanel, BoxLayout.Y_AXIS));
settingsPanel.setBorder(IdeBorderFactory.createTitledBorder("Options", true));
for (ProjectMigrationWithOptions.Option option : ListSequence.fromList(mySession.getOptions().optionsList())) {
JComponent c = option.createComponent();
myComponents.put(option, c);
settingsPanel.add(c);
}
mainPanel.add(settingsPanel);
}
}
private JComponent createMigrationsInfo() {
final Project project = mySession.getProject();
// root
final DefaultMutableTreeNode root = new DefaultMutableTreeNode("empty");
project.getRepository().getModelAccess().runReadAction(new Runnable() {
public void run() {
MigrationManager manager = mySession.getMigrationManager();
final Icon migrationIcon = ApplicationManager.getApplication().getComponent(GlobalIconManager.class).getIconFor(MetaAdapterFactory.getConcept(0x9074634404fd4286L, 0x97d5b46ae6a81709L, 0x73e8a2c68b62c6a3L, "jetbrains.mps.lang.migration.structure.MigrationScript"));
// project migrations
final DefaultMutableTreeNode croot = new DefaultMutableTreeNode("Cleanups");
final DefaultMutableTreeNode proot = new DefaultMutableTreeNode("Project Migrations");
Sequence.fromIterable(manager.getProjectMigrationsToApply()).visitAll(new IVisitor<ProjectMigration>() {
public void visit(ProjectMigration it) {
DefaultMutableTreeNode node = new InitialStep.MyTreeNode(it.getDescription(), migrationIcon);
if (it instanceof CleanupProjectMigration) {
croot.add(node);
} else {
proot.add(node);
}
}
});
if (croot.children().hasMoreElements()) {
root.add(croot);
}
if (proot.children().hasMoreElements()) {
root.add(proot);
}
List<ScriptApplied> migrationsApplied = manager.getModuleMigrationsToApply(MigrationModuleUtil.getMigrateableModulesFromProject(project));
Iterable<BaseScriptReference> scripts = ListSequence.fromList(migrationsApplied).select(new ISelector<ScriptApplied, BaseScriptReference>() {
public BaseScriptReference select(ScriptApplied it) {
return it.getScriptReference();
}
}).distinct();
// language migrations
final Map<SLanguage, DefaultMutableTreeNode> l2n = MapSequence.fromMap(new HashMap<SLanguage, DefaultMutableTreeNode>());
Sequence.fromIterable(scripts).ofType(MigrationScriptReference.class).select(new ISelector<MigrationScriptReference, SLanguage>() {
public SLanguage select(MigrationScriptReference it) {
return it.getLanguage();
}
}).distinct().visitAll(new IVisitor<SLanguage>() {
public void visit(SLanguage it) {
MapSequence.fromMap(l2n).put(it, new InitialStep.MyTreeNode(NameUtil.compactNamespace(it.getQualifiedName()), MPSIcons.Nodes.Language));
}
});
Sequence.fromIterable(scripts).ofType(MigrationScriptReference.class).visitAll(new IVisitor<MigrationScriptReference>() {
public void visit(MigrationScriptReference it) {
String caption = check_nznoqw_a0a0a0a61a0e0j(it.resolve(false));
MapSequence.fromMap(l2n).get(it.getLanguage()).add(new InitialStep.MyTreeNode(caption, migrationIcon));
}
});
int migratedModulesNum = ListSequence.fromList(migrationsApplied).where(new IWhereFilter<ScriptApplied>() {
public boolean accept(ScriptApplied it) {
return it.getScriptReference() instanceof MigrationScriptReference;
}
}).select(new ISelector<ScriptApplied, SModule>() {
public SModule select(ScriptApplied it) {
return it.getModule();
}
}).distinct().count();
final DefaultMutableTreeNode lroot = new DefaultMutableTreeNode("Language Migrations (" + migratedModulesNum + " modules)");
Sequence.fromIterable(MapSequence.fromMap(l2n).values()).visitAll(new IVisitor<DefaultMutableTreeNode>() {
public void visit(DefaultMutableTreeNode it) {
lroot.add(it);
}
});
if (lroot.children().hasMoreElements()) {
root.add(lroot);
}
// module migrations
final Map<SModule, DefaultMutableTreeNode> m2n = MapSequence.fromMap(new HashMap<SModule, DefaultMutableTreeNode>());
Sequence.fromIterable(scripts).ofType(RefactoringScriptReference.class).select(new ISelector<RefactoringScriptReference, SModule>() {
public SModule select(RefactoringScriptReference it) {
return it.getModule();
}
}).distinct().visitAll(new IVisitor<SModule>() {
public void visit(SModule it) {
MapSequence.fromMap(m2n).put(it, new DefaultMutableTreeNode(NameUtil.compactNamespace(it.getModuleName())));
}
});
Sequence.fromIterable(scripts).ofType(RefactoringScriptReference.class).visitAll(new IVisitor<RefactoringScriptReference>() {
public void visit(RefactoringScriptReference it) {
String caption = check_nznoqw_a0a0a0a52a0e0j(it.resolve(false));
DefaultMutableTreeNode node = new DefaultMutableTreeNode(caption);
MapSequence.fromMap(m2n).get(it.getModule()).add(node);
}
});
int migratedModulesNum2 = ListSequence.fromList(migrationsApplied).where(new IWhereFilter<ScriptApplied>() {
public boolean accept(ScriptApplied it) {
return it.getScriptReference() instanceof RefactoringScriptReference;
}
}).select(new ISelector<ScriptApplied, SModule>() {
public SModule select(ScriptApplied it) {
return it.getModule();
}
}).distinct().count();
final DefaultMutableTreeNode mroot = new DefaultMutableTreeNode("Module Migrations (" + migratedModulesNum2 + " modules)");
Sequence.fromIterable(MapSequence.fromMap(m2n).values()).visitAll(new IVisitor<DefaultMutableTreeNode>() {
public void visit(DefaultMutableTreeNode it) {
mroot.add(it);
}
});
if (mroot.children().hasMoreElements()) {
root.add(mroot);
}
}
});
JTree tree = new JTree(root);
tree.setRootVisible(false);
tree.setCellRenderer(new DefaultTreeCellRenderer() {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
if (value instanceof InitialStep.MyTreeNode) {
setIcon(as_nznoqw_a0a0a0b0a0a0a0i0j(value, InitialStep.MyTreeNode.class).getIcon());
} else {
setIcon((expanded ? IdeIcons.OPENED_FOLDER : IdeIcons.CLOSED_FOLDER));
}
return this;
}
});
for (int i = 0; i < tree.getRowCount(); i++) {
tree.expandRow(i);
}
JBScrollPane scrollPane = new JBScrollPane(tree);
JPanel panel = new JPanel(new BorderLayout());
panel.add(scrollPane, BorderLayout.CENTER);
panel.setPreferredSize(new Dimension((int) panel.getPreferredSize().getWidth(), 100));
return panel;
}
@Override
public Object getNextStepId() {
return MigrationStep.ID;
}
@Override
public Object getPreviousStepId() {
return null;
}
@Override
public String cancelButtonLabel() {
return "Cancel";
}
@Override
public String nextButtonLabel() {
return "Migrate";
}
@Override
public void commit(AbstractWizardStepEx.CommitType commitType) throws CommitStepException {
super.commit(commitType);
for (ProjectMigrationWithOptions.Option option : SetSequence.fromSet(myComponents.keySet())) {
JComponent c = myComponents.get(option);
Object val = option.getValue(c);
mySession.getOptions().setOptionValue(option, val);
}
}
private static class MyTreeNode extends DefaultMutableTreeNode {
private Icon myIcon;
public MyTreeNode(String caption, Icon icon) {
super(caption);
myIcon = icon;
}
public Icon getIcon() {
return myIcon;
}
}
private static String check_nznoqw_a0a0a0a61a0e0j(MigrationScript checkedDotOperand) {
if (null != checkedDotOperand) {
return checkedDotOperand.getCaption();
}
return null;
}
private static String check_nznoqw_a0a0a0a52a0e0j(RefactoringScript checkedDotOperand) {
if (null != checkedDotOperand) {
return checkedDotOperand.getCaption();
}
return null;
}
private static <T> T as_nznoqw_a0a0a0b0a0a0a0i0j(Object o, Class<T> type) {
return (type.isInstance(o) ? (T) o : null);
}
}