Archive

Archive for January, 2009

Success! Fixing the ja_JP locale from being set in compiler arguments

January 26th, 2009 Gareth No comments

Ha ha! I figured out (after just blogging my frustration in my previous post) how to change the Additional compiler arguments that automatically are added when creating a new project from ja_JP to en_US.

There is a file called config.xml (on my PC it is located in C:\Program Files\Adobe\Flex Builder 3 Plug-in\eclipse\plugins\com.adobe.flexbuilder.flex_3.0.214193) that gets installed when updating via the Adobe updater. I’m not quite sure what happened, but it decided that my compiler arguments should be set to ja_JP. In order to fix this problem, just change this line from

<locale>ja_JP</locale>

to

<locale>en_US</locale>

This will now add en_US as an additional compiler argument instead of ja_JP. Now I can get back to coding again :)

Why is locale set to ja_JP?

January 26th, 2009 Gareth 2 comments

This is annoying the heck out of me. For some reason, I’m not sure when, my locale got set to ja_JP instead of en_US I did the update in eclipse and I’m wondering if it somehow got the japanese version of the SDK instead of the US version.

Each time I create a new project I get -locale ja_JP added to the compiler arguments. It’s an easy fix, just remove that or change it to en_US, but I have to do that each time I create a new project. If I could even find the file that’s making that setting, I would be happy to manually alter it, but I haven’t even been able to find that. My flex-config.xml file appears to be set to en_US also, so I have no clue how ja_JP is getting in there.

If anyone has any ideas, I’d be extremely grateful.

Frameworks Galore

January 19th, 2009 Gareth 2 comments

Well, not too many, but I would say just enough to ease the use of the Flex framework itself, and deciding how best to lay out your code. Up to this point, I have not tried to use a framework for my code, mainly because I did not really see the need for one. I had my own basic structure that I used for all of my code and tried to stick with that when methodology when creating/laying out my projects. At my last job, my co-worker and I were of a pretty similar mindset, so it was pretty simple to not stray too far from each other’s best programming practices.

At my current employer we have been transitioning from a ColdFusion front and back end to a ColdFusion back end and Flex on the client. We have a relatively small team, with varied knowledge of Flex and OOP, but everyone is willing to learn, and seems to do so pretty quickly. With this in mind, we thought that a framework may assist those without the in depth Flex knowledge to come up to speed a little quicker by not having to wonder where to place certain components that they end up building, and can use the other code they find for groundwork. This combined with the fact that even though the group is small, frameworks do seem to facilitate easier code reuse, and take the guesswork out of best practices. Also, due to time constraints, our most recent project turned into a somewhat procedural behemoth of an application, that now is in need of some refactoring, and a framework would certainly help by easing how to split everything up.

I started to get the urge to look into frameworks again after reading FrameworkQuest 2008 over at insiderRIA.com. Tony Hillerson did a great job of presenting all of the various frameworks, in a very unbiased fashion, I thought. I figured, rather than just taking his word for it, I would dive into the frameworks myself and take a small project I created using my usual code structure and break that out using a framework. Nothing helps to send the message home than to write it yourself.

The 4 frameworks I decided to implement were:
my own
Cairngorm
Mate
and Swiz
I had ruled out PureMVC from my list of frameworks, mostly because I felt that if I was going to use a non-standard framework, that I wanted something that would make my life easier, and, at first glance, while it seemed to be quite powerful, PureMVC seemed to a little too abstract for what I needed it for (especially for those just getting into Flex).

Over the next couple of weeks, I’ll post my trials and tribulations with each framework as I went through it, how I dealt with the problems I came across, and my humble reasoning for the framework I chose. Hopefully it will give others insight into the frameworks (for those of us who have never used them before), and if others are actually reading this blog, will help out by commenting on things that I’m either doing incorrectly, or could be done easier.

Categories: Flex, Framework, code Tags: , , , ,

Problem solved transferring typed objects to Flex

January 14th, 2009 Gareth 7 comments

