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.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.openapi.util.Comparing;
46 import com.intellij.xdebugger.XDebugProcess;
47 import com.intellij.xdebugger.XDebugProcessStarter;
48 import com.intellij.xdebugger.XDebugSession;
49 import com.intellij.xdebugger.XDebuggerManager;
50 import org.jetbrains.annotations.NotNull;
51 import org.jetbrains.annotations.Nullable;
53 public class DebuggerPanelsManager implements ProjectComponent {
54 private static final Logger LOG = Logger.getInstance("#com.intellij.debugger.ui.DebuggerPanelsManager");
56 private final Project myProject;
57 private final ExecutionManager myExecutionManager;
59 //private final PositionHighlighter myEditorManager;
60 //private final HashMap<ProcessHandler, DebuggerSessionTab> mySessionTabs = new HashMap<ProcessHandler, DebuggerSessionTab>();
62 public DebuggerPanelsManager(Project project, final EditorColorsManager colorsManager, ExecutionManager executionManager) {
64 myExecutionManager = executionManager;
66 //myEditorManager = new PositionHighlighter(myProject, getContextManager());
68 //final EditorColorsListener myColorsListener = new EditorColorsListener() {
69 // public void globalSchemeChange(EditorColorsScheme scheme) {
70 // myEditorManager.updateContextPointDescription();
73 //colorsManager.addEditorColorsListener(myColorsListener);
74 //Disposer.register(project, new Disposable() {
75 // public void dispose() {
76 // colorsManager.removeEditorColorsListener(myColorsListener);
80 //getContextManager().addListener(new DebuggerContextListener() {
81 // public void changeEvent(final DebuggerContextImpl newContext, int event) {
82 // if (event == DebuggerSession.EVENT_PAUSE) {
83 // DebuggerInvocationUtil.invokeLater(myProject, new Runnable() {
84 // public void run() {
85 // toFront(newContext.getDebuggerSession());
93 private DebuggerStateManager getContextManager() {
94 return DebuggerManagerEx.getInstanceEx(myProject).getContextManager();
100 * @deprecated to remove in IDEA 15
102 public RunContentDescriptor attachVirtualMachine(Executor executor,
103 @NotNull ProgramRunner runner,
104 @NotNull ExecutionEnvironment environment,
105 RunProfileState state,
106 RunContentDescriptor reuseContent,
107 RemoteConnection remoteConnection,
108 boolean pollConnection) throws ExecutionException {
109 return attachVirtualMachine(new ExecutionEnvironmentBuilder(environment)
112 .contentToReuse(reuseContent)
113 .build(), state, remoteConnection, pollConnection);
117 public RunContentDescriptor attachVirtualMachine(@NotNull ExecutionEnvironment environment,
118 RunProfileState state,
119 RemoteConnection remoteConnection,
120 boolean pollConnection) throws ExecutionException {
121 return attachVirtualMachine(new DefaultDebugUIEnvironment(environment, state, remoteConnection, pollConnection));
125 public RunContentDescriptor attachVirtualMachine(DebugUIEnvironment environment) throws ExecutionException {
126 final DebugEnvironment modelEnvironment = environment.getEnvironment();
127 final DebuggerSession debuggerSession = DebuggerManagerEx.getInstanceEx(myProject).attachVirtualMachine(modelEnvironment);
128 if (debuggerSession == null) {
132 final DebugProcessImpl debugProcess = debuggerSession.getProcess();
133 if (debugProcess.isDetached() || debugProcess.isDetaching()) {
134 debuggerSession.dispose();
137 if (modelEnvironment.isRemote()) {
138 // optimization: that way BatchEvaluator will not try to lookup the class file in remote VM
139 // which is an expensive operation when executed first time
140 debugProcess.putUserData(BatchEvaluator.REMOTE_SESSION_KEY, Boolean.TRUE);
143 XDebugSession debugSession =
144 XDebuggerManager.getInstance(myProject).startSessionAndShowTab(modelEnvironment.getSessionName(), environment.getReuseContent(), new XDebugProcessStarter() {
147 public XDebugProcess start(@NotNull XDebugSession session) {
148 return JavaDebugProcess.create(session, debuggerSession);
151 return debugSession.getRunContentDescriptor();
156 public void projectOpened() {
157 myProject.getMessageBus().connect(myProject).subscribe(RunContentManager.TOPIC, new RunContentWithExecutorListener() {
159 public void contentSelected(@Nullable RunContentDescriptor descriptor, @NotNull Executor executor) {
160 if (executor == DefaultDebugExecutor.getDebugExecutorInstance()) {
161 DebuggerSession session = descriptor == null ? null : getSession(myProject, descriptor);
162 if (session != null) {
163 getContextManager().setState(session.getContextManager().getContext(), session.getState(), DebuggerSession.EVENT_CONTEXT, null);
166 getContextManager().setState(DebuggerContextImpl.EMPTY_CONTEXT, DebuggerSession.STATE_DISPOSED, DebuggerSession.EVENT_CONTEXT, null);
172 public void contentRemoved(@Nullable RunContentDescriptor descriptor, @NotNull Executor executor) {
178 public void projectClosed() {
183 public String getComponentName() {
184 return "DebuggerPanelsManager";
188 public void initComponent() {
192 public void disposeComponent() {
195 public static DebuggerPanelsManager getInstance(Project project) {
196 return project.getComponent(DebuggerPanelsManager.class);
200 public MainWatchPanel getWatchPanel() {
201 DebuggerSessionTab sessionTab = getSessionTab();
202 return sessionTab != null ? sessionTab.getWatchPanel() : null;
206 public DebuggerSessionTab getSessionTab() {
207 DebuggerContextImpl context = DebuggerManagerEx.getInstanceEx(myProject).getContext();
208 return getSessionTab(context.getDebuggerSession());
211 public void showFramePanel() {
212 DebuggerSessionTab sessionTab = getSessionTab();
213 if (sessionTab != null) {
214 sessionTab.showFramePanel();
218 public void toFront(DebuggerSession session) {
219 DebuggerSessionTab sessionTab = getSessionTab(session);
220 if (sessionTab != null) {
221 sessionTab.toFront(true, null);
225 private static DebuggerSession getSession(Project project, RunContentDescriptor descriptor) {
226 for (JavaDebugProcess process : XDebuggerManager.getInstance(project).getDebugProcesses(JavaDebugProcess.class)) {
227 if (Comparing.equal(process.getProcessHandler(), descriptor.getProcessHandler())) {
228 return process.getDebuggerSession();
235 private DebuggerSessionTab getSessionTab(DebuggerSession session) {
236 //return session != null ? getSessionTab(session.getProcess().getExecutionResult().getProcessHandler()) : null;