2 * Copyright 2000-2015 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.tree.render.BatchEvaluator;
28 import com.intellij.execution.ExecutionException;
29 import com.intellij.execution.Executor;
30 import com.intellij.execution.configurations.RemoteConnection;
31 import com.intellij.execution.configurations.RunProfileState;
32 import com.intellij.execution.executors.DefaultDebugExecutor;
33 import com.intellij.execution.runners.ExecutionEnvironment;
34 import com.intellij.execution.runners.ExecutionEnvironmentBuilder;
35 import com.intellij.execution.runners.ProgramRunner;
36 import com.intellij.execution.ui.RunContentDescriptor;
37 import com.intellij.execution.ui.RunContentManager;
38 import com.intellij.execution.ui.RunContentWithExecutorListener;
39 import com.intellij.openapi.components.ProjectComponent;
40 import com.intellij.openapi.project.Project;
41 import com.intellij.openapi.util.Comparing;
42 import com.intellij.xdebugger.XDebugProcess;
43 import com.intellij.xdebugger.XDebugProcessStarter;
44 import com.intellij.xdebugger.XDebugSession;
45 import com.intellij.xdebugger.XDebuggerManager;
46 import org.jetbrains.annotations.NotNull;
47 import org.jetbrains.annotations.Nullable;
49 public class DebuggerPanelsManager implements ProjectComponent {
50 private final Project myProject;
52 public DebuggerPanelsManager(Project project) {
56 private DebuggerStateManager getContextManager() {
57 return DebuggerManagerEx.getInstanceEx(myProject).getContextManager();
63 * @deprecated to remove in IDEA 15
65 public RunContentDescriptor attachVirtualMachine(Executor executor,
66 @NotNull ProgramRunner runner,
67 @NotNull ExecutionEnvironment environment,
68 RunProfileState state,
69 RunContentDescriptor reuseContent,
70 RemoteConnection remoteConnection,
71 boolean pollConnection) throws ExecutionException {
72 return attachVirtualMachine(new ExecutionEnvironmentBuilder(environment)
75 .contentToReuse(reuseContent)
76 .build(), state, remoteConnection, pollConnection);
80 public RunContentDescriptor attachVirtualMachine(@NotNull ExecutionEnvironment environment,
81 RunProfileState state,
82 RemoteConnection remoteConnection,
83 boolean pollConnection) throws ExecutionException {
84 return attachVirtualMachine(new DefaultDebugUIEnvironment(environment, state, remoteConnection, pollConnection));
88 public RunContentDescriptor attachVirtualMachine(DebugUIEnvironment environment) throws ExecutionException {
89 final DebugEnvironment modelEnvironment = environment.getEnvironment();
90 final DebuggerSession debuggerSession = DebuggerManagerEx.getInstanceEx(myProject).attachVirtualMachine(modelEnvironment);
91 if (debuggerSession == null) {
95 final DebugProcessImpl debugProcess = debuggerSession.getProcess();
96 if (debugProcess.isDetached() || debugProcess.isDetaching()) {
97 debuggerSession.dispose();
100 if (modelEnvironment.isRemote()) {
101 // optimization: that way BatchEvaluator will not try to lookup the class file in remote VM
102 // which is an expensive operation when executed first time
103 debugProcess.putUserData(BatchEvaluator.REMOTE_SESSION_KEY, Boolean.TRUE);
106 XDebugSession debugSession =
107 XDebuggerManager.getInstance(myProject).startSessionAndShowTab(modelEnvironment.getSessionName(), environment.getReuseContent(), new XDebugProcessStarter() {
110 public XDebugProcess start(@NotNull XDebugSession session) {
111 return JavaDebugProcess.create(session, debuggerSession);
114 return debugSession.getRunContentDescriptor();
119 public void projectOpened() {
120 myProject.getMessageBus().connect(myProject).subscribe(RunContentManager.TOPIC, new RunContentWithExecutorListener() {
122 public void contentSelected(@Nullable RunContentDescriptor descriptor, @NotNull Executor executor) {
123 if (executor == DefaultDebugExecutor.getDebugExecutorInstance()) {
124 DebuggerSession session = descriptor == null ? null : getSession(myProject, descriptor);
125 if (session != null) {
126 getContextManager().setState(session.getContextManager().getContext(), session.getState(), DebuggerSession.EVENT_CONTEXT, null);
129 getContextManager().setState(DebuggerContextImpl.EMPTY_CONTEXT, DebuggerSession.STATE_DISPOSED, DebuggerSession.EVENT_CONTEXT, null);
135 public void contentRemoved(@Nullable RunContentDescriptor descriptor, @NotNull Executor executor) {
141 public void projectClosed() {
146 public String getComponentName() {
147 return "DebuggerPanelsManager";
151 public void initComponent() {
155 public void disposeComponent() {
158 public static DebuggerPanelsManager getInstance(Project project) {
159 return project.getComponent(DebuggerPanelsManager.class);
162 private static DebuggerSession getSession(Project project, RunContentDescriptor descriptor) {
163 for (JavaDebugProcess process : XDebuggerManager.getInstance(project).getDebugProcesses(JavaDebugProcess.class)) {
164 if (Comparing.equal(process.getProcessHandler(), descriptor.getProcessHandler())) {
165 return process.getDebuggerSession();