I really do like using the struct method of transferring typed objects from ColdFusion to Flex. However, today I ran into an issue when passing them to Flex from ColdFusion.

I had been following along with Dan Vega’s posts on RocketFM, and I decided to download the code and tinker with it (as all good little programmers should :) ). I had added a FileInfo VO to Flex and one to ColdFusion. Then within the FileManager object, I was returning the newly typed object ( temp['__type__'] = “RocketFM.src.org.vega.FileInfo”; ). However, every time I returned the the data, I kept getting a plain, ol’ generic object. On to the debug steps:

I had recently removed my CF7 install, so I thought that maybe that had messed something up within my CF8 install. I restarted my CF instance ( a couple of times in the end), turned various settings on and off, all to no avail. I tried changing where I was pointing my CFC and my aliasing of them, but everything seemed to be correct there also. I then fired up an old project that I had previously used that I knew was returning typed objects. I ran it through the debugger, and lo and behold…typed objects still. So, I ruled out CF as being the problem (I’m using the same instance across all of my projects…not the best method, but until I get more than 5 different CF projects I’m working on, I should be ok)

I then decided it must be something on the Flex side of things. I got distracted with something else, and just as my mind had begun to relax, the solution hit me…I haven’t included the FileInfo class in my project anywhere (queue head slapping moment). Flex only includes classes that are referenced somewhere in the project, even though the VO was included in the project directory structure, I was not referencing it anywhere in the code (as all I cared about at first was that the data would be returned as typed objects). I then added the import statement and created a private variable that would all me to just reference the VO in Flex. Once this had been added, the typed objects were returning like champs back to the Flex project. A somewhat simple solution in the end, but unless you’re actually thinking about it, very easy to overlook as well.

Flex Date Utils – daysInYear, isLeapYear

January 4th, 2009 Gareth 10 comments

These are the final 2 methods in my DateUtils Class of the FlexDateUtils library. I’ve got a Business class, a Holiday class and a DaylightSavingTimeUS class that are all included in the FlexDateUtils library, and I may go over those in the future, but not right now.

/**
 * Gets the number of days in the year
 * 
 * @param date	The date to check
 * 
 * @return		The total number of days in the year
 */
public static function daysInYear( date:Date ):Number {
	return DateUtils.dateDiff(
				DateUtils.DAY_OF_MONTH,
				new Date( date.fullYear, DateUtils.monthAsNumber( DateUtils.JANUARY ), 1 ),
				DateUtils.dateAdd( DateUtils.YEAR, 1, new Date( date.fullYear, DateUtils.monthAsNumber( DateUtils.JANUARY ), 1 ) ) );
}

Very straight forward method. This calculates how many days are in the year that is passed in. Essentially I’m doing a date difference between the first of January of the requested year, and the first of January of the next year. The method returns the number of days for that year.

So in order to use this method, first import the package

import com.flexoop.utilities.dateutils.DateUtils;

and then:

private var _total:Number = DateUtils.daysInYear( new Date( 2009, DateUtils.monthAsNumber( DateUtils.JANUARY ) ) );

This would return 365.

/**
 * Determines whether the year is a leap year or not
 * 
 * @param date	The date to check
 * 
 * @return		<code>true</code> means it is a leap year, <code>false</code> means it is not a leap year.
 */
public static function isLeapYear( date:Date ):Boolean {
	return daysInYear( date ) > 365;
}

Another very straight forward method. It accepts a date and determines if the year is a leap year.

So in order to use this method, first import the package

import com.flexoop.utilities.dateutils.DateUtils;

and then:

private var _isLeapYear:Boolean = DateUtils.isLeapYear( new Date( 2009, DateUtils.monthAsNumber( DateUtils.JANUARY ) ) );

This would return false as 2009 is not a leap year.

All methods in this series are describing code in the FlexDateUtils package I put on Google code.

