<commit_before>/*
 * Copyright (C) 2019 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.reecedunn.intellij.plugin.intellij.execution.ui

import com.intellij.execution.process.ProcessHandler
import com.intellij.execution.ui.ConsoleView
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.openapi.project.Project
import com.intellij.ui.OnePixelSplitter
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.components.panels.VerticalLayout
import com.intellij.ui.content.ContentManager
import com.intellij.ui.content.TabbedPaneContentUI
import com.intellij.ui.content.impl.ContentImpl
import com.intellij.ui.content.impl.ContentManagerImpl
import com.intellij.util.Range
import uk.co.reecedunn.intellij.plugin.core.execution.ui.ConsoleViewEx
import uk.co.reecedunn.intellij.plugin.core.execution.ui.ConsoleViewImpl
import uk.co.reecedunn.intellij.plugin.core.ui.Borders
import uk.co.reecedunn.intellij.plugin.intellij.execution.process.QueryProcessHandlerBase
import uk.co.reecedunn.intellij.plugin.intellij.execution.process.QueryResultListener
import uk.co.reecedunn.intellij.plugin.intellij.execution.process.QueryResultTime
import uk.co.reecedunn.intellij.plugin.intellij.resources.PluginApiBundle
import uk.co.reecedunn.intellij.plugin.processor.query.QueryError
import uk.co.reecedunn.intellij.plugin.processor.query.QueryResult
import uk.co.reecedunn.intellij.plugin.xpath.model.XsDurationValue
import java.awt.*
import java.io.PrintWriter
import java.io.StringWriter
import javax.swing.JComponent
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.SwingConstants
import javax.swing.border.EmptyBorder

class QueryConsoleView(val project: Project) : ConsoleViewImpl(), QueryResultListener {
    companion object {
        private const val SPLITTER_KEY = "XQueryIntelliJPlugin.QueryResultView.Splitter"
    }

    private var contentManager: ContentManager? = null
    private var consoles: ArrayList<Pair<ConsoleView, String>> = ArrayList()

    private var summary: JLabel? = null
    private var table: QueryResultTable? = null

    fun addConsoleView(consoleView: ConsoleView, name: String) {
        consoles.add(Pair(consoleView, name))
    }

    private fun createResultTable(): JComponent {
        table = QueryResultTable(
            QueryResultItemTypeColumn(sortable = false),
            QueryResultMimeTypeColumn(sortable = false)
        )

        table!!.selectionModel.addListSelectionListener {
            table?.selectedObject?.second?.let { range ->
                consoles.filterIsInstance<ConsoleViewEx>().forEach { it.scrollToTop(range.from) }
            }
        }

        return JBScrollPane(table)
    }

    private fun createResultPanel(): JComponent {
        summary = JLabel()

        val infoPanel = JPanel(VerticalLayout(0))
        summary!!.border = EmptyBorder(7, 4, 8, 4)
        infoPanel.add(summary)

        val panel = JPanel(GridBagLayout())
        val constraints = GridBagConstraints(
            0, 0, 1, 1, 1.0, 0.0,
            GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,
            Insets(0, 0, 0, 0),
            0, 0
        )
        panel.add(infoPanel, constraints)

        constraints.gridy = 1
        constraints.weighty = 1.0
        constraints.fill = GridBagConstraints.BOTH
        panel.add(createResultTable(), constraints)

        infoPanel.border = Borders.TableHeaderBottom
        return panel
    }

    // region ConsoleView

    override fun clear() {
        consoles.forEach { it.first.clear() }

        summary!!.text = "\u00A0"

        table?.removeAll()
        table?.isRunning = false
        table?.hasException = false
    }

    override fun print(text: String, contentType: ConsoleViewContentType) {
        consoles.forEach { it.first.print(text, contentType) }
    }

    override fun getContentSize(): Int = consoles.firstOrNull()?.first?.contentSize ?: 0

    override fun attachToProcess(processHandler: ProcessHandler?) {
        (processHandler as? QueryProcessHandlerBase)?.addQueryResultListener(this, this)
        consoles.forEach { it.first.attachToProcess(processHandler) }
    }

    override fun getComponent(): JComponent {
        if (table == null) {
            val contentUI = TabbedPaneContentUI(SwingConstants.TOP)

            contentManager = ContentManagerImpl(contentUI, false, project)
            consoles.forEach {
                contentManager!!.addContent(ContentImpl(it.first.component, it.second, false))
            }

            val splitPane = OnePixelSplitter(false)
            splitPane.firstComponent = contentManager!!.component
            splitPane.secondComponent = createResultPanel()<commit_msg>Use the ContentFactory to create the ContentManager instance.<commit_after>/*
 * Copyright (C) 2019 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.reecedunn.intellij.plugin.intellij.execution.ui

import com.intellij.execution.process.ProcessHandler
import com.intellij.execution.ui.ConsoleView
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.openapi.project.Project
import com.intellij.ui.OnePixelSplitter
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.components.panels.VerticalLayout
import com.intellij.ui.content.ContentFactory
import com.intellij.ui.content.ContentManager
import com.intellij.ui.content.TabbedPaneContentUI
import com.intellij.ui.content.impl.ContentImpl
import com.intellij.util.Range
import uk.co.reecedunn.intellij.plugin.core.execution.ui.ConsoleViewEx
import uk.co.reecedunn.intellij.plugin.core.execution.ui.ConsoleViewImpl
import uk.co.reecedunn.intellij.plugin.core.ui.Borders
import uk.co.reecedunn.intellij.plugin.intellij.execution.process.QueryProcessHandlerBase
import uk.co.reecedunn.intellij.plugin.intellij.execution.process.QueryResultListener
import uk.co.reecedunn.intellij.plugin.intellij.execution.process.QueryResultTime
import uk.co.reecedunn.intellij.plugin.intellij.resources.PluginApiBundle
import uk.co.reecedunn.intellij.plugin.processor.query.QueryError
import uk.co.reecedunn.intellij.plugin.processor.query.QueryResult
import uk.co.reecedunn.intellij.plugin.xpath.model.XsDurationValue
import java.awt.*
import java.io.PrintWriter
import java.io.StringWriter
import javax.swing.JComponent
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.SwingConstants
import javax.swing.border.EmptyBorder

class QueryConsoleView(val project: Project) : ConsoleViewImpl(), QueryResultListener {
    companion object {
        private const val SPLITTER_KEY = "XQueryIntelliJPlugin.QueryResultView.Splitter"
    }

    private var contentManager: ContentManager? = null
    private var consoles: ArrayList<Pair<ConsoleView, String>> = ArrayList()

    private var summary: JLabel? = null
    private var table: QueryResultTable? = null

    fun addConsoleView(consoleView: ConsoleView, name: String) {
        consoles.add(Pair(consoleView, name))
    }

    private fun createResultTable(): JComponent {
        table = QueryResultTable(
            QueryResultItemTypeColumn(sortable = false),
            QueryResultMimeTypeColumn(sortable = false)
        )

        table!!.selectionModel.addListSelectionListener {
            table?.selectedObject?.second?.let { range ->
                consoles.filterIsInstance<ConsoleViewEx>().forEach { it.scrollToTop(range.from) }
            }
        }

        return JBScrollPane(table)
    }

    private fun createResultPanel(): JComponent {
        summary = JLabel()

        val infoPanel = JPanel(VerticalLayout(0))
        summary!!.border = EmptyBorder(7, 4, 8, 4)
        infoPanel.add(summary)

        val panel = JPanel(GridBagLayout())
        val constraints = GridBagConstraints(
            0, 0, 1, 1, 1.0, 0.0,
            GridBagConstraints.FIRST_LINE_START, GridBagConstraints.HORIZONTAL,
            Insets(0, 0, 0, 0),
            0, 0
        )
        panel.add(infoPanel, constraints)

        constraints.gridy = 1
        constraints.weighty = 1.0
        constraints.fill = GridBagConstraints.BOTH
        panel.add(createResultTable(), constraints)

        infoPanel.border = Borders.TableHeaderBottom
        return panel
    }

    // region ConsoleView

    override fun clear() {
        consoles.forEach { it.first.clear() }

        summary!!.text = "\u00A0"

        table?.removeAll()
        table?.isRunning = false
        table?.hasException = false
    }

    override fun print(text: String, contentType: ConsoleViewContentType) {
        consoles.forEach { it.first.print(text, contentType) }
    }

    override fun getContentSize(): Int = consoles.firstOrNull()?.first?.contentSize ?: 0

    override fun attachToProcess(processHandler: ProcessHandler?) {
        (processHandler as? QueryProcessHandlerBase)?.addQueryResultListener(this, this)
        consoles.forEach { it.first.attachToProcess(processHandler) }
    }

    override fun getComponent(): JComponent {
        if (table == null) {
            val contentUI = TabbedPaneContentUI(SwingConstants.TOP)
            contentManager = ContentFactory.SERVICE.getInstance().createContentManager(contentUI, true, project)

            consoles.forEach {
                contentManager!!.addContent(ContentImpl(it.first.component, it.second, false))
            }

            val splitPane = OnePixelSplitter(false)
            splitPane.firstComponent = contentManager!!.component
            splitPane.secondComponent = createResultPanel()
{'fraction_non_alphanumeric': 0.09202453987730061, 'fraction_numerical': 0.006418121755545068, 'mean_word_length': 3.4502309953800925, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2375859', 'n_tokens_mistral': 3054, 'n_tokens_neox': 2944, 'n_words': 638}