I Think ∴ I Am

Java, Flex, RIA, AJAX, Flash, Life, Food, Kites…

Archive for March, 2008

Creating a dynamic tree in flex

Posted by vipuljhawar on March 28, 2008

Tree control is one of the most powerful control in Flex. You must have stumbled upon N number of articles explaining the tree control, where they pick up the data from an XML for the tree, but that does not really happen too often if you have a tree which is to be built dynamically as loading a new XML everytime to build a tree is a costly affair.

So, let’s create a tree in Flex using actionscript objects which may be serialized version of server side objects or you may want to create a tree using some business logic rather than some plain input text. So, we will create a TreeNode class in Actionscript which will represent each and every node in the tree. The advantage of working with node objects is that you can have different types of objects at different levels and also use the inheritance concept pretty nicely. So a basic node may be a Node with variables – id, name, type.  A subclass of this node may be a PersonNode with additional variables – age, sex etc… and you can go on like this creating trees with the apt information and you can display any kind of data just by clicking on the node by fetching that node from the tree.

The top level class will be TreeNode.

public class TreeNode {

     public var id : int;

     public var name : String;

     public var type : int;

     public var children : ArrayCollection;

     public function addChild(node : TreeNode) : void {

      if (this.children == null)

          this.children = new ArrayCollection();

          children.addItem(node);

     }
//Similarly you could have getNode();

}

A sublcass of this could be a DataTreeNode

public class DataTreeNode extends TreeNode {

     public var data : Object;    

}

Now you can simply create a tree in Flex, by creating a RootNode of type DataTreeNode.

var rootNode : DataTreeNode = new DataTreeNode();

rootNode.id = 0;

rootNode.name = “Root”;

rootNode.type = -1;

Now you can keep creating nodes of different types and keep adding to the rootNode. Just assign the rootNode to you tree as a dataProvider and there you are, the flex tree will display automatically. I have found this approach much better and easy as it allows easy extension and also allows me to do much more on when i click a treeNode. Also, this way you don’t solely rely on the Tree api in Flex to do something with your tree, you can manipulate or swift your tree in any manner you want and reap the benefits.

If you have better way of creating dynamic tree in flex do comment, i would be more than pleased to improve or use that for my project, with all the regard definitely :-) .

Posted in Flex, Flex Tree control | Tagged: , , , | 4 Comments »

Comparable datagrid in flex

Posted by vipuljhawar on March 27, 2008

Using a datagrid in Flex seems very easy, but it has the inherent problem as ready to eat meal. It may not allow you to do all the modifications and may not allow you to present it in a view you desire.

A few months back i had a requirement to create a datagrid, which displayed data column wise instead row wise i.e. the datagrid was suppose to be used for comparing and viewing data across columns rather than rows. So, how do we deal with such a problem in Flex. If you are working on HTML, its easy as you can just alter the logic in the for loop printing the rows. The whole point is using multi dimension arrays.

After finding no easy way to do it in Flex, i had to use to the same programming talent to display data column wise.

As datagrid expects the dataprovider to be in the form of an Array or ArrayCollection of objects and then it iterates over these objects to display a property of these objects in each column when you use the dataField property or you can reformat your test using the labelFunction property for the column. I used a combination of programming logic and labelFunction to display the list.

Lets say we intend to display Person objects columnWise, as displayed below.comparable-table.png

Now we would receive person object from the server to the client and there is no way that you can use labelfield property or the lablefunction straight away to create such a table.

The wrokaround is to keep the person array aside and create an array of indexes equal to the number of rows that each column would have or the max. number of rows that a column could have in the whole table. So we will create an array with numbers {1,2,3,4…} each number denoting a particular row in the column. Now, iterate through the person object array and keep on adding DataGridColumn objects to the datagrid for as many Person object. So, the dataprovider to the datagrid is an array of indexes and the number of columns is equal to the number of Person objects. Assign a labelFunction to each column such that

public function getCompareLabel(item : Object, column : DataGridColumn) : String {
var cIndex : int = int(Number(column.dataField)); //this will give you the column for which you have to get data.
if (cIndex == 0)
return getRowLabel(item, column); //Now in getRowLabel just write a switch function which will give label’s for each row, such as “First”, “Last” etc. This gives the first column.
var person : PersonDAO = personArray[cIndex-1] as PersonDAO;
//Now from second column onwards we will get the particular person object from the dataArray and display data for a particular field accordingly.
switch(item) {
case 1 : return getCmpStr(person.firstName);
case 2 : return getCmpStr(person.lastName);
case 3 : return getCmpStr(person.age);
….

…..

}

The output will be a table like above. Strange, isn’t it Flex is suppose to make things easier but i had to swing around the whole place to get this is in place.

Posted in Flex | Tagged: , , | Leave a Comment »

iPhone must have apps

Posted by vipuljhawar on March 15, 2008

It’s time to identify the must have apps on your iPhone. The list would be regularly updated with time as and when you see new apps flowing in but as of now i find the following worth installing. I am having a satisfactory usage experience on 1.1.3 with these apps.

1. WebSearch  – One stop app for searching Google, Amazon, Dictionary.com, ebay, wikipedia etc.

2. LocateMe – Gives your precise location on the planet. Nice to have if you are stuck in a unknown place sometime but would you be connected to the internet that time :-) . Gotta get Edge activated for that.

3.  SysInfo – Meta info about your iPhone.

