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.util.ContentsUtil;
28 import com.intellij.vcs.log.impl.VcsLogContentProvider;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
32 public class CloseLogTabAction extends CloseTabToolbarAction {
34 public void update(@NotNull AnActionEvent e) {
36 if (e.getProject() == null) {
37 e.getPresentation().setEnabledAndVisible(false);
40 ContentManager contentManager = getContentManager(e.getProject());
41 if (contentManager == null || getTabbedContent(contentManager) == null) {
42 e.getPresentation().setEnabledAndVisible(false);
45 e.getPresentation().setEnabledAndVisible(true);
48 public void actionPerformed(@NotNull AnActionEvent e) {
49 Project project = e.getProject();
50 assert project != null;
52 ContentManager contentManager = getContentManager(project);
53 if (contentManager == null) return;
54 Content selectedContent = getTabbedContent(contentManager);
55 if (selectedContent != null) {
56 ContentsUtil.closeContentTab(contentManager, selectedContent);
61 private static Content getTabbedContent(@NotNull ContentManager contentManager) {
62 Content content = contentManager.getSelectedContent();
63 if (content != null) {
64 if (ContentUtilEx.isContentTab(content, VcsLogContentProvider.TAB_NAME)) return content;
70 private static ContentManager getContentManager(@NotNull Project project) {
71 ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.VCS);
72 if (toolWindow == null) return null;
73 return toolWindow.getContentManager();