<mx:Flexing> A Paritosh Bisi’s Blog

On Rich Internet Applications, Adobe Flex, ActionScript, Web Technologies and more…

Updating Flex SDK

Posted by Paritosh Bisi on October 11, 2009

Recently I thought of updating the Flex SDK, for my flex builder 3. Thought of sharing the steps with all as I took quite some time to discover how to do it.

First download the flex sdk from here. Then unzip the files and name the rename the folder to 3.4.0 or simply keep as it is. Copy this folder and paste in the following location, C:\Program Files\Adobe\Flex Builder 3\sdks\. After that run flex builder. Go to Project –> Properties, then click Flex Compiler tab. Then click on Configure Flex Compiler… link. Then click Add button, browse for the Flex SDK location (eg: C:\Program Files\Adobe\Flex Builder 3\sdks\3.4.0), automatically Flex SDK name will come, or you can rename to some custom names also. That’s all we have successfully installed the new flex builder SDK. Now select the new SDK check box and click Apply. That’s all. All our applications will be compiled using the new Flex SDK.

Happy Flexing… :-)

Posted in Flex Builder | Leave a Comment »

Problem with Flex ‘Module’ – Percentages for height and width

Posted by Paritosh Bisi on August 30, 2009

Recently while working with Flex Modules I came across an interesting find. Whenever I was setting the module’s height and width to “100%”, it was simply ignoring. So after googling a lot I came across the following tech note from adobe.

http://www.adobe.com/support/documentation/en/flex/2/releasenotes_flex201_sdk.html

Here it is clearly noted that, “Module ignores percentages for width/height, always sizing to content”. And the work around for this is to add percentWidth="100" percentHeight="100" instead of height=”100%” width=”100%”.

Example:

<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" percentWidth="100" percentHeight="100">

So, while developing modules, whenever we need to set height and width to 100 percentages, we need to carefully implement like above.

Happy Flexing :-)

Posted in Flex Builder | Leave a Comment »

Taking snapshot from Flex applications

Posted by Paritosh Bisi on January 26, 2009

Recently I got a requirement to take snapshot from applications. I got the basic idea from the following blog…

http://www.mehtanirav.com/2007/09/10/taking-a-snapshot-of-flex-app-from-flex

Using Flex 3, taking a snapshot is quite easy, as Flex 3 provides us the built in APIs to take a snapshot. Using the ImageSnapshot class this can be achieved. Flex 3 also provides two image encoders JPEGEncoder and PNGEncoder.  Check the following link to see how to take snapshot using Flex 3.

But when you use the PNGEncoder or JPEGEncoder while processing the snapshot the UI freezes, because actionscript is single threaded – everything just runs on the browser’s main thread. To avoid the UI freeze we have to use a third party class called  JPEGAsyncEncoder check this link for more detils and codes.

Using Flex 2 also we can take snapshot. We have to use BitmapData, JPEGAsyncEncoder, ByteArray classes. For sending to server we need HTTPService, and server side scripting language like PHP. To download the converted JPEG image we need URLRequest and FileReference classes. We can take snapshot of any component which extends UIComponent class.

Sample Code [Take a snapshot of a canvas and save the JPEG image in server and download to local file system]

//FLEX Codes
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”>

<mx:Script>
  <![CDATA[
   //Call this function to take snapshot
   //@param : pass the UIComponent (whose snapshot you want) as parameter
   public function takeSnapshot(target:UIComponent) :void {
        //Save bitmapdata
        var bd:BitmapData = new BitmapData(target.height, target.width);
        bd.draw(target);
        //Encode using JPEGAsyncEncoder
        var encoder:JPEGAsyncEncoder = new JPEGAsyncEncoder(80);
        encoder.PixelsPerIteration = 128;
        encoder.addEventListener(JPEGAsyncCompleteEvent.JPEGASYNC_COMPLETE, encodeDone);
        encoder.addEventListener(ProgressEvent.PROGRESS, encodeProg);
        encoder.encode(bd);
    }//End takeSnapshot

    //Encoding progress
    private function encodeProg(event: ProgressEvent):void {
    //Show progress using event.bytesLoaded and event.bytesTotal
    }

    //Encoding done event handler
    private function encodeDone(event: JPEGAsyncCompleteEvent):void {
      var ba:ByteArray = event.ImageData;
      //Encode using Base64Encoder
      var be:Base64Encoder = new Base64Encoder();
      be.encodeBytes(ba);
      var encodedData:String = be.flush();
      //Object to send to PHP
      var objSend:Object = new Object;
      objSend.encodedData = encodedData;
      //Send data to PHP
      writeData.send( objSend );
    }//End encodeDone

    //Result handler
    public function resultHandler( event: ResultEvent ):void {
      var res:String = event.result.toString();
      if(res == "SUCCESS"){
        Alert.show("Save file ?", "Encoder Test",
                   Alert.OK, this,
                   downloadFile, null, Alert.OK );
      }else {
        Alert.show("Error occured");
      }
    }//End resultHandler

    //Download code
    public function downloadFile(eventObj:CloseEvent):void {
        var fileUrl:String = http://localhost/encoder/test.jpeg;
        urlReq = new URLRequest(fileUrl);
        fileRef = new FileReference();
        //Add event listeners
        fileRef.addEventListener(ProgressEvent.PROGRESS, progEvent);
        //Start download   
      fileRef.download(urlReq);
      }
    }//End downloadFile

    //Download progress
    private function progEvent(evt: ProgressEvent):void{
      //show some progress using evt.bytesLoaded and evt.bytesTotal
    }//End progEvent

  ]]>
