added a bunch more swing helper methods; such as an easy function for doing the dreaded grid bag layout and helper properties so its easy to set a width or height for size/preferredSize/minimumSize/maximumSize on components

This commit is contained in:
James Strachan
2012-09-17 12:04:42 +01:00
parent 9304ec103d
commit 65564537dd
3 changed files with 123 additions and 6 deletions
@@ -1,11 +1,59 @@
package kotlin.swing
import java.awt.Dimension
import java.awt.Component
import javax.swing.JComponent
var JComponent.description: String?
get() {
return getAccessibleContext()?.getAccessibleDescription()
}
set(value) {
getAccessibleContext()?.setAccessibleDescription(value)
}
get() {
return getAccessibleContext()?.getAccessibleDescription()
}
set(value) {
getAccessibleContext()?.setAccessibleDescription(value)
}
var JComponent.preferredWidth: Int
get() = getPreferredSize().width()
set(value) {
setPreferredSize(getPreferredSize().width(value))
}
var JComponent.preferredHeight: Int
get() = getPreferredSize().height()
set(value) {
setPreferredSize(getPreferredSize().height(value))
}
var Component.minimumWidth: Int
get() = getMinimumSize().width()
set(value) {
setMinimumSize(getMinimumSize().width(value))
}
var Component.minimumHeight: Int
get() = getMinimumSize().height()
set(value) {
setMinimumSize(getMinimumSize().height(value))
}
var Component.maximumWidth: Int
get() = getMaximumSize().width()
set(value) {
setMaximumSize(getMaximumSize().width(value))
}
var Component.maximumHeight: Int
get() = getMaximumSize().height()
set(value) {
setMaximumSize(getMaximumSize().height(value))
}
var Component.width: Int
get() = getSize().width()
set(value) {
setSize(getSize().width(value))
}
var Component.height: Int
get() = getSize().height()
set(value) {
setSize(getSize().height(value))
}
@@ -0,0 +1,32 @@
package kotlin.swing
import java.awt.Dimension
import java.awt.Component
import javax.swing.JComponent
/**
* Returns the width of the dimension or zero if its null
*/
fun Dimension?.width(): Int {
return if (this == null) 0 else this.width
}
/**
* Returns the height of the dimension or zero if its null
*/
fun Dimension?.height(): Int {
return if (this == null) 0 else this.height
}
/**
* Returns a new dimension with the same height and the given width
*/
fun Dimension?.width(newValue: Int): Dimension {
return Dimension(newValue, height())
}
/**
* Returns a new dimension with the same width and the given height
*/
fun Dimension?.height(newValue: Int): Dimension {
return Dimension(width(), newValue)
}
@@ -0,0 +1,37 @@
package kotlin.swing
import java.awt.GridBagConstraints
/**
* Helper function to create a [GridBagConstraints]
*/
fun gridBagContraints(gridx: Int? = null, gridy: Int? = null, fill: Int? = null, anchor: Int? = null,
gridwidth: Int? = null, gridheight: Int? = null,
weightx: Double? = null, weighty: Double? = null): GridBagConstraints {
val answer = GridBagConstraints()
if (gridx != null) {
answer.gridx = gridx
}
if (gridy != null) {
answer.gridy = gridy
}
if (fill != null) {
answer.fill = fill
}
if (anchor != null) {
answer.anchor = anchor
}
if (gridwidth != null) {
answer.gridwidth = gridwidth
}
if (gridheight != null) {
answer.gridheight = gridheight
}
if (weightx != null) {
answer.weightx = weightx
}
if (weighty != null) {
answer.weighty = weighty
}
return answer
}