2 * Copyright 2000-2016 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.vcs.log.ui.actions;
18 import com.intellij.ide.actions.CloseTabToolbarAction;
19 import com.intellij.openapi.actionSystem.AnActionEvent;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.wm.ToolWindow;
22 import com.intellij.openapi.wm.ToolWindowId;
23 import com.intellij.openapi.wm.ToolWindowManager;
24 import com.intellij.ui.content.ContentManager;
25 import com.intellij.ui.content.TabbedContent;
26 import com.intellij.util.ContentUtilEx;
27 import com.intellij.vcs.log.impl.VcsLogContentProvider;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
33 public class CloseLogTabAction extends CloseTabToolbarAction {
35 public void update(@NotNull AnActionEvent e) {
37 if (e.getProject() == null) {
38 e.getPresentation().setEnabledAndVisible(false);
41 ContentManager contentManager = getContentManager(e.getProject());
42 if (contentManager == null || getTabbedContent(contentManager) == null) {
43 e.getPresentation().setEnabledAndVisible(false);
46 e.getPresentation().setEnabledAndVisible(true);
49 public void actionPerformed(@NotNull AnActionEvent e) {
50 Project project = e.getProject();
51 assert project != null;
53 ContentManager contentManager = getContentManager(project);
54 if (contentManager == null) return;
55 TabbedContent tabbedContent = getTabbedContent(contentManager);
56 if (tabbedContent == null) return;
58 if (tabbedContent.getTabs().size() > 1) {
59 JComponent component = tabbedContent.getComponent();
60 tabbedContent.removeContent(component);
61 contentManager.setSelectedContent(tabbedContent, true, true);
64 contentManager.removeContent(tabbedContent, true);
69 private static TabbedContent getTabbedContent(@NotNull ContentManager contentManager) {
70 TabbedContent tabbedContent = ContentUtilEx.findTabbedContent(contentManager, VcsLogContentProvider.TAB_NAME);
71 if (tabbedContent != contentManager.getSelectedContent()) return null;
76 private static ContentManager getContentManager(@NotNull Project project) {
77 ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.VCS);
78 if (toolWindow == null) return null;
79 return toolWindow.getContentManager();