Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Right Click Context Menu Option

leave a comment »

Hey! I was working on a project where I needed to use different ContextMenus on different objects. Hence to make my life easier, I developed a class for RightClickMenus. I would like to share it as this may help some one to speed up development.

I have not placed comments in the RightClickMenus class as the method API names are self-explanatory.

//import statement
import com.utils.RightClickMenus;

//Declare RightClickMenus Instance
//pass the object on which context menus needs tobe viewed
//pass a boolean to indicate whether you want to hide the buildInItems
var rtClickMenus:RightClickMenus = new RightClickMenus( this, false );

//Adding Custom menu Items
//pass menu names
//pass menu click funtion name if you want to add an handler
//pass a boolean value if you want a separator above this menu item
rtClickMenus.addMenuItem( “Menu 1”, onMenuCall_1 );
rtClickMenus.addMenuItem( “Developed by deepanjandas”, null, true );
rtClickMenus.addMenuItem( “Menu 2”, onMenuCall_2 );
rtClickMenus.addMenuItem( “© Deepanjan” );

//Add a separator at a particular position
rtClickMenus.addSeparatorAt( 3 );

//Remove a custom menu item from a particular position
//rtClickMenus.removeMenuItemAt( 4 );

//Remove a separator from a particular position
//rtClickMenus.removeSeparatorAt( 3 );

//Remove all custom menu items
//rtClickMenus.removeAllItems();

//ContentMenuItem select event handlers
function onMenuCall_1( event:Event ) {
trace( “onMenuCall_1 called” );
}
function onMenuCall_2( event:ContextMenuEvent ) {
trace( “onMenuCall_2 called” );
}

/**************** RightClickMenus.as ********************/

package com.utils
{
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.ui.ContextMenuBuiltInItems;
import flash.events.ContextMenuEvent;
import flash.system.System;

public class RightClickMenus
{
private var contectMenuObject:Object;
private var showBuildInItems:Boolean;
private var menuCollection:Array;
private var customContextMenu:ContextMenu;

public function RightClickMenus( object:Object, showBuildInItems:Boolean=false )
{
contectMenuObject = object;
menuCollection = [];
this.showBuildInItems = showBuildInItems;

customContextMenu = new ContextMenu();

if ( !showBuildInItems )
customContextMenu.hideBuiltInItems();
}

public function addMenuItemAt( position:int, _name:String, listener:Function=null, separator:Boolean=false ):void
{
var item:ContextMenuItem = new ContextMenuItem( _name, separator );
if( listener != null )
item.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, listener );

if ( position < 0 ) {
menuCollection.push( item );
} else {
var startArray:Array = menuCollection.slice( 0, position-1 );
var endArray:Array = menuCollection.slice( position );
menuCollection = [];
menuCollection.join( startArray );
menuCollection.push( item );
menuCollection.join( endArray );
}

buildCustomMenus();
}

public function addMenuItem( _name:String, listener:Function=null, separator:Boolean=false ):void
{
addMenuItemAt( -1, _name, listener, separator )
}

public function removeMenuItemAt( position:int ):void
{
menuCollection.splice( position – 1, 1 );

removeSeparatorAt( position );

buildCustomMenus();
}

public function removeAllItems():void
{
menuCollection = [];

buildCustomMenus();
}

public function disableMenuAt( position:int ):void
{
if ( position < 1 || position > menuCollection.length )
return;

var item:ContextMenuItem = menuCollection[( position – 1 )];
item.enabled = false;
}

public function enableMenuAt( position:int ):void
{
if ( position < 1 || position > menuCollection.length )
return;

var item:ContextMenuItem = menuCollection[( position – 1 )];
item.enabled = true;
}

public function removeSeparatorAt( position:int ):void
{
if (menuCollection[( position )] == null) {
return;
}
var item:ContextMenuItem = menuCollection[( position )];
item.separatorBefore = false;
}

public function addSeparatorAt( position:int ):void
{
if ( position < 1 || position > menuCollection.length )
return;

var item:ContextMenuItem = menuCollection[( position )];
item.separatorBefore = true;
}

private function buildCustomMenus():void
{
customContextMenu.customItems = [];
for ( var i:int = 0; i < menuCollection.length; i++ ) {
customContextMenu.customItems.push( menuCollection[i] );
}
contectMenuObject.contextMenu = customContextMenu;
}
}
}

Advertisements

Written by Deepanjan Das

June 19, 2010 at 10:18 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: