get ToolWindow and Content instances from context DataProvider (IDEA-200294)

This commit is contained in:
Sergey Simonchik
2018-10-10 17:53:28 +03:00
parent 072f3a1f11
commit 52046ae37f
2 changed files with 47 additions and 32 deletions
@@ -1,32 +0,0 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.wm;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.ui.content.Content;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class ToolWindowContextMenuActionBase extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
final ToolWindow toolWindow = ToolWindowManager.getActiveToolWindow();
if (toolWindow == null)
return;
Content selectedContent = toolWindow.getContentManager().getSelectedContent();
actionPerformed(e, toolWindow, selectedContent);
}
@Override
public void update(@NotNull AnActionEvent e) {
super.update(e);
final ToolWindow toolWindow = ToolWindowManager.getActiveToolWindow();
if (toolWindow == null)
return;
Content selectedContent = toolWindow.getContentManager().getSelectedContent();
update(e, toolWindow, selectedContent);
}
public abstract void update(@NotNull AnActionEvent e, @NotNull ToolWindow activeToolWindow, @Nullable Content selectedContent);
public abstract void actionPerformed(@NotNull AnActionEvent e, @NotNull ToolWindow activeToolWindow, @Nullable Content selectedContent);
}
@@ -0,0 +1,47 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.wm;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.wm.impl.content.BaseLabel;
import com.intellij.ui.content.Content;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class ToolWindowContextMenuActionBase extends AnAction {
@Override
public final void actionPerformed(@NotNull AnActionEvent e) {
ToolWindow toolWindow = e.getDataContext().getData(PlatformDataKeys.TOOL_WINDOW);
if (toolWindow == null) {
return;
}
Content content = getContextContent(e, toolWindow);
actionPerformed(e, toolWindow, content);
}
@Override
public final void update(@NotNull AnActionEvent e) {
ToolWindow toolWindow = e.getDataContext().getData(PlatformDataKeys.TOOL_WINDOW);
if (toolWindow == null) {
e.getPresentation().setEnabledAndVisible(false);
return;
}
Content content = getContextContent(e, toolWindow);
update(e, toolWindow, content);
}
public abstract void update(@NotNull AnActionEvent e, @NotNull ToolWindow toolWindow, @Nullable Content content);
public abstract void actionPerformed(@NotNull AnActionEvent e, @NotNull ToolWindow toolWindow, @Nullable Content content);
@Nullable
private static Content getContextContent(@NotNull AnActionEvent e, @NotNull ToolWindow toolWindow) {
BaseLabel baseLabel = ObjectUtils.tryCast(e.getData(PlatformDataKeys.CONTEXT_COMPONENT), BaseLabel.class);
Content selectedContent = baseLabel != null ? baseLabel.getContent() : null;
if (selectedContent == null) {
selectedContent = toolWindow.getContentManager().getSelectedContent();
}
return selectedContent;
}
}