Flex: Manually centering a popup based on browser width & height

September 3rd, 2010 Erich Cervantez No comments

Most Flex developers know that the default PopUpManager in Flex has a centerPopUp method that allows you to center the popup over the display object passed in through the method signature:

1
2
3
var _popup:MyPopup = new Popup();
PopUpManager.addPopUp( _popup, this );
PopUpManager.centerPopUp( this );

You may even switch out the “this” reference and replace with “parent” or “parent.parentApplication” to center the popup on the entire application.

This can be especially true if you’re opening a popup from a small component in your application and don’t necessary want the popup to get centered on that one single component.

Unfortunately, if the dimensions of your parent Application are larger than the dimensions of the viewable browser window, you will have to center the popup manually rather than use the built-in centerPopUp() function.

Here’s how I center popups manually in Flex:

First, in your HTML template, I create a JavaScript function to return the browser width and height. I found this handy function on the web somewhere and it seems to do the trick:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function getBrowserHeight()
{
	var winW = 630, winH = 460;
 
	if (parseInt(navigator.appVersion)>3) {
	 if (navigator.appName=="Netscape") {
	  winW = window.innerWidth;
	  winH = window.innerHeight;
	 }
	 if (navigator.appName.indexOf("Microsoft")!=-1) {
	  winW = document.body.offsetWidth;
	  winH = document.body.offsetHeight;
	 }
	}
 
	return winH;
}

In this case, it’s only returning height (which I’m mostly interested in), however you can modify it to return both attributes if you’d like.

In Flex, I make a call to JavaScript using ExternalInterface during application startup and store that value somewhere (say on the Model) where I can access it later:

1
model.browserHeight = ExternalInterface.call( "getBrowserHeight" );

Now, whenever I open a popup, I use the browserHeight I’ve stored on the Model and manually compute a centered position on the screen:

1
2
3
4
5
6
7
8
9
10
/**
 * Opens this popup
 */
public static function open( parent:UIComponent, model:MyModel) : void
{
	var _popup:MyPopup= new MyPopup();				
	PopUpManager.addPopUp( _popup, parent.parentApplication as DisplayObject, true );
	_popup.y = ( model.browserHeight / 2 ) - ( _popup.height / 2 );
	_popup.x = ( ((parent.parentApplication.width) / 2) - (_popup.width / 2) ); 
}

In this example, I place a static function within the popup class itself, which nicely encapsulates that logic. I also pass in some UIComponent. This is used mostly to get access to the parentApplication object, which I can use to figure out the application’s total width.

As you can see, by dividing the application width and height, then subtracting the popup’s width and height, you can manually center the popup in the visible area of the browser regardless of the application’s full height (which could be thousands of pixels high in some scrollable instances).

Opening the popup from a view is as simple as calling the static function:

1
SubmitAdPopup.open( this, model );

I’ve run into this situation quite often in various Flex applications so I hope this may help others who finds themselves in the same boat.

Opinions? I’m curious in seeing how others solved this riddle. ;)

Categories: Adobe Flex Tags:

Flex : Allowing mouse events to “pass through” an overlay

August 10th, 2010 Erich Cervantez No comments

I ran into an issue today I didn’t immediately have an answer for.

I added an overlay to the map of my Xuland flex-based social networking tool which resembled a radar (concentric circles, tinted slightly green):

Xuland Radar

Before adding the radar overlay, I could drag the map as normal, however after adding the overlay atop the visible map area, the map no longer responded to mouse drag events.

Before writing a lot of code to catch and re-dispatch mouse events from the overlay to the map, I admit I felt a little silly after discovering a simple property that just needed to get added to my overlay:

1
2
3
4
<map:RadarOverlay
	id="radar"
	buttonMode="true" useHandCursor="true"
	mouseEnabled="false"/>

As the API mentions: “Specifies whether this object receives mouse messages. The default value is true, which means that by default any InteractiveObject instance that is on the display list receives mouse events. If mouseEnabled is set to false, the instance does not receive any mouse events. Any children of this instance on the display list are not affected. ”

And it worked perfectly!

Categories: Adobe Flex, Xuland Tags:

Flex 4: Switching to Railo from ColdFusion

