get rid of RunContentDescriptor.getRestarter()
authorVladimir Krivosheev <vladimir.krivosheev@jetbrains.com>
Wed, 13 Aug 2014 12:39:57 +0000 (14:39 +0200)
committerVladimir Krivosheev <vladimir.krivosheev@jetbrains.com>
Wed, 13 Aug 2014 12:41:11 +0000 (14:41 +0200)
platform/lang-api/src/com/intellij/execution/runners/ExecutionUtil.java
platform/lang-api/src/com/intellij/execution/ui/RunContentDescriptor.java
platform/lang-api/src/com/intellij/execution/ui/RunContentManager.java
platform/lang-impl/src/com/intellij/execution/impl/ExecutionManagerImpl.java
platform/lang-impl/src/com/intellij/execution/runners/RerunTestsAction.java
platform/lang-impl/src/com/intellij/execution/runners/RunTab.java
platform/lang-impl/src/com/intellij/execution/ui/RunContentManagerImpl.java
platform/testRunner/src/com/intellij/execution/testframework/autotest/AutoTestManager.java
platform/testRunner/src/com/intellij/execution/testframework/autotest/ToggleAutoTestAction.java

index 10640776ba9ae70d17e2bdc2e8db0510eb9706ed..9ff501f7715a4ce54c94301a4ba9acb502b55554 100644 (file)
@@ -18,21 +18,27 @@ package com.intellij.execution.runners;
 
 import com.intellij.execution.*;
 import com.intellij.execution.configurations.RunProfile;
+import com.intellij.execution.process.ProcessHandler;
 import com.intellij.execution.process.ProcessNotCreatedException;
+import com.intellij.execution.ui.RunContentDescriptor;
+import com.intellij.ide.DataManager;
 import com.intellij.ide.util.PropertiesComponent;
 import com.intellij.notification.NotificationGroup;
 import com.intellij.notification.NotificationListener;
 import com.intellij.notification.NotificationType;
+import com.intellij.openapi.actionSystem.LangDataKeys;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.MessageType;
 import com.intellij.openapi.wm.ToolWindowManager;
+import com.intellij.ui.content.Content;
 import com.intellij.util.ObjectUtils;
 import com.intellij.util.ui.UIUtil;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
+import javax.swing.*;
 import javax.swing.event.HyperlinkEvent;
 import javax.swing.event.HyperlinkListener;
 
@@ -62,15 +68,15 @@ public class ExecutionUtil {
     if (e instanceof RunCanceledByUserException) return;
 
     LOG.debug(e);
-    
+
     String description = e.getMessage();
     HyperlinkListener listener = null;
-    
+
     if (description == null) {
       LOG.warn("Execution error without description", e);
       description = "Unknown error";
     }
-    
+
     if ((description.contains("87") || description.contains("111") || description.contains("206")) &&
         e instanceof ProcessNotCreatedException &&
         !PropertiesComponent.getInstance(project).isTrueValue("dynamic.classpath")) {
@@ -111,6 +117,33 @@ public class ExecutionUtil {
     });
   }
 