</mx:Script>
    <!–HTTPService to  talk with PHP–>
  <mx:HTTPService id=”writeData” url=” http://localhost/encoder/encode.php”
                  method=”POST” resultFormat=”text” result=”resultHandler(event)”/>
</mx:Application>

//PHP Script
//Encoder.php
//Saved at the location http://localhost/encoder/encode.php as per  the example

<?php

$encodedJPEGData = $_POST["encodedData"];
//Decode and save as a jpeg file
if ($encodedJPEGData != “”) {
   $binaryData = base64_decode($encodedJPEGData);
   file_put_contents(“test.jpeg”, $binaryData);
   $result = “SUCCESS”;
} else {
   //Some Error Occured
   $result = “ERROR”;
  }

//Send Result to Flex
echo $result;

?>

Hope this example will be useful for you.

Happy Flexing :-)

Posted in Flex Builder | Tagged: , , | 3 Comments »

Communicating Flex & JavaScript – Simple Example

Posted by Paritosh Bisi on December 25, 2008

If we google for communication between Flex & JavaScript we can get hundreds of examples. But for understanding the basic concept we need a very simple example, how to call JavaScript functions from Flex and Flex functions from JavaScript.

In this example we will see, My Flex application contains one Button and one TextInput control, and Html wraper also contains one Button and one Text Box. When I write something in Flex application’s TextInput and click button, we will show an JavaScript alert showing the Text of TextInput, and when I write something in HTML TextBox and click button I will show a Flex Alert containing the Text of TextBox.

For communication between Flex and JavaScript we need the ExternalInterface class.

In my example, whenever I am clicking the Button in my Flex Application, I am calling a function named callJs  in JavaScript and passing the value (Text) of TextInput. The Flex code will be like this below…

ExternalInterface.call("callJs", txtDisplay.text);

The JavaScript function will accept the Text and show in JavaScript Alert. The JavaScript code I need to put inside the HTML wrapper for my application and will look like this below…

<script language="javascript">
    function callJs(message){
        alert("Message from FLEX : " + message);
    }
</script>

That’s all… I successfully communicated to JavaScript from Flex.

Now,vice-versa… I will call Flex function [callFromJs()] from JavaScript. So firstly I need to allow this function to be called from JavaScript. So in the Application Initialise method I need to put the following code…

private function initApp():void{
   ExternalInterface.addCallback("callFlexFromJs", callFromJs);
}

callFromJs() function in Flex is like this below…

private function callFromJs(strFromJs:String):void{
   Alert.show("Message from JS : " + strFromJs);
}

Note:- callFlexFromJs is something like an Alias function name which is exposed to the Js layer, from Js layer we can call the method callFromJs in flex through this alias name, like below…

In the HTML wrapper side the JavaScript code which calls the Flex function looks like this below…

<script language="javascript">
    function callFlex(){
        element = document.getElementById("TestExternalInterface");
        element2 = document.getElementById("txtcallflex");
        txtForFlex = element2.value;
        element.callFlexFromJs(txtForFlex);//Here we are calling the alias name of the method
    }
</script>

Note:- TestExternalInterface, is the id of <object> tag and name of the <embed> tag, in my HTML wrapper. This is needed to identify my flex object in the HTML wrapper.

txtcallflex,  is the id of the TextInput in my HTML wrapper whose value I am passing to Flex.

To know more about the wrapper check the following link.

http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001579.html

That’s all… I successfully communicated to JavaScript from Flex and vice versa.

The complete code in Flex side…

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="initApp()">
<mx:Script>
  <![CDATA[
    import mx.controls.Alert;
    private function initApp():void{
      ExternalInterface.addCallback("callFlex", callFromJs);
    }
    private function callFromJs(strFromJs:String):void{
      Alert.show("Message Coming from Js : " + strFromJs);
    }
    private function callJs():void{
      if (ExternalInterface.available){
        ExternalInterface.call("callJs", txtDisplay.text);
      }
      else{
        Alert.show("ExternalInterface not available");
      }
    }
  ]]>
</mx:Script>
  <mx:Button id="btnCallJs" x="178" y="10" label="Call JS" click="callJs()"/>
  <mx:TextInput id="txtDisplay" x="10" y="10"/>
</mx:Application>

If you have any doubt do leave a comment.

Happy Flexing…:-)

Posted in Uncategorized | 1 Comment »

Disable creation of backup files in EditPlus

Posted by Paritosh Bisi on December 11, 2008