July 5th, 2010 Erich Cervantez 1 comment

With the Xuland Flex-based social networking application I’ve been working on for the past eight months, I decided to switch things up on the back-end, most notably in the services layer where I’ve been using ColdFusion 9 for my data-access objects.

ColdFusion has been a perfect fit for Flex since I had used the software quite extensively for much of my programming career. However, as I prepare Xuland for go-live (August 13th – which also coincides with the Adobe MAX Awards deadline for entry date), I realized that without a license for the software I would be restricted to shared hosting services – a solution that would not work in the long-run, especially if Xuland were to suddenly gain popularity.

As I considered dedicated or “cloud” hosting, I would have to either buy an $8,000 ColdFusion license (umm, no) or switch to an alternative. BlueDragon piqued my interest, but it unfortunately does not yet support AMF. Without the ability to remote, my application would launch already dead in the water.

Another alternative, completely unknown to me before today, is Railo (pronounced “Rhylo” after a fictitious alien dog in Star Trek).

“Railo is a free, open-source alternative for ColdFusion application development.” It currently supports the AMF3 format (whereas in the last version it only supported AMF0) and comes shipped with its own application server and servlet engine (Caucho Resin). In fact, the Express versions can be downloaded, configured for Flex and launched within minutes. The best part is it’s completely FREE.

I’ve since switched from ColdFusion to Railo and with hardly any tweaking, Xuland was running against Railo executing the same CFC’s I had already written for ColdFusion 9. After starting the Railo service locally the first time, it automatically created the necessary Flex remoting-config and services-config XML files needed in the WEB-INF directory. Remoting worked perfectly, and I dare say it seems a bit faster (I have not run any benchmarks quite yet, however).

If you’re looking for a solution similar to this, definitely consider Railo as it works as well as ColdFusion without the sheer cost.

Categories: Adobe Flex, ColdFusion, General, Xuland Tags:

Flex Random Password Generator

June 26th, 2010 Erich Cervantez 4 comments

Here’s a nice static method for creating random passwords…should the need ever arise ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static function generateRandomPassword( length:int = 7 ) : String
{
	//characters to use in password
	var _salt : String = "abchefghjkmnpqrstuvwxyz0123456789ABCHEFGHJKMNPQRSTUVWXYZ";
 
	//initialize vars
	var _password : String = '';			
	var _i:Number = 0;
 
	//loop 
	while ( _i <= length )
	{
		var _num : Number = Math.random() * _salt.length;
		_password += _salt.charAt( _num );
		_i++;
	}
 
	return _password;
}

Enjoy!

Categories: Adobe Flash, Adobe Flex Tags:

Flex 4: Changing the “displayAsPassword” default character

June 22nd, 2010 Erich Cervantez 1 comment

Today I ran into a situation where I wanted to display my password characters as a bullet versus the default asterisk (“*”).

I thought this would be pretty simple to do but realized after digging through the Flex 4 SDK that there was no public property I could set to change this. I tried hacking commitProperties and a couple of other methods but came up short in those areas as well until I found the exact spot in the Flex libraries where this character was defined:

RichEditableText.as (line 679)

1
2
3
4
    /**
     *  @private
     */
    mx_internal var passwordChar:String = "*";

A-ha! I see here that it’s prefixed with the “mx_internal” namespace and I remembered from other examples around the web that you can easily tap into that namespace and modify properties not normally meant to be modified.

Luckily, I had already extended the TextInput class for various other reasons and decided to add an event listener for the CREATION_COMPLETE lifecycle event of the component. This was added in the constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import mx.core.mx_internal;
 
/**
 * Constructor
 */
public function myTextInput()
{
	super();
 
	this.addEventListener( FlexEvent.CREATION_COMPLETE, onCreationComplete );
}
 
private function onCreationComplete( event : FlexEvent ) : void
{
	//change internal passwordChar to a bullet versus an asterisk
	this.textDisplay.mx_internal::passwordChar = "●";
}

…and voila! By tapping into the mx_internal namespace, I found I could modify the normally private variable “passwordChar” and set it to something I preferred better (in this case, the bullet point used in some sites like Twitter).

Hope this helps someone!

