diff --git a/plugins/android/resources/messages/AndroidBundle.properties b/plugins/android/resources/messages/AndroidBundle.properties index aa8879c1e49a..245396e2e4ee 100644 --- a/plugins/android/resources/messages/AndroidBundle.properties +++ b/plugins/android/resources/messages/AndroidBundle.properties @@ -237,4 +237,5 @@ android.sdk.configure.jdk.error=Please configure internal JDK no.jdk.for.android.found.error=No Java SDK of appropriate version found. In addition to the Android SDK, you need to define a JSDK 1.5 or 1.6 no.jdk.error=You need to create at least one JSDK of versions 1.5 or 1.6 cannot.parse.sdk.error=Cannot parse Android SDK -android.add.sdk.tooltip=Add SDK \ No newline at end of file +android.add.sdk.tooltip=Add SDK +android.console.tool.window.title=Android Console \ No newline at end of file diff --git a/plugins/android/src/org/jetbrains/android/actions/RunAndroidSdkManagerAction.java b/plugins/android/src/org/jetbrains/android/actions/RunAndroidSdkManagerAction.java index 70107303956b..4335f68bdfc3 100644 --- a/plugins/android/src/org/jetbrains/android/actions/RunAndroidSdkManagerAction.java +++ b/plugins/android/src/org/jetbrains/android/actions/RunAndroidSdkManagerAction.java @@ -79,6 +79,6 @@ public class RunAndroidSdkManagerAction extends AnAction { } GeneralCommandLine commandLine = new GeneralCommandLine(); commandLine.setExePath(sdkPath + File.separator + AndroidUtils.toolPath(SdkConstants.androidCmdName())); - AndroidUtils.runExternalToolInSeparateThread(project, commandLine, null); + AndroidUtils.runExternalToolInSeparateThread(project, commandLine); } } diff --git a/plugins/android/src/org/jetbrains/android/facet/AndroidFacet.java b/plugins/android/src/org/jetbrains/android/facet/AndroidFacet.java index 0ab1c1cdefc9..cfefaef580c7 100644 --- a/plugins/android/src/org/jetbrains/android/facet/AndroidFacet.java +++ b/plugins/android/src/org/jetbrains/android/facet/AndroidFacet.java @@ -319,7 +319,7 @@ public class AndroidFacet extends Facet { return myAvdManager; } - public void launchEmulator(@Nullable final String avdName, @NotNull final String commands, @Nullable final ProcessHandler handler) { + public void launchEmulator(@Nullable final String avdName, @NotNull final String commands) { AndroidPlatform platform = getConfiguration().getAndroidPlatform(); if (platform != null) { final String emulatorPath = platform.getSdk().getLocation() + File.separator + AndroidUtils.toolPath(EMULATOR); @@ -335,7 +335,7 @@ public class AndroidFacet extends Facet { commandLine.addParameter(s); } } - AndroidUtils.runExternalToolInSeparateThread(getModule().getProject(), commandLine, handler); + AndroidUtils.runExternalToolInSeparateThread(getModule().getProject(), commandLine); } } diff --git a/plugins/android/src/org/jetbrains/android/newProject/AndroidModuleBuilder.java b/plugins/android/src/org/jetbrains/android/newProject/AndroidModuleBuilder.java index 0278ef242abd..5c4c5ac34c27 100644 --- a/plugins/android/src/org/jetbrains/android/newProject/AndroidModuleBuilder.java +++ b/plugins/android/src/org/jetbrains/android/newProject/AndroidModuleBuilder.java @@ -22,6 +22,7 @@ import com.intellij.CommonBundle; import com.intellij.execution.RunManagerEx; import com.intellij.execution.RunnerAndConfigurationSettings; import com.intellij.execution.configurations.GeneralCommandLine; +import com.intellij.execution.ui.ConsoleViewContentType; import com.intellij.facet.FacetManager; import com.intellij.facet.ModifiableFacetModel; import com.intellij.ide.util.projectWizard.JavaModuleBuilder; @@ -234,7 +235,8 @@ public class AndroidModuleBuilder extends JavaModuleBuilder { @Override public void run() { final Project project = module.getProject(); - final String result = AndroidUtils.runExternalTool(project, commandLine, null); + AndroidUtils.runExternalTool(project, commandLine, true); + StartupManager.getInstance(project).runWhenProjectIsInitialized(new Runnable() { public void run() { FileDocumentManager.getInstance().saveAllDocuments(); @@ -247,8 +249,8 @@ public class AndroidModuleBuilder extends JavaModuleBuilder { public void run() { if (contentRoot.findChild(SdkConstants.FN_ANDROID_MANIFEST_XML) == null) { - Messages.showErrorDialog(project, "The project wasn't generated by 'android' tool\n" + (result != null ? result : ""), - CommonBundle.getErrorTitle()); + AndroidUtils.printMessageToConsole(project, "The project wasn't generated by 'android' tool.", + ConsoleViewContentType.ERROR_OUTPUT); } ApplicationManager.getApplication().runWriteAction(new Runnable() { diff --git a/plugins/android/src/org/jetbrains/android/run/AndroidRunningState.java b/plugins/android/src/org/jetbrains/android/run/AndroidRunningState.java index 822ee80d70e9..2512e1513b55 100644 --- a/plugins/android/src/org/jetbrains/android/run/AndroidRunningState.java +++ b/plugins/android/src/org/jetbrains/android/run/AndroidRunningState.java @@ -356,14 +356,14 @@ public abstract class AndroidRunningState implements RunProfileState, AndroidDeb chooseAvd(); } if (myAvdName != null) { - myFacet.launchEmulator(myAvdName, myCommandLine, myProcessHandler); + myFacet.launchEmulator(myAvdName, myCommandLine); } else if (getProcessHandler().isStartNotified()) { getProcessHandler().destroyProcess(); } } else { - myFacet.launchEmulator(myAvdName, myCommandLine, myProcessHandler); + myFacet.launchEmulator(myAvdName, myCommandLine); } } } diff --git a/plugins/android/src/org/jetbrains/android/run/AvdChooser.java b/plugins/android/src/org/jetbrains/android/run/AvdChooser.java index c6a6736cd2b7..d9adfc649da2 100644 --- a/plugins/android/src/org/jetbrains/android/run/AvdChooser.java +++ b/plugins/android/src/org/jetbrains/android/run/AvdChooser.java @@ -116,7 +116,7 @@ public class AvdChooser extends DialogWrapper { public void actionPerformed(ActionEvent e) { GeneralCommandLine commandLine = new GeneralCommandLine(); commandLine.setExePath(androidToolPath); - AndroidUtils.runExternalToolInSeparateThread(project, commandLine, null); + AndroidUtils.runExternalToolInSeparateThread(project, commandLine); } }); updateTable(); diff --git a/plugins/android/src/org/jetbrains/android/run/DeviceChooser.java b/plugins/android/src/org/jetbrains/android/run/DeviceChooser.java index 574039a42c0b..518d82cf7596 100644 --- a/plugins/android/src/org/jetbrains/android/run/DeviceChooser.java +++ b/plugins/android/src/org/jetbrains/android/run/DeviceChooser.java @@ -221,7 +221,7 @@ public class DeviceChooser extends DialogWrapper implements AndroidDebugBridge.I if (chooser.getExitCode() != OK_EXIT_CODE) return; if (avd == null) return; } - myFacet.launchEmulator(avd != null ? avd.getName() : null, "", null); + myFacet.launchEmulator(avd != null ? avd.getName() : null, ""); } } diff --git a/plugins/android/src/org/jetbrains/android/util/AndroidUtils.java b/plugins/android/src/org/jetbrains/android/util/AndroidUtils.java index 3414508a63c4..8991ce00c238 100644 --- a/plugins/android/src/org/jetbrains/android/util/AndroidUtils.java +++ b/plugins/android/src/org/jetbrains/android/util/AndroidUtils.java @@ -29,7 +29,10 @@ import com.intellij.execution.ExecutionException; import com.intellij.execution.RunManagerEx; import com.intellij.execution.RunnerAndConfigurationSettings; import com.intellij.execution.configurations.GeneralCommandLine; +import com.intellij.execution.impl.ConsoleViewImpl; import com.intellij.execution.process.*; +import com.intellij.execution.ui.ConsoleView; +import com.intellij.execution.ui.ConsoleViewContentType; import com.intellij.ide.util.DefaultPsiElementCellRenderer; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; @@ -53,12 +56,20 @@ import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.wm.ToolWindow; +import com.intellij.openapi.wm.ToolWindowAnchor; +import com.intellij.openapi.wm.ToolWindowManager; +import com.intellij.openapi.wm.ex.ToolWindowManagerEx; +import com.intellij.openapi.wm.ex.ToolWindowManagerListener; import com.intellij.psi.*; import com.intellij.psi.xml.XmlAttribute; import com.intellij.psi.xml.XmlAttributeValue; import com.intellij.psi.xml.XmlFile; import com.intellij.psi.xml.XmlTag; import com.intellij.ui.awt.RelativePoint; +import com.intellij.ui.content.Content; +import com.intellij.ui.content.ContentManagerAdapter; +import com.intellij.ui.content.impl.ContentImpl; import com.intellij.util.PsiNavigateUtil; import com.intellij.util.containers.HashSet; import com.intellij.util.ui.UIUtil; @@ -125,6 +136,8 @@ public class AndroidUtils { public static final int TIMEOUT = 300000; + private static final Key CONSOLE_VIEW_KEY = new Key("AndroidConsoleView"); + private AndroidUtils() { } @@ -432,25 +445,19 @@ public class AndroidUtils { } public static void runExternalToolInSeparateThread(@NotNull final Project project, - @NotNull final GeneralCommandLine commandLine, - @Nullable final ProcessHandler processHandler) { + @NotNull final GeneralCommandLine commandLine) { ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { public void run() { - runExternalTool(project, commandLine, processHandler); + runExternalTool(project, commandLine, false); } }); } - @Nullable - public static String runExternalTool(final Project project, - GeneralCommandLine commandLine, - ProcessHandler processHandler) { + public static void runExternalTool(final Project project, GeneralCommandLine commandLine, boolean printOutputToConsole) { String[] commands = commandLine.getCommands(); String command = StringUtil.join(commands, " "); LOG.info("Execute: " + command); - if (processHandler != null && !processHandler.isProcessTerminated()) { - processHandler.notifyTextAvailable(command + '\n', ProcessOutputTypes.STDOUT); - } + StringBuilder messageBuilder = new StringBuilder(); String result; boolean success = false; @@ -461,21 +468,19 @@ public class AndroidUtils { catch (ExecutionException e) { result = e.getMessage(); } - if (result != null && !success) { - final String errorMessage = result; - if (processHandler != null) { - processHandler.notifyTextAvailable(errorMessage + '\n', ProcessOutputTypes.STDERR); - processHandler.destroyProcess(); - } - else { - UIUtil.invokeLaterIfNeeded(new Runnable() { - public void run() { - Messages.showErrorDialog(project, errorMessage, AndroidBundle.message("emulator.error.dialog.title")); - } - }); - } + + if (result != null && printOutputToConsole) { + final ConsoleViewContentType contentType = success ? + ConsoleViewContentType.NORMAL_OUTPUT : + ConsoleViewContentType.ERROR_OUTPUT; + final String finalResult = result; + UIUtil.invokeLaterIfNeeded(new Runnable() { + @Override + public void run() { + printMessageToConsole(project, finalResult, contentType); + } + }); } - return result; } public static String getSimpleNameByRelativePath(String relativePath) { @@ -589,4 +594,50 @@ public class AndroidUtils { } } } + + public static void printMessageToConsole(@NotNull Project project, @NotNull String s, @NotNull ConsoleViewContentType contentType) { + activateConsoleToolWindow(project); + final ConsoleView consoleView = project.getUserData(CONSOLE_VIEW_KEY); + + if (consoleView != null) { + consoleView .print(s + '\n', contentType); + } + } + + private static void activateConsoleToolWindow(@NotNull Project project) { + final ToolWindowManager manager = ToolWindowManager.getInstance(project); + final String toolWindowId = AndroidBundle.message("android.console.tool.window.title"); + + ToolWindow toolWindow = manager.getToolWindow(toolWindowId); + if (toolWindow != null) { + return; + } + + toolWindow = manager.registerToolWindow(toolWindowId, true, ToolWindowAnchor.BOTTOM); + final ConsoleView console = new ConsoleViewImpl(project, false); + project.putUserData(CONSOLE_VIEW_KEY, console); + toolWindow.getContentManager().addContent(new ContentImpl(console.getComponent(), "", false)); + + final ToolWindowManagerListener listener = new ToolWindowManagerListener() { + @Override + public void toolWindowRegistered(@NotNull String id) { + } + + @Override + public void stateChanged() { + ToolWindow window = manager.getToolWindow(toolWindowId); + if (window != null && !window.isVisible()) { + manager.unregisterToolWindow(toolWindowId); + ((ToolWindowManagerEx)manager).removeToolWindowManagerListener(this); + } + } + }; + + toolWindow.show(new Runnable() { + @Override + public void run() { + ((ToolWindowManagerEx)manager).addToolWindowManagerListener(listener); + } + }); + } }