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

    Simple Drag and Drop AIR

    June 18th, 2007

    Drag and Drop from the desktop to an AIR application is one of the new features of AIR.

    Basically, I add the event listeners to the application to handle the drag events. Then when a file is dropped onto the application, I check the file extension and then handle it if is an image file or throw an alert if not. You can certainly update the switch statement to handle additional file extensions differently.

    Here is some sample code:

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

        <mx:Script>
            <![CDATA[
                import flash.desktop.DragActions;
                import mx.controls.Alert;
                import mx.controls.Image;
                import flash.filesystem.File;
                import flash.desktop.TransferableData;
                import flash.desktop.TransferableFormats;
                import flash.events.NativeDragEvent;
                import flash.desktop.DragManager;
            
                private function init():void{
                    this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
                                this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
                                this.addEventListener(NativeDragEvent.NATIVE_DRAG_EXIT,onDragExit);
                }
               
            public function onDragIn(event:NativeDragEvent):void{
                       DragManager.acceptDragDrop(this);
                }

                public function onDrop(event:NativeDragEvent):void{
                    DragManager.dropAction = DragActions.COPY;
                    var dropfiles:Array = event.transferable.dataForFormat(TransferableFormats.FILE_LIST_FORMAT) as Array;
                    for each (var file:File in dropfiles){
                        switch (file.extension){   
                            case "png" :
                                addImage(file.nativePath);
                                break;
                            case "jpg" :
                                addImage(file.nativePath);
                                break;
                            case "gif" :
                                addImage(file.nativePath);
                                break;
                            default:
                               Alert.show("Unmapped Extension");
                        }
                  }
                }
              
                public function onDragExit(event:NativeDragEvent):void{
                    trace("Drag exit event.");
                }
                
                private function addImage(nativePath:String):void{
                    var i:Image = new Image();
                if(Capabilities.os.search("Mac") >
    = 0){
                                i.source = "file://" + nativePath;
                            } else {
                        i.source = nativePath;
                            }
                this.addChild(i);
                }
           
            ]]>
        </mx:Script>
    </mx:WindowedApplication>

    AIR Embedded Database Code Exerpts 2

    June 13th, 2007

    Here is today’s excerpt. This is the data access class that is used to save and retrieve the userid from the SQLite database. Here is the flow:

    When the application launches, the PreferencesDAO class is instantiated. If a userid can not be retrieved from the local database, a Cairngorm event is dispatched to get a new userid from a remote SQL server via ColdFusion. The userid is then saved to the local database using the savePreferences() function.

    package com.everythingflex.employeeDirectory.dao
    {
        import com.adobe.cairngorm.control.CairngormEventDispatcher;
        import com.everythingflex.employeeDirectory.control.GetUserIdEvent;
        import com.everythingflex.employeeDirectory.model.ModelLocator;
       
        import flash.data.SQLStatement;
        import flash.events.SQLErrorEvent;
        import flash.events.SQLEvent;
        import flash.data.SQLResult;
       
        public class PreferencesDAO
        {
           
            private var model:ModelLocator = ModelLocator.getInstance();
           
            private var savePrefSQL:SQLStatement;
            private var getUserIdSQL:SQLStatement;
           
            private function statusHandler(event:SQLEvent):void {
            //  model.log += event;
            }
           
            // save preferences data
            public function savePreferences(userId:int):void{
                var sqlText:String = "INSERT INTO Preferences" +
                " (USER_ID)" +
                " VALUES(" + "'" + userId + "')"
                savePrefSQL = new SQLStatement();
                savePrefSQL.sqlConnection = model.db;
                savePrefSQL.addEventListener(SQLEvent.RESULT, savePreferencesResult);
                savePrefSQL.addEventListener(SQLErrorEvent.ERROR, statusHandler);
                savePrefSQL.text = sqlText;
                savePrefSQL.execute();
                model.log += sqlText + " \n";
            }
           
            private function savePreferencesResult(event:SQLEvent):void{
                model.log += "add successful \n";
            }
           
            // get userid from local data
            public function getUserId():void{
                var sqlText:String = "SELECT USER_ID FROM Preferences";
                getUserIdSQL = new SQLStatement();
                getUserIdSQL.sqlConnection = model.db;
                getUserIdSQL.addEventListener(SQLEvent.RESULT, getUserIdResult);
                getUserIdSQL.addEventListener(SQLErrorEvent.ERROR, statusHandler);
                getUserIdSQL.text = sqlText;
                getUserIdSQL.execute();
                model.log +=  sqlText + " \n";
            }
           
            // if local userid does not exist, retieve a new userid from remote server
            private function getUserIdResult(event:SQLEvent):void{
                var result:SQLResult = getUserIdSQL.getResult();
                if(result.data.length > 0){
                    model.userId = result.data[0]["USER_ID"];
                } else {
                    getRemoteUserId();
                }
            }
               
            // if connected, retrieve a new userid from remote server   
            private function getRemoteUserId():void{
                var getUserIdEvent :GetUserIdEvent = new GetUserIdEvent();
              CairngormEventDispatcher.getInstance().dispatchEvent( getUserIdEvent );
            }
        }
           
       
    }

    AIR Embedded Database Code Exerpts 1

    June 12th, 2007

    Here are some excerpts for the AIR Embedded Database post.

    The class below on init will attempt to open the database file and if it is not found will then create the database file and then call the functions to create the tables.

    Creating the database:

    package com.everythingflex.employeeDirectory.utils
    {
        import com.adobe.cairngorm.control.CairngormEventDispatcher;
        import com.everythingflex.employeeDirectory.control.GetUserIdEvent;
        import com.everythingflex.employeeDirectory.dao.EmployeeDAO;
        import com.everythingflex.employeeDirectory.dao.PreferencesDAO;
        import com.everythingflex.employeeDirectory.model.ModelLocator;
       
        import flash.data.SQLResult;
        import flash.data.SQLStatement;
        import flash.events.SQLErrorEvent;
        import flash.events.SQLEvent;
        import flash.filesystem.File;
       
        public class CreateTables
        {
           
            private var model:ModelLocator = ModelLocator.getInstance();
       
            private var employeeDAO:EmployeeDAO = new EmployeeDAO();
            private var preferencesDAO:PreferencesDAO = new PreferencesDAO();

            private var createEmployeesSQL:SQLStatement;
            private var createPreferencesSQL:SQLStatement;

            // check for existance of local database and either open existing or create new tables
            public function init():void{
                var dbFile:File = File.applicationStorageDirectory.resolve("EmployeeDirectory.db");
                if(dbFile.exists) {
                    model.db.addEventListener(SQLEvent.OPEN, statusHandler);
                    model.db.open(dbFile);
                } else {
                    model.db.addEventListener(SQLEvent.OPEN, newDatabaseHandler);
                    model.db.open(dbFile)
                }
            }
           
            // get local data
            private function statusHandler(event:SQLEvent):void {
                employeeDAO.getEmployees();
                preferencesDAO.getUserId();
            }
           
            //  create new tables
            private function newDatabaseHandler(event:SQLEvent):void {
                createEmployeesTable();
                createPreferencesTable();
            }
           
            // create Employees table
            private function createEmployeesTable():void{
                var sqlText:String = "CREATE TABLE Employees(EMAIL TEXT, EMPLOYEE_ID INTEGER PRIMARY KEY, FIRST_NAME TEXT, JOB_TITLE TEXT, LAST_NAME TEXT, USER_ID INTEGER)";
                createEmployeesSQL = new SQLStatement();
                createEmployeesSQL.sqlConnection = model.db;
                createEmployeesSQL.addEventListener(SQLEvent.RESULT, createEmployeesTableResult);
                createEmployeesSQL.addEventListener(SQLErrorEvent.ERROR, statusHandler);
                createEmployeesSQL.text = sqlText;
                createEmployeesSQL.execute();
                model.log +=  sqlText + " \n";
            }
           
            private function createEmployeesTableResult(event:SQLEvent):void{
                model.log += "Employees table created \n";
            }
           
            // Create Preferences table
            private function createPreferencesTable():void{
                var sqlText:String = "CREATE TABLE Preferences(USER_ID INTEGER)";
                createPreferencesSQL = new SQLStatement();
                createPreferencesSQL.sqlConnection = model.db;
                createPreferencesSQL.addEventListener(SQLEvent.RESULT, createPreferencesTableResult);
                createPreferencesSQL.addEventListener(SQLErrorEvent.ERROR, statusHandler);
                createPreferencesSQL.text = sqlText;
                createPreferencesSQL.execute();
                model.log +=  sqlText + " \n";
            }
           
            // get a new unique userId from remote server
            private function createPreferencesTableResult(event:SQLEvent):void{
                model.log += "Preferences table created \n";
                var getUserIdEvent : GetUserIdEvent = new GetUserIdEvent();
              CairngormEventDispatcher.getInstance().dispatchEvent( getUserIdEvent );
            }
           
        }
    }

    I will post some additional excerpts when I get the time.


    Flex 2 Metadata Tags

    April 17th, 2007

    wddj51.jpg

    My article on Flex 2 Metadata tags “Telling the Compiler how to Compile” is now available in the current issue of the Web Developer’s & Designer’s Journal. Check it out on pages 26 - 31. You can read it here.


    Flickrin Apollo Tutorial

    April 6th, 2007

    Flickrin is a real world application for searching photos online using Flickr API created by Ashwinee Kumar Dash. Currently in version 1.0, the user can do a basic keyword search for thumbnails.

    flickrin.jpg

    Link to tutorial:
    http://flnotes.wordpress.com/2007/04/05/flickrin-ver-10-flex-flickr-apollo-mashup-tutorial/

    Link to sources:
    http://www.ashwineedash.com/download/Flickrin_1_src_files.zip

    Link to application:
    http://www.ashwineedash.com/download/flickrin1.0.zip