Categories: Adobe Flex Tags:

Adobe Targets Apple in New Ad Campaign

May 13th, 2010 Erich Cervantez 1 comment

Today, Adobe launched a nationwide advertising campaign promoting “choice” and criticizing those (read: Apple) that try to control it.

Adobe Loves Apple

In addition to the ad campaign, a new web page was added to Adobe’s website: http://www.adobe.com/choice/?sdid=GXRUX, which discusses the Open Screen Project, and includes a Flash fact sheet (no doubt, from Adobe product manager Mike Chambers), and a message from Adobe’s founders John Warnock and Chuck Geschke.

This is undoubtedly in response to Steve Job’s memo published on Apple’s website, http://www.apple.com/hotnews/thoughts-on-flash/ back on April 29th. The letter, which publicly blasts Flash as being a closed, proprietary technology that is no longer relevant or in “touch” with today’s mobile devices has received a massive response from the Adobe community and today from Adobe, itself.

Read other posts about this subject:

Mashable: http://mashable.com/2010/05/13/adobe-responds-flash/

NY Times: http://bits.blogs.nytimes.com/2010/05/13/next-round-in-the-adobe-apple-fight/

Categories: General Tags:

Apple vs Adobe Peace Plan from InfoWorld

May 10th, 2010 Erich Cervantez No comments

InfoWorld has published a four-point “peace plan” aimed at reducing collateral damage from the current Apple vs Adobe war.

The article discusses the history between the two companies, outlines a compromise and allows readers to vote on the proposal.

http://www.infoworld.com/print/122878

Categories: Adobe Flash, General Tags:

The Future of Web Content – HTML5, Flash & Mobile Apps

I didn’t get a chance to read this article back in February by ColdFusion-mastermind Jeremy Allaire, but it’s a good one nevertheless and still very relevant for those of us in the Flash community.

Give it a try:
The Future of Web Content – HTML5, Flash & Mobile Apps

Categories: Adobe Flash, General Tags:

Ellen’s iPhone Commercial

Haha “My fingers… are so much thicker than I remembered…”

This video was embedded using the YouTuber plugin by Roy Tanck. Adobe Flash Player is required to view the video.

Categories: General Tags:

Latest Flash & Flex Developer’s Magazine

In case anyone’s interested, the latest FFDM has been released.

Getting your hands on the PDF here:
http://download.ffdmag.com/en/ffdmag_03_2010.pdf

FFDM cover

;)

Categories: General Tags:

Flex 4 Auto-Resizeable TextArea Component

April 6th, 2010 Erich Cervantez 7 comments

Sometimes I feel like a big dummy, especially when I spend a couple of hours trying to programmatically create an auto-resizeable TextArea in Flex 4.

There are several Flex 3 examples out there, some that call invalidateSize() and others that use methods in the mx_internal namespace for dynamically determining how many lines of text are displaying and resizing the component that way.

Nothing was working for me until I realized that Flex 4 has this functionality built right in! Of course…*bangs head on desk*.

The Spark version of the TextArea class has a “heightInLines” property which you can use to specify the height in lines (imagine that!?) of the component when not explicitly setting a “height” in pixels or a percentage. It turns out that you can set this property to NaN (or “Not A Number”), which then causes the component to resize itself automatically as the number of lines of text changes. Genius!

Remember, explicitly setting a height will prevent this action from working so use “minHeight” and “minWidth” instead to set the default starting size of the TextArea:

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<s:Application 
	xmlns:fx="http://ns.adobe.com/mxml/2009" 
	xmlns:s="library://ns.adobe.com/flex/spark">
 
	<s:TextArea 
		id="resizeableTextArea"
		heightInLines="{NaN}"
		minHeight="50"/>
 
</s:Application>

Enjoy!

Categories: General Tags:

Flex 4 and Flash Builder 4 Released!

March 21st, 2010 Erich Cervantez No comments

Not much else I can add…I’ve been working daily with Flex 4 for the past six months and I think it’s a major step forward in RIA.

Here’s the official announcement:

http://blogs.adobe.com/flex/archives/2010/03/flex_4_sdk_and_flash_builder_4.html

Categories: General Tags:

Flash Builder 4 Content-Assist / Code-Hint Bug

March 21st, 2010 Erich Cervantez No comments

Working with a Beta anything can be frustrating at times ( read: a LOT of times). The Swiz Framework and Flash Builder 4 Beta have been no exception.

The latest issue I’ve stumbled upon was so frustrating I had to blog about it: the content assist (or code-hinting) in Flash Builder just seems to not work for some packages – most notably, the Flash packages.

After trying many different things, I finally discovered a message in the Adobe forums:

The latest builds of SDK 4.0 have been updated in preparation for including playerglobal.swc for Flash Player 10.1. Flash Builder Beta 2 can not find playerglobal.swc due to the addition of the {targetPlayerMinorVersion} variable found in the flex-config.xml file.

When using recent nightly SDKs with Flash Builder Beta 2, please modify them as follows:

1) Rename the folder <nightly sdk location>/frameworks/libs/player/10.0 to “10″
2) Edit the file <nightly sdk location>/frameworks/flex-config.xml and remove “.{targetPlayerMinorVersion}” and save

When you restart Flash Builder, we will now find a correct location for playerglobal.swc, allowing for code hints and many other features to work properly.

Jason San Jose
Quality Engineer, Flash Builder

There are a couple of instances of {targetPlayerMinorVersion} in the flex-config so remember to remove both.

My flash packages now show up again in the content-assist context menu and all is right with the world ;)

Categories: General Tags:

Adobe Flex 4.1 Language Reference

March 20th, 2010 Erich Cervantez 1 comment

