package jetbrains.mps.debug.api; /*Generated by MPS */ import com.intellij.openapi.components.ProjectComponent; import java.util.Map; import com.intellij.execution.process.ProcessHandler; import java.util.HashMap; import java.util.List; import java.util.ArrayList; import com.intellij.execution.ui.RunContentWithExecutorListener; import com.intellij.openapi.project.Project; import com.intellij.execution.ui.RunContentDescriptor; import com.intellij.util.messages.MessageBusConnection; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import com.intellij.execution.ui.RunContentManager; import java.util.Set; import java.util.HashSet; import com.intellij.execution.Executor; public class DebugSessionManagerComponent implements ProjectComponent { private final Map<ProcessHandler, AbstractDebugSession> myProcessesToSessions = new HashMap<ProcessHandler, AbstractDebugSession>(1); private final List<DebugSessionManagerComponent.DebugSessionListener> myCurrentDebugSessionListeners = new ArrayList<DebugSessionManagerComponent.DebugSessionListener>(); private final RunContentWithExecutorListener myRunContentListener = new DebugSessionManagerComponent.MyRunContentListener(); private final Project myProject; private RunContentDescriptor mySelectedContent = null; private MessageBusConnection myConnection = null; public DebugSessionManagerComponent(Project project) { myProject = project; } public Project getProject() { return myProject; } @NotNull @Override public String getComponentName() { return "MPS Debug Manager"; } @Override public void initComponent() { } @Override public void disposeComponent() { } @Override public void projectOpened() { addRunContentListener(); } @Override public void projectClosed() { removeRunContentListener(); } @Nullable public AbstractDebugSession getDebugSessionByCurrentTab() { if (mySelectedContent == null) { return null; } return getDebugSession(mySelectedContent.getProcessHandler()); } private void addRunContentListener() { myConnection = myProject.getMessageBus().connect(myProject); myConnection.subscribe(RunContentManager.TOPIC, myRunContentListener); } private void removeRunContentListener() { myConnection.disconnect(); } private void fireSelectedDebugSessionChanged(AbstractDebugSession debugSession) { for (DebugSessionManagerComponent.DebugSessionListener listener : getAllCurrentDebugSessionListeners()) { listener.currentSessionChanged(debugSession); } } private void fireSessionDetached(AbstractDebugSession debugSession) { for (DebugSessionManagerComponent.DebugSessionListener listener : getAllCurrentDebugSessionListeners()) { listener.detached(debugSession); } } private void fireSessionRegistered(AbstractDebugSession debugSession) { for (DebugSessionManagerComponent.DebugSessionListener listener : getAllCurrentDebugSessionListeners()) { listener.registered(debugSession); } } public AbstractDebugSession getDebugSession(ProcessHandler processHandler) { return myProcessesToSessions.get(processHandler); } public Set<AbstractDebugSession> getDebugSessions() { synchronized (myProcessesToSessions) { return new HashSet<AbstractDebugSession>(myProcessesToSessions.values()); } } public void addDebugSession(AbstractDebugSession session) { ProcessHandler processHandler = session.getProcessHandler(); assert processHandler != null; synchronized (myProcessesToSessions) { myProcessesToSessions.put(processHandler, session); } session.sessionRegistered(this); fireSessionRegistered(session); } public void removeDebugSession(AbstractDebugSession session) { fireSessionDetached(session); session.sessionUnregistered(this); myProcessesToSessions.remove(session.getProcessHandler()); } public void addDebugSessionListener(DebugSessionManagerComponent.DebugSessionListener listener) { synchronized (myCurrentDebugSessionListeners) { myCurrentDebugSessionListeners.add(listener); } } public void removeDebugSessionListener(DebugSessionManagerComponent.DebugSessionListener listener) { synchronized (myCurrentDebugSessionListeners) { myCurrentDebugSessionListeners.remove(listener); } } public List<DebugSessionManagerComponent.DebugSessionListener> getAllCurrentDebugSessionListeners() { synchronized (myCurrentDebugSessionListeners) { return new ArrayList<DebugSessionManagerComponent.DebugSessionListener>(myCurrentDebugSessionListeners); } } public static DebugSessionManagerComponent getInstance(@NotNull Project project) { return project.getComponent(DebugSessionManagerComponent.class); } public interface DebugSessionListener { void registered(AbstractDebugSession session); void currentSessionChanged(AbstractDebugSession session); void detached(AbstractDebugSession session); } public static abstract class DebugSessionAdapter implements DebugSessionManagerComponent.DebugSessionListener { public DebugSessionAdapter() { } @Override public void registered(AbstractDebugSession session) { } @Override public void currentSessionChanged(AbstractDebugSession session) { } @Override public void detached(AbstractDebugSession session) { } } private class MyRunContentListener implements RunContentWithExecutorListener { private MyRunContentListener() { } public void contentSelected(@Nullable RunContentDescriptor descriptor, @NotNull Executor executor) { AbstractDebugSession debugSession; if (descriptor != null) { mySelectedContent = descriptor; debugSession = getDebugSession(descriptor.getProcessHandler()); fireSelectedDebugSessionChanged(debugSession); } } public void contentRemoved(@Nullable RunContentDescriptor descriptor, @NotNull Executor executor) { // todo maybe do something } } }