EA-71665 - IAE: ContentManagerImpl.setSelectedContent
[idea/community.git] / platform / xdebugger-impl / src / com / intellij / xdebugger / impl / ui / DebuggerSessionTabBase.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.xdebugger.impl.ui;
17
18 import com.intellij.debugger.ui.DebuggerContentInfo;
19 import com.intellij.execution.ExecutionManager;
20 import com.intellij.execution.configurations.RunConfigurationBase;
21 import com.intellij.execution.configurations.RunProfile;
22 import com.intellij.execution.executors.DefaultDebugExecutor;
23 import com.intellij.execution.runners.RunContentBuilder;
24 import com.intellij.execution.runners.RunTab;
25 import com.intellij.execution.ui.ConsoleViewContentType;
26 import com.intellij.execution.ui.ExecutionConsole;
27 import com.intellij.execution.ui.ObservableConsoleView;
28 import com.intellij.execution.ui.layout.LayoutAttractionPolicy;
29 import com.intellij.execution.ui.layout.LayoutViewOptions;
30 import com.intellij.ide.impl.ProjectUtil;
31 import com.intellij.ide.ui.customization.CustomActionsSchema;
32 import com.intellij.openapi.actionSystem.ActionGroup;
33 import com.intellij.openapi.application.ApplicationManager;
34 import com.intellij.openapi.project.Project;
35 import com.intellij.openapi.util.registry.Registry;
36 import com.intellij.openapi.wm.ToolWindow;
37 import com.intellij.psi.search.GlobalSearchScope;
38 import com.intellij.ui.AppIcon;
39 import com.intellij.ui.content.Content;
40 import com.intellij.ui.content.ContentManager;
41 import com.intellij.util.ArrayUtil;
42 import com.intellij.util.ui.UIUtil;
43 import com.intellij.xdebugger.XDebuggerBundle;
44 import org.jetbrains.annotations.NotNull;
45 import org.jetbrains.annotations.Nullable;
46
47 import java.util.Collection;
48
49 /**
50  * @author nik
51  */
52 public abstract class DebuggerSessionTabBase extends RunTab {
53   protected ExecutionConsole myConsole;
54
55   public DebuggerSessionTabBase(@NotNull Project project, @NotNull String runnerId, @NotNull String sessionName, @NotNull GlobalSearchScope searchScope) {
56     super(project, searchScope, runnerId, XDebuggerBundle.message("xdebugger.default.content.title"), sessionName);
57
58     myUi.getDefaults()
59       .initTabDefaults(0, XDebuggerBundle.message("xdebugger.debugger.tab.title"), null)
60       .initFocusContent(DebuggerContentInfo.FRAME_CONTENT, XDebuggerUIConstants.LAYOUT_VIEW_BREAKPOINT_CONDITION)
61       .initFocusContent(DebuggerContentInfo.CONSOLE_CONTENT, LayoutViewOptions.STARTUP, new LayoutAttractionPolicy.FocusOnce(false));
62   }
63
64   protected static ActionGroup getCustomizedActionGroup(final String id) {
65     return (ActionGroup)CustomActionsSchema.getInstance().getCorrectedAction(id);
66   }
67
68   protected void attachNotificationTo(final Content content) {
69     if (myConsole instanceof ObservableConsoleView) {
70       ObservableConsoleView observable = (ObservableConsoleView)myConsole;
71       observable.addChangeListener(new ObservableConsoleView.ChangeListener() {
72         @Override
73         public void contentAdded(final Collection<ConsoleViewContentType> types) {
74           if (types.contains(ConsoleViewContentType.ERROR_OUTPUT) || types.contains(ConsoleViewContentType.NORMAL_OUTPUT)) {
75             content.fireAlert();
76           }
77         }
78       }, content);
79       RunProfile profile = getRunProfile();
80       if (profile instanceof RunConfigurationBase && !ApplicationManager.getApplication().isUnitTestMode()) {
81         observable.addChangeListener(new RunContentBuilder.ConsoleToFrontListener((RunConfigurationBase)profile,
82                                                                                   myProject,
83                                                                                   DefaultDebugExecutor.getDebugExecutorInstance(),
84                                                                                   myRunContentDescriptor,
85                                                                                   myUi),
86                                      content);
87       }
88     }
89   }
90
91   @Nullable
92   protected RunProfile getRunProfile() {
93     return myEnvironment != null ? myEnvironment.getRunProfile() : null;
94   }
95
96
97   public void select() {
98     if (ApplicationManager.getApplication().isUnitTestMode()) return;
99
100     UIUtil.invokeLaterIfNeeded(new Runnable() {
101       @Override
102       public void run() {
103         if (myRunContentDescriptor != null) {
104           ToolWindow toolWindow = ExecutionManager.getInstance(myProject).getContentManager()
105             .getToolWindowByDescriptor(myRunContentDescriptor);
106           Content content = myRunContentDescriptor.getAttachedContent();
107           if (toolWindow == null || content == null) return;
108           ContentManager manager = toolWindow.getContentManager();
109           if (ArrayUtil.contains(content, manager.getContents()) && !manager.isSelected(content)) {
110             manager.setSelectedContent(content);
111           }
112         }
113       }
114     });
115   }
116
117   public void toFront(boolean focus, @Nullable final Runnable onShowCallback) {
118     if (ApplicationManager.getApplication().isUnitTestMode()) return;
119
120     ApplicationManager.getApplication().invokeLater(new Runnable() {
121       @Override
122       public void run() {
123         if (myRunContentDescriptor != null) {
124           ToolWindow toolWindow = ExecutionManager.getInstance(myProject).getContentManager()
125             .getToolWindowByDescriptor(myRunContentDescriptor);
126           if (toolWindow != null) {
127             if (!toolWindow.isVisible()) {
128               toolWindow.show(onShowCallback);
129             }
130             //noinspection ConstantConditions
131             toolWindow.getContentManager().setSelectedContent(myRunContentDescriptor.getAttachedContent());
132           }
133         }
134       }
135     });
136
137     if (focus) {
138       ApplicationManager.getApplication().invokeLater(new Runnable() {
139         @Override
140         public void run() {
141           boolean focusWnd = Registry.is("debugger.mayBringFrameToFrontOnBreakpoint");
142           ProjectUtil.focusProjectWindow(myProject, focusWnd);
143           if (!focusWnd) {
144             AppIcon.getInstance().requestAttention(myProject, true);
145           }
146         }
147       });
148     }
149   }
150 }