I hope that this wasn’t too dry a topic for everyone that followed along. I just get excited when I’m describing code I have written and as much as my wife looks like she’s interested in what I’m saying, I see her eyes glazing over every now and then :) , and I hope that others find new code as interesting as I do. I wanted to make sure I got descriptions of everything in the DateUtils library to make it easier for anyone trying to figure out a use for it. I think I’ll leave the library descriptions alone and go back to posting solutions to problems I’ve faced at work/home or interesting tidbits I’ve come across. Perhaps I can revisit any new items I add in the future.

Flex Date Utils – totalDayOfWeekInMonth, monthAsNumber

January 3rd, 2009 Gareth No comments

These 2 methods are quite useful. I use monthAsNumber quite frequently throughout the DateUtils library as it prevents any confusion about which month I am referencing (using the constants for a month rather than the numeric version, which in Flex and many programming languages is 0-11 rather than 1-12)

/**
 * Gets the total number of dayOfWeek in the month
 * 
 * @param strDayOfWeek	The day of week to check
 * @param date			The date containing the month and year
 * 
 * @return				The number of <code>strDayOfWeek</code> in that month and year
 */
public static function totalDayOfWeekInMonth( strDayOfWeek:String, date:Date ):Number {
	var _startDate:Date = DateUtils.dayOfWeekIterationOfMonth( 1, strDayOfWeek, date );
	var _totalDays:Number = DateUtils.dateDiff( DateUtils.DAY_OF_MONTH, _startDate, new Date( date.fullYear, date.month, DateUtils.daysInMonth( date ) ) );
	// have to add 1 because have to include first day that is found i.e. if wed is on 2nd of 31 day month, would total 5, of if wed on 6th, would total 4
	return Math.floor( _totalDays / 7 ) + 1;
}

This method calculates the total number of a specific day of the week for a specified month. The method accepts dayOfWeek as a string (use one the constants to make this easier) and a date containing the month and year requested. It then returns a number with the total number of that day of the week for that month.

So in order to use this method, first import the package

import com.flexoop.utilities.dateutils.DateUtils;

and then:

private var _total:Number = DateUtils.totalDayOfWeekInMonth( DateUtils.Wednesday, new Date( 2009, DateUtils.monthAsNumber( JANUARY ) ) );

which would return 4, as there are 4 Wednesdays in January 2009

/**
 * Formats a month to the numeric version of the month
 * 
 * @param strMonth	The month to convert
 * 
 * @return			A formatted month or -1 if month not found
 */
public static function monthAsNumber( strMonth:String ):Number {
	return ( objMonth[ strMonth ] >= 0 ) ? objMonth[ strMonth ] : -1;
}

This method takes a string version of the month, and then converts it to the numeric version of that month. I use objMonth[ strMonth ] >= 0 rather than just objMonth[ strMonth ], as 0 is also “false” and was returning -1 instead of 0 (for January). This is quite a useful method for me, and, even though it is more typing, I feel it makes more sense to someone skimming/reading the code, which prevents headaches in the future. I even found one example I gave that was using 1, when I actually meant 0, for January, so I altered the example to use this method to remove all confusion.

All methods in this series are describing code in the FlexDateUtils package I put on Google code.

Flex Date Utils – dayOfWeekIterationOfMonth

January 2nd, 2009 Gareth 1 comment

This one is fairly complex, but quite useful, so I’m just going to demo one method today.

/**
 * Gets the Xth day of the month.
 * e.g. get the 3rd Wednesday of the month
 * 
 * @param iteration		The iteration of the month to get e.g. 4th or Last
 * @param strDayOfWeek	The day of the week as a string
 * @param date			The date containing the month and year
 * 
 * @return				The date of the xth dayOfWeek of the month 
 */
