From a6e1fbe4683d07603402f57a4af89c2be8f449e4 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Wed, 6 Feb 2008 01:03:55 +0000 Subject: [PATCH] Made my CommandButton factory method the constructor. I had initially been hesitant to do this because of the parameter that can be one of two things, and the Java side of me wanted to hold on to compile-time checking, but the convenience is too damn convenient. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@408 ed5b42cb-e716-0410-a449-f6a68f950b19 --- src/as/com/threerings/flex/ChatControl.as | 4 +-- src/as/com/threerings/flex/CommandButton.as | 36 ++++++------------- .../com/threerings/flex/CommandLinkButton.as | 19 ++++++++-- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/as/com/threerings/flex/ChatControl.as b/src/as/com/threerings/flex/ChatControl.as index 0b0dd8a5..e9f4a091 100644 --- a/src/as/com/threerings/flex/ChatControl.as +++ b/src/as/com/threerings/flex/ChatControl.as @@ -59,9 +59,7 @@ public class ChatControl extends HBox addChild(_txt = new ChatInput()); _txt.addEventListener(FlexEvent.ENTER, sendChat, false, 0, true); - _but = new CommandButton(); - _but.label = sendButtonLabel; - _but.setCallback(sendChat); + _but = new CommandButton(sendButtonLabel, sendChat); addChild(_but); if (!isNaN(height)) { diff --git a/src/as/com/threerings/flex/CommandButton.as b/src/as/com/threerings/flex/CommandButton.as index 1ecbb409..085097d3 100644 --- a/src/as/com/threerings/flex/CommandButton.as +++ b/src/as/com/threerings/flex/CommandButton.as @@ -34,37 +34,23 @@ import com.threerings.util.CommandEvent; public class CommandButton extends Button { /** - * A convenient factory method for creating CommandButtons. + * Create a command button. * - * Usage: addChild(CommandButton.create("OK", function () :void { - * trace("ok clicked"); - * })); + * @param label the label text for the button. + * @param cmdOrFn either a String, which will be the CommandEvent command to dispatch, + * or a function, which will be called when clicked. + * @param arg the argument for the CommentEvent or the function. If the arg is an Array + * then those parameters are used for calling the function. */ - public static function create ( - label :String, cmdOrFn :* = null, arg :Object = null) :CommandButton + public function CommandButton (label :String = null, cmdOrFn :* = null, arg :Object = null) { - var cb :CommandButton = new CommandButton(); - cb.label = label; - if (cmdOrFn is Function) { - cb.setCallback(cmdOrFn as Function, arg) - - } else if (cmdOrFn is String) { - cb.setCommand(String(cmdOrFn), arg); - - } else if (cmdOrFn != null) { + if (cmdOrFn != null && !(cmdOrFn is String) && !(cmdOrFn is Function)) { // runtime errors suck, but this is actionscript throw new Error("cmdOrFn must be a String or Function."); } - - return cb; - } - - /** - * Create a command button. - */ - public function CommandButton (cmd :String = null, arg :Object = null) - { - setCommand(cmd, arg); + this.label = label; + _cmdOrFn = cmdOrFn; + _arg = arg; buttonMode = true; } diff --git a/src/as/com/threerings/flex/CommandLinkButton.as b/src/as/com/threerings/flex/CommandLinkButton.as index 2548fff4..0fa5afb4 100644 --- a/src/as/com/threerings/flex/CommandLinkButton.as +++ b/src/as/com/threerings/flex/CommandLinkButton.as @@ -33,9 +33,24 @@ import com.threerings.util.CommandEvent; */ public class CommandLinkButton extends LinkButton { - public function CommandLinkButton (cmd :String = null, arg :Object = null) + /** + * Create a command link button. + * + * @param label the label text for the button. + * @param cmdOrFn either a String, which will be the CommandEvent command to dispatch, + * or a function, which will be called when clicked. + * @param arg the argument for the CommentEvent or the function. If the arg is an Array + * then those parameters are used for calling the function. + */ + public function CommandLinkButton (label :String = null, cmdOrFn :* = null, arg :Object = null) { - setCommand(cmd, arg); + if (cmdOrFn != null && !(cmdOrFn is String) && !(cmdOrFn is Function)) { + // runtime errors suck, but this is actionscript + throw new Error("cmdOrFn must be a String or Function."); + } + this.label = label; + _cmdOrFn = cmdOrFn; + _arg = arg; } /**