Placing a ContextMenu on TextArea in AIR 1.5

The TextArea in Flex comes with its own contextual menu, so adding one does absolutely nothing but leave you wondering what the heck you are doing wrong. In the bug report there is a bit of a work around for AIR 1.5 which requires you to access the “textField” property of the TextArea and add your contextMenu to that item. Apparently there are not enough people using custom contextMenu’s to justify fixing the bug. The problem gets a bit trickier as the textField is protected in Flex Builder 3, so you can’t just do:

myTextArea.textfield.contextMenu = myMenu;

You need to expose or get at the textField to add the menu. You can do this by using mx_internal (careful, kid, or you’ll put your eye out with that thing!). So you can throw a little hack together to get your context menu on your text area:

private function createMenu():void
{
     var mainMenu:ContextMenu = new ContextMenu();

     var menuitem:ContextMenuItem = new ContextMenuItem("hello mom!");
     menuitem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleContextMenuEvent);
     mainMenu.addItem(menuitem);

     textInput.contextMenu = mainMenu;

     var txt:TextField = textInput.mx_internal::getTextField() as TextField;
     txt.contextMenu = mainMenu;
}

private function handleContextMenuEvent(event:ContextMenuEvent):void
{
    var menuitem:ContextMenuItem = event.target as ContextMenuItem;
    trace("context menu event: " + menuitem);
}

<mx:TextArea id="textInput"  width="100%" height="100%">

Notice that I added the contextMenu to two spots, the textField inside the TextArea, and the TextArea itself, this will show only the “hello mom!”. If you wanted to just add “hello mom!” to the existing menu options of the TextArea (copy/paste/select all), then you can just add the menu to the textField only.

I thought I might be able to accomplish the same behavior using an AIR NativeMenu/NativeMenuItem. Using a NativeMenu instead of a ContextMenu item on a TextArea throws this error:

ReferenceError: Error #1069: Property clipboardItems not found on flash.display.NativeMenu and there is no default value.
at flash.display::InteractiveObject/onContextMenuEvent()

The workaround would then be to create a NativeMenu popup and position on the cursor position of the TextArea.

private function createMenu():void
{
     var mainMenu:NativeMenu = new NativeMenu();

     var menuitem:NativeMenuItem = new NativeMenuItem("hello mom!");
     menuitem.addEventListener(Event.DISPLAYING, handleDisplaying);
     mainMenu.addItem(menuitem);

     mainMenu.display(NativeApplication.nativeApplication.activeWindow.stage, 20, 20);
}

private function handleDisplaying(event:Event):void
{
    trace("native menu displayed");
}

-Mr

PhoneSeek an Adobe AIR application to track GPS enabled mobile devices

PhoneSeek is an application to track any device that uses InstaMapper. InstaMapper is free real-time GPS tracking service. To use InstaMapper you need a GPS enabled device with the free InstaMapper tracking software installed along with a API Key from InstaMapper’s web site for each device you own. You can track mobile phones (iPhone, G1, Blackberry) or even automobiles.

Use Case

With PhoneSeek I can add my InstaMapper API Key for my phone and track it from the desktop. I can also add and track multiple devices. On my G1 phone, I installed the Android version of the InstaMapper tracking software. This software allows me to send my phone’s GPS data to InstaMapper service where I can view it either on their website or consume the data through InstaMapper’s web service. In PhoneSeek I just add the API Key to begin tracking the advice as soon as your phone starts to transmit GPS data.

Get PhoneSeek

Download the latest version from this location.

Development Background

PhoneSeek might be used to track devices from your family members, or maybe track down a lost or stolen phone. However, Phoneseek was basically created as a test project to learn PureMVC and also experiment with the new Google Maps API SWC for Adobe AIR. Previous versions of the Google Maps API SWC only worked for web-based applications.

Coming from Cairngorm to PureMVC was quite an experience. At first it was really painful because I had a tendency to over think things, or try to structure things more the Cairngorm ways (like dude, where are my delegates and commands).

I really had a hard time understanding where to put certain code, like update code, or code to set the window location on startup, and even how to pass around value objects used by more than one view. It took some time to get used to the PureMVC way, but all in all, its not a bad framework to work with. However, I still prefer Cairngorm and have since started to investigate and really enjoy Mate.

Credit for some of the graphics goes to DragonArtz Designs.

- Mister

Cairngorm Plugin for Flex

If you are a heavy user or closet fan of Cairngorm (like me) then you would be interested in knowing that there is a new Cairngorm plug-in out from Adobe that generates some of the Cairngorm code automatically from Flex Builder 3. Also read some of the notes that Brian Deitte wrote after attending the Adobe MAX session “RIA Development with Cairngorm: Tips from the Experts” which I unfortunately did not attend. Brian will also be on the newly formed Cairngorm Committee which will guide the future of Cairngorm

Cairngorm Plugin

Just one small problem I ran into when installing the plugin, I received the following error:

Cairngorm Plugin (0.0.6.200811131413) requires plug-in "org.eclipse.emf.codegen".

To resolve this, I selected the Europa Discovery Site from the wizard along with the Caingrom plugin option created by following the installation instructions. I then used the “Select Required” button to install the needed eclipse plugins for codegen that are required by the Cairngorm plug-in. This installs the EMF (Eclipse Modeling Framework).

-Mister

Reblog this post [with Zemanta]

Drop your framework?

After attending an interesting session at Adobe MAX 2008 titled “The Flex Architecture Face-off”. At first I though the speakers (Yakov Fain, Chafic Kazoun, Todd Anderson, and Joshua Noble) were going to debate which framework was the best, you know, Cairngorm, PureMVC, Mate, and others. Initially the speaker surveyed which frameworks developers were using and not surprisingly, most were using Cairngorm (followed by PureMVC, then Mate). Then the speaker asked who wasn’t using a framework at all, and I was again surprised to see that about half of the audience wasn’t using any framework. You see I come from the thought that you need to have a framework in your box of tools to build most projects, they help coordinate group efforts, and they offer support. I had always just assumed that most Flex developers would be using some type of framework and that the flavor of framework was the only point of contention.

What made this particular session interesting was how the discussion turned into a debate, not about one framework versus another, but a debate about if you want to use a framework at all. The panel was mostly split. Chafic and Yakov were obviously not for using frameworks, while Todd and Joshua recommended using frameworks. The reason for using a framework varied. Some developers use them because it helps to coordinate the work when you have groups of developers, others use them because the client had stipulated that it was a “best” practice to have a framework. This make sense because you know that even though projects have a development life cycle, they will also have a maintenance life cycle. This means that the next group of developers coming in after you will need to go through your code and using a framework seems to facilitate this process.

Continue reading