Archive

Archive for December, 2009

Simple CSV Exporter

December 27, 2009 1 comment

Hi guys, when i wanted to write a  post about this topic i was clear that i am not gonna do something which no one would have done before but it will at least save others time who doesn’t familiar with CSV export. Unfortunately we doesn’t have enough example available for CSV export in web and that’s what compromised myself to write a post about it.

Now check out the simple CSV exporter utility class pasted below. Its a singleton class and  has method named exportCSV which writes the object from the collection to a chosen CSV (string delimited file) file. But when i wrote the class, i had two things in mind that not always developers want to write all the properties of an object, most of the times it will be only few so i wrote this method which receives array of properties of an object in the order it has to be written. And also the string delimiter will different from the requirements of an application so one can also pass the string delimiter they would like to use against the default string delimiter (“\t”) .

package
{
  import flash.filesystem.File;
  import flash.filesystem.FileMode;
  import flash.filesystem.FileStream;

  public class CSVExporter
  {
    protected var file : File;
    protected var dataProvider : Array;
    protected var columnFields : Array;
    protected var fileStream : FileStream;
    protected var columnSeparator : String;

    protected static var NEW_LINE : String = "\n";
    private static var instance : CSVExporter;

    public static function getInstance():CSVExporter
    {
      if(instance == null)
        instance = new CSVExporter();
      return instance;
    }

    public function exportCSV(file : File,
                              dataProvider : Array,
                              columnFields : Array,
                              columnSeparator : String = "\t")
    {
      this.file = file;
      this.dataProvider = dataProvider;
      this.columnFields = columnFields;
      this.columnSeparator = columnSeparator;

      fileStream = new FileStream();
      fileStream.open(file, FileMode.WRITE);
      writeHeaderRow();
      parseDataProvider();
      fileStream.close();
    }

    protected function writeHeaderRow():void
    {
      for(var index = 0; index < columnFields.length; index++)
      {
        fileStream.writeUTFBytes(columnFields[index].toUpperCase());

        if(index != columnFields.length - 1)
          fileStream.writeUTFBytes(this.columnSeparator);
      }
      fileStream.writeUTFBytes(NEW_LINE);
    }

    protected function parseDataProvider():void
    {
      for(var index : int = 0; index < this.dataProvider.length; index++)
      {
        writeDataRow(dataProvider[index] as Object);
        if(index != dataProvider.length-1)
          fileStream.writeUTFBytes(NEW_LINE);
      }
    }

    protected function writeDataRow(data : Object):void
    {
      for(var index : int = 0; index < columnFields.length; index++)
      {
        var property : String = columnFields[index] as String;

        if(data[property] != null)
          fileStream.writeUTFBytes(data[property] as String);

        if(index != columnFields.length-1)
          fileStream.writeUTFBytes(columnSeparator);
      }
    }
  }
}

Here it goes, the method which invokes CSV Exporter :-

protected function writeCollection():void
{
  var dataCollection : ArrayCollection = new ArrayCollection();
  dataCollection.addItem({name : "Sachin", age : 35, role : "All-rounder", state : "Mumbai"});
  dataCollection.addItem({name : "Sehwag", age : 30, role : "All-rounder", state : "Delhi"});
  dataCollection.addItem({name : "Gambhir", age : 25, role : "Batsmen", state : "Delhi"});
  dataCollection.addItem({name : "Yuvraj", age : 28, role : "All-rounder", state : "Punjab"});
  dataCollection.addItem({name : "Virat", age : 24, role : "Batsmen", state : "Delhi"});
  dataCollection.addItem({name : "Dhoni", age : 25, role : "Wicket-keeper",state : "Jharkand"});

  var  file : File = File.desktopDirectory.resolvePath("friends.csv");
  CSVExporter.getInstance().exportCSV(file, dataCollection.toArray(), ["name","role","state"]);
}

I hope you would have got brief insight about CSV exporting at the end of this post. If this post dint impress you for what you have looked for, please drop your comments i try to look out a solution for your need. Thank you for reading this post.

Move child of container anywhere

December 20, 2009 Leave a comment

Are you the one who faced tough times when you had to move child of container somewhere else than the position it has to be laid out and ended up using Canvas container . Consider the scenario, you have a VBox container and it contains three children. Now if you would like to position third child wherever you click within the container. Is it possible unless it is a canvas container ?. If you say, we cant, still it has logic as we all know the container and it sub-classes layout the component based on its layout behavior and it wont bent down as we wish to place the control.

But its certainly possible. Set the includeInLayout property of child (you want  to move around) to false and set the x and y position of child by invoking move method on the child control. It will be laid out as we intended. The reason behind the trick is, if you make the includeInLayout property of the control to false, it wont be considered when the container lays out the children and it will leave you to take care of the positioning of control. Thats how it works, got it ???.

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

    <mx:Script>
        <![CDATA[

            protected function  onInit():void
            {
                container.addEventListener(MouseEvent.CLICK, onMouseClick);
            }

            protected function onMouseClick(event : MouseEvent):void
            {
                floatMe.move(event.localX, event.localY);
            }

        ]]>
    </mx:Script>

    <mx:VBox id="container"  width="350" height="150" backgroundColor="#FFFFFF"
        borderStyle="solid" horizontalScrollPolicy="off" verticalScrollPolicy="off">
        <mx:Button id="floatMe" label="Wanna place me here" includeInLayout="false" />
    </mx:VBox>

</mx:Application>