+  public static void restartIfActive(@NotNull RunContentDescriptor descriptor) {
+    ProcessHandler processHandler = descriptor.getProcessHandler();
+    if (processHandler != null
+        && processHandler.isStartNotified()
+        && !processHandler.isProcessTerminating()
+        && !processHandler.isProcessTerminated()) {
+      restart(descriptor);
+    }
+  }
+
+  public static void restart(@NotNull RunContentDescriptor descriptor) {
+    restart(descriptor.getComponent());
+  }
+
+  public static void restart(@NotNull Content content) {
+    restart(content.getComponent());
+  }
+
+  private static void restart(@Nullable JComponent component) {
+    if (component != null) {
+      ExecutionEnvironment environment = LangDataKeys.EXECUTION_ENVIRONMENT.getData(DataManager.getInstance().getDataContext(component));
+      if (environment != null) {
+        restart(environment);
+      }
+    }
+  }
+
   public static void restart(@NotNull ExecutionEnvironment environment) {
     if (!ExecutorRegistry.getInstance().isStarting(environment)) {
       ExecutionManager.getInstance(environment.getProject()).restartRunProfile(environment);
index 916f353cde78df216e10275e06f2614b713d9970..29fda6e58b63e41b9728614549558fd3817ffe77 100644 (file)
@@ -46,9 +46,6 @@ public class RunContentDescriptor implements Disposable, DataProvider {
   private Computable<JComponent> myFocusComputable = null;
   private boolean myAutoFocusContent = false;
 
-  /**
-   * Used to hack {@link com.intellij.execution.runners.RestartAction}
-   */
   private Content myContent;
   private Runnable myRestarter;
 
@@ -135,25 +132,30 @@ public class RunContentDescriptor implements Disposable, DataProvider {
     return myHelpId;
   }
 
-  /**
-   * @see #myContent
-   */
-  public void setAttachedContent(final Content content) {
-    myContent = content;
-  }
-
-  /**
-   * @see #myContent
-   */
+  @Nullable
   public Content getAttachedContent() {
     return myContent;
   }
 
+  public void setAttachedContent(@NotNull Content content) {
+    myContent = content;
+  }
+
   @Nullable
+  @Deprecated
+  /**
+   * @deprecated Use {@link com.intellij.execution.runners.ExecutionUtil#restart(RunContentDescriptor)} instead
+   * to remove in IDEA 15
+   */
   public Runnable getRestarter() {
     return myRestarter;
   }
 
+  @SuppressWarnings("UnusedDeclaration")
+  @Deprecated
+  /**
+   * @deprecated to remove in IDEA 15
+   */
   public void setRestarter(@Nullable Runnable runnable) {
     myRestarter = runnable;
   }
index b9f649378acb48234f32b66805e830c805942335..acf3437a86333e31b0288cdbfaea288006bad0f1 100644 (file)
@@ -28,7 +28,11 @@ import org.jetbrains.annotations.Nullable;
 import java.util.List;
 
 public interface RunContentManager {
+  @SuppressWarnings("UnusedDeclaration")
   @Deprecated
+  /**
+   * @deprecated Use {@link LangDataKeys#RUN_CONTENT_DESCRIPTOR} instead
+   */
   DataKey<RunContentDescriptor> RUN_CONTENT_DESCRIPTOR = LangDataKeys.RUN_CONTENT_DESCRIPTOR;
 
   @Nullable
@@ -90,4 +94,4 @@ public interface RunContentManager {
 
   @Nullable
   ToolWindow getToolWindowByDescriptor(@NotNull RunContentDescriptor descriptor);
-}
\ No newline at end of file
+}
index d0c8637af03a0375bb92d6bab21da630cb628b7f..498c763bf6e3ada2739d84f985f2b52ac832bc44 100644 (file)
@@ -391,6 +391,7 @@ public class ExecutionManagerImpl extends ExecutionManager implements ProjectCom
 
   private static void start(@NotNull ExecutionEnvironment environment) {
     if (environment.getRunner() == null) {
+      @SuppressWarnings("deprecation")
       Runnable restarter = environment.getContentToReuse() == null ? null : environment.getContentToReuse().getRestarter();
       if (restarter != null) {
         restarter.run();
index e73cf8cd992fa1a3e5cc9ec492a551da5b0da943..8d3bf86635986b16816fdd774540816697541eac 100644 (file)
@@ -27,7 +27,6 @@ public class RerunTestsAction extends DumbAwareAction implements AnAction.Transp
   private static final List<ExecutionEnvironment> REGISTRY = ContainerUtil.createLockFreeCopyOnWriteList();
 
   public static void register(@NotNull final ExecutionEnvironment environment) {
-    assert environment.getContentToReuse() != null;
     REGISTRY.add(environment);
     Disposer.register(environment, new Disposable() {
       @Override
index dbb34958bfd59b9f9ccbf222a9535a8d5bbe9d72..5c9db725ec139ae4533d6d7db629467ab962e0a1 100644 (file)
@@ -23,6 +23,7 @@ import com.intellij.execution.configurations.RunProfile;
 import com.intellij.execution.configurations.SearchScopeProvider;
 import com.intellij.execution.process.ProcessHandler;
 import com.intellij.execution.ui.ExecutionConsole;
+import com.intellij.execution.ui.RunContentDescriptor;
 import com.intellij.execution.ui.RunnerLayoutUi;
 import com.intellij.openapi.actionSystem.DataProvider;
 import com.intellij.openapi.actionSystem.LangDataKeys;
@@ -66,6 +67,13 @@ public abstract class RunTab extends LogConsoleManagerBase implements DataProvid
     else if (LangDataKeys.EXECUTION_ENVIRONMENT.is(dataId)) {
       return getEnvironment();
     }
+    else if (LangDataKeys.RUN_CONTENT_DESCRIPTOR.is(dataId)) {
+      ExecutionEnvironment environment = getEnvironment();
+      RunContentDescriptor descriptor = environment == null ? null : environment.getContentToReuse();
+      if (descriptor != null) {
+        return descriptor;
+      }
+    }
     return null;
   }
 
index 7ac561bd1d21a2ca18b410e9d5428cfaa64d2cec..3d05f2bb1bbf4f4836bfbae781798b745a646c02 100644 (file)
@@ -412,7 +412,7 @@ public class RunContentManagerImpl implements RunContentManager, Disposable {
   public static void copyContentAndBehavior(@NotNull RunContentDescriptor descriptor, @Nullable RunContentDescriptor contentToReuse) {
     if (contentToReuse != null) {
       Content attachedContent = contentToReuse.getAttachedContent();
-      if (attachedContent.isValid()) {
+      if (attachedContent != null && attachedContent.isValid()) {
         descriptor.setAttachedContent(attachedContent);
       }
       if (contentToReuse.isReuseToolWindowActivation()) {
@@ -511,7 +511,7 @@ public class RunContentManagerImpl implements RunContentManager, Disposable {
   }
 
   @Nullable
-  public static RunContentDescriptor getRunContentDescriptorByContent(@NotNull final Content content) {
+  private static RunContentDescriptor getRunContentDescriptorByContent(@NotNull final Content content) {
     return content.getUserData(DESCRIPTOR_KEY);
   }
 
index fb433b23ec567b531966fd1c39d07bc01862bcdd..3f15ee3f6de34b25035ee21f432c131880ffc999 100644 (file)
@@ -2,9 +2,12 @@ package com.intellij.execution.testframework.autotest;
 
 import com.intellij.execution.DelayedDocumentWatcher;
 import com.intellij.execution.process.ProcessHandler;
+import com.intellij.execution.runners.ExecutionUtil;
 import com.intellij.execution.ui.RunContentDescriptor;
-import com.intellij.execution.ui.RunContentManagerImpl;
+import com.intellij.ide.DataManager;
 import com.intellij.ide.util.PropertiesComponent;
+import com.intellij.openapi.actionSystem.DataContext;
+import com.intellij.openapi.actionSystem.LangDataKeys;
 import com.intellij.openapi.components.ServiceManager;
 import com.intellij.openapi.fileEditor.FileEditorManager;
 import com.intellij.openapi.project.Project;
@@ -15,6 +18,7 @@ import com.intellij.ui.content.Content;
 import com.intellij.util.Consumer;
 import com.intellij.util.containers.WeakHashMap;
 
+import javax.swing.*;
 import java.util.Collections;
 import java.util.Set;
 
@@ -79,21 +83,21 @@ public class AutoTestManager {
   }
 
   private static void runAutoTest(Content content) {
-    RunContentDescriptor descriptor = RunContentManagerImpl.getRunContentDescriptorByContent(content);
-    if (descriptor == null) {
-      return;
-    }
-    Runnable restarter = descriptor.getRestarter();
-    if (restarter == null) {
-      return;
-    }
-    final ProcessHandler processHandler = descriptor.getProcessHandler();
-    if (processHandler != null && !processHandler.isProcessTerminated()) {
-      return;
+    JComponent component = content.getComponent();
+    if (component != null) {
+      DataContext dataContext = DataManager.getInstance().getDataContext(component);
+      RunContentDescriptor descriptor = LangDataKeys.RUN_CONTENT_DESCRIPTOR.getData(dataContext);
+      if (descriptor != null) {
+        ProcessHandler processHandler = descriptor.getProcessHandler();
+        if (processHandler != null && !processHandler.isProcessTerminated()) {
+          return;
+        }
+
+        descriptor.setActivateToolWindowWhenAdded(false);
+        descriptor.setReuseToolWindowActivation(true);
+        ExecutionUtil.restart(content);
+      }
     }
-    descriptor.setActivateToolWindowWhenAdded(false);
-    descriptor.setReuseToolWindowActivation(true);
-    restarter.run();
   }
 
   int getDelay() {
index 893ddb320aabf02a46d2b7785847f545c2216b39..256ff0277c83bf99e6723843cd5c1b5f22a736af 100644 (file)
@@ -2,10 +2,10 @@ package com.intellij.execution.testframework.autotest;
 
 import com.intellij.execution.runners.ExecutionEnvironment;
 import com.intellij.execution.ui.RunContentDescriptor;
-import com.intellij.execution.ui.RunContentManager;
 import com.intellij.icons.AllIcons;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.CommonDataKeys;
+import com.intellij.openapi.actionSystem.LangDataKeys;
 import com.intellij.openapi.actionSystem.ToggleAction;
 import com.intellij.openapi.project.Project;
 
@@ -21,15 +21,15 @@ public class ToggleAutoTestAction extends ToggleAction {
 
   @Override
   public boolean isSelected(AnActionEvent e) {
-    Project project = e.getData(CommonDataKeys.PROJECT);
-    RunContentDescriptor descriptor = e.getData(RunContentManager.RUN_CONTENT_DESCRIPTOR);
+    Project project = e.getProject();
+    RunContentDescriptor descriptor = e.getData(LangDataKeys.RUN_CONTENT_DESCRIPTOR);
     return !(project == null || descriptor == null) && AutoTestManager.getInstance(project).isAutoTestEnabled(descriptor);
   }
 
   @Override
   public void setSelected(AnActionEvent e, boolean state) {
     Project project = e.getData(CommonDataKeys.PROJECT);
-    RunContentDescriptor descriptor = e.getData(RunContentManager.RUN_CONTENT_DESCRIPTOR);
+    RunContentDescriptor descriptor = e.getData(LangDataKeys.RUN_CONTENT_DESCRIPTOR);
     if (project != null && descriptor != null) {
       AutoTestManager.getInstance(project).setAutoTestEnabled(descriptor, state);
     }