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.Content;
25 import com.intellij.ui.content.ContentManager;
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;
31 public class CloseLogTabAction extends CloseTabToolbarAction {
33 public void update(@NotNull AnActionEvent e) {
35 if (e.getProject() == null) {
36 e.getPresentation().setEnabledAndVisible(false);
39 ContentManager contentManager = getContentManager(e.getProject());
40 if (contentManager == null || getTabbedContent(contentManager) == null) {
41 e.getPresentation().setEnabledAndVisible(false);
44 e.getPresentation().setEnabledAndVisible(true);
47 public void actionPerformed(@NotNull AnActionEvent e) {
48 Project project = e.getProject();
49 assert project != null;
51 ContentManager contentManager = getContentManager(project);
52 if (contentManager == null) return;
53 Content selectedContent = getTabbedContent(contentManager);
54 if (selectedContent != null) {
55 ContentUtilEx.closeContentTab(contentManager, selectedContent);
60 private static Content getTabbedContent(@NotNull ContentManager contentManager) {
61 Content content = contentManager.getSelectedContent();
62 if (content != null) {
63 if (ContentUtilEx.isContentTab(content, VcsLogContentProvider.TAB_NAME)) return content;
69 private static ContentManager getContentManager(@NotNull Project project) {
70 ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.VCS);
71 if (toolWindow == null) return null;
72 return toolWindow.getContentManager();