4. Finder – Nice app to browse the filesystem and manage files and directories.

5. weDict – dictionaries on your iPhone.

6. Tris – Tetris for iPhone. One of my favourites, i love Tetris game and install it wherever possible. I even have it on IDE – IntelliJ :-) .

7. CameraPro – iPhone camera software sucks. This is a nice substitute.

8. MxTube – WOW, you can download youtube videos and persist them on your iPhone forever. It’s beta now but i think once the final release is out this is going to be one of the most hpt iPhone apps. Job’s would lose his cents as people wouldn’t buy videos from iTunes store.

9. FTP daemon – This is required if you wanna access your iPhone via FTP. One you have installed this, you can FTP files to your iPhone by using any FTP client on any remote machine.

10. Unix Subsystem – This is required by many apps as a base app.

11. Open SSH – SSH to your iPhone using Putty if you are using Windows on your PC.

So keep installing and enjoying, if you find another interesting app, do leave a comment.

Posted in Baby Steps to iPhone | Tagged: , , , , | Leave a Comment »

iPhone review, my first thoughts

Posted by vipuljhawar on March 14, 2008

iPhone

Ok It’s time to start tapping the iPhone icons. Mine is a factory made 1.1.3 16 GB :-) , no upgrades till date. The first things you should not miss on iPhone are the built in apps.Like a gentleman apple expects you to use these apps, and not any other from the so called untrusted sources:-

1. Youtube – Aaah!!! who didnt want youtube on their phone, i wish we had wi-fi everywhere i went in India.

2. iPod – This is great, I guess now it’s time for me to get the FM transmitter/adapter for iPhone so that i can use the iPod in my car/home over any speakers. Will have to get it from US now.

3. Google Maps – Great to see maps of India on your mobile, not pretty precise but considering the mughals never planned Delhi thinking we would tap our phone and use a map, so it’s kinna ok to get a fair idea. I am sure it will be much better in month’s to come. No driving instructions though but you can definitely spot the road in front of your house at least if you are in Delhi. The pin drop is great.

4. Safari – Oh this is killer, but no flash support :-( .

5. Mail – Looks good, but i would prefer this app to be password protected, what if i lose my phone or someone steals it (it happens too often in India), he enjoys my mail too so this is a security issue according to me.

6. Stocks – Good if you are into the bourses. The yahoo finance widget a true copy of the kanfabulator desktop widgets but it’s worth it. By default you see NYSE but can add BSE, NSE scrips too.

7. Photos – ok, not much impressed.

8. Camera – The software looks like as if it was developed before someone even thought of iPhone :-) .

9. Weather – Good, again the yahoo widget, you can add indian cities too.

10. Itunes – I never use it because i fear something may get updated and my phone may be bricked.

Some more default apps but these were worth mentioning.

But one things which sucks is the iTunes sync. This is why i hate it, and where they lock you into the apple world. How can ppl so blindly say that only Microsoft blocks other software and plug-ins, I guess iPhone is one the most locked in products.

The negatives as per initial usage:-

1. The phone charger is horrible, two piece device. Maybe guys using iPod wouldn’t feel so but normal mobile phone user would. It’s so difficult to carry the charger in pieces. Bad design.

2. The battery life isn’t so great, but considering it’s a full fledged mini PC, so there would be battery limitations.

3. The earphones socket is non standard one so you plug any regular male speaker or earphone plugs into the phone, so it becomes useless in your car until i buy a kinna converter now.

4. Ooooops iTunes sync sucks. Steve Job’s expects you to buy everything from videos to ringtones :-) on the iTunes store.

5. Using the speaker phone is an issue because of the volume.

But, still i must say i am impressed.

Trust me if iPhone could read and write vis-a-vis USB drive, it would have been a hit among the females too :-) (Am i doubting their geek quotient, nope). I mean iTunes really blocks it’s use as an external hard disk or USB drive but we will use it that way through FTP, SSH or something else.

Now it’s time to load the real apps which make this phone rock, yup the servers, the clients, the games and the etc’s. It wouldn’t a be mobile anymore now.

Posted in Baby Steps to iPhone | Tagged: , , , | Leave a Comment »

Ha ha I am a geek, i got an iphone :-)

Posted by vipuljhawar on March 14, 2008

Hey Hey !!! That’s what you feel when you own and use an iPhone in India. Yup, becuae you need to buy it in GREY market or get a friend or contact of your’s to get it from US. Then you get it jailbroken, unlocked and wait for the ringer to ring and whooooooosh… it rings.

Wow man the hacker inside you is satisfied when you see all this happening in front of you. My experience was amazing. Once i opened the box and held the 16 GB phone iPhone in hand, i was like wow !!!. I headed straight to Bhuvan at my opposite cubicle and it was plugged into his mac. You load ziPhone and click and click and then suddenly then you see hex characters scrolling on your screen, Man u r about to enter the Matrix :-) and then the beauty restarts. It’s a different feeling to get your mobile working like this out of a hack and then the journey begins. Since, then i have been really hooked to the sexy gadget and will have many more posts on it soon.

Although i don’t consider Job’s or Mac any better than Gates or Windows but Job’s got his hands right on iPhone, it’s worth the price. Let’s see if we really have apps flowing in on iPhone from the open source community rather then the app store where Job’s plans to keep 30 cents from every dollar in his pocket. That’s bad steve.

Posted in Baby Steps to iPhone | Tagged: , , | Leave a Comment »