The Essential Guide to Flash CS4 AIR Development book is oriented to Flash developers interested in building desktop applications via Adobe AIR. You can order The Essential Guide to Flash CS4 AIR Development on Amazon or buy it on local bookstore.
This is the sixth part of the series dedicated to AIR menus (Chapter
6 of the Essential Guide to Flash CS4 AIR Development). You can read
the previous articles here:
- Creating AIR submenus for a native menu with Flash CS4 - Part 2 (The Essential Guide to Flash CS4 AIR Development Highlight)
- Creating AIR native menu with Flash CS4 - Part 1 (The Essential Guide to Flash CS4 AIR Development Highlight)
- Using the menuItemSelected() method with Flash CS4
- Working with AIR Application and Window menus
- Accessing the AIR XML file descriptor through an AIR menu item with Flash CS4
As mentioned at the beginning of this chapter, context menus appear at the cursor when the user right- clicks (on Windows) or Ctrl- clicks (on the Mac). Contrary to Flash applications for the Web, AIR applications don’t offer any default context menus, so it’s up to the developer to create and populate context menus for the application if necessary.
Context menus can be created with two different ActionScript classes:
- The NativeMenu class
- The ContextMenu class
You can only use the NativeMenu class for AIR applications, whereas you can use the ContextMenu class for both AIR applications and those that have been created to be executed in a web environment with Flash Player. The ContextMenu class is less flexible and offers fewer possibilities to developers, regardless of the fact that it extends the NativeMenu class.
The ContextMenu class extends the NativeMenu class in AIR applications as well as in traditional Flash applications. The NativeMenu class can be used only for AIR applications. Now you can create an AIR application that uses both types of context menus.
Creating context menus
In this section, you will create native and nonnative context menus. Start by opening the ch06p03.fla project in Flash CS4. You’ll notice that the stage of the project contains the following two buttons, in addition to the output TextArea:
- nativeBtn
- contextBtn
Next, you’ll associate a different context menu with each button and use the TextArea as a console for the application. Open the document class that is associated with the Flash project by clicking the Edit class definition icon in the Document Properties panel. Figure 6-9 displays the stage of the application.
The Ch6p03.as class starts by declaring its namespace and the external classes it needs to import for the correct execution of the application. Here’s the code:
package com.comtaste.foed.essentialair.chapter6
{
import fl.controls.Button;
import fl.controls.TextArea;
import flash.display.MovieClip;
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.events.ContextMenuEvent;
import flash.events.Event;
import flash.filesystem.File;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
public class Ch06p03 extends MovieClip
{
After declaring the external classes, you have to list the necessary class variables. These variables include the components on the stage of the Flash project. Finally, you need to show the instances of the two menus that you will build and populate. The code is as follows:
// onstage components
public var nativeBtn:Button;
public var contextBtn:Button;
public var output:TextArea;
// class properties
private var nativeMenuRoot:NativeMenu;
private var contextMenuRoot:ContextMenu;
Continue by defining the class constructor method and executing the super constructor class. You want to build two context menus in this application: one that uses AIR’s native menus, and another that uses context menus of the ContextMenu class , which can also be used in Flash applications for the Web.
public function ch06p03()
{
super();
Next, launch the createNativeMenu() method . This method will instantiate and populate the native menu, which will be associated with the contextMenu property of the nativeBtn button on the stage.
// generate native menu to use
createNativeMenu();
// assign native menu to right- click on button
nativeBtn.contextMenu = nativeMenuRoot;
Next, you execute the createContextMenu() method to instantiate and populate a context menu of the ContextMenu class . This menu will be stored in the contextMenuRoot class variable and also assigned to the contextMenu property of the contextBtn button . Here’s the code:
// generate context menu to use
createContextMenu();
// assign context menu to right- click on button
contextBtn.contextMenu = contextMenuRoot;
}
Once you’ve completed the execution of the constructor method, your application will be ready. Each of the two buttons on the stage of the Flash project will have its own context menu. The nativeBtn button will use a context menu based on the instance of the NativeMenu class. The contextBtn button will use an instance of the ContextMenu class for its context menu.
This is the complete ActionScript class:
package com.comtaste.foed.essentialair.chapter6
{
import fl.controls.Button;
import fl.controls.TextArea;
import flash.display.MovieClip;
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.events.ContextMenuEvent;
import flash.events.Event;
import flash.filesystem.File;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
public class Ch06p03 extends MovieClip
{
// onstage components
public var nativeBtn:Button;
public var contextBtn:Button;
public var output:TextArea;
// class properties
private var nativeMenuRoot:NativeMenu;
private var contextMenuRoot:ContextMenu;
public function Ch06p03()
{
super();
// generate native menu to use
createNativeMenu();
// assign native menu to right- click on button
nativeBtn.contextMenu = nativeMenuRoot;
// generate context menu to use
createContextMenu();
// assign context menu to right- click on button
contextBtn.contextMenu = contextMenuRoot;
}
}
}
xdfc
Posted by: retro jordan | December 01, 2010 at 09:53 AM
regardless of the fact that it extends the NativeMenu class.
Posted by: Gangmu | November 12, 2010 at 06:37 AM
New properties also give you more information about the printer, such as printable area, whether the printer will print in color, and whether the print job is currently active.friend,it's my first time to come to your belong,but i was deeply inpressed by it.
For your attractive contents i would visit your place continuously.
Posted by: Air Jordan shoes | October 31, 2010 at 02:29 PM