public static function dayOfWeekIterationOfMonth( iteration:Number, strDayOfWeek:String, date:Date ):Date {
	// get the numeric day of the week for the requested day
	var _dayOfWeek:Number = dayOfWeekAsNumber( strDayOfWeek );
	// get the date for the first of the month
	var _firstOfMonth:Date = new Date( date.fullYear, date.month, 1 );
	// calculate how many days to add to get to the requested day from the first of the month
	var _daysToAdd:Number = _dayOfWeek - DateUtils.dayOfWeek( _firstOfMonth );
	// if dayOfWeek is before the first of the month, get the dayOfWeek for the following week
	if ( _daysToAdd < 0 ) {
		_daysToAdd += 7;
	}
	// set the date to the first day of the week for the requested date
	var _firstDayOfWeekOfMonth:Date = DateUtils.dateAdd( DateUtils.DAY_OF_MONTH, _daysToAdd, _firstOfMonth );
	// return the date if iteration is 1
	if ( iteration == 1 ) {
		return _firstDayOfWeekOfMonth;
	} else {
		// if requesting an iteration that is more than is in that month or requesting the last day of week of month
		// return last date for that day of week of month
		if ( ( DateUtils.totalDayOfWeekInMonth( strDayOfWeek, date ) < iteration ) || ( iteration == DateUtils.LAST ) ) {
			iteration = DateUtils.totalDayOfWeekInMonth( strDayOfWeek, date );
		}
		// subtract 1 as it starts from the first dayOfWeek of month
		return DateUtils.dateAdd( DateUtils.WEEK, iteration - 1, _firstDayOfWeekOfMonth );
	}
}

The name is a little cryptic, but it returns the total number of a day of the week in a specified month. I heavily commented this method in order to be less confusing for anyone who might be trying to decipher what is going on, so I won’t go over what is being done within the method. The method accepts 3 parameters: iteration which represents which dayofweek for a month to get, 1, 2, 3, 4, 5, or LAST (if using the constant), the string representation of the day of the week, and the date containing the month and year to query. It returns the date for the specified iteration.

To implement, first import the package:

import com.flexoop.utilities.dateutils.DateUtils;

then:

private var _dateOfIteration:Date = DateUtils.dayOfWeekIterationOfMonth( 3, DateUtils.TUESDAY, new Date( 2008, DateUtils.monthAsNumber( DateUtils.MAY ) ) );

This will return May 20th, 2008, the 3rd Tuesday in May.

All methods in this series are describing code in the FlexDateUtils package I put on Google code.

Flex Date Utils – daysInMonth, dayOfWeekAsString

January 1st, 2009 Gareth 2 comments

These 2 methods are pretty self explanatory, daysInMonth and dayOfWeekAsString

/**
 * Gets the days in the month
 * 
 * @param date	The date to check
 * 
 * @return		The number of days in the month
 */
public static function daysInMonth( date:Date ):Number {
	// get the first day of the next month
	var _localDate:Date = new Date( date.fullYear, DateUtils.dateAdd( DateUtils.MONTH, 1, date ).month, 1 );
	// subtract 1 day to get the last day of the requested month
	return DateUtils.dateAdd( DateUtils.DAY_OF_MONTH, -1, _localDate ).date;
}

This method calculates how many days are in the month for the date passed in to the method. It takes the date parameter and sets a temporary date to the first day of the next month. Then it subtracts 1 day from that date to get the last day of the requested month.

So in order to use this method, first import the package

import com.flexoop.utilities.dateutils.DateUtils;

and then:

private var _total:Number = DateUtils.daysInMonth( new Date( 2009, DateUtils.monthAsNumber( DateUtils.JANUARY ), 1 ) );

This would return 31 to _total.

/**
 * Formats a date to the string version of the day of the week
 * 
 * @param date	The date to format
 * 
 * @return		A formatted day of week
 */
public static function dayOfWeekAsString( date:Date ):String {
	return DateUtils.dateFormat( date, "EEEE" );
}

This method returns a formatted date for the day of week. Nothing crazy here.

So in order to use this method, first import the package

import com.flexoop.utilities.dateutils.DateUtils;

and then:

private var _dow:String = DateUtils.dayOfWeekAsString( new Date( 2009, DateUtils.monthAsNumber( DateUtils.JANUARY ), 1 ) );

And this would return, Thursday

All methods in this series are describing code in the FlexDateUtils package I put on Google code.