I had trouble locating the Flex 4 Beta Language Reference online today, although I had a location bookmarked (http://help.adobe.com/en_US/Flex/4.0/langref/), it no longer appears to work.

Figuring other folks may have a similar problem, I got a hold of the latest language reference (4.1.0.14883) and uploaded it to eonflex:

http://www.eonflex.com/flex/4.1/langref/package-summary.html

Here is the link to the Adobe Flex 4 LiveDocs also:

http://help.adobe.com/en_US/Flex/4.0/UsingSDK/index.html

I’ll update it as much as possible until Adobe publishes the reference permanently on their own.

Enjoy!

Categories: General Tags:

Building Swiz Framework from Github

March 13th, 2010 Erich Cervantez 1 comment

Although you can easily just download the Swiz 1.0 Beta SWC (above), you can also check-out the project from github (the Swiz repository) into Flex Builder and build it yourself.

To do so (from scratch), try out these steps:

Install EGit

Create Flex Library Project and import from github

  • In Flex Builder, right-click in the Navigator pane, and choose “New –> Flex Library Project”. Give it a name, something like “SwizFramework”
  • Click Finish
  • Right click on the newly created project, and select Import –> Git –> Git Repository
  • On the Swiz github page (http://github.com/swiz/swiz-framework, you’ll see a read-only URI to the repository: git://github.com/swiz/swiz-framework.git
  • Copy that link into “Source Git Repository” import window (URI field) that opened up in Flex Builder
  • The rest of the fields should auto-magically populate
  • Click Next to see the existing branches for this repo…just check the “master” and hit Next
  • In the Directory field, click Browse, and navigate to the “src” folder of the library project you just created
  • Uncheck the “Import projects after clone” checkbox and click Finish
  • The Swiz framework will be checked-out from github into your src folder
  • Right-click on the library project and choose properties
  • Access Flex Library Build Path from the left menu, then under “Main source folder”, click Browse and find the src folder within the code you just checked out from github. The path will probably be “src\swiz-framework\src”.
  • Now access “Flex Library Compiler” option from the left menu and add this to the “additional compiler arguments” field:

-keep-as3-metadata+=Inject,Autowire,Outject,Mediate,Dispatcher,PostConstruct

  • Right above that in the Namespace URL field, type “http://swiz.swizframework.org”
  • Underneath that, in the Manifest file field, Browse to the manifest.xml file that’s in your src folder and click OK

That’s it…now you can include this Library Project in any Flex project you may already have by adding it to the Library Path tab under the Flex Build Path property screen in your project’s Properties, or you can grab the newly compiled SWC from the framework project’s /bin folder if you need more portability.

Hopefully this process is relatively clear for those of you that need the latest up-to-the-minute version of Swiz ;)

Categories: Adobe Flex, Swiz Framework Tags:

Xuland on Facebook

March 4th, 2010 Erich Cervantez No comments

My little Flex 4, Swiz and ColdFusion powered social networking experiment, Xuland, now has a Facebook “fan” page for anyone that’s uses that site (eh…what’s that site again…”facebook??”):

http://www.facebook.com/pages/Xuland/302813766356

I’ll periodically post updates (generally non-technical), screenshots and whatever else I can to keep anyone interested up-to-date. I think it’s a brilliant idea for a site, the design/UI is easy & clean and I think everyone’s going to like it.

A lot.

xuland

Categories: General, Xuland Tags:

Swiz Framework 1.0 BETA Released

March 3rd, 2010 Erich Cervantez 2 comments

The latest and greatest release for the little-framework-that-could Swiz has finally been promoted to “BETA” status. Grab the SWC here: http://www.swizframework.org/files/swiz-v1.0.0-beta.swc. Read the full list of changes/additions here: http://swizframework.org/2010/03/swiz-1-0-0-beta-now-available/

I’ve been using the Swiz Framework for my Xuland project (still in development) for the last four months and have been quite happy with the tried-and-true 0.6.4 “pre-alpha” release. Even though the versioning of Swiz has been a hotly debated topic in the Swiz forums, I have felt through my own experiences with Swiz that 0.6.4 was as Production-ready as you could get.

Still I’m excited about the new things the Swiz team has introduced and decided today to catch up on all of the forum postings (http://groups.google.com/group/swiz-framework) and blog rolls.

If you’d like to learn how to check-out the project for yourself from github, see my article regarding Swiz here.

How to add a SWF to PowerPoint 2007

February 15th, 2010 Erich Cervantez 2 comments

I was in the process of writing up a PowerPoint that discussed Flex 4 States, Styling & Skinning and merely wanted to add an example states SWF to the document (rather than switch out of PowerPoint during the presentation and over to a browser to view the example) and much to my dismay found out it wasn’t that easy!

After trying to figure it out myself through the tool, I eventually had to resort to several Google searches for my answer. I landed on this page tucked away deep in Microsoft’s Support libraries: http://support.microsoft.com/kb/291875, which reads:

PowerPoint 2007
Make sure that the Flash Player is installed on the computer. Then, follow these steps:

1. In PowerPoint, display in normal view the slide on which you want to play the animation.
2. Click the Microsoft Office Button, and then click PowerPoint Options.
3. Click Popular, and then click to select the Show Developer tab in the Ribbon check box under Top options for working with PowerPoint, and then click OK.
4. On the Developer tab, click More Controls in the Controls group.
5. In the list of controls, click Shockwave Flash Object, click OK, and then drag on the slide to draw the control.
6. Resize the control by dragging the sizing handles.
7. Right-click the Shockwave Flash Object, and then click Properties.
8. On the Alphabetic tab, click the Movie property.
9. In the value column (the blank cell next to Movie), type the full drive path, including the file name (for example, C\:MyFile.swf) or uniform resource locator (URL) to the Flash file that you want to play.

Make sure you resize the control to the visual layout of the embedded SWF (otherwise you’ll see some oddly colored “whitespace” around the SWF).

Anyway, I tried these steps and was successful. Hopefully this also saves someone else some time ;)

Categories: General Tags:

Flex 4 & ColdFusion 9 Remoting Recap

December 29th, 2009 Erich Cervantez No comments

It’s been a couple of years since I worked with ColdFusion and Flex together, but I remember I always like the combination and chose it for my new little Xuland social networking project I’ve been working on (see previous posts).

I had to remember how the whole Flex-ColdFusion remoting setup would work again and I had to overcome a couple of hurdles. I thought I’d share the experience as it would have helped me get up to speed quickly.

Here’s a quick recap of setting up your Flex application to remote to ColdFusion:

  1. Install ColdFusion (I installed mine to the default C:\ColdFusion9 directory)
  2. In FlexBuilder, create a new Flex project
  3. While creating the project, select an Application server type of “ColdFusion” and check the “Use remote object access service” with the “ColdFusion Flash Remoting” option selected.
    .
    servertechnology
  4. On the next screen, in my case I’m using a Standalone installation and had to uncheck the “Use default location for local ColdFusion server”. Of course, the root folder was C:\ColdFusion9. I clicked Validate Configuration which validated that the root folder existed.
    .
    serverlocation
  5. I happen to use ColdFusion primarily for access to a MySQL datasource, so I had to setup the datasource in the ColdFusion admin first (this information should be readily available anywhere)
  6. Once the datasource was setup, I first created a value-object (VO) in ColdFusion to represent the object (in this case, a User) I wanted to pass back to the Flex application
    .
    UserVO.cfc:

    1
    2
    3
    4
    5
    
    <cfcomponent output="false" alias="com.xyz.coldfusion.vo.UserVO">
    	<cfproperty name="userID" type="numeric"/>
    	<cfproperty name="username" type="string"/>
    	<cfproperty name="password" type="string"/>
    </cfcomponent>
  7. After the VO, I created a data-access object (DAO) to query the datasource for a user and return a UserVO to the calling Flex application
    .

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    <cfcomponent output="yes">
    	<cffunction name="getUserByID" 
    		access="remote" 
    		returntype="com.xyz.coldfusion.vo.UserVO">
     
    		<cfargument name="userID" type="numeric" required="yes"> 
     
    		<cfquery name="getUser" datasource="xuland">
            	SELECT * 
    		FROM tblUsers
    		WHERE userID = '#userID#'
    		</cfquery>
     
    		<cfreturn getUser>
     
    	</cffunction>
    </cfcomponent>
  8. Back in the Flex project, I setup my RemoteObject tag to point towards the ColdFusion DAO component I created in the last step:
    .

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    <!-- Remote Services -->
    <mx:RemoteObject id="userService"
    	 destination="ColdFusion"
    	 source="com.xyz.coldfusion.dao.UserDAO"
    	 showBusyCursor="true">
     
    	<mx:method name="getUserByID" 
    		   result="resultHandler(event)" 
    		   fault="faultHandler(event)"/>
     
    </mx:RemoteObject>
  9. Create your result and fault handlers (here we just display an Alert)
    .

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    <fx:Script>
    	<![CDATA[
    		import mx.controls.Alert;
    		import mx.rpc.events.FaultEvent;
    		import mx.rpc.events.ResultEvent;
     
    		private function resultHandler( event : ResultEvent ) : void
    		{
    			Alert.show( event.result.toString() );
    		}
     
    		private function faultHandler( event : FaultEvent ) : void 
    		{
    			Alert.show( event.fault.faultString );
    		}
    	]]>
    </fx:Script>
  10. Finally create a button or method to call the service:
    .

    1
    
    <mx:Button click="userService.getUserByID( 1 )"/>
  11. Much of this logic may be abstracted off into controllers, delegates, commands or other framework-specific components but this is a fairly simple example of installing, configuring and executing a remote object service call from Flex to ColdFusion.

Categories: Adobe Flash, Adobe Flex, ColdFusion, Events Tags:

Adobe Flex 4 LiveDocs Documentation & Language Reference

December 29th, 2009 Erich Cervantez 2 comments

At work, I wanted to review some information on the Adobe Flex 4 LiveDocs help documentation and had a lot of trouble finding it online.

Luckily at some point in the past, I managed to bookmark it at home and am reposting it here in case anyone else might be interested. Here is the link to the Flex 4 help documentation, which is a fantastic resource. I hope it comes in handy ;)

Adobe Flex 4 Documentation:

http://help.adobe.com/en_US/Flex/4.0/UsingSDK/index.html (PDF)

Flex 4 ActionScript Language Reference:

http://livedocs.adobe.com/flex/gumbo/langref/index.html

Download the trial copy of Flash Builder 4, read the above documentation and experiment by building your own test applications.

There are quite a few changes from Flex 3 to Flex 4 and the earlier you get acclimated to the new features, the better prepared you’ll be when the final product ships ;) Good luck!

Categories: Adobe Flash, Adobe Flex, Events Tags: