rearranging and docs
This commit is contained in:
@@ -86,21 +86,3 @@ fun <T> Component.bind(
|
|||||||
// initialize component with the property's value
|
// initialize component with the property's value
|
||||||
propertyToComponent()
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
39
src/main/kotlin/net/eksb/kswingutil/ext/BindExt.kt
Normal file
39
src/main/kotlin/net/eksb/kswingutil/ext/BindExt.kt
Normal 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 }
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package net.eksb.kswingutil.ext
|
package net.eksb.kswingutil.ext
|
||||||
|
|
||||||
import net.eksb.kswingutil.property.Property
|
|
||||||
import net.eksb.kswingutil.ext.bind
|
|
||||||
import java.awt.Component
|
import java.awt.Component
|
||||||
import java.awt.Frame
|
import java.awt.Frame
|
||||||
import java.awt.event.ComponentAdapter
|
import java.awt.event.ComponentAdapter
|
||||||
@@ -13,13 +11,14 @@ import javax.swing.SwingUtilities
|
|||||||
import kotlin.system.exitProcess
|
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 =
|
fun Component.getFrame(): Frame =
|
||||||
this as? Frame ?: SwingUtilities.getAncestorOfClass(Frame::class.java,this) as 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) {
|
fun Component.onDispose(block:()->Unit) {
|
||||||
addComponentListener(
|
addComponentListener(
|
||||||
object: ComponentAdapter() {
|
object: ComponentAdapter() {
|
||||||
@@ -38,6 +37,9 @@ fun Component.onDispose(block:()->Unit) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exit the JVM when this frame is closed.
|
||||||
|
*/
|
||||||
fun JFrame.exitOnClose() {
|
fun JFrame.exitOnClose() {
|
||||||
addWindowListener(object: WindowAdapter() {
|
addWindowListener(object: WindowAdapter() {
|
||||||
override fun windowClosed(e: WindowEvent) {
|
override fun windowClosed(e: WindowEvent) {
|
||||||
|
|||||||
@@ -4,10 +4,19 @@ import java.awt.Component
|
|||||||
import java.awt.event.MouseAdapter
|
import java.awt.event.MouseAdapter
|
||||||
import java.awt.event.MouseEvent
|
import java.awt.event.MouseEvent
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the supplied function whenever mouse button 1 is clicked on this
|
||||||
|
* component.
|
||||||
|
*/
|
||||||
fun Component.onClick1(block:(MouseEvent)->Unit) {
|
fun Component.onClick1(block:(MouseEvent)->Unit) {
|
||||||
onClick(MouseEvent.BUTTON1, block)
|
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) {
|
fun Component.onClick(button:Int?=null, block:(MouseEvent)->Unit) {
|
||||||
addMouseListener(object: MouseAdapter() {
|
addMouseListener(object: MouseAdapter() {
|
||||||
override fun mouseClicked(event: MouseEvent) {
|
override fun mouseClicked(event: MouseEvent) {
|
||||||
|
|||||||
@@ -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,
|
fun Container.button(constraints:Any?=null, text:String?=null, block:JButton.()->Unit = {}):JButton = add(constraints,
|
||||||
JButton(text),block)
|
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(
|
fun JComponent.onHover(
|
||||||
enter:(MouseEvent)->Unit,
|
enter:(MouseEvent)->Unit,
|
||||||
exit:(MouseEvent)->Unit
|
exit:(MouseEvent)->Unit
|
||||||
@@ -103,8 +107,11 @@ fun JComponent.onHover(
|
|||||||
exit.invoke(e)
|
exit.invoke(e)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a Property that is true when the mouse is hovered over this component.
|
||||||
|
*/
|
||||||
fun JComponent.hoverProperty(): Property<Boolean> =
|
fun JComponent.hoverProperty(): Property<Boolean> =
|
||||||
MutableProperty(false).apply {
|
MutableProperty(false).apply {
|
||||||
onHover(
|
onHover(
|
||||||
@@ -113,6 +120,9 @@ fun JComponent.hoverProperty(): Property<Boolean> =
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make this component's text bold.
|
||||||
|
*/
|
||||||
fun JComponent.bold() {
|
fun JComponent.bold() {
|
||||||
font = font.deriveFont(Font.BOLD)
|
font = font.deriveFont(Font.BOLD)
|
||||||
repaint()
|
repaint()
|
||||||
|
|||||||
@@ -37,3 +37,24 @@ fun <T> propertyFrom(vararg props:Property<*>, op:()->T): Property<T> =
|
|||||||
* Get a `Property<Boolean>` that is `!this.value`.
|
* Get a `Property<Boolean>` that is `!this.value`.
|
||||||
*/
|
*/
|
||||||
operator fun Property<Boolean>.not():Property<Boolean> = map { ! it }
|
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 }
|
||||||
|
|||||||
Reference in New Issue
Block a user