package jetbrains.mps.baseLanguage.unitTest.execution.tool;
/*Generated by MPS */
import com.intellij.openapi.Disposable;
import javax.swing.Icon;
import com.intellij.util.Alarm;
import jetbrains.mps.baseLanguage.unitTest.execution.client.TestRunState;
import com.intellij.openapi.util.Disposer;
import org.jetbrains.annotations.Nullable;
import jetbrains.mps.smodel.ModelAccess;
import com.intellij.icons.AllIcons;
public class TestTreeIconAnimator implements Disposable, Runnable {
private static final int FRAMES_COUNT = 8;
private static final int MOVIE_TIME = 800;
private static final int FRAME_TIME = MOVIE_TIME / FRAMES_COUNT;
private static Icon[] FRAMES = new Icon[FRAMES_COUNT];
private long myLastInvocationTime = -1;
private Alarm myAlarm;
private TestRunState myState;
private TestTree myTestTree;
public TestTreeIconAnimator(TestTree testTree) {
Disposer.register(testTree, this);
myTestTree = testTree;
}
public void init(TestRunState state) {
myState = state;
myAlarm = new Alarm();
}
@Override
public void run() {
String methodName = myState.getCurrentMethod();
if (methodName != null) {
final long time = System.currentTimeMillis();
if (time - myLastInvocationTime >= FRAME_TIME) {
myLastInvocationTime = time;
String className = myState.getCurrentClass();
TestMethodTreeNode methodTreeNode = myTestTree.get(className, methodName);
updateTreeNode(methodTreeNode);
TestCaseTreeNode testTreeNode = myTestTree.get(className);
updateTreeNode(testTreeNode);
}
}
scheduleRepaint();
}
public void scheduleRepaint() {
if (myAlarm == null) {
return;
}
myAlarm.cancelAllRequests();
if (myState.getCurrentMethod() != null) {
myAlarm.addRequest(this, FRAME_TIME);
}
}
public void stopMovie() {
cancelAlarm();
}
@Override
public void dispose() {
cancelAlarm();
}
private void updateTreeNode(@Nullable final BaseTestTreeNode node) {
if (node == null) {
return;
}
ModelAccess.instance().runReadAction(new Runnable() {
public void run() {
node.renewPresentation();
}
});
}
private void cancelAlarm() {
if (myAlarm != null) {
myAlarm.cancelAllRequests();
myAlarm = null;
}
}
public static Icon getCurrentFrame() {
final int index = (int) ((System.currentTimeMillis() % MOVIE_TIME) / FRAME_TIME);
return FRAMES[index];
}
static {
FRAMES[0] = AllIcons.RunConfigurations.TestInProgress1;
FRAMES[1] = AllIcons.RunConfigurations.TestInProgress2;
FRAMES[2] = AllIcons.RunConfigurations.TestInProgress3;
FRAMES[3] = AllIcons.RunConfigurations.TestInProgress4;
FRAMES[4] = AllIcons.RunConfigurations.TestInProgress5;
FRAMES[5] = AllIcons.RunConfigurations.TestInProgress6;
FRAMES[6] = AllIcons.RunConfigurations.TestInProgress7;
FRAMES[7] = AllIcons.RunConfigurations.TestInProgress8;
}
}