Showing posts with label Flex. Show all posts
Showing posts with label Flex. Show all posts

Wednesday, June 2, 2010

Flex Glass Headers

When i first started to work in Flex almost a year back, I was so eagerly looking to grab as much goodies as possible and make my application look spiffy. I remember staying all night long at work so many times, just to explore options by visiting many sites and applications built using Flex, primarily to get sources of inspiration.

One such application was the Flex Style Explorer. The one thing that I loved the most was the mild glossy look it had on its title; Luckily it had the view source enabled and I went on to see how the guys had done that.

It was a mere reference to a transparent flash sprite compiled into a swiff file and was referred from inside the CSS - set as the backgroundImage of the panel.

I went on to build my own header using flash in the next few minutes and added my own version of the glossy look to my application.

Click here to see the image of my app title before and after.

It was such a mild difference, people could hardly notice it; but it hugely enhanced the overall aesthetics of my application. People LOVED the look and feel but no one guessed it. I went on to use this header smartly across the application onto different controls wherever possible.

Flex Renderer Recycling

Thought it would be useful to just mention about renderer recycling 'feature' that is inherent in the Flex UI control handling.

Often this could burn your time if not handled properly at the time of building your application.

The Problem:
When you scroll through a list control which has a huge list of items and is bound with an itemRenderer, the items in the list control tend to shift its positions randomly, as you scroll.

The Reason:
Flex by default tries to reuse the item renderers. In other words lets say your list control has about 40 items. It is not necessary that Flex would generate 40 itemRenderers to display the list.
Instead, based on the display area available, it would generate a few itemRenderers, just enough to handle the current display area and probably a few more for buffering.

As the users scroll through the same itemRenderers get reused and the contents that need to be displayed, in other words the next item data will be rewritten onto that itemRenderer.

So in total the creationComplete event on the item renderer would just fire once and it is not necessary that it would fire for every item added to the list again because of this reusing concept.





The Solution:
Override the set data function on your itemRenderer which would give you access to the actual item that is being bound to the itemRenderer at that point in time, and in this function you could take care of binding the item the way you want.

The set data is bound to get called many times during the process of creation/change resize etc...
For an example of overcoming the renderer recycling issue and how to override the set data function, check the example here.

Flex TileList ItemRenderer with Drag and Drop Sample

Moving on to Flex 4 since last month, I think it is time to publish a few of the small things I had done in Flex 3.

This one is a simple but nice - drag and drop example from one of the modules I had built. It is not the entire application of course; just a stripped down to basics version that just explains a few

The example handles issues like renderer recycling and explains how a custom item renderer can be used which in turn could have its own set of child controls.

The source for this can be found here and a live example here.

P.S.: When viewing the demo, just click on Open if your browser prompts you about handling the file.

Friday, August 14, 2009

Flex Time

The last 3 to 4 weeks were terribly tiresome. The application that we were building was already running on a stiff deadline and we had entered into the meat of it at this point. It was thrilling to execute everything that was earlier planned. I kept foraging my desk draw for the invaluable information I had had hurriedly scribbled some weeks back. They help me to remind myself of what exactly was discussed. Somehow, no soft copy could do that for me.

Anyways, working late night continuously has helped me gain some decent level of fluency in coding with Flex. The Java Web Services were pretty straightforward.
Also, since I was at times given company by my colleague who is from Orissa, I have learnt how to swear in Orriya. I often swear at the Flex builder for being so shamelessly slow or at some of the insipid methods or definitions in ActionScript.

Somehow a few of the bigger challenges have been taken care of. There are a few more to go before we reach the comfort zone. Of course, when the testing phase is on it could all come down – but then that is a different story altogether.

Wednesday, July 8, 2009

Dynamic Menu using Flex

I think it is time now to share some of the initial work I have done in Flex which served as a part of my learning process as well.

A dynamic menu - fully populated from data stored in MS SQL - fed via a web service.
It is important to mention that most of the functionality was pretty much handled in the web service (IMO, that is how it should be).

The only thing I learnt in flex through this project was to use the Menu control and to perform a simple WS call.

The database has a table 'tbl_menu' that stores all the menu details. It references itself via the field ParentMenu_id for sub menus.



