rearranging and docs

This commit is contained in:
2025-07-17 02:04:28 +00:00
parent 1bb524646e
commit 57cd742d34
6 changed files with 87 additions and 24 deletions

View File

@@ -86,21 +86,3 @@ fun <T> Component.bind(
// initialize component with the property's value
propertyToComponent()
}
fun JComponent.bindTooltipText(property:Property<String>) {
bind(property) {
toolTipText = it
}
}
fun JComponent.bindBackground(property:Property<Color>) {
bind(property) {
background = it
}
}
fun JComponent.bindBorder(property:Property<Border>) {
bind(property) {
border = it
}
}

View File

@@ -0,0 +1,39 @@
package net.eksb.kswingutil.ext
import net.eksb.kswingutil.property.Property
import java.awt.Color
import java.awt.Component
import javax.swing.JComponent
import javax.swing.border.Border
/**
* Bind the supplied property to this component's tooltip text.
*/
fun JComponent.bindTooltipText(property:Property<String>) {
bind(property) {
toolTipText = it
}
}
/**
* Bind the supplied property to this component's background color.
*/
fun JComponent.bindBackground(property:Property<Color>) {
bind(property) {
background = it
}
}
/**
* Bind the supplied property to this component's border.
*/
fun JComponent.bindBorder(property:Property<Border>) {
bind(property) {
border = it
}
}
/**
* Bind this component to be enabled when [property] is enabled.
*/
fun Component.enabledWhen(property:Property<Boolean>) = bind(property) { isEnabled = it }

View File

@@ -1,7 +1,5 @@
package net.eksb.kswingutil.ext
import net.eksb.kswingutil.property.Property
import net.eksb.kswingutil.ext.bind
import java.awt.Component
import java.awt.Frame
import java.awt.event.ComponentAdapter
@@ -13,13 +11,14 @@ import javax.swing.SwingUtilities
import kotlin.system.exitProcess
/**
* Bind this component to be enabled when [property] is enabled.
* Get the frame that this component is or is in.
*/
fun Component.enabledWhen(property:Property<Boolean>) = bind(property) { isEnabled = it }
fun Component.getFrame(): Frame =
this as? Frame ?: SwingUtilities.getAncestorOfClass(Frame::class.java,this) as Frame
/**
* Execute the supplied function when this component is disposed.
*/
fun Component.onDispose(block:()->Unit) {
addComponentListener(
object: ComponentAdapter() {
@@ -38,6 +37,9 @@ fun Component.onDispose(block:()->Unit) {
})
}
/**
* Exit the JVM when this frame is closed.
*/
fun JFrame.exitOnClose() {
addWindowListener(object: WindowAdapter() {
override fun windowClosed(e: WindowEvent) {

View File

@@ -4,10 +4,19 @@ import java.awt.Component
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
/**
* Execute the supplied function whenever mouse button 1 is clicked on this
* component.
*/
fun Component.onClick1(block:(MouseEvent)->Unit) {
onClick(MouseEvent.BUTTON1, block)
}
/**
* Execute the supplied function whenever the specified mouse button
* is clicked on this component.
* If [button] is null, execute the function for any mouse button.
*/
fun Component.onClick(button:Int?=null, block:(MouseEvent)->Unit) {
addMouseListener(object: MouseAdapter() {
override fun mouseClicked(event: MouseEvent) {

View File

@@ -91,6 +91,10 @@ fun <T> JComboBox<T>.bind(property:MutableProperty<T>) {
fun Container.button(constraints:Any?=null, text:String?=null, block:JButton.()->Unit = {}):JButton = add(constraints,
JButton(text),block)
/**
* Execute the [enter] function when the mouse enters this component,
* and the [exit] function when the mouse exits this component.
*/
fun JComponent.onHover(
enter:(MouseEvent)->Unit,
exit:(MouseEvent)->Unit
@@ -103,8 +107,11 @@ fun JComponent.onHover(
exit.invoke(e)
}
})
}
/**
* Create a Property that is true when the mouse is hovered over this component.
*/
fun JComponent.hoverProperty(): Property<Boolean> =
MutableProperty(false).apply {
onHover(
@@ -113,6 +120,9 @@ fun JComponent.hoverProperty(): Property<Boolean> =
)
}
/**
* Make this component's text bold.
*/
fun JComponent.bold() {
font = font.deriveFont(Font.BOLD)
repaint()

View File

@@ -37,3 +37,24 @@ fun <T> propertyFrom(vararg props:Property<*>, op:()->T): Property<T> =
* Get a `Property<Boolean>` that is `!this.value`.
*/
operator fun Property<Boolean>.not():Property<Boolean> = map { ! it }
/**
* Get a `Property<Boolean>` that is this property's value or the supplied
* property's value.
*/
fun Property<Boolean>.or(other:Property<Boolean>): Property<Boolean> =
propertyFrom(this, other) { this.value || other.value }
/**
* Get a `Property<Boolean>` that is this property's value and the supplied
* property's value.
*/
fun Property<Boolean>.and(other:Property<Boolean>): Property<Boolean> =
propertyFrom(this, other) { this.value && other.value }
/**
* Get a `Property<Boolean>` that is this property's value xor the supplied
* property's value.
*/
fun Property<Boolean>.xor(other:Property<Boolean>): Property<Boolean> =
propertyFrom(this, other) { this.value xor other.value }