Commit de4b5be3 authored by Alexander Koshevoy's avatar Alexander Koshevoy
Browse files

PY-29597 showProcessExecutionErrorDialog() method refactoring

parent db6d4035
Showing with 32 additions and 26 deletions
+32 -26
......@@ -285,7 +285,7 @@ class PyAddSdkDialog private constructor(private val project: Project?,
Messages.showErrorDialog(e.localizedMessage, "Error")
}
else {
showExecutionErrorDialog(project, cause)
showProcessExecutionErrorDialog(project, cause)
}
return
}
......
......@@ -72,35 +72,28 @@ internal fun show(panel: JPanel, stepContent: Component) {
(panel.layout as CardLayout).show(panel, stepContentName)
}
fun showExecutionErrorDialog(project: Project?, e: PyExecutionException) {
val errorMessage = JBLabel("<html>${e.command} could not complete successfully. " +
"Please see the command's output for information about resolving this problem.</html>",
Messages.getErrorIcon(), SwingConstants.LEFT)
val formBuilder = FormBuilder().addComponent(errorMessage)
val commandOutputTextField = JTextPane().apply {
val stdoutStyle = addStyle("stdoutStyle", null)
StyleConstants.setFontFamily(stdoutStyle, Font.MONOSPACED)
val stderrStyle = addStyle("stderrStyle", stdoutStyle)
StyleConstants.setForeground(stderrStyle, JBColor.RED)
document.apply {
e.stdout.let {
if (it.isNotEmpty()) insertString(length, it + "\n", stdoutStyle)
}
e.stderr.let {
if (it.isNotEmpty()) insertString(length, it + "\n", stderrStyle)
}
insertString(length, "Process finished with exit code ${e.exitCode}", stdoutStyle)
}
internal fun showProcessExecutionErrorDialog(project: Project?, e: PyExecutionException) {
val errorMessageText = "${e.command} could not complete successfully. " +
"Please see the command's output for information about resolving this problem."
// HTML format for text in `JBLabel` enables text wrapping
val errorMessageLabel = JBLabel(UIUtil.toHtml(errorMessageText), Messages.getErrorIcon(), SwingConstants.LEFT)
val commandOutputTextPane = JTextPane().apply {
appendProcessOutput(e.stdout, e.stderr, e.exitCode)
background = JBColor.WHITE
isEditable = false
}
formBuilder.addComponentFillVertically(BorderLayoutPanel().apply {
val commandOutputPanel = BorderLayoutPanel().apply {
border = IdeBorderFactory.createTitledBorder("Command output", false)
addToCenter(JBScrollPane(commandOutputTextField, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_NEVER))
}, UIUtil.DEFAULT_VGAP)
addToCenter(JBScrollPane(commandOutputTextPane, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_NEVER))
}
val formBuilder = FormBuilder()
.addComponent(errorMessageLabel)
.addComponentFillVertically(commandOutputPanel, UIUtil.DEFAULT_VGAP)
object : DialogWrapper(project) {
init {
......@@ -110,8 +103,21 @@ fun showExecutionErrorDialog(project: Project?, e: PyExecutionException) {
override fun createActions(): Array<Action> = arrayOf(okAction)
override fun createCenterPanel(): JComponent {
return formBuilder.panel.apply { preferredSize = Dimension(600, 300) }
override fun createCenterPanel(): JComponent = formBuilder.panel.apply {
preferredSize = Dimension(600, 300)
}
}.showAndGet()
}
private fun JTextPane.appendProcessOutput(stdout: String, stderr: String, exitCode: Int) {
val stdoutStyle = addStyle(null, null)
StyleConstants.setFontFamily(stdoutStyle, Font.MONOSPACED)
val stderrStyle = addStyle(null, stdoutStyle)
StyleConstants.setForeground(stderrStyle, JBColor.RED)
document.apply {
arrayOf(stdout to stdoutStyle, stderr to stderrStyle).forEach { (std, style) ->
if (std.isNotEmpty()) insertString(length, std + "\n", style)
}
insertString(length, "Process finished with exit code $exitCode", stdoutStyle)
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment