diff --git a/src/main/kotlin/net/eksb/kswingutil/ext/Bind.kt b/src/main/kotlin/net/eksb/kswingutil/ext/Bind.kt index 6147210..c635ac9 100644 --- a/src/main/kotlin/net/eksb/kswingutil/ext/Bind.kt +++ b/src/main/kotlin/net/eksb/kswingutil/ext/Bind.kt @@ -86,21 +86,3 @@ fun Component.bind( // initialize component with the property's value propertyToComponent() } - -fun JComponent.bindTooltipText(property:Property) { - bind(property) { - toolTipText = it - } -} - -fun JComponent.bindBackground(property:Property) { - bind(property) { - background = it - } -} - -fun JComponent.bindBorder(property:Property) { - bind(property) { - border = it - } -} diff --git a/src/main/kotlin/net/eksb/kswingutil/ext/BindExt.kt b/src/main/kotlin/net/eksb/kswingutil/ext/BindExt.kt new file mode 100644 index 0000000..e908ae5 --- /dev/null +++ b/src/main/kotlin/net/eksb/kswingutil/ext/BindExt.kt @@ -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) { + bind(property) { + toolTipText = it + } +} + +/** + * Bind the supplied property to this component's background color. + */ +fun JComponent.bindBackground(property:Property) { + bind(property) { + background = it + } +} + +/** + * Bind the supplied property to this component's border. + */ +fun JComponent.bindBorder(property:Property) { + bind(property) { + border = it + } +} + +/** + * Bind this component to be enabled when [property] is enabled. + */ +fun Component.enabledWhen(property:Property) = bind(property) { isEnabled = it } diff --git a/src/main/kotlin/net/eksb/kswingutil/ext/Component.kt b/src/main/kotlin/net/eksb/kswingutil/ext/Component.kt index 23b1fd8..42d65e2 100644 --- a/src/main/kotlin/net/eksb/kswingutil/ext/Component.kt +++ b/src/main/kotlin/net/eksb/kswingutil/ext/Component.kt @@ -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) = 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) { diff --git a/src/main/kotlin/net/eksb/kswingutil/ext/Events.kt b/src/main/kotlin/net/eksb/kswingutil/ext/Events.kt index f167c36..1475545 100644 --- a/src/main/kotlin/net/eksb/kswingutil/ext/Events.kt +++ b/src/main/kotlin/net/eksb/kswingutil/ext/Events.kt @@ -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) { diff --git a/src/main/kotlin/net/eksb/kswingutil/ext/Ext.kt b/src/main/kotlin/net/eksb/kswingutil/ext/Ext.kt index 4f1b551..610e22f 100644 --- a/src/main/kotlin/net/eksb/kswingutil/ext/Ext.kt +++ b/src/main/kotlin/net/eksb/kswingutil/ext/Ext.kt @@ -91,6 +91,10 @@ fun JComboBox.bind(property:MutableProperty) { 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 = MutableProperty(false).apply { onHover( @@ -113,6 +120,9 @@ fun JComponent.hoverProperty(): Property = ) } +/** + * Make this component's text bold. + */ fun JComponent.bold() { font = font.deriveFont(Font.BOLD) repaint() diff --git a/src/main/kotlin/net/eksb/kswingutil/property/PropertyExt.kt b/src/main/kotlin/net/eksb/kswingutil/property/PropertyExt.kt index 6d91914..378cbc1 100644 --- a/src/main/kotlin/net/eksb/kswingutil/property/PropertyExt.kt +++ b/src/main/kotlin/net/eksb/kswingutil/property/PropertyExt.kt @@ -37,3 +37,24 @@ fun propertyFrom(vararg props:Property<*>, op:()->T): Property = * Get a `Property` that is `!this.value`. */ operator fun Property.not():Property = map { ! it } + +/** + * Get a `Property` that is this property's value or the supplied + * property's value. + */ +fun Property.or(other:Property): Property = + propertyFrom(this, other) { this.value || other.value } + +/** + * Get a `Property` that is this property's value and the supplied + * property's value. + */ +fun Property.and(other:Property): Property = + propertyFrom(this, other) { this.value && other.value } + +/** + * Get a `Property` that is this property's value xor the supplied + * property's value. + */ +fun Property.xor(other:Property): Property = + propertyFrom(this, other) { this.value xor other.value }