Saturday, December 22, 2012

Flex: Displayable Image from an Image Resource

  Following is a class which can be used to convert and an image resource (here a png file) to a displayable image (mx.controls.Image) instance in Flex. Here a static function 'displayableImageFromClass(Image, int, int): void ' does the work. The function's height and width parameters are optional and they specify the height and width of the displayable image that is returned.

================================================================

package com.shyarmal.drawing.utils {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.utils.ByteArray;
   
    import mx.controls.Image;
    import mx.graphics.codec.PNGEncoder;
   

    public class ImageUtils {
       
        /**
         * png image encoder.
         */
        private static var encoder:PNGEncoder = new PNGEncoder();
       
        public function ImageUtils() {
        }
       
        /**
         * Given a class (with an embeded png image), 
         * creates a corresponding displayable image,
         * if imageClass is not null.
         *
         * @param imageClass - The image class from 
         *            which the image has to be created.
         * @param height - Height of the final image (optional).
         * @param width - Width of the final image (optional).
         * @return - mx.controls.Image produced.
         */
        public static function displayableImageFromClass( 
                   imageClass: Class, height: int = 25, 
                                   width: int = 25): Image {
            var i:Image = new Image();
            if (imageClass != null) {
                var bitMap: Bitmap = new imageClass();
                var bitmapData: BitmapData = bitMap.bitmapData;
                bitmapData.draw(bitMap);
                var imageToDisplay:ByteArray = 
                         encoder.encode(bitmapData);
                i.source = imageToDisplay;
                i.height = height;
                i.width = width;
            }
            return i;
        }
    }
}

================================================================

  If the 'imageClass' parameter is null, simply a new instance of mx.controls.Image is formed and returned. If not a 'Bitmap' is formed by instantiating the 'imageClass' passed in. Then its 'BitmapData' is derived. A byte array is constructed with the help of PNGEncoder and assigned to the source of the image instance. Image is returned after setting its dimensions.

Invocation of the method can be done as below;

[Embed (source="/assets/images/your_png.png" )] 
var resource: Class; // define a class with the actual resource.

// height and width can be passed if necessary.
var i:Image = ImageUtils.displayableImageFromClas(resource);



thanks,
Shyarmal.

Thursday, December 13, 2012

Distinguish 'double click' from 'click' in Flex

   Defining behavior for both 'click' and 'double click' events in Flex is tricky, as when a double click is done, the click event gets fired. Therefore there needs to be a workaround to solve this issue. First of all add event listeners for 'click' and 'double click' (this can be done either in mxml or actionscrips which I have chosen the later).


//Adding click listener
component.addEventListener(MouseEvent.CLICK, localMouseClickEvent, false, 0, true);

//Adding double click listener
component.addEventListener(MouseEvent.DOUBLE_CLICK, localMouseDoubleClickedEvent, false, 0, true);

//n.b. here component refers to any mx UIComponent 


   According to the code above if a mouse click is issued, 'localMouseClickEvent(MouseEvent)' method is called and for a double click, 'localMouseDoubleClickedEvent(MouseEvent)' should be called. However, when a double click is done, 'localMouseClickEvent(MouseEvent)' triggers.Both functions are defined below. 


/** reference to the deferred click operation */
private var deferredClickReference: Number = 0;


protected function localMouseClickEvent (event: MouseEvent) : void {    
          clearInterval(deferredClickReference); // clearing previous schedule
         
          // defer operation                   
          deferredClickReference = setInterval(deferredMouseClickEvent, 300); 
}
   
protected function deferredMouseClickEvent() : void {
        clearInterval(deferredClickReference); // clearing previous schedule  
        
       // code to execute for 'click'
}

protected function localMouseDoubleClickedEvent(event:MouseEvent):void {
         // revoking deferred click operation
         clearInterval(deferredClickReference);

        // code to execute for 'double click'
}


   The workaround is to defer the single click operations to see if it is actually followed by another click. Another function, 'deferredMouseClickEvent()' is defined, and invoked from localMouseClickEvent(event: MouseEvent0)' scheduled to be triggered after 300 ms. This scheduling is done by 'setInterval(deferredMouseClickEvent, 300)' , which returns a reference to the schedule (this is assigned to 'deferredClickReference'). So if a click is followed by another within 300 ms, double click event operations will be run, otherwise that of click
   'clearInterval(deferredClickReference)' resets the reference, which revokes the scheduled single click operation. This is done before scheduling the deferred operation, after execution of the same and in double click event's function (to prevent deferredMouseClickEvent() run).


 thanks,
Shyarmal.

Wednesday, December 5, 2012

Delete XML nodes in Flex

This is about deleting a node from XML in Flex. Here our XML is assigned to a variable, treeXml.

var treeXml: XML = <root>
                       <node label="abc">
                             <node label="Page"></node>
                             <node label="Business"></node>
                             <node label="Application"></node>
                             <node label="Loyalty"></node>
                        </node>
                        <node label="def">
                             <node label="Page"></node>
                             <node label="Business"></node>
                             <node label="Application"></node>
                             <node label="Loyalty"></node>
                        </node> 
                     </root>

Suppose we need to delete the node with label 'abc'. The way to do it is;

delete treeXml.node.(@label=="abc")[0];

Above code deletes the child (node) which is having the attribute label equal to 'abc'. Notice '[0]' at the end of the statement. After this, treeXml would be;

                    <root>
                         <node label="def">
                              <node label="Page"></node>
                              <node label="Business"></node>
                              <node label="Application"></node>
                              <node label="Loyalty"></node>
                         </node> 
                    </root>

Thanks,
Danuka.