RSS LinkedIn Twitter

Flex Menu Monkey Patch: Allowing a Branch to be Selected

May 1st, 2009 Categories: Actionscript, Flex 2, Flex 3, Monkey Patching

So the interaction designers decided that the branches of a menu needed to be selectable (picture a warped checkbox tree, only it’s a menu… and there aren’t any checkboxes). The default behavior of a Menu in Flex can be described as follows:

1. If you click on a leaf, the leaf is selected.
2. If you click on a branch, the sub menu opens up.
3. If you hover over a branch, the sub menu opens up.

I needed to change #2 to select the branch.

The first thing that I did was sublcass Menu (we’ll call it NewMenu), and overrode (with copies) two obvious methods on the Menu class:

[as3]
override protected function mouseUpHandler(event : MouseEvent) : void
override protected function mouseDownHandler(event : MouseEvent) : void
[/as3]

In those two methods you’ll need to remove any expressions containing something similar to the following:

[as3]_dataDescriptor.isBranch(item)[/as3]

This will allow the branches of your sub class to be treated as leaves. The problem is that Menu class contains this line:

[as3]private var subMenu:Menu;[/as3]

which forces all sub menus to be of type Menu– the only NewMenu will be at the very top! We need all sub menus to be NewMenus. So this is where the monkey patching comes in. You have to change subMenu to a protected variable. Then in your NewMenu class, you can override the function:

[as3]mx_internal override function openSubMenu(row : IListItemRenderer) : void[/as3]

And change the line:
[as3]menu = new Menu();[/as3]
to
[as3]menu = new NewMenu();[/as3]

And you’re done! The reason you need subMenu to be protected is because later on in the method you assign menu to subMenu, and you couldn’t do that from a subclass when it was private. Voila. Interaction designers satisfied.

Tags: , , , ,

4 Responses to “Flex Menu Monkey Patch: Allowing a Branch to be Selected”

  1. KonstantinMiller
    July 6th, 2009 at 14:07
    1

    How soon will you update your blog? I’m interested in reading some more information on this issue.

  2. Tonibache
    March 8th, 2010 at 10:19
    2

    Is it possible to get the source code of this extended Menu ?

  3. jwd
    March 8th, 2010 at 10:28
    3

    http://googolflex.com/content/Menu.as

    This is the only code I have, which might not have the changes… let me know if it doesn’t and I’ll try searching a bit harder.

  4. Rajesh
    June 15th, 2011 at 11:02
    4

    Can you list the steps at how we can use this Menu file in the project.

Leave a Comment

*