The only input the Flex page needs is a cleanly formatted xml string. So that is what the WS is going to return:


[WebMethod]
public string GetMenuByClass(String usrclass)
{
StringBuilder sb = new StringBuilder();

Dictionary> dict = new Dictionary>();
dict = GetMenuDict(usrclass);

sb.Append("");
sb.Append("");
foreach (int key in dict.Keys)
{
sb.Append(AddXmlString(dict[key]));
}
sb.Append("
");

return sb.ToString();
}


It is just a plain method that uses StringBuilder to append the xml string. There are a hundred different ways to construct the xml, but that is not part of this sample.

Also you would notice soon that we are using a simple object class 'MenuInfo' that represents the actual DB entity which will be used that to pass around data.

Next, the GetMenuDict function takes care of populating a dictionary with all the menu data it pulled from the DB:


private Dictionary> GetMenuDict(string userAccessCode)
{
Dictionary> dict = new Dictionary>();

List menuItems = new List();

menuItems = GetMenuFromDB(userAccessCode);

foreach (MenuInfo m in menuItems)
{
// root element - 0 or NULL
if (m.Parent_Link_id == 0)
{
if (dict.ContainsKey(m.Parent_Link_id))
{
dict[m.Parent_Link_id].Add(m);
}
else
{
List lst = new List();
lst.Add(m);
dict.Add(m.Parent_Link_id, lst);
}
}
else
{
// not a root element - parent exists. find it and add this guy to his list
MenuInfo mInfo = null;

foreach (int key in dict.Keys)
{
mInfo = FindMenuInfoRecursive(m.Parent_Link_id, dict[key]);
if (mInfo != null)
{
mInfo.menuList.Add(m);
}
}
}
}

return dict;
}


The GetmenuFromDB just reads the data from the Database using a datareader and returns a list of MenuInfo. The more inportant function is the FindMenuInfoRecursive, which takes care of handling the sub menus. With this method the sample supports infinite level of sub listing menu items, by just setting the appropriate ParentMenu_id.


private MenuInfo FindMenuInfoRecursive(int id, List menuList)
{
MenuInfo menu = null;

foreach (MenuInfo item in menuList)
{
if (item.Menu_id == id)
{
menu = item;
break;
}
else
{
menu = FindMenuInfoRecursive(id, item.menuList);
if (menu != null)
{
break;
}
else
{
continue;
}
}
}

//finally return the menu object
return menu;
}


The AddXmlString is the method we are going to see next:


private string AddXmlString(List menuList)
{
StringBuilder sb = new StringBuilder();

foreach (MenuInfo m in menuList)
{
if (m.menuList.Count > 0)
{
//should continue
sb.Append(String.Format("", m.Name, m.Name));
sb.Append(AddXmlString(m.menuList));
sb.Append(String.Format("
", m.Name, m.Name));
}
else
{
sb.Append(String.Format("", m.Name, m.Name));
}
}
return sb.ToString();
}


With all this done at the WS, it leaves us with very little to do at the Flex end. We would just be making the call to the WS and merely bind the results.


public function WSMenuSvcResultHandler(event:ResultEvent):void{
var dataXML:XML = new XML();
dataXML = XML(event.result.toString());

var myMenu:Menu = Menu.createMenu(null, dataXML, false);
myMenu.labelField="@label";
myMenu.show(10, 20);
}


The full C# source for this example is here and the Flex source is here.

Saturday, June 13, 2009

Going with the tide...Flex and Java

For the past few months, I have been coding lesser and lesser in .Net. That was because I was doing some groundwork for the next project, which we had planned to build in Flex consuming either Java or .Net Web Services. Compared to my exposure in Java, I can say I know .Net like the back of my hand. But then, that is what willful programming was all about - exploring new horizons. Added with that was my personal interest in wanting to learn and work in Flex. Besides, I should also learn to go with the tide, shouldn’t I?

I have a moderate level of experience working in Flash, but that was 4 years ago when my friend and I would stay awake all night building flash web templates. She was exceptionally creative at designing and I focused more on the ActionScript.

Also having worked in WPF, I was basing myself a lot on these experiences and began building some sample applications as I learnt. So going forward, one can expect a lot of Flex and Java related posts more often.