package jetbrains.mps.debugger.api.ui.executionPoint;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.debugger.core.CurrentLinePositionComponentEx;
import jetbrains.mps.debug.api.AbstractDebugSession;
import com.intellij.openapi.components.ProjectComponent;
import jetbrains.mps.debug.api.SessionChangeListener;
import jetbrains.mps.debug.api.DebugSessionManagerComponent;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import org.jetbrains.mps.openapi.model.SNodeReference;
import jetbrains.mps.debug.api.programState.IStackFrame;
import jetbrains.mps.debug.api.programState.ILocation;
import jetbrains.mps.debug.api.source.SourcePosition;
import jetbrains.mps.debug.api.source.PositionProvider;
import jetbrains.mps.debug.api.source.NodeSourcePosition;
import jetbrains.mps.debug.api.SessionChangeAdapter;

@GeneratedClass(node = "r:b844984f-e192-4128-bae9-ea8360d94146(jetbrains.mps.debugger.api.ui.executionPoint)/1482618131069247085", model = "r:b844984f-e192-4128-bae9-ea8360d94146(jetbrains.mps.debugger.api.ui.executionPoint)")
public class CurrentLinePositionComponent extends CurrentLinePositionComponentEx<AbstractDebugSession> implements ProjectComponent {
  private final SessionChangeListener myChangeListener = new MySessionChangeListener();
  private final DebugSessionManagerComponent.DebugSessionListener myCurrentDebugSessionListener = new MyCurrentDebugSessionListener();
  public CurrentLinePositionComponent(Project project) {
    super(project);
  }
  @Override
  public void projectOpened() {
  }
  @Override
  public void projectClosed() {
  }
  @NotNull
  @Override
  public String getComponentName() {
    return "Current Line Position Project Component";
  }
  @Override
  public void initComponent() {
    super.init();
    DebugSessionManagerComponent component = DebugSessionManagerComponent.getInstance(myProject);
    component.addDebugSessionListener(myCurrentDebugSessionListener);
  }
  @Override
  public void disposeComponent() {
    DebugSessionManagerComponent component = DebugSessionManagerComponent.getInstance(myProject);
    component.removeDebugSessionListener(myCurrentDebugSessionListener);
    super.dispose();
  }
  @Override
  protected AbstractDebugSession getCurrentSession() {
    return DebugSessionManagerComponent.getInstance(myProject).getDebugSessionByCurrentTab();
  }
  protected Collection<? extends AbstractDebugSession> getAllSessions() {
    return DebugSessionManagerComponent.getInstance(myProject).getDebugSessions();
  }
  @Override
  public SNodeReference getNode(AbstractDebugSession session) {
    IStackFrame stackFrame = session.getUiState().getStackFrame();
    if (stackFrame != null) {
      ILocation location = stackFrame.getLocation();
      SourcePosition position = PositionProvider.getInstance(myProject).getPosition(location, session);
      return (position instanceof NodeSourcePosition ? ((NodeSourcePosition) position).getNode() : null);
    }
    return null;
  }
  private class MyCurrentDebugSessionListener implements DebugSessionManagerComponent.DebugSessionListener {
    private MyCurrentDebugSessionListener() {
    }
    @Override
    public void registered(AbstractDebugSession session) {
      session.addChangeListener(myChangeListener);
    }
    @Override
    public void currentSessionChanged(AbstractDebugSession newDebugSession) {
      CurrentLinePositionComponent.this.currentSessionChanged(newDebugSession);
    }
    @Override
    public void detached(AbstractDebugSession newDebugSession) {
      detachPainter(newDebugSession);
      newDebugSession.removeChangeListener(myChangeListener);
    }
  }
  private class MySessionChangeListener extends SessionChangeAdapter {
    private MySessionChangeListener() {
    }
    @Override
    public void stateChanged(AbstractDebugSession session) {
      reAttachPainter(session, true);
    }
    @Override
    public void paused(AbstractDebugSession debugSession) {
      reAttachPainter(debugSession, true);
    }
    @Override
    public void resumed(AbstractDebugSession debugSession) {
      detachPainter(debugSession);
    }
  }
}
