I had to create an application menu for one of my projects which would run
on an external XML resource. The idea was to abstract away as much of the
core NativeMenu and NativeMenuItem API and be able to create recursive menus
in a globally-understood format. The result was the AppMenu class which I
have pasted below. Constructive criticism is welcome.
Menu-generator class
package com.pranavnegandhi.pdl.ui
{
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.events.Event;
/**
* An XML-driven application menu generator
*
* @author Pranav Negandhi ([email protected])
*/
public class AppMenu extends NativeMenu
{
/**
* Object constructor
*/
public function AppMenu(xmlMenu:XML)
{
this.m_createMenuFromXML(xmlMenu);
}
/**
* Recursive method to generate a menu hierarchy out of an XML resource
*
* @param parent
* @param m
*
* @private
*/
private function m_createMenuFromXML(parent:XML, m:NativeMenuItem =
null):void
{
var children:XMLList;
var n:NativeMenuItem;
var l:String;
var c:int;
var i:int;
children = parent.children();
c = children.length();
for (i = 0; i < c; i++)
{
l = children[...@label;
n = new NativeMenuItem(l);
n.name = l;
/**
* If the parent menu (m) is null, it means that the new
* NativeMenuItem has to be added to the application root
* menu and can be added directly.
*
* If it is not null then it means that the new NativeMenuItem
* is being added to an existing menu item in the menu
* hierarchy. Hence, the submenu of m must be created first
* and then the new menu item should be added to that
* submenu.
*/
if (null == m)
this.addItem(n);
else
{
if (null == m.submenu) m.submenu = new NativeMenu();
m.submenu.addItem(n);
}
// Recurse the function if the current XML node has any children
if (children[i].children().length())
this.m_createMenuFromXML(children[i], n);
}
}
}
}
Usage example
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:ui="com.pranavnegandhi.pdl.ui.*" initialize="m_initialize()">
<mx:Script>
<![CDATA[
import com.pranavnegandhi.pdl.ui.AppMenu
import flash.display.NativeMenu;
import flash.events.Event;
[Embed("/assets/text/menu_structure.xml",
mimeType="application/octet-stream")]
private static const MENU_STRUCTURE:Class;
private function m_initialize():void
{
var xmlMenu:XML;
var menuFile:NativeMenu;
var menuHelp:NativeMenu;
xmlMenu = new XML(new Main.MENU_STRUCTURE());
this.nativeWindow.menu = new AppMenu(xmlMenu);
menuFile = AppMenu(this.nativeWindow.menu).getItemByName("&File").submenu;
menuFile.getItemByName("&Logout").addEventListener(Event.SELECT,
this.m_dlgLogoutCreate);
menuFile.getItemByName("&Exit").addEventListener(Event.SELECT,
this.m_dlgQuitCreate);
menuHelp = AppMenu(this.nativeWindow.menu).getItemByName("&Help").submenu;
menuHelp.getItemByName("&About").addEventListener(Event.SELECT,
this.m_dlgAboutCreate);
}
private function m_dlgAboutCreate(e:Event):void
{
trace("About");
}
private function m_dlgLogoutCreate(e:Event):void
{
trace("Logout");
}
private function m_dlgQuitCreate(e:Event):void
{
trace("Quit");
}
]]>
</mx:Script>
</mx:WindowedApplication>
Sample menu structure
<?xml version="1.0" encoding="utf-8" ?>
<menu>
<menu label="&File">
<menu label="&Logout" />
<menu label="&Exit" />
</menu>
<menu label="&View" />
<menu label="&Help">
<menu label="&About" />
</menu>
</menu>
HTH.
.p
--
You received this message because you are subscribed to the Google Groups "Flex
India Community" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/flex_india?hl=en.