Optimizing code

24 06 2010

I posted about shortcuts in eclipse in my earlier post, those I use while writing code. Most of the times I try to write code as fast as possible but always fail to do that. There will be different thoughts while writing the code and gets confused and wastes time. I thought to optimize the code while writing it for the first time itself. I don’t know its correct to think like that or not. I always try to minimize the code of lines. I googled about tips and tricks for coding I got this links

http://www.insideria.com/2009/04/51-actionscript-30-and-flex-op.html

http://osflash.org/as3_speed_optimizations.It will be helpful for ActionScript 3.0 guys.





Search with in DataGrid in Flex

25 05 2010

Searching data with in the DataGrid, For my recent project I need this requirement. I googled a bit and found this link

Here is a nice working sample and code is also available. Thanks for that blogger.





ComboBox ItemRenderer in flex DataGrid

9 10 2009

For my Application I need a CobmboBox in Datagrid, and the combo will have a list of data. For this I googled a few samples. Here is a sample code, I modified it with my requirements.

My requirement is populate the Datagrid and there will be columns in datagrid that can be modified and updated to the database. To achive this I created ComboBox as ItemRenderer in the DataGridColumn. The data to the Combo must be dynamic.

Sample Code

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
 creationComplete="fillDataGrid()">

 <mx:Script>
 <![CDATA[
 import mx.collections.ArrayCollection;

 //  data provider of combo in datagrid, can get dynamic data by a service
 private var comboData:Array = new Array("Value0","Value1","Value2","Value3");

 /**
  *  populate the datagrid with sample data
  */
 private function fillDataGrid():void {
 var arrColl:ArrayCollection = new ArrayCollection;
 for (var i:int=0,j:int=0; i<50; i++,j++ ) {
 if ( j>3 ) j=0;

// create an object that will be provided to combo
 var obj:Object = new Object;
 obj.value = "Value"+j;
 obj.list = comboData;

// data is for first column, com is for the itemrenderer in that com object
 //value is slectedItem of combo and list is the provider of that combo
 arrColl.addItem({data:"data"+i, com:obj});

}

dg.dataProvider = arrColl;

}
 ]]>

</mx:Script>
 <mx:DataGrid id="dg" x="60" y="21" rowCount="5">
 <mx:columns>
 <mx:DataGridColumn dataField="data" headerText="Data" />
 <mx:DataGridColumn headerText="Value" itemRenderer="DynRender" />
 </mx:columns>
 </mx:DataGrid>
 </mx:Application>

DynRender.mxml


<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" horizontalScrollPolicy="off">

<mx:Script>
<![CDATA[

 override public function set data(value:Object):void {
   if(value != null) {
    super.data = value;
    mycombo.selectedItem=data.com.value;
   }

 }
 ]]>

</mx:Script>
<mx:ComboBox id="mycombo" dataProvider="{data.com.list}" width="100%"  />
</mx:Canvas>

The required data for the combo box is provided by the collection in ‘com’ item, In that we will provide the list item as dataprovider of the combo box. And value will the the selectedItem.





Embedding font in Flex 4 (spark components)

3 10 2009

Embedding fonts in Flex 3 can be achived by


@font-face {
 src : url("comic.TTF");
 fontFamily : comic;
 }

But this will not work in flex 4. We need to add one more property ‘cff’ and setting it to true.


<?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"
xmlns:mx="library://ns.adobe.com/flex/halo"
minWidth="1024" minHeight="768">

 <fx:Style>
 @namespace mx "library://ns.adobe.com/flex/halo";
 @namespace s "library://ns.adobe.com/flex/spark";

 @font-face {
 src : url("comic.TTF");
 fontFamily : comic;
 cff : true;
 }
 .myStyle {
 font-family : comic;
 font-size : 24;
 }
 </fx:Style>

 <s:RichText fontFamily="comic" x="40" y="23">
 Embedding font in flex 4 - RichText
 </s:RichText>
 <s:SimpleText styleName="myStyle" x="40" y="63">
 Embedding font in flex 4 - RichText
 </s:SimpleText>
</s:Application>





Library Project with source (.net)

6 07 2009

Hi,

Here I want to share a small Library Project done while doing a course at NIIT (2006-2007) with my friends Sachin, Erendra, Shilpa.

This project was developed on ASP.NET, MSSQL (don’t remember version).

This project will have Registration for users, Login, Book issue, Returning books ,Admin and Librarian modules.

Here I am attaching the code of Project which includes presentations, Documentations and source code.





Flex with Struts 2 with Source

26 06 2009

