<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.log

import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.openapi.actionSystem.ActionPlaces
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.ui.content.ContentFactory
import uk.co.reecedunn.intellij.plugin.core.event.Stopwatch
import uk.co.reecedunn.intellij.plugin.core.execution.ui.ConsoleViewEx
import uk.co.reecedunn.intellij.plugin.core.execution.ui.TextConsoleView
import uk.co.reecedunn.intellij.plugin.core.ui.Borders
import uk.co.reecedunn.intellij.plugin.intellij.settings.QueryProcessorSettingsCellRenderer
import uk.co.reecedunn.intellij.plugin.intellij.settings.QueryProcessors
import uk.co.reecedunn.intellij.plugin.processor.log.LogViewProvider
import uk.co.reecedunn.intellij.plugin.processor.query.QueryProcessorSettings
import javax.swing.DefaultComboBoxModel
import javax.swing.JComboBox
import javax.swing.JComponent
import javax.swing.JPanel

class QueryLogViewer : ToolWindowFactory, DumbAware {
    private var logView: QueryLogViewerUI? = null

    override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
        if (logView == null) {
            logView = QueryLogViewerUI(project)
        }

        val content = ContentFactory.SERVICE.getInstance().createContent(logView?.panel, null, false)
        val contentManager = toolWindow.contentManager
        contentManager.removeAllContents(true)
        contentManager.addContent(content)
        contentManager.setSelectedContent(content)
        toolWindow.show(null)
    }
}

class QueryLogViewerUI(val project: Project) {
    // region Filter :: Server

    private var queryProcessor: JComboBox<QueryProcessorSettings>? = null

    private fun createQueryProcessorUI() {
        val model = DefaultComboBoxModel<QueryProcessorSettings>()
        queryProcessor = ComboBox(model)

        queryProcessor!!.renderer = QueryProcessorSettingsCellRenderer()
        queryProcessor!!.addActionListener {
            populateLogFiles()
        }

        QueryProcessors.getInstance().processors.forEach { processor ->
            processor.connection?.let { model.addElement(processor) }
        }
    }

    // endregion
    // region Filter :: Log File

    private var logFile: JComboBox<String>? = null

    private fun createLogFileUI() {
        logFile = ComboBox()
        logFile!!.addActionListener {
            lines = -1
            populateLogFile()
        }
    }

    private fun populateLogFiles() {
        val session = try {
            (queryProcessor?.selectedItem as? QueryProcessorSettings?)?.session
        } catch (e: Exception) {
            null
        }
        if (session is LogViewProvider) {
            session.logs().execute { logs ->
                logFile?.removeAllItems()
                logs.forEach { logFile?.addItem(it) }
            }.onException { logFile?.removeAllItems() }
        } else {
            logFile?.removeAllItems()
        }
    }

    // endregion
    // region Log View

    private var logConsole: ConsoleViewEx? = null
    private var logView: JComponent? = null
    private var lines: Int = -1

    private fun createConsoleEditor() {
        logConsole = TextConsoleView(project)
        logView = logConsole?.component

        logConsole?.setConsoleBorder(Borders.ConsoleToolbarTop)
        logConsole?.createActionToolbar(ActionPlaces.UNKNOWN)
    }

    private fun populateLogFile() {
        val session = try {
            (queryProcessor?.selectedItem as? QueryProcessorSettings?)?.session
        } catch (e: Exception) {
            null
        }
        if (session is LogViewProvider) {
            val logFile = logFile?.selectedItem as? String
            if (logFile != null) {
                session.log(logFile).execute { log ->
                    val offset = logConsole!!.offset
                    val isAtEnd = offset == logConsole!!.contentSize

                    if (lines == -1) {
                        logConsole?.clear()
                    }
                    log.withIndex().forEach { line ->
                        if (line.index > lines) {
                            logConsole?.print(line.value, ConsoleViewContentType.NORMAL_OUTPUT)
                            logConsole?.print("\n", ConsoleViewContentType.NORMAL_OUTPUT)
                            lines = line.index
                        }
                    }

                    if (isAtEnd) {
                        logConsole?.scrollTo(logConsole!!.contentSize)
                    }
                }.onException { logConsole?.clear() }
            }
        } else {
            logConsole?.clear()
        }
    }

    // endregion
    // region Form

    var panel: JPanel? = null

    var refresh: Stopwatch? = null

    private fun createUIComponents() {
        createQueryProcessorUI()
        createLogFileUI()
        createConsoleEditor()

        refresh = object : Stopwatch() {
            override fun isRunning(): Boolean = true

            override fun onInterval() = populateLogFile()
        }
        refresh?.start(5000)
    }

