• 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>

    Indy Flex User Group Tomorrow!

    June 18th, 2007

    Can’t make it to D.C. for Flex maniacs next week? You can see my presentation tomorrow night at the Indy Flex User Group. The title is “Custom Formatter, Validator, and Effects Components”.

    Flex Maniacs Presentation

    Here are the details:

    Meeting Date:
    Tuesday June 19th, 2007
    Meeting Time:
    6:30 PM

    Location:
    LANTech Training
    The Pyramids,
    3500 DePauw Blvd.
    Pyramid 3, Floor 2
    Indianapolis, IN 46268


    Connect.Discover.Enspire

    June 14th, 2007

    Well MAX 2007 has made a major shift from Engage to Connect.Discover.Enspire.

    Connect.

    * Connect with the Community.

    * Strengthen Relationships.

    * Foster Ecosystem and Evangelism.

    Discover.

    * Discover the latest technology.

    * Learn new skills.

    * Enable rich, engaging experiences.

    Inspire.

    * Find inspiration in leading developers / designers.

    * Inspire each other to build innovative projects.

    This is a very good thing, and let me tell you why. Adobe has taken the advise (complaints) from the community and decided to return MAX to its roots which was a community developer conference.

    Ted Patrick is now running the show for MAX 2007 and has really dedicated himself to making this years MAX conference one that is targeted at the community and not an Adobe sales pitch. What does this mean? It means that the community will have more of a presence in the speaker list.

    Stay tuned for more info and say thank you to Ted next time you see him as he is working his *ss off to make MAX the best event ever.


    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.