<commit_before>package org.runestar.client.api

import org.kxtra.slf4j.loggerfactory.getLogger
import org.runestar.client.common.ICON
import org.runestar.client.plugins.PluginHandle
import org.runestar.client.plugins.PluginLoader
import java.awt.Window
import java.awt.event.ItemEvent
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import java.util.*
import javax.swing.*
import javax.swing.Timer
import javax.swing.event.PopupMenuEvent
import javax.swing.event.PopupMenuListener

internal class PluginsWindow(private val pluginLoader: PluginLoader) : JFrame("Plugins") {

    private val timer = Timer(600, null)

    private val checkBox = JCheckBox("enabled")

    private var currentPlugin: PluginHandle? = null

    private val openButton = JButton("open...")

    private val comboModelVector = Vector<PluginHandle>()

    val logger = getLogger()

    init {
        iconImage = ICON
        val popup = JPopupMenu().apply {
            add(JMenuItem("settings").apply {
                addActionListener { currentPlugin?.let { openFile(it.settingsFile) } }
            })
            add(JMenuItem("log").apply {
                addActionListener { currentPlugin?.let { openFile(it.logFile) } }
            })
            add(JMenuItem("directory").apply {
                addActionListener { currentPlugin?.let { openFile(it.directory) } }
            })
        }
        add(Box.createHorizontalBox().apply {
            add(checkBox.apply {
                isEnabled = false
                addActionListener {
                    val cp = currentPlugin
                    if (cp != null) {
                        if (isSelected) cp.start() else cp.stop()
                    }
                }
            })
            refreshPlugins()
            add(JComboBox<PluginHandle>(DefaultComboBoxModel(comboModelVector)).apply {
                model.selectedItem = null
                maximumRowCount *= 2
                addPopupMenuListener(object : PopupMenuListener {
                    override fun popupMenuCanceled(e: PopupMenuEvent) {}
                    override fun popupMenuWillBecomeInvisible(e: PopupMenuEvent) {}
                    override fun popupMenuWillBecomeVisible(e: PopupMenuEvent) {
                        model = DefaultComboBoxModel(comboModelVector)
                        model.selectedItem = currentPlugin
                    }
                })
                addItemListener {
                    logger.info(it.toString())
                    if (it.stateChange != ItemEvent.SELECTED) return@addItemListener
                    if (it.item == currentPlugin) return@addItemListener
                    logger.info(it.item.toString())
                    currentPlugin = it.item as PluginHandle
                    checkBox.isEnabled = true
                    openButton.isEnabled = true
                }
            })
            add(openButton.apply {
                isEnabled = false
                addActionListener {
                    popup.show(this, 0, bounds.height)
                }
            })
        })
        timer.apply {
            addActionListener {
                val cp = currentPlugin
                if (cp != null && checkBox.isSelected != cp.isRunning) {
                    checkBox.isSelected = cp.isRunning
                    revalidate()
                    repaint()
                }
            }
            start()
        }
        addWindowListener(object : WindowAdapter() {
            override fun windowClosing(e: WindowEvent) {
                timer.stop()
            }
        })
        type = Window.Type.UTILITY
        pack()
        isResizable = false
        defaultCloseOperation = WindowConstants.DISPOSE_ON_CLOSE
        setLocationRelativeTo(Application.frame)
        isVisible = true
    }

    private fun refreshPlugins() {
        comboModelVector.clear()
        comboModelVector.addAll(pluginLoader.plugins.sortedBy { it.name })
    }
}<commit_msg>Refresh pluginswindow better<commit_after>package org.runestar.client.api

import org.runestar.client.common.ICON
import org.runestar.client.plugins.PluginHandle
import org.runestar.client.plugins.PluginLoader
import java.awt.Window
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.*
import javax.swing.event.PopupMenuEvent
import javax.swing.event.PopupMenuListener

internal class PluginsWindow(private val pluginLoader: PluginLoader) : JFrame("Plugins") {

    private val timer = Timer(600, null)

    private val checkBox = JCheckBox("enabled")

    private var currentPlugin: PluginHandle? = null

    private val openButton = JButton("open...")

    private val comboModel = DefaultComboBoxModel<PluginHandle>()

    init {
        iconImage = ICON
        val popup = JPopupMenu().apply {
            add(JMenuItem("settings").apply {
                addActionListener { currentPlugin?.let { openFile(it.settingsFile) } }
            })
            add(JMenuItem("log").apply {
                addActionListener { currentPlugin?.let { openFile(it.logFile) } }
            })
            add(JMenuItem("directory").apply {
                addActionListener { currentPlugin?.let { openFile(it.directory) } }
            })
        }
        add(Box.createHorizontalBox().apply {
            add(checkBox.apply {
                isEnabled = false
                addActionListener {
                    val cp = currentPlugin
                    if (cp != null) {
                        if (isSelected) cp.start() else cp.stop()
                    }
                }
            })
            refreshPlugins()
            add(JComboBox<PluginHandle>(comboModel).apply {
                model.selectedItem = null
                maximumRowCount *= 2
                addPopupMenuListener(object : PopupMenuListener {
                    override fun popupMenuCanceled(e: PopupMenuEvent) {}
                    override fun popupMenuWillBecomeInvisible(e: PopupMenuEvent) {
                        currentPlugin = selectedItem as PluginHandle?
                        if (currentPlugin != null) {
                            checkBox.isEnabled = true
                            openButton.isEnabled = true
                        }
                    }
                    override fun popupMenuWillBecomeVisible(e: PopupMenuEvent) {
                        refreshPlugins()
                    }
                })
            })
            add(openButton.apply {
                isEnabled = false
                addActionListener {
                    popup.show(this, 0, bounds.height)
                }
            })
        })
        timer.apply {
            addActionListener {
                val cp = currentPlugin
                if (cp != null && checkBox.isSelected != cp.isRunning) {
                    checkBox.isSelected = cp.isRunning
                    revalidate()
                    repaint()
                }
            }
            start()
        }
        addWindowListener(object : WindowAdapter() {
            override fun windowClosing(e: WindowEvent) {
                timer.stop()
            }
        })
        type = Window.Type.UTILITY
        pack()
        isResizable = false
        defaultCloseOperation = WindowConstants.DISPOSE_ON_CLOSE
        setLocationRelativeTo(Application.frame)
        isVisible = true
    }

    private fun refreshPlugins() {
        val selected = comboModel.selectedItem
        comboModel.removeAllElements()
        pluginLoader.plugins.sortedBy { it.name }.forEach { comboModel.addElement(it) }
        comboModel.selectedItem = selected
    }
}
{'fraction_non_alphanumeric': 0.083539684608367, 'fraction_numerical': 0.0014335983318128503, 'mean_word_length': 1.6093165589935396, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3519597', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1834, 'n_words': 351}