    // endregion
}<commit_msg>Don't auto-refresh the query log view.<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.log

import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.openapi.actionSystem.ActionPlaces
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.ui.content.ContentFactory
import uk.co.reecedunn.intellij.plugin.core.execution.ui.ConsoleViewEx
import uk.co.reecedunn.intellij.plugin.core.execution.ui.TextConsoleView
import uk.co.reecedunn.intellij.plugin.core.ui.Borders
import uk.co.reecedunn.intellij.plugin.intellij.settings.QueryProcessorSettingsCellRenderer
import uk.co.reecedunn.intellij.plugin.intellij.settings.QueryProcessors
import uk.co.reecedunn.intellij.plugin.processor.log.LogViewProvider
import uk.co.reecedunn.intellij.plugin.processor.query.QueryProcessorSettings
import javax.swing.DefaultComboBoxModel
import javax.swing.JComboBox
import javax.swing.JComponent
import javax.swing.JPanel

class QueryLogViewer : ToolWindowFactory, DumbAware {
    private var logView: QueryLogViewerUI? = null

    override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
        if (logView == null) {
            logView = QueryLogViewerUI(project)
        }

        val content = ContentFactory.SERVICE.getInstance().createContent(logView?.panel, null, false)
        val contentManager = toolWindow.contentManager
        contentManager.removeAllContents(true)
        contentManager.addContent(content)
        contentManager.setSelectedContent(content)
        toolWindow.show(null)
    }
}

class QueryLogViewerUI(val project: Project) {
    // region Filter :: Server

    private var queryProcessor: JComboBox<QueryProcessorSettings>? = null

    private fun createQueryProcessorUI() {
        val model = DefaultComboBoxModel<QueryProcessorSettings>()
        queryProcessor = ComboBox(model)

        queryProcessor!!.renderer = QueryProcessorSettingsCellRenderer()
        queryProcessor!!.addActionListener {
            populateLogFiles()
        }

        QueryProcessors.getInstance().processors.forEach { processor ->
            processor.connection?.let { model.addElement(processor) }
        }
    }

    // endregion
    // region Filter :: Log File

    private var logFile: JComboBox<String>? = null

    private fun createLogFileUI() {
        logFile = ComboBox()
        logFile!!.addActionListener {
            lines = -1
            populateLogFile()
        }
    }

    private fun populateLogFiles() {
        val session = try {
            (queryProcessor?.selectedItem as? QueryProcessorSettings?)?.session
        } catch (e: Exception) {
            null
        }
        if (session is LogViewProvider) {
            session.logs().execute { logs ->
                logFile?.removeAllItems()
                logs.forEach { logFile?.addItem(it) }
            }.onException { logFile?.removeAllItems() }
        } else {
            logFile?.removeAllItems()
        }
    }

    // endregion
    // region Log View

    private var logConsole: ConsoleViewEx? = null
    private var logView: JComponent? = null
    private var lines: Int = -1

    private fun createConsoleEditor() {
        logConsole = TextConsoleView(project)
        logView = logConsole?.component

        logConsole?.setConsoleBorder(Borders.ConsoleToolbarTop)
        logConsole?.createActionToolbar(ActionPlaces.UNKNOWN)
    }

    private fun populateLogFile() {
        val session = try {
            (queryProcessor?.selectedItem as? QueryProcessorSettings?)?.session
        } catch (e: Exception) {
            null
        }
        if (session is LogViewProvider) {
            val logFile = logFile?.selectedItem as? String
            if (logFile != null) {
                session.log(logFile).execute { log ->
                    val offset = logConsole!!.offset
                    val isAtEnd = offset == logConsole!!.contentSize

                    if (lines == -1) {
                        logConsole?.clear()
                    }
                    log.withIndex().forEach { line ->
                        if (line.index > lines) {
                            logConsole?.print(line.value, ConsoleViewContentType.NORMAL_OUTPUT)
                            logConsole?.print("\n", ConsoleViewContentType.NORMAL_OUTPUT)
                            lines = line.index
                        }
                    }

                    if (isAtEnd) {
                        logConsole?.scrollTo(logConsole!!.contentSize)
                    }
                }.onException { logConsole?.clear() }
            }
        } else {
            logConsole?.clear()
        }
    }

    // endregion
    // region Form

    var panel: JPanel? = null

    private fun createUIComponents() {
        createQueryProcessorUI()
        createLogFileUI()
        createConsoleEditor()
    }

    // endregion
}
{'fraction_non_alphanumeric': 0.08973252804141502, 'fraction_numerical': 0.0022433132010353752, 'mean_word_length': 2.352907144923344, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '4157677', 'n_tokens_mistral': 3098, 'n_tokens_neox': 3010, 'n_words': 676}