• Home
  • About Me
  • AIR Central
  • AS3 Libs
  • Books
  • Flex Central
  • Resources
  • The Guru's
  •  

    More fun with AIR LocalConnection (Source Included)

    January 11th, 2008

    This sample has 2 components. The AIR portion and the Flex web portion. It allows you to drag and image into the AIR application and send it to the browser based Flex application via LocalConnection.

    The AIR application accepting the image:
    eflex012.jpg

    The image now showing in the browser:
    eflex013.jpg

    Download and install this AIR application. Run the application, click the link to http://www.everythingflex.com/flex3/DropIt/Index.html to open the browser portion of the application.

    As long as you have green check marks within both the AIR and Flex apps you are ready to give it a shot. Drag a small gif or jpg into the red box of the AIR application, wait a few seconds and you should get an Alert telling you to check your browser.

    Here is the source (the AIR and Flex apps also have view source enabled)

    AIR:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="absolute" creationComplete="init()"
        horizontalScrollPolicy="off" verticalScrollPolicy="off"
        width="425" height="325" backgroundColor="#FFFFFF" viewSourceURL="srcview/index.html">
        <mx:Script>
            <![CDATA[
                import mx.events.FlexEvent;
                import mx.graphics.codec.JPEGEncoder;
                import mx.controls.Alert;
                import flash.net.LocalConnection;
                import flash.events.StatusEvent;
                
                import mx.controls.Image;
                import flash.filesystem.File;
               
                [Bindable]
                private var targetURL:String = "http://www.everythingflex.com/flex3/DropIt/Index.html";
               
                public var outbound:LocalConnection = new LocalConnection();
                public var inbound:LocalConnection = new LocalConnection();

                [Bindable]
                private var isConnected:Boolean = true;

                [Bindable]
                [Embed(source="y.gif")]
                private var yes:Class;
               
                [Bindable]
                [Embed(source="x.gif")]
                private var no:Class;
               
                private var timer:Timer;
               
                private var bitmapData:BitmapData;

                public function init():void{
                    isConnected = false;
                    outbound.addEventListener(StatusEvent.STATUS, onStatus);               
                    inbound = new LocalConnection();
                    inbound.allowDomain('*');
                    inbound.client = this;
                    try {
                        inbound.connect("aircon");
                    } catch (error:ArgumentError) {
                        Alert.show("Connection Failure");
                    }
                    timer = new Timer(1000, 0);
                    timer.addEventListener("timer", timerHandler);
                    timer.start();   
                    this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
                  this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
                }
       
                public function timerHandler(event:TimerEvent):void {              
                    outbound.send("everythingflex.com:flexcon", "testCon", outbound.domain);
                }

                public function testCon(s:String):void {
                    isConnected = true;
                }

                public function getResponse(s:String):void {
                    Alert.show(s,"Success");
                }

                private function onStatus(event:StatusEvent):void {
                    if(event.level == "error"){
                        isConnected = false;
                    } else{
                        isConnected = true;
                    }
                }
                
            
                private function onDragIn(event:NativeDragEvent):void{
                  NativeDragManager.acceptDragDrop(this);
                }

            private function onDrop(event:NativeDragEvent):void{
              var dropfiles:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
              for each (var file:File in dropfiles){
                switch (file.extension.toLowerCase()){   
                  case "png" :
                    addImage(file.nativePath);
                    break;
                  case "jpg" :
                    addImage(file.nativePath);
                    break;
                  case "jpeg" :
                    addImage(file.nativePath);
                    break;
                  case "gif" :
                    addImage(file.nativePath);
                    break;
                  default:
                    Alert.show("Unmapped Extension");
                  }
                }
              }
             
              private function addImage(nativePath:String):void{
                var i:Image = new Image();
                i.visible = false;
                i.addEventListener(FlexEvent.UPDATE_COMPLETE,sendJPEG);
                if(Capabilities.os.search("Mac") >= 0){
                  i.source = "file://" + nativePath;
                } else {
                  i.source = nativePath;
                }
                this.addChild(i);
              }
             
              private function sendJPEG(event:FlexEvent):void{
                try{
                var img:Image = event.target as Image;
                 bitmapData = new BitmapData(img.width, img.height);
                bitmapData.draw(img);
                var bitmap:Bitmap = new Bitmap(bitmapData);
                 var jpg:JPEGEncoder = new JPEGEncoder();
                 var ba:ByteArray = jpg.encode(bitmapData);
                 outbound.send("everythingflex.com:flexcon", "receiveImage", ba);
                } catch(e:Error){
                 
                }
              }
             
              private function setVisuals(b:Boolean):Boolean{
                    if(b){
                        received.source = yes;
                        message.text = "Drop a small image in the zone below";
                    } else {
                        received.source = no;
                        message.text = "Make sure browser is open and pointing to:"
                    }
                    return b;
                }
                
                private function openBrowser():void{
                    var url:String = targetURL;
                    var urlRequest:URLRequest = new URLRequest(url);
                    navigateToURL(urlRequest);
                }
                
            ]]>
        </mx:Script>

        <mx:Label text="Connected to Flex" x="146.5" y="282"/>
        <mx:Image id="received" x="260.5" y="282" width="16" height="18" source="{no}"/>   
        <mx:Text id="message" x="10" y="10" width="403" textAlign="center" color="#000000" fontWeight="bold"/>
        <mx:LinkButton id="link" visible="{!setVisuals(isConnected)}" click="openBrowser()" label="{targetURL}" x="10" y="34" textAlign="center" width="100%"/>
        <mx:HBox id="dropzone" x="10" y="57" width="403" height="218" borderStyle="solid" borderThickness="3" borderColor="#E81414">
        </mx:HBox>
           
    </mx:WindowedApplication>

    Flex:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
        creationComplete="init()" viewSourceURL="srcview/index.html">
        <mx:Script>
            <![CDATA[
                import flash.net.LocalConnection;
                import mx.controls.Alert;
               
                private var inbound:LocalConnection = new LocalConnection();;
                private var outbound:LocalConnection = new LocalConnection();;
               
                private var outboundDomain:String;
               
                private var timer:Timer;
               
                [Bindable]
                [Embed(source="y.gif")]
                private var yes:Class;
               
                [Bindable]
                [Embed(source="x.gif")]
                private var no:Class;
               
                public function init():void{
                    outbound.addEventListener(StatusEvent.STATUS, onStatus);   
                    inbound.allowDomain('*');
                    inbound.client = this;
                    try {
                        inbound.connect("flexcon");
                    } catch (error:ArgumentError) {
                        Alert.show("Connection failed, already in use", "Error");
                    }
                }

                private function onStatus(event:StatusEvent):void {
                    if(event.level == "error"){
                        received.source = no;
                    } else{
                        received.source = yes;
                    }
                }
       
                public function testCon(s:String):void{
                    outboundDomain = s;
                    outbound.send(outboundDomain + ":aircon", "testCon", "OK");
                    timer = new Timer(1000, 0);
                    timer.addEventListener("timer", timerHandler);
                    timer.start();
                }
               
                public function timerHandler(event:TimerEvent):void {              
                    outbound.send(outboundDomain + ":aircon", "testCon", "OK");
                }

                public function receiveImage(ba:ByteArray):void {
                    img.source = ba;
                    respond();

                }

                private function respond():void {
                    outbound.send(outboundDomain + ":aircon", "getResponse", 'Check you browser');
                }

            ]]>

        </mx:Script>

        <mx:Canvas horizontalCenter="0" verticalCenter="0" backgroundColor="#FFFFFF" width="100%" height="100%">
            <mx:Image id="img" x="10" y="36"/>
            <mx:Label text="Connected to AIR" x="241" y="14" color="#000000"/>
            <mx:Image id="received" x="352" y="14" width="16" height="16" source="{no}"/>
        </mx:Canvas>
    </mx:Application>

    Spelling Plus Library

    January 8th, 2008

    spellcheck.jpg

    Grant Skinner has updated the Spelling Plus Library for doing Flash/Flex spell checking.


    Flex and AIR User Group Pre-release Tour

    January 7th, 2008
    flex2.png air.png

    Flex 3 and AIR are getting close to launch and in preparation, the Adobe
    Platform Evangelist team is traveling to select cities to show off the
    great new features and some brand new demos.

    Flex 3 is a feature-packed release, adding new UI components like the
    advanced datagrid and improved CSS capabilities; powerful tooling
    additions like refactoring; and extensive testing tools including memory
    and performance profiling, plus the addition of the automated testing
    framework to Flex Builder.

    Adobe AIR is game-changing in so many ways, extending rich applications
    to the desktop, enabling access to the local file system, system tray,
    notifications and much more. Now you can write desktop applications
    using the same skills that you’ve been already using to create great web
    apps including both Flex and AJAX.

    Don’t miss out on the opportunity to see and hear about this highly
    anticipated release of Flex 3 and AIR during this special pre-release
    tour. Plus, in addition to giving away some one of a kind Flex/AIR
    branded schwag, we will also be raffling off a copy of Flex Builder 3
    Professional (pending availability) and a full commercial copy of CS3
    Web Premium at this event!

    Check out the comprehensive listing of dates at flex.org/tour to see if
    the tour is coming to your city!


    FotoBooth for AIR beta 3

    January 7th, 2008

    I have finally gotten around to porting FotoBooth to AIR Beta 3. I have added the badge installer to the AIR applications page or you can use the installer below.




    360Flex Shedule is up

    January 4th, 2008

    360flex.png

    The schedule for 360Flex Atlanta has been posted: