From 962a3d29f5cfb295a424db6058dd60c6899feb33 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Fri, 21 Sep 2012 15:48:01 +0100 Subject: [PATCH] deal with null events being fired (a common way to just fire actions) and allow optional description and icon parameters to be specified --- .../src/main/kotlin/kotlin/swing/Actions.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Actions.kt b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Actions.kt index 66d9db06f24..e2e96e647ce 100644 --- a/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Actions.kt +++ b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Actions.kt @@ -3,17 +3,25 @@ package kotlin.swing import java.awt.event.ActionEvent import javax.swing.AbstractAction import javax.swing.Action +import javax.swing.Action.* import javax.swing.Icon /** * Helper method to create an action from a function */ -fun action(text: String, icon: Icon? = null, fn: (ActionEvent) -> Unit): Action { - return object: AbstractAction(text, icon) { +fun action(text: String, description: String? = null, mnemonic: Int? = null, icon: Icon? = null, fn: (ActionEvent) -> Unit): Action { + val answer = object: AbstractAction(text, icon) { public override fun actionPerformed(e: ActionEvent?) { - if (e != null) { - (fn)(e) + val event: ActionEvent = if (e != null) { + e + } else { + // lets create a dummy event + ActionEvent(this, ActionEvent.ACTION_PERFORMED, text) } + (fn)(event) } } + if (description != null) answer.putValue(SHORT_DESCRIPTION, description) + if (mnemonic != null) answer.putValue(MNEMONIC_KEY, mnemonic) + return answer } \ No newline at end of file