In my previous post (http://niamathbasha.wordpress.com/2008/09/01/configuring-struts-2-with-flex/) I discussed about configuring flex with Struts 2. So many guys asking me to provide the working sample. So I configured a small Login application.

Download the sample application which uses Flex as UI and Struts 2  as Back-end.cairngorm-template.rar





BlazeDS TurnKey

25 06 2009

First download BlazeDs from here, unzip it and rename it as blazeds and place the folder in c:\ drive. This will have Tomcat 6, a sample database(HSQL DB).

Now working with this, Start the database(C:\blazeds\sampledb\startdb.bat) and then start tomcat (C:\blazeds\tomcat\bin\startup.bat)

Open the browser and enter http://localhost:8400/samples/ and take test drive.





Java Code Names

17 06 2009
  • JDK 1.1.4  (Sparkler Sept 12, 1997)
  • J2SE 1.2 (Playground Dec 4, 1998)
  • J2SE 1.3 (Kestrel May 8, 2000)
  • J2SE 1.4.0 (Merlin Feb 13, 2002)
  • J2SE 5.0 (Tiger Sept 29, 2004)
  • Java SE 6 (Mustang Dec 11, 2006)
  • Java SE 7 (Dolphin)




Exporting DataGrid to CSV using Flex with Java

12 06 2009

Exporting of DataGrid data to CSV format in Flex and Action Script 3.0

Converting Data Grid data to String (csv format):


private function exportToCSV():String {
 var xmlListColl:XMLListCollection = new XMLListCollection;
 xmlListColl= myDataGrid.dataProvider as XMLListCollection;

var csvData:String = '';
 // To get Headers
 var attrList:XMLList = xmlListColl[0].@*;
 for (var i:int = 0; i < attrList.length(); i++) {
 csvData += attrList[i].name() + ',';
 }

 csvData += '\n';
 //    To get actual data
 for ( var j:int = 0; j &amp;amp;lt; xmlListColl.length; j++) {
 var record:XMLList = xmlListColl[j].@*;
 for ( var k:int = 0; k < record.length(); k++) {
 csvData += record[k] + ',';
 }
 csvData += '\n';
 }
 return csvData;
 }

The above function will convert DataGrid data(if its data is in xml format) to csv format.

If you want to create a csv file then you have to send to the server and then download the csv file.

For sending this data to server side (implemented in java) you can use simple HTTPService in flex

and sending with the paramater – csvData what we created in above function.

In Java we will use a simple servlet.


byte[] output = request.getParameter( "input" ).getBytes();

 response.setContentType("text/csv");

 response.setHeader("Content-Disposition", "attachment; name=\"Result\"; filename=\"Result.csv\"");

 response.setContentLength( output.length );

 response.getOutputStream().write( output );

Just copy the above code in servlet that will return an output stream. Take this responce to flex

use ExternalInterface call to accept that stream and it will show you dialog box with Open and Save option

in this way we can export the datagrid data to a csv file.





Event handling in flex

25 05 2009

Let us take a small working sample of an application with events.

Create a flex project:

Here we are creating two canvases by using events we are going to communicate.

In main application we are adding listner

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”
creationComplete=”init()” xmlns:local=”*”>
<mx:Script>
<![CDATA[
private function init():void {
this.addEventListener(MouseEvent.CLICK, clickEventHandler);
}

public function clickEventHandler(e:MouseEvent):void {
vs.selectedIndex = 1;
}

]]>
</mx:Script>
<mx:ViewStack id=”vs”>
<local:canvasOne />
<local:canvasTwo />
</mx:ViewStack>

</mx:Application>

First we have to add listner of an event for which we want to do some action on perticular event.

Here we added listner for the MouseClick event.

canvasOne.mxml:

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Canvas xmlns:mx=”http://www.adobe.com/2006/mxml”
backgroundColor=”red” width=”400″ height=”300″>
<mx:Script>
<![CDATA[
public function clickEvent():void {
this.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
}
]]>
</mx:Script>
<mx:Label text=”Before dispatch event” />
<mx:Button label=”dispatch” click=”clickEvent()”  x=”32″ y=”37″/>
</mx:Canvas>

Here we are dispatching an event, that will be handled in the main application.

The click handler in main application will have the logic to do further calculations.

Here we just changeing the index of the view stack.

canvasTwo.mxml:

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Canvas xmlns:mx=”http://www.adobe.com/2006/mxml”
backgroundColor=”green” width=”400″ height=”300″>
<mx:Label text=”After dispatch event” />
</mx:Canvas>








Follow

Get every new post delivered to your Inbox.