RunDashboard: fix single selection actions visibility

This commit is contained in:
Konstantin Aleev
2019-02-26 16:14:15 +03:00
parent c7a53f545d
commit e5dda07deb
3 changed files with 31 additions and 7 deletions
@@ -25,7 +25,7 @@ public class CopyConfigurationAction extends AnAction {
@Override
public void update(@NotNull AnActionEvent e) {
Project project = e.getProject();
RunDashboardRunConfigurationNode node = project == null ? null : RunDashboardActionUtils.getTargets(e).single();
RunDashboardRunConfigurationNode node = project == null ? null : RunDashboardActionUtils.getTarget(e);
boolean enabled = node != null && RunDashboardManager.getInstance(project).isShowConfigurations() &&
RunManager.getInstance(node.getProject()).hasSettings(node.getConfigurationSettings());
e.getPresentation().setEnabled(enabled);
@@ -39,7 +39,7 @@ public class CopyConfigurationAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Project project = e.getProject();
RunDashboardRunConfigurationNode node = project == null ? null : RunDashboardActionUtils.getTargets(e).single();
RunDashboardRunConfigurationNode node = project == null ? null : RunDashboardActionUtils.getTarget(e);
if (node == null) return;
RunManager runManager = RunManager.getInstance(project);
@@ -29,7 +29,7 @@ public class EditConfigurationAction extends AnAction {
@Override
public void update(@NotNull AnActionEvent e) {
Project project = e.getProject();
RunDashboardRunConfigurationNode node = project == null ? null : RunDashboardActionUtils.getTargets(e).single();
RunDashboardRunConfigurationNode node = project == null ? null : RunDashboardActionUtils.getTarget(e);
boolean enabled = node != null && RunManager.getInstance(node.getProject()).hasSettings(node.getConfigurationSettings());
e.getPresentation().setEnabled(enabled);
boolean popupPlace = ActionPlaces.isPopupPlace(e.getPlace());
@@ -42,7 +42,7 @@ public class EditConfigurationAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Project project = e.getProject();
RunDashboardRunConfigurationNode node = project == null ? null : RunDashboardActionUtils.getTargets(e).single();
RunDashboardRunConfigurationNode node = project == null ? null : RunDashboardActionUtils.getTarget(e);
if (node == null) return;
RunDialog.editConfiguration(project, node.getConfigurationSettings(),
@@ -9,6 +9,10 @@ import com.intellij.util.containers.JBIterable;
import com.intellij.util.containers.JBTreeTraverser;
import com.intellij.util.containers.TreeTraversal;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
class RunDashboardActionUtils {
private RunDashboardActionUtils() {
@@ -19,18 +23,38 @@ class RunDashboardActionUtils {
return getTargets(e, RunDashboardRunConfigurationNode.class);
}
@Nullable
static RunDashboardRunConfigurationNode getTarget(@NotNull AnActionEvent e) {
Object[] items = e.getData(PlatformDataKeys.SELECTED_ITEMS);
if (items == null || items.length != 1 || !(items[0] instanceof RunDashboardRunConfigurationNode)) return null;
return (RunDashboardRunConfigurationNode)items[0];
}
@NotNull
static <T> JBIterable<T> getTargets(@NotNull AnActionEvent e, @NotNull Class<T> clazz) {
return JBIterable.of(e.getData(PlatformDataKeys.SELECTED_ITEMS)).filter(clazz);
Object[] items = e.getData(PlatformDataKeys.SELECTED_ITEMS);
if (items == null) return JBIterable.empty();
List<T> result = new ArrayList<>();
for (Object item : items) {
if (!clazz.isInstance(item)) {
return JBIterable.empty();
}
result.add(clazz.cast(item));
}
return JBIterable.from(result);
}
@NotNull
static JBIterable<RunDashboardRunConfigurationNode> getLeafTargets(@NotNull AnActionEvent e) {
JBIterable<Object> roots = JBIterable.of(e.getData(PlatformDataKeys.SELECTED_ITEMS));
return JBTreeTraverser.from(o -> o instanceof GroupingNode ? ((GroupingNode)o).getChildren() : null)
JBIterable<Object> leaves = JBTreeTraverser.from(o -> o instanceof GroupingNode ? ((GroupingNode)o).getChildren() : null)
.withRoots(roots)
.traverse(TreeTraversal.LEAVES_DFS)
.filter(RunDashboardRunConfigurationNode.class)
.unique();
if (leaves.filter(leaf -> !(leaf instanceof RunDashboardRunConfigurationNode)).isNotEmpty()) return JBIterable.empty();
return leaves.filter(RunDashboardRunConfigurationNode.class);
}
}