package jetbrains.mps.baseLanguage.unitTest.execution.tool;
/*Generated by MPS */
import javax.swing.JPanel;
import jetbrains.mps.baseLanguage.unitTest.execution.client.TestView;
import com.intellij.openapi.progress.util.ColorProgressBar;
import javax.swing.JLabel;
import jetbrains.mps.baseLanguage.unitTest.execution.client.TestRunState;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import com.intellij.execution.process.ProcessOutputTypes;
import javax.swing.SwingUtilities;
import com.intellij.execution.process.ProcessListener;
import com.intellij.execution.process.ProcessAdapter;
import com.intellij.execution.process.ProcessEvent;
import com.intellij.openapi.application.ApplicationManager;
public class ProgressLine extends JPanel implements TestView {
private final ColorProgressBar myProgressBar = new ColorProgressBar();
private final JLabel myStateLabel = new JLabel("Starting...");
private boolean myTestsBuilt = false;
private TestRunState myState;
public ProgressLine(TestRunState testState) {
super(new GridLayout(1, 2));
myState = testState;
add(myStateLabel);
final JPanel progress = new JPanel(new GridBagLayout());
progress.add(myProgressBar, new GridBagConstraints(0, 0, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 0, 0, 2), 0, 0));
myProgressBar.setColor(ColorProgressBar.GREEN);
add(progress);
myTestsBuilt = true;
init();
}
@Override
public void update() {
if (myState.getAvailableText() != null || ProcessOutputTypes.SYSTEM.equals(myState.getKey())) {
return;
}
final int defectedTests = myState.getFailedTests();
final int totalTests = myState.getTotalTests();
final int completedTests = myState.getCompletedTests();
final String testName = myState.getCurrentMethod();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
updateProgressBar(defectedTests, totalTests, completedTests);
updateLabel(defectedTests, totalTests, completedTests, testName);
}
});
}
@Override
public void init() {
}
private void updateProgressBar(int defected, int total, int completed) {
if (defected > 0) {
myProgressBar.setColor(ColorProgressBar.RED);
} else if (myState.isTerminated() && !((total == completed))) {
myProgressBar.setColor(ColorProgressBar.YELLOW);
}
if (total != 0) {
myProgressBar.setFraction(completed * 1. / total);
}
}
private void updateLabel(int defected, int total, int completed, String testName) {
StringBuilder sb = new StringBuilder();
boolean done = total == completed || testName == null;
boolean terminated = myState.isTerminated();
if (done) {
sb.append(" Done: " + completed + " of " + total + " ");
testName = "";
} else if (terminated) {
sb.append(" Terminated: " + completed + " of " + total + " ");
testName = "";
}
if (defected > 0) {
sb.append(" Failed: " + defected);
}
if (!(terminated) && !(done)) {
sb.append(" Running: " + completed + " of " + total);
}
myStateLabel.setText(sb + " " + testName);
}
public ProcessListener getProcessListener() {
return new ProcessAdapter() {
@Override
public void processTerminated(ProcessEvent p0) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
if (!(myTestsBuilt) && myProgressBar.getFraction() == 0.0) {
myProgressBar.setColor(ColorProgressBar.RED);
myProgressBar.setFraction(1.0);
myStateLabel.setText("Failed to start");
}
}
});
}
};
}
}