2 * Copyright 2000-2014 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.DebugEnvironment;
19 import com.intellij.debugger.DebugUIEnvironment;
20 import com.intellij.debugger.DebuggerManagerEx;
21 import com.intellij.debugger.DefaultDebugUIEnvironment;
22 import com.intellij.debugger.engine.DebugProcessImpl;
23 import com.intellij.debugger.engine.JavaDebugProcess;
24 import com.intellij.debugger.impl.DebuggerContextImpl;
25 import com.intellij.debugger.impl.DebuggerSession;
26 import com.intellij.debugger.impl.DebuggerStateManager;
27 import com.intellij.debugger.ui.impl.MainWatchPanel;
28 import com.intellij.debugger.ui.tree.render.BatchEvaluator;
29 import com.intellij.execution.ExecutionException;
30 import com.intellij.execution.ExecutionManager;
31 import com.intellij.execution.Executor;
32 import com.intellij.execution.configurations.RemoteConnection;
33 import com.intellij.execution.configurations.RunProfileState;
34 import com.intellij.execution.executors.DefaultDebugExecutor;
35 import com.intellij.execution.runners.ExecutionEnvironment;
36 import com.intellij.execution.runners.ExecutionEnvironmentBuilder;
37 import com.intellij.execution.runners.ProgramRunner;
38 import com.intellij.execution.ui.RunContentDescriptor;
39 import com.intellij.execution.ui.RunContentManager;
40 import com.intellij.execution.ui.RunContentWithExecutorListener;
41 import com.intellij.openapi.components.ProjectComponent;
42 import com.intellij.openapi.diagnostic.Logger;
43 import com.intellij.openapi.editor.colors.EditorColorsManager;
44 import com.intellij.openapi.project.Project;
45 import com.intellij.xdebugger.XDebugProcess;
46 import com.intellij.xdebugger.XDebugProcessStarter;
47 import com.intellij.xdebugger.XDebugSession;
48 import com.intellij.xdebugger.XDebuggerManager;
49 import org.jetbrains.annotations.NotNull;
50 import org.jetbrains.annotations.Nullable;
52 public class DebuggerPanelsManager implements ProjectComponent {
53 private static final Logger LOG = Logger.getInstance("#com.intellij.debugger.ui.DebuggerPanelsManager");
55 private final Project myProject;
56 private final ExecutionManager myExecutionManager;
58 //private final PositionHighlighter myEditorManager;
59 //private final HashMap<ProcessHandler, DebuggerSessionTab> mySessionTabs = new HashMap<ProcessHandler, DebuggerSessionTab>();
61 public DebuggerPanelsManager(Project project, final EditorColorsManager colorsManager, ExecutionManager executionManager) {
63 myExecutionManager = executionManager;
65 //myEditorManager = new PositionHighlighter(myProject, getContextManager());
67 //final EditorColorsListener myColorsListener = new EditorColorsListener() {
68 // public void globalSchemeChange(EditorColorsScheme scheme) {
69 // myEditorManager.updateContextPointDescription();
72 //colorsManager.addEditorColorsListener(myColorsListener);
73 //Disposer.register(project, new Disposable() {
74 // public void dispose() {
75 // colorsManager.removeEditorColorsListener(myColorsListener);
79 //getContextManager().addListener(new DebuggerContextListener() {
80 // public void changeEvent(final DebuggerContextImpl newContext, int event) {
81 // if (event == DebuggerSession.EVENT_PAUSE) {
82 // DebuggerInvocationUtil.invokeLater(myProject, new Runnable() {
83 // public void run() {
84 // toFront(newContext.getDebuggerSession());
92 private DebuggerStateManager getContextManager() {
93 return DebuggerManagerEx.getInstanceEx(myProject).getContextManager();
99 * @deprecated to remove in IDEA 15
101 public RunContentDescriptor attachVirtualMachine(Executor executor,
102 @NotNull ProgramRunner runner,
103 @NotNull ExecutionEnvironment environment,
104 RunProfileState state,
105 RunContentDescriptor reuseContent,
106 RemoteConnection remoteConnection,
107 boolean pollConnection) throws ExecutionException {
108 return attachVirtualMachine(new ExecutionEnvironmentBuilder(environment)
111 .contentToReuse(reuseContent)
112 .build(), state, remoteConnection, pollConnection);
116 public RunContentDescriptor attachVirtualMachine(@NotNull ExecutionEnvironment environment,
117 RunProfileState state,
118 RemoteConnection remoteConnection,
119 boolean pollConnection) throws ExecutionException {
120 return attachVirtualMachine(new DefaultDebugUIEnvironment(environment, state, remoteConnection, pollConnection));
124 public RunContentDescriptor attachVirtualMachine(DebugUIEnvironment environment) throws ExecutionException {
125 final DebugEnvironment modelEnvironment = environment.getEnvironment();
126 final DebuggerSession debuggerSession = DebuggerManagerEx.getInstanceEx(myProject).attachVirtualMachine(modelEnvironment);
127 if (debuggerSession == null) {
131 final DebugProcessImpl debugProcess = debuggerSession.getProcess();
132 if (debugProcess.isDetached() || debugProcess.isDetaching()) {
133 debuggerSession.dispose();
136 if (modelEnvironment.isRemote()) {
137 // optimization: that way BatchEvaluator will not try to lookup the class file in remote VM
138 // which is an expensive operation when executed first time
139 debugProcess.putUserData(BatchEvaluator.REMOTE_SESSION_KEY, Boolean.TRUE);
142 XDebugSession debugSession =
143 XDebuggerManager.getInstance(myProject).startSessionAndShowTab(modelEnvironment.getSessionName(), environment.getReuseContent(), new XDebugProcessStarter() {
146 public XDebugProcess start(@NotNull XDebugSession session) {
147 return JavaDebugProcess.create(session, debuggerSession);
150 return debugSession.getRunContentDescriptor();
155 public void projectOpened() {
156 myProject.getMessageBus().connect(myProject).subscribe(RunContentManager.TOPIC, new RunContentWithExecutorListener() {
158 public void contentSelected(@Nullable RunContentDescriptor descriptor, @NotNull Executor executor) {
159 if (executor == DefaultDebugExecutor.getDebugExecutorInstance()) {
160 DebuggerSession session = descriptor == null ? null : getSession(myProject, descriptor);
161 if (session != null) {
162 getContextManager().setState(session.getContextManager().getContext(), session.getState(), DebuggerSession.EVENT_CONTEXT, null);
165 getContextManager().setState(DebuggerContextImpl.EMPTY_CONTEXT, DebuggerSession.STATE_DISPOSED, DebuggerSession.EVENT_CONTEXT, null);
171 public void contentRemoved(@Nullable RunContentDescriptor descriptor, @NotNull Executor executor) {
177 public void projectClosed() {
182 public String getComponentName() {
183 return "DebuggerPanelsManager";
187 public void initComponent() {
191 public void disposeComponent() {
194 public static DebuggerPanelsManager getInstance(Project project) {
195 return project.getComponent(DebuggerPanelsManager.class);
199 public MainWatchPanel getWatchPanel() {
200 DebuggerSessionTab sessionTab = getSessionTab();
201 return sessionTab != null ? sessionTab.getWatchPanel() : null;
205 public DebuggerSessionTab getSessionTab() {
206 DebuggerContextImpl context = DebuggerManagerEx.getInstanceEx(myProject).getContext();
207 return getSessionTab(context.getDebuggerSession());
210 public void showFramePanel() {
211 DebuggerSessionTab sessionTab = getSessionTab();
212 if (sessionTab != null) {
213 sessionTab.showFramePanel();
217 public void toFront(DebuggerSession session) {
218 DebuggerSessionTab sessionTab = getSessionTab(session);
219 if (sessionTab != null) {
220 sessionTab.toFront(true, null);
224 private static DebuggerSession getSession(Project project, RunContentDescriptor descriptor) {
225 for (XDebugSession session : XDebuggerManager.getInstance(project).getDebugSessions()) {
226 if (session.getRunContentDescriptor().equals(descriptor)) {
227 XDebugProcess process = session.getDebugProcess();
228 if (process instanceof JavaDebugProcess) {
229 return ((JavaDebugProcess)process).getDebuggerSession();
237 private DebuggerSessionTab getSessionTab(DebuggerSession session) {
238 //return session != null ? getSessionTab(session.getProcess().getExecutionResult().getProcessHandler()) : null;