package jetbrains.mps.ide.migration.wizard; /*Generated by MPS */ import com.intellij.openapi.wm.impl.status.InlineProgressIndicator; import javax.swing.JPanel; import javax.swing.JLabel; import jetbrains.mps.progress.ProgressMonitorAdapter; import javax.swing.JComponent; import java.awt.BorderLayout; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.progress.ProgressManager; import javax.swing.BorderFactory; import com.intellij.ui.IdeBorderFactory; import com.intellij.openapi.application.ModalityState; import jetbrains.mps.baseLanguage.closures.runtime.Wrappers; import javax.swing.SwingUtilities; import com.intellij.openapi.project.Project; import jetbrains.mps.ide.project.ProjectHelper; import com.intellij.openapi.ui.Messages; import java.lang.reflect.InvocationTargetException; import com.intellij.openapi.progress.TaskInfo; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NonNls; import jetbrains.mps.ide.ThreadUtils; public class MigrationStep extends BaseStep { public static final String ID = "migration"; private InlineProgressIndicator myProgress; private MigrationSession mySession; private MigrationTask myTask; private JPanel myErrorPanel; private JLabel myErrorLabel; public MigrationStep(MigrationSession session) { super("Migration in Progress", ID); mySession = session; myProgress = new MigrationStep.MyInlineProgressIndicator(); myTask = new MigrationTask(session, new ProgressMonitorAdapter(myProgress)); } @Override protected final void doCreateComponent(JComponent mainPanel) { mainPanel.setLayout(new BorderLayout()); mainPanel.add(myProgress.getComponent(), BorderLayout.NORTH); this.myErrorPanel = new JPanel(new BorderLayout()); mainPanel.add(myErrorPanel, BorderLayout.CENTER); this.myErrorLabel = new JLabel(); myErrorPanel.add(new JPanel(), BorderLayout.CENTER); } @Override public void _init() { super._init(); ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { // do nothing } ProgressManager.getInstance().runProcess(new Runnable() { public void run() { while (!(myTask.isComplete())) { myTask.run(); if (mySession.getErrorDescriptor() != null) { if (!(mySession.getErrorDescriptor().canIgnore())) { ApplicationManager.getApplication().invokeLater(new Runnable() { public void run() { myErrorPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(20, 0, 10, 0), IdeBorderFactory.createTitledBorder("Error", true))); myErrorLabel.setText("<html>" + mySession.getErrorDescriptor().getMessage() + "</html>"); myErrorPanel.add(MigrationStep.this.myErrorLabel, BorderLayout.NORTH); myTask.forceComplete(); myProgress.setFraction(1.0); fireStateChanged(); } }, ModalityState.stateForComponent(myErrorPanel)); break; } else if (!(showError())) { myTask.forceComplete(); myProgress.setFraction(1.0); fireStateChanged(); break; } } } fireStateChanged(); } }, myProgress); } }); } public boolean showError() { // false - stop, true - continue final Wrappers._boolean res = new Wrappers._boolean(false); try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { Project project = ProjectHelper.toIdeaProject(mySession.getProject()); String msg = mySession.getErrorDescriptor().getMessage(); msg = msg.replaceAll("<br>", "\n"); res.value = Messages.showYesNoDialog(project, msg + "Continue migration?", "Errors detected", "Ignore and Continue", "Stop Migration", null) == Messages.YES; } }); } catch (InvocationTargetException e) { } catch (InterruptedException e) { } return res.value; } @Override public Object getNextStepId() { return null; } @Override public Object getPreviousStepId() { return null; } @Override public boolean isComplete() { return myTask.isComplete(); } @Override public boolean canBeCancelled() { return false; } private class MyInlineProgressIndicator extends InlineProgressIndicator { public MyInlineProgressIndicator() { super(false, new TaskInfo() { @NotNull public String getTitle() { return "Migration in progress"; } public String getCancelText() { return ""; } public String getCancelTooltipText() { return ""; } public boolean isCancellable() { return false; } @NonNls public String getProcessId() { return "migration"; } }); setIndeterminate(false); setFraction(0.0); } @Override protected void queueProgressUpdate() { if (ThreadUtils.isInEDT()) { updateAndRepaint(); } else { ApplicationManager.getApplication().invokeLater(new Runnable() { public void run() { updateAndRepaint(); } }, ModalityState.stateForComponent(getComponent())); } } @Override protected void queueRunningUpdate(@NotNull Runnable update) { throw new UnsupportedOperationException(); } @Override protected boolean isFinished() { return myTask.isComplete(); } } }