To disable creation of the unwanted backup files (*.bak) while editing in EditPlus, follow the step.
Go to Tools -> Preferences -> ‘Files’ page and turn off  ‘Create backup file when saving‘ option.

Happy Coding…:-)

Posted in General | Leave a Comment »

CTRL-T does not work in Firefox sometimes

Posted by Paritosh Bisi on December 10, 2008

Many of us might have faced the issue that, while running flex applications ctrl-t does not work in Firefox sometimes. That is because, when current page displayed has a flash frame and the frame is selected, ctrl-t is not passed to the browser from the frame so ctrl-T don’t work.

Having any solution or workaround for this, please do leave a comment.

Happy Browsing…:-)

Posted in Firefox | 11 Comments »

Flex SDK 3.2 and Flex Builder 3.0.2 Released

Posted by Paritosh Bisi on November 17, 2008

Adobe released Flex SDK 3.2 and Flex Builder 3.0.2, for more informations and to download check the following link…

http://www.adobe.com/devnet/flex/articles/sdk32_fb302.html

Happy Flexing…:-)

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

Tweak OpenOffice applications to load faster

Posted by Paritosh Bisi on November 15, 2008

OpenOffice applications generally take a lot of time to load at startup. Follow these simple steps to tweak OpenOffice to load it much faster than default…

  1. Open any OpenOffice application.
  2. Go to Tools > Options,
  3. Select Memory from left side list.
  4. Under Graphics cache, change the settings as below…
    • Use of OpenOffice.org – 128MB.
    • Memory per object – 20.0MB.
  5. Under Cache for inserted objects, change the settings as below…
    • Number of objects – 20.
  6. Now select Java from the left side list.
  7. Under Java options, deselect/uncheck Use a java runtime environment.
  8. Now click OK, and close OpenOffice, and we are done.

Now onwards when you will start OpenOffice applications it will load much faster than default.

[ Note:- These settings are recommended for systems with RAM capacity of at least 1GB ]

Happy Tweaking…:-)

Posted in General | Tagged: | Leave a Comment »

Working with SVG

Posted by Paritosh Bisi on November 8, 2008

SVG stands for Scalable Vector Graphics, it defines graphics in XML format which can be rendered in browser.

Copy the following codes into Notepad and save the file as “Circle.svg”. 

<?xml version=”1.0″ standalone=”no”?>

<!DOCTYPE svg PUBLIC “-//W3C//DTD SVG 1.1//EN” “http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd”>

<svg width=”100%” height=”100%” version=”1.1″ xmlns=”http://www.w3.org/2000/svg”>

<circle cx=”100″ cy=”50″ r=”40″ stroke=”black” stroke-width=”2″ fill=”red”/>

</svg>

Open this file in your browser, and you can see a circle drawn in your browser. :-)

Note: Firefox 1.5+, Opera 9+, and Google Chrome supports SVG format by default. If you are using any other browser like IE you need to install SVG Viewer which you can download free form adobe from here.

The main advantages of using SVG format over  other graphic formats like JPEG or GIF are…

  • SVG files can be read and modified by a large range of tools (e.g. notepad).
  • SVG files are smaller and more compressible than JPEG and GIF images.

For more details and advance topics / Information about SVG, please check the following links…

http://www.adobe.com/svg/

http://www.w3schools.com/svg/default.asp

Posted in General | 1 Comment »

Firefox, setup Flash Tracer

Posted by Paritosh Bisi on November 5, 2008

Flash Tracer is the most useful debugging tool for Flash/Flex developers.

Here’s the simple steps to make it work…

  1. First install Debug version of flash player in firefox. Download and install the Flash Player Debug version from here. Check whether debug version is installed properly by going to this link. You should see DebugPlayer : Yes, like below…Flash Player Version Detection
  2. Install Flash Tracer addon for firefox. For Firefox 2 install from here. For Firefox 3 download and install from here.
  3. After restarting firefox, open flashtracer (Tools > Flash Tracer). Click the Preference icon at the lower right corner. In the General Settings tab > Select output file, locate and replace the file flashlog.txt, in the following locations…
    Windows XP: C:\Documents and settings\{user}\Application Data\Macromedia\Flash Player\Logs\flashlog.txt
    Windows Vista: C:\Users\{user}\AppData\Roaming\Macromedia\Flash Player\Logs\flashlog.txt,
    Note:- If some how Logs folder is missing, create it, and create a textfile named flashlog inside it.
  4. Create a file named mm.cfg, in the location,
    Windows XP -  C:\Documents and settings\{user}\
    Windows Vista – C:\Users\{user}\
    and put the following contents…
    (Note:- In TraceOutPutFileName, put the path of flashlog.txt, Here I have put the path as per Windows Vista.) TraceOutPutFileName=C:\Users\Paritosh Bisi\AppData\Roaming\Macromedia\Flash Player\Logs\flashlog.txt
    ErrorReportingEnable=1
    TraceOutputFileEnable=1
    MaxWarnings=100

Restart Firefox, and we are done.

Happy Flexing…:-)

Posted in Firefox, Flash Player | Tagged: , | Leave a Comment »