Tuesday, April 20, 2010

mx.controls.MenuBar

Recently I was working on a project where someone wanted the menu to open when went over. I thought how hard could this be there should be a hook in there to say open on mouse over but as usual I was wrong.
So I went digging and experimenting, it was interesting to see how many ways people have tried to solve this issue and talk about complicated. I am from the old school where the main design pattern is KISS (keep it simple stupid).
So I decide I could write one myself and hopefully make it less complicated, and surprisingly enough it worked out perfectly (well so far it has not been totally tested out yet).
Basically you extend mx.controls.MenuBar
In the constructor add two event listeners, set the buttomMode to true
Public function myMenuBar()
{
super;
this.buttonMode = true;
this.addEventListener(MouseEvent.MOUSE_OVER,mouseRollOverHandler);
}
The opening of the menu is done by the mouseRollOverHandler, which checks to see if the target is a MenuBarItem and if it is fires off a MouseEvent.MOUSE_DOWN, this will basically make it seem like the user clicked the menu bar.
private function mouseRollOverHandler(event:MouseEvent):void
{
     if (event.target is MenuBarItem)
     {
          event.target.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
     }
}
I hope this was of some help.

Tuesday, April 13, 2010

Custom Phone Number Formatter

Well it has been a while since I have posted anything mainly because I have been busy at work trying to get a new version of our application completed and into production.


I am going to try and post a couple of items that I have had to deal with lately that caused me some issues. The first item I am going to look at is a custom phone number formatter, I build it out of a need to allowing the user to enter just about anything in there and have the system properly format the phone number with an extension.


I did the usual Google search and saw different pieces but never really liked any single one. So what I came up with is a custom phone number formatter that extends mx.formatters.PhoneFormatter. The format function basically checks to see the length of the phone number and performs the formatting based on the length.


If the length is 0 or less than 10, we place an error in the error variable and clear out the phone number


if( phoneNbr.length == 0 || phoneNbr.length < 10)


{


error="Invalid Length Phone Number";


phoneNbr = "";


}


If the length is 10 then we basically return the phone number that is formatted by the supper's format.


However if the length is greater than 10 we then strip off any spaces and ()-.x in the number then reassemble the phone number into a (###) ###-#### x#### format the x#### will basically pick up the remaining characters.


I hope this is of some help to everyone.

Friday, February 5, 2010

Flex compc

For those that do not know what compc is, it is a flex component compiler, it generates a .swc file that can be used as a library in your Flex application.

I was doing some blog reading today and came across someone talking about ANT and Flex specifically compc. I have had my own issues with the Flex Ant tasks in the past one of them is the way compc makes you list out the files. I thought this was a bit strange that I could not just pass in a path to the source files and have it pick them up. So after some surfing around and thinking I was able to come up with this. It looks at the directory you provide and includes/excludes any files you would like in property. This property is used as an input to the include-classes attribute of the compc command.

 

   1: <target name="_resolve-class-path" depends="clean">



   2: <path id="list_1">



   3:         <fileset dir="${flex-src.dir}">



   4:             <include name="**/**"/>



   5:             <exclude name="**/**.bak"/>



   6:         </fileset>



   7:     </path>



   8:     <pathconvert property="project_classes_property" pathsep=" " dirsep="." refid="list_1">



   9:         <map from="${basedir}\src\" to=""/>



  10:         <mapper>



  11:             <chainedmapper>



  12:                 <globmapper from="*.as" to="*"/>



  13:             </chainedmapper>



  14:             <chainedmapper>



  15:                 <globmapper from="*.mxml" to="*"/>



  16:             </chainedmapper>



  17:         </mapper>



  18:     </pathconvert>



  19: </target>







and my compc looks like this now






<compc output="${basedir}/util.swc" include-classes="${project_classes_property}">



I can not take full credit it for it but thought I would pass it along to others.

Wednesday, January 6, 2010

iBatis.. i whatis

You read that correctly iBatis. “It is a persistence framework which automates the mapping between SQL databases and objects in Java, .NET, and Ruby on Rails. In Java, the objects are POJOs (Plain Old Java Objects). The mappings are decoupled from the application logic by packaging the SQL statements in XML configuration files. The result is a significant reduction in the amount of code that a developer needs to access a relational database using lower level APIs like JDBC and ODBC.“ (http://en.wikipedia.org/wiki/IBATIS)

According the WIKI it is best used when the developer “does not have full control over the SQL database schema”. For the project I am currently working on nothing can be truer, as one of the developers we have little control of the database and sometimes the SQL that is executed.
So why am I writing about it, well in the past month or so I have gotten a crash course into this framework as a decision was made to remove Hibernate and replace it with iBatis. The reasons were many and I will not go into them now as personally I feel better about it.

One of the main advantages in my book is the simplicity of this mapping: Basically you read in a configuration file that has the mapping files in it. This mapping file is in XML (I am not sure if that is a pro or a con yet) that houses SQL that is right not HQL or something else actual SQL. This file also contains the actual mappings that list out the input/output parameters and how they relate to the query. These parameters can be in a MAP or a POJO. I have used both so far, and have done other items which I found very difficult and frustrating with Hibernate (this could be because of my lack of knowledge into Hibernate).

So let’s get into a little bit of code OK so complicated code always hate examples that are simply Hello World why not go the extra mile and show something useful for once. In our application we call store information that the user may have entered, we create an XML document and store it in the DB as a CLOB. With Hibernate we were getting the data from the UI and then creating and XML document or getting the information from the DB and then having to parse it out into our POJOs. With iBatis we did things a bit differently; we removed the creation and parsing of the XML document into a callback handler. This handler implemented TypeHandlerCallback and basically either parsed the CLOB into the correct POJO or created the XML document based on the data from the UI without the need of the application developer ever having to call it.
Here is a row from our mapping document for the input parameter:
<parameter property="userData" jdbcType="clob" typeHandler="com.xyz.dao.UserDataCallbackHandler" />




Output Parameter looks very much the same:




<result property="userData" column="USER_DATA_CLOB" jdbcType="clob" javaType="com.xyz.vo.UserCriteriaVO" typeHandler="com.xyz.dao.UserDataCallbackHandler"/>







I want to look at the result as it is a bit more complicated than the parameter.


property – maps to the field in the POJO that will hold this column


column – is the actual DB column name that is returned from your query


typeHandler – this is the definition of the callback handler





Here are some interesting items to note:





  • The typeHandler must be the fully qualified name. In some areas of mapping file you can alias the object but this is not one of those areas.




  • The type handler cannot be the DAO, I tried to make it so I did not have to create a new class but iBatis had issues so a separate class had to be created.




I have ended up using the Callback Handler in numerous places where I want a conversions done or other cases complex object built.