view src/name/blackcap/clipman/Misc.kt @ 52:c8ec2d7af3fb

Correct bad APPDATA code.
author davidb
date Tue, 14 Apr 2020 15:17:45 -0700
parents 3f8409470fdf
children
line wrap: on
line source

/*
 * Miscellaneous utility stuff.
 */
package name.blackcap.clipman

import java.awt.Dimension
import java.awt.Toolkit
import java.nio.charset.Charset
import javax.swing.*
import javax.swing.text.JTextComponent
import kotlin.annotation.*
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.*

/**
 * Name of the character set (encoding) we preferentially use for many
 * things.
 */
val CHARSET_NAME = "UTF-8"
val CHARSET = Charset.forName(CHARSET_NAME)

/**
 * Delegate that makes a var that can only be set once. This is commonly
 * needed in Swing, because some vars inevitably need to be declared at
 * outer levels but initialized in the Swing event dispatch thread.
 *
 * @param <T> type of the associated value
 */
class SetOnceImpl<T: Any>: ReadWriteProperty<Any?,T> {
    private var setOnceValue: T? = null

    override operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
        if (setOnceValue == null) {
            throw RuntimeException("${property.name} has not been initialized")
        } else {
            return setOnceValue!!
        }
    }

    @Synchronized
    override operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T): Unit {
        if (setOnceValue != null) {
            throw RuntimeException("${property.name} has already been initialized")
        }
        setOnceValue = value
    }
}

/**
 * Normal way to create a setOnce var:
 * var something: SomeType by setOnce()
 */
fun <T: Any> setOnce(): SetOnceImpl<T> = SetOnceImpl<T>()

/**
 * Run something in the Swing thread, asynchronously.
 * @param block lambda containing code to run
 */
fun inSwingThread(block: () -> Unit) {
    SwingUtilities.invokeLater(Runnable(block))
}

/**
 * Run something in the Swing thread, synchronously.
 * @param block lambda containing code to run
 */
fun inSynSwingThread(block: () -> Unit) {
    SwingUtilities.invokeAndWait(Runnable(block))
}

/**
 * Autosize a JTextComponent to a given width.
 * @param width the width
 */
fun JTextComponent.autoSize(width: Int): Unit {
    val SLOP = 10
    val dim = Dimension(width, width)
    preferredSize = dim
    size = dim
    val r = modelToView(document.length)
    preferredSize = Dimension(width, r.y + r.height + SLOP)
}

/**
 * Make a shortcut for a menu item, using the standard combining key
 * (control, command, etc.) for the system we're on.
 * @param key KeyEvent constant describing the key
 */
fun JMenuItem.makeShortcut(key: Int): Unit {
    val SC_KEY_MASK = Toolkit.getDefaultToolkit().menuShortcutKeyMask
    setAccelerator(KeyStroke.getKeyStroke(key, SC_KEY_MASK))
}

/**
 * Given a MenuElement object, get the item whose text matches the
 * specified text.
 * @param text to match
 * @return first matched element, null if no match found
 */
fun MenuElement.getItem(name: String) : JMenuItem? {
    subElements.forEach {
        val jMenuItem = it.component as? JMenuItem
        if (jMenuItem?.text == name) {
            return jMenuItem
        }
    }
    return null
}