java debugger cleanup
[idea/community.git] / java / debugger / impl / src / com / intellij / debugger / ui / DebuggerPanelsManager.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.debugger.ui;
17
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;
48
49 public class DebuggerPanelsManager implements ProjectComponent {
50   private final Project myProject;
51
52   public DebuggerPanelsManager(Project project) {
53     myProject = project;
54   }
55
56   private DebuggerStateManager getContextManager() {
57     return DebuggerManagerEx.getInstanceEx(myProject).getContextManager();
58   }
59
60   @Nullable
61   @Deprecated
62   /**
63    * @deprecated to remove in IDEA 15
64    */
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)
73                                   .executor(executor)
74                                   .runner(runner)
75                                   .contentToReuse(reuseContent)
76                                   .build(), state, remoteConnection, pollConnection);
77   }
78
79   @Nullable
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));
85   }
86
87   @Nullable
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) {
92       return null;
93     }
94
95     final DebugProcessImpl debugProcess = debuggerSession.getProcess();
96     if (debugProcess.isDetached() || debugProcess.isDetaching()) {
97       debuggerSession.dispose();
98       return null;
99     }
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);
104     }
105
106     XDebugSession debugSession =
107       XDebuggerManager.getInstance(myProject).startSessionAndShowTab(modelEnvironment.getSessionName(), environment.getReuseContent(), new XDebugProcessStarter() {
108         @Override
109         @NotNull
110         public XDebugProcess start(@NotNull XDebugSession session) {
111           return JavaDebugProcess.create(session, debuggerSession);
112         }
113       });
114     return debugSession.getRunContentDescriptor();
115   }
116
117
118   @Override
119   public void projectOpened() {
120     myProject.getMessageBus().connect(myProject).subscribe(RunContentManager.TOPIC, new RunContentWithExecutorListener() {
121       @Override
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);
127           }
128           else {
129             getContextManager().setState(DebuggerContextImpl.EMPTY_CONTEXT, DebuggerSession.STATE_DISPOSED, DebuggerSession.EVENT_CONTEXT, null);
130           }
131         }
132       }
133
134       @Override
135       public void contentRemoved(@Nullable RunContentDescriptor descriptor, @NotNull Executor executor) {
136       }
137     });
138   }
139
140   @Override
141   public void projectClosed() {
142   }
143
144   @Override
145   @NotNull
146   public String getComponentName() {
147     return "DebuggerPanelsManager";
148   }
149
150   @Override
151   public void initComponent() {
152   }
153
154   @Override
155   public void disposeComponent() {
156   }
157
158   public static DebuggerPanelsManager getInstance(Project project) {
159     return project.getComponent(DebuggerPanelsManager.class);
160   }
161
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();
166       }
167     }
168     return null;
169   }
170 }