<commit_before>/*
 * Copyright 2015-2016 <NAME> <<EMAIL>>
 *
 * This file is part of IxIn.
 *
 * 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 pw.phylame.qaf.ixin

import pw.phylame.ycl.util.Provider
import java.awt.BorderLayout
import java.awt.Component
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import javax.swing.*

var JToolBar.isLocked: Boolean get() = !isFloatable
    set(value) {
        isFloatable = !value
    }

class StatusBar : JPanel(BorderLayout()) {
    companion object {
        var border = 2
    }

    val label: JLabel = JLabel()

    var text: String get() = label.text
        set (value) {
            previous = value
            label.text = value
        }

    init {
        label.border = BorderFactory.createEmptyBorder(0, StatusBar.border, 0, 0)
        add(label, BorderLayout.LINE_START)
    }

    fun perform(text: String) {
        label.text = text
    }

    fun reset() {
        label.text = previous
    }

    // previous text
    private var previous: String? = null
}

fun mnemonicLabel(text: String): JLabel {
    val result = Ixin.mnemonicOf(text)
    val label = JLabel(result.name)
    if (result.isEnable) {
        label.displayedMnemonic = result.mnemonic
        label.displayedMnemonicIndex = result.index
    }
    return label
}

var <T : JToolBar> T.isTextHidden: Boolean get() = components.any { it is AbstractButton && it.hideActionText }
    set(value) {
        components.forEach {
            if (it is AbstractButton) {
                it.hideActionText = value
            }
        }
    }

fun Component.withLabel(text: String): JLabel {
    val label = mnemonicLabel(text)
    label.labelFor = this
    return label
}

fun alignedPane(alignment: Int, space: Int, vararg components: Component): JPanel? {
    if (components.isEmpty()) {
        return null
    }

    val pane = JPanel()
    pane.layout = BoxLayout(pane, BoxLayout.LINE_AXIS)

    if (alignment != -1 && alignment != SwingConstants.LEFT) {
        pane.add(Box.createHorizontalGlue())
    }

    val end = components.size - 1
    for (ix in 0..end - 1) {
        pane.add(components[ix])
        pane.add(Box.createHorizontalStrut(space))
    }
    pane.add(components[end])

    if (alignment != -1 && alignment != SwingConstants.RIGHT) {
        pane.add(Box.createHorizontalGlue())
    }
    return pane
}

fun <T : Component> T.performOn(form: Form, provider: Provider<String>): T {
    addMouseListener(StatusPerformer(provider, form))
    return this
}

fun <T : Component> T.performOn(form: Form, text: String): T = performOn(form, Provider { text })

fun <T : Component> T.performOn(form: Form, action: Action): T = performOn(form, Provider {
    action[Action.LONG_DESCRIPTION] ?: ""
})

private class StatusPerformer(val provider: Provider<String>, val form: Form) : MouseAdapter() {
    private var closed = true

    override fun mouseEntered(e: MouseEvent) {
        showTip()
    }

    override fun mouseExited(e: MouseEvent) {
        closeTip()
    }

    override fun mouseReleased(e: MouseEvent) {
        closeTip()
    }

    private fun showTip() {
        val text = provider.provide()
        if (text.isNotEmpty()) {
            form.statusBar?.perform(text)
            closed = false
        }
    }

    private fun closeTip() {
        if (!closed) {
            form.statusBar?.reset()
            closed = true
        }
    }
}
<commit_msg>bugfix<commit_after>/*
 * Copyright 2015-2016 <NAME> <<EMAIL>>
 *
 * This file is part of IxIn.
 *
 * 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 pw.phylame.qaf.ixin

import pw.phylame.ycl.util.Provider
import java.awt.BorderLayout
import java.awt.Component
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import javax.swing.*

var JToolBar.isLocked: Boolean get() = !isFloatable
    set(value) {
        isFloatable = !value
    }

class StatusBar : JPanel(BorderLayout()) {
    companion object {
        var borderSize = 2
    }

    val label: JLabel = JLabel()

    var text: String get() = label.text
        set (value) {
            previous = value
            label.text = value
        }

    init {
        label.border = BorderFactory.createEmptyBorder(0, borderSize, 0, 0)
        add(JSeparator(), BorderLayout.PAGE_START)
        add(label, BorderLayout.LINE_START)
    }

    fun perform(text: String) {
        label.text = text
    }

    fun reset() {
        label.text = previous
    }

    // previous text
    private var previous: String? = null
}

fun mnemonicLabel(text: String): JLabel {
    val result = Ixin.mnemonicOf(text)
    val label = JLabel(result.name)
    if (result.isEnable) {
        label.displayedMnemonic = result.mnemonic
        label.displayedMnemonicIndex = result.index
    }
    return label
}

var <T : JToolBar> T.isTextHidden: Boolean get() = components.any { it is AbstractButton && it.hideActionText }
    set(value) {
        components.forEach {
            if (it is AbstractButton) {
                it.hideActionText = value
            }
        }
    }

fun Component.withLabel(text: String): JLabel {
    val label = mnemonicLabel(text)
    label.labelFor = this
    return label
}

fun alignedPane(alignment: Int, space: Int, vararg components: Component): JPanel? {
    if (components.isEmpty()) {
        return null
    }

    val pane = JPanel()
    pane.layout = BoxLayout(pane, BoxLayout.LINE_AXIS)

    if (alignment != -1 && alignment != SwingConstants.LEFT) {
        pane.add(Box.createHorizontalGlue())
    }

    val end = components.size - 1
    for (ix in 0..end - 1) {
        pane.add(components[ix])
        pane.add(Box.createHorizontalStrut(space))
    }
    pane.add(components[end])

    if (alignment != -1 && alignment != SwingConstants.RIGHT) {
        pane.add(Box.createHorizontalGlue())
    }
    return pane
}

fun <T : Component> T.performOn(form: Form, provider: Provider<String>): T {
    addMouseListener(StatusPerformer(provider, form))
    return this
}

fun <T : Component> T.performOn(form: Form, text: String): T = performOn(form, Provider { text })

fun <T : Component> T.performOn(form: Form, action: Action): T = performOn(form, Provider {
    action[Action.LONG_DESCRIPTION] ?: ""
})

private class StatusPerformer(val provider: Provider<String>, val form: Form) : MouseAdapter() {
    private var closed = true

    override fun mouseEntered(e: MouseEvent) {
        showTip()
    }

    override fun mouseExited(e: MouseEvent) {
        closeTip()
    }

    override fun mouseReleased(e: MouseEvent) {
        closeTip()
    }

    private fun showTip() {
        val text = provider.provide()
        if (text.isNotEmpty()) {
            form.statusBar?.perform(text)
            closed = false
        }
    }

    private fun closeTip() {
        if (!closed) {
            form.statusBar?.reset()
            closed = true
        }
    }
}

{'fraction_non_alphanumeric': 0.10782586814292903, 'fraction_numerical': 0.005284348263714142, 'mean_word_length': 2.5725842696629213, 'pattern_counts': {'":': 0, '<': 21, '<?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': '4151261', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2362, 'n_words': 666}