diff options
Diffstat (limited to 'platform/lang-impl/src/com/intellij/execution/runners/FakeRerunAction.java')
-rw-r--r-- | platform/lang-impl/src/com/intellij/execution/runners/FakeRerunAction.java | 116 |
1 files changed, 105 insertions, 11 deletions
diff --git a/platform/lang-impl/src/com/intellij/execution/runners/FakeRerunAction.java b/platform/lang-impl/src/com/intellij/execution/runners/FakeRerunAction.java index 45393c328e69..ecc8fa553121 100644 --- a/platform/lang-impl/src/com/intellij/execution/runners/FakeRerunAction.java +++ b/platform/lang-impl/src/com/intellij/execution/runners/FakeRerunAction.java @@ -15,33 +15,127 @@ */ package com.intellij.execution.runners; +import com.intellij.execution.ExecutionBundle; +import com.intellij.execution.ExecutorRegistry; +import com.intellij.execution.impl.ExecutionManagerImpl; +import com.intellij.execution.process.ProcessHandler; +import com.intellij.execution.ui.RunContentDescriptor; +import com.intellij.icons.AllIcons; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.LangDataKeys; import com.intellij.openapi.actionSystem.Presentation; import com.intellij.openapi.project.DumbAware; import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import javax.swing.*; +import java.awt.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; /** * @author Roman.Chernyatchik */ -public class FakeRerunAction extends AnAction implements DumbAware { - protected static final List<RestartAction> registry = ContainerUtil.createLockFreeCopyOnWriteList(); +class FakeRerunAction extends AnAction implements DumbAware { + @SuppressWarnings("deprecation") + static final List<RestartAction> registry = ContainerUtil.createLockFreeCopyOnWriteList(); @Override - public void actionPerformed(AnActionEvent e) { - RestartAction action = RestartAction.findActualAction(); - if (action != null && action.isEnabled()) { - action.actionPerformed(e); + public void update(AnActionEvent event) { + Presentation presentation = event.getPresentation(); + ExecutionEnvironment environment = getEnvironment(event); + if (environment != null) { + presentation.setText(ExecutionBundle.message("rerun.configuration.action.name", environment.getRunProfile().getName())); + presentation.setIcon(ExecutionManagerImpl.isProcessRunning(getDescriptor(event)) ? AllIcons.Actions.Restart : environment.getExecutor().getIcon()); + presentation.setEnabledAndVisible(isEnabled(event)); + return; } + + FakeRerunAction action = findActualAction(event); + presentation.setEnabled(action != null && action.isEnabled(event)); + presentation.setVisible(false); } @Override - public void update(AnActionEvent e) { - final Presentation presentation = e.getPresentation(); - RestartAction action = RestartAction.findActualAction(); - presentation.setEnabled(action != null && action.isEnabled()); - presentation.setVisible(false); + public void actionPerformed(AnActionEvent event) { + ExecutionEnvironment environment = getEnvironment(event); + if (environment != null) { + ExecutionUtil.restart(environment); + return; + } + + FakeRerunAction action = findActualAction(event); + if (action != null && action.isEnabled(event)) { + action.actionPerformed(event); + } + } + + @Nullable + protected RunContentDescriptor getDescriptor(AnActionEvent event) { + return event.getData(LangDataKeys.RUN_CONTENT_DESCRIPTOR); + } + + @Nullable + protected ExecutionEnvironment getEnvironment(AnActionEvent event) { + return event.getData(LangDataKeys.EXECUTION_ENVIRONMENT); + } + + protected boolean isEnabled(AnActionEvent event) { + RunContentDescriptor descriptor = getDescriptor(event); + ProcessHandler processHandler = descriptor == null ? null : descriptor.getProcessHandler(); + ExecutionEnvironment environment = getEnvironment(event); + return environment != null && + !ExecutorRegistry.getInstance().isStarting(environment) && + !(processHandler != null && processHandler.isProcessTerminating()); + } + + @Nullable + private JComponent getRunComponent(@NotNull AnActionEvent event) { + RunContentDescriptor descriptor = getDescriptor(event); + return descriptor == null ? null : descriptor.getComponent(); + } + + @Nullable + private static FakeRerunAction findActualAction(@NotNull final AnActionEvent event) { + if (registry.isEmpty()) { + return null; + } + + List<FakeRerunAction> candidates = new ArrayList<FakeRerunAction>(registry); + Collections.sort(candidates, new Comparator<FakeRerunAction>() { + @Override + public int compare(@NotNull FakeRerunAction action1, @NotNull FakeRerunAction action2) { + boolean isActive1 = action1.isEnabled(event); + if (isActive1 != action2.isEnabled(event)) { + return isActive1 ? -1 : 1; + } + + JComponent component1 = action1.getRunComponent(event); + JComponent component2 = action2.getRunComponent(event); + Window window1 = component1 == null ? null : SwingUtilities.windowForComponent(component1); + Window window2 = component2 == null ? null : SwingUtilities.windowForComponent(component2); + if (window1 == null) { + return 1; + } + if (window2 == null) { + return -1; + } + + boolean showing1 = component1.isShowing(); + boolean showing2 = component2.isShowing(); + if (showing1 && !showing2) { + return -1; + } + if (showing2 && !showing1) { + return 1; + } + return (window1.isActive() ? -1 : 1); + } + }); + return candidates.get(0); } } |