IDEADEV-38479 Show run configuration's icon in "Run"-window
[fedora-idea.git] / java / debugger / impl / src / com / intellij / debugger / ui / DebuggerPanelsManager.java
blob99b914cceb73875f625b9c33e5524c40f2242cb0
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.debugger.ui;
18 import com.intellij.debugger.DebuggerInvocationUtil;
19 import com.intellij.debugger.DebuggerManagerEx;
20 import com.intellij.debugger.engine.DebugProcessImpl;
21 import com.intellij.debugger.impl.DebuggerContextImpl;
22 import com.intellij.debugger.impl.DebuggerContextListener;
23 import com.intellij.debugger.impl.DebuggerSession;
24 import com.intellij.debugger.impl.DebuggerStateManager;
25 import com.intellij.debugger.ui.impl.MainWatchPanel;
26 import com.intellij.debugger.ui.tree.render.BatchEvaluator;
27 import com.intellij.execution.ExecutionException;
28 import com.intellij.execution.ExecutionManager;
29 import com.intellij.execution.Executor;
30 import com.intellij.execution.configurations.ModuleRunProfile;
31 import com.intellij.execution.configurations.RemoteConnection;
32 import com.intellij.execution.configurations.RemoteState;
33 import com.intellij.execution.configurations.RunProfileState;
34 import com.intellij.execution.executors.DefaultDebugExecutor;
35 import com.intellij.execution.process.ProcessHandler;
36 import com.intellij.execution.runners.ExecutionEnvironment;
37 import com.intellij.execution.runners.ProgramRunner;
38 import com.intellij.execution.ui.RunContentDescriptor;
39 import com.intellij.execution.ui.RunContentListener;
40 import com.intellij.execution.ui.RunContentManager;
41 import com.intellij.openapi.Disposable;
42 import com.intellij.openapi.components.ProjectComponent;
43 import com.intellij.openapi.diagnostic.Logger;
44 import com.intellij.openapi.editor.colors.EditorColorsListener;
45 import com.intellij.openapi.editor.colors.EditorColorsManager;
46 import com.intellij.openapi.editor.colors.EditorColorsScheme;
47 import com.intellij.openapi.project.Project;
48 import com.intellij.openapi.util.Disposer;
49 import org.jetbrains.annotations.NotNull;
50 import org.jetbrains.annotations.Nullable;
52 import java.util.HashMap;
54 public class DebuggerPanelsManager implements ProjectComponent {
55 private static final Logger LOG = Logger.getInstance("#com.intellij.debugger.ui.DebuggerPanelsManager");
57 private final Project myProject;
58 private final ExecutionManager myExecutionManager;
60 private final PositionHighlighter myEditorManager;
61 private final HashMap<ProcessHandler, DebuggerSessionTab> mySessionTabs = new HashMap<ProcessHandler, DebuggerSessionTab>();
63 public DebuggerPanelsManager(Project project, final EditorColorsManager colorsManager, ExecutionManager executionManager) {
64 myProject = project;
65 myExecutionManager = executionManager;
67 myEditorManager = new PositionHighlighter(myProject, getContextManager());
69 final EditorColorsListener myColorsListener = new EditorColorsListener() {
70 public void globalSchemeChange(EditorColorsScheme scheme) {
71 myEditorManager.updateContextPointDescription();
74 colorsManager.addEditorColorsListener(myColorsListener);
75 Disposer.register(project, new Disposable() {
76 public void dispose() {
77 colorsManager.removeEditorColorsListener(myColorsListener);
79 });
81 getContextManager().addListener(new DebuggerContextListener() {
82 public void changeEvent(final DebuggerContextImpl newContext, int event) {
83 if (event == DebuggerSession.EVENT_PAUSE) {
84 DebuggerInvocationUtil.invokeLater(myProject, new Runnable() {
85 public void run() {
86 toFront(newContext.getDebuggerSession());
88 });
91 });
94 private DebuggerStateManager getContextManager() {
95 return DebuggerManagerEx.getInstanceEx(myProject).getContextManager();
98 @Nullable
99 public
100 RunContentDescriptor attachVirtualMachine(Executor executor,
101 ProgramRunner runner,
102 ExecutionEnvironment environment,
103 RunProfileState state,
104 RunContentDescriptor reuseContent,
105 RemoteConnection remoteConnection,
106 boolean pollConnection) throws ExecutionException {
108 final DebuggerSession debuggerSession =
109 DebuggerManagerEx.getInstanceEx(myProject).attachVirtualMachine(executor, runner, (ModuleRunProfile) environment.getRunProfile(), state, remoteConnection, pollConnection);
110 if (debuggerSession == null) {
111 return null;
114 final DebugProcessImpl debugProcess = debuggerSession.getProcess();
115 if (debugProcess.isDetached() || debugProcess.isDetaching()) {
116 return null;
118 if (state instanceof RemoteState) {
119 // optimization: that way BatchEvaluator will not try to lookup the class file in remote VM
120 // which is an expensive oparation when executed first time
121 debugProcess.putUserData(BatchEvaluator.REMOTE_SESSION_KEY, Boolean.TRUE);
124 final DebuggerSessionTab sessionTab = new DebuggerSessionTab(myProject, debuggerSession.getSessionName(), environment.getRunProfile().getIcon());
125 Disposer.register(myProject, sessionTab);
126 RunContentDescriptor runContentDescriptor =
127 sessionTab.attachToSession(debuggerSession, runner, environment);
128 if (reuseContent != null) {
129 final ProcessHandler prevHandler = reuseContent.getProcessHandler();
130 if (prevHandler != null) {
131 final DebuggerSessionTab prevSession = mySessionTabs.get(prevHandler);
132 if (prevSession != null) {
133 sessionTab.reuse(prevSession);
137 mySessionTabs.put(runContentDescriptor.getProcessHandler(), sessionTab);
138 return runContentDescriptor;
142 public void projectOpened() {
143 final RunContentManager contentManager = myExecutionManager.getContentManager();
144 LOG.assertTrue(contentManager != null, "Content manager is null");
146 final RunContentListener myContentListener = new RunContentListener() {
147 public void contentSelected(RunContentDescriptor descriptor) {
148 DebuggerSessionTab sessionTab = descriptor != null ? getSessionTab(descriptor.getProcessHandler()) : null;
150 if (sessionTab != null) {
151 getContextManager()
152 .setState(sessionTab.getContextManager().getContext(), sessionTab.getSession().getState(), DebuggerSession.EVENT_CONTEXT, null);
154 else {
155 getContextManager()
156 .setState(DebuggerContextImpl.EMPTY_CONTEXT, DebuggerSession.STATE_DISPOSED, DebuggerSession.EVENT_CONTEXT, null);
160 public void contentRemoved(RunContentDescriptor descriptor) {
161 DebuggerSessionTab sessionTab = getSessionTab(descriptor.getProcessHandler());
162 if (sessionTab != null) {
163 mySessionTabs.remove(descriptor.getProcessHandler());
164 Disposer.dispose(sessionTab);
169 contentManager.addRunContentListener(myContentListener, DefaultDebugExecutor.getDebugExecutorInstance());
170 Disposer.register(myProject, new Disposable() {
171 public void dispose() {
172 contentManager.removeRunContentListener(myContentListener);
177 public void projectClosed() {
180 @NotNull
181 public String getComponentName() {
182 return "DebuggerPanelsManager";
185 public void initComponent() {
188 public void disposeComponent() {
191 public static DebuggerPanelsManager getInstance(Project project) {
192 return project.getComponent(DebuggerPanelsManager.class);
195 @Nullable
196 public MainWatchPanel getWatchPanel() {
197 DebuggerSessionTab sessionTab = getSessionTab();
198 return sessionTab != null ? sessionTab.getWatchPanel() : null;
201 @Nullable
202 public DebuggerSessionTab getSessionTab() {
203 DebuggerContextImpl context = DebuggerManagerEx.getInstanceEx(myProject).getContext();
204 return getSessionTab(context.getDebuggerSession());
207 public void showFramePanel() {
208 DebuggerSessionTab sessionTab = getSessionTab();
209 if (sessionTab != null) {
210 sessionTab.showFramePanel();
214 public void toFront(DebuggerSession session) {
215 DebuggerSessionTab sessionTab = getSessionTab(session);
216 if (sessionTab != null) {
217 sessionTab.toFront();
221 private DebuggerSessionTab getSessionTab(ProcessHandler processHandler) {
222 return mySessionTabs.get(processHandler);
225 @Nullable
226 private DebuggerSessionTab getSessionTab(DebuggerSession session) {
227 return session != null ? getSessionTab(session.getProcess().getExecutionResult().getProcessHandler()) : null;