<?xml version="1.0" encoding="utf-8"?>
<!--
/**                                   
 * Web App Solution Confidential Information
 * Copyright 2009, Web App Solution, Inc.
 *
 * @author Brian Riley 
 * @date May, 13, 2009
 *
 * Flex + Cairngorm + Spring ActionScript + Tomcat + BlazeDS + Spring Java + Hibernate + MySQL Tutorial Part 2
 *
 * Part 3 extends the basic Flex + Cairngorm Employee Management Console application by adding 
 * the Spring ActionScript Framework and injecting services into the Business Delegates at runtime.
 */
 -->
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:controllers="com.wasi.employeeconsole.controllers.*"
    pageTitle="Employee Management Console 3 :: Flex + Cairngorm + Spring AS (Inject Services)" 
    creationComplete="init()"
    viewSourceURL="srcview/index.html">
    
    <mx:Script>
        <![CDATA[
            import mx.resources.ResourceBundle;
            import mx.controls.Alert;
            import org.springextensions.actionscript.context.support.FlexXMLApplicationContext;
            import com.wasi.employeeconsole.models.CairngormModelLocator;
            import com.wasi.employeeconsole.events.EmployeeEvent;
            
            [ResourceBundle("ClassReferences")]
            private var springClassRefs:ResourceBundle;
            
            /**
             * Defines the application specific context by which objects and 
             * their dependencies are loaded.
             */
            private var applicationContext:FlexXMLApplicationContext;
            
            private function init():void
            {
                trace("Application init");
                
                this.loadSpringAppContext();
            }
            
            /**
             * Initializes the <code>applicationContext</code> instance and adds 
             * listeners for the context file loading events.
             */
            private function loadSpringAppContext():void
            {    
                trace("Application loadSpringAppContext");
                
                var applicationContextURL:String;
                
                applicationContextURL = "../assets/springactionscript/applicationContext.xml";
                
                // Initializes the <code>applicationContext</code> instance and adds listeners for the context file loading events.
                this.applicationContext = new FlexXMLApplicationContext(applicationContextURL);
                this.applicationContext.addEventListener(Event.COMPLETE, applicationContextLoadResult);
                this.applicationContext.addEventListener(IOErrorEvent.IO_ERROR, applicationContextLoadFault);
                this.applicationContext.load();
            }
            
            /**
             * <code>IApplicationContextLoader.applicationContextLoadResult</code>
             * implementation which handles successful loading of the context
             * document.
             */
            public function applicationContextLoadResult(event:Event):void
            {
                trace(    "Application applicationContextLoadResult " + 
                        "\n" + 
                        "--------------------------------------------" + 
                        "\n" + 
                        "Spring ActionScript Conext Filed Loaded" + 
                        "\n" + 
                        "--------------------------------------------");
                
                this.applicationContext.getObject("delegateLocator");
            }
            
            /**
             * <code>IApplicationContextLoader.applicationContextLoadFault</code>
             * implementation which handles an exception when loading the context
             * document.
             */
            public function applicationContextLoadFault(iOErrorEvent:IOErrorEvent):void
            {
                trace("Application applicationContextLoadFault " + iOErrorEvent.text);
                
                // we'll go with a simple stoopid apprach for this right now and just throw an alert
                Alert.show("Spring Application Context Failed to Load", "Error");
            }
            
            private function getList(event:MouseEvent):void
            {
                trace("Application getList");
                
                new EmployeeEvent(EmployeeEvent.GET_LIST).dispatch();
            }
        ]]>
    </mx:Script>
    
    <controllers:CairngormFrontController id="frontController" />
    
    <mx:VBox width="800" height="562" y="10" x="10">

        <mx:ApplicationControlBar width="100%" borderColor="#408080" fillColors="[#408080, #408080]" fillAlphas="[0.53, 0.53]" autoLayout="true">

            <mx:Text text="Employee Management Console" fontSize="26" fontWeight="bold" fontStyle="italic" themeColor="#ff8000" alpha="0.52"/>

        </mx:ApplicationControlBar>

        <mx:HBox x="10" y="10" width="800" height="455" id="hbox1">

            <mx:Panel id="listEmployeePanel" width="100%" height="455" layout="absolute" title="Employee List" cornerRadius="8" borderColor="#408080" fontSize="13">

                <mx:DataGrid 
                    id="dgrid" 
                    dataProvider="{CairngormModelLocator.getInstance().employeeList}"
                    width="100%" height="411" 
                    bottom="0" right="0">

                    <mx:columns>

                        <mx:DataGridColumn headerText="Employee ID" dataField="id"/>

                        <mx:DataGridColumn headerText="First Name" dataField="firstName"/>

                        <mx:DataGridColumn headerText="Last Name" dataField="lastName"/>

                    </mx:columns>

                </mx:DataGrid>

            </mx:Panel>
            
            <mx:Panel  id="editEmployeePanel" width="100%" height="455" layout="absolute" title="Edit Employee" cornerRadius="8" borderColor="#408080" fontSize="13">

                    <mx:Form id="editform" x="38.5" y="114">

                        <mx:FormItem label="Employee ID">

                            <mx:TextInput id="empID" editable="false" backgroundColor="#d7d7d7"/>

                        </mx:FormItem>

                        <mx:FormItem label="First Name">

                            <mx:TextInput id="fname"/>

                        </mx:FormItem>

                        <mx:FormItem label="Last Name">

                            <mx:TextInput id="lname"/>

                        </mx:FormItem>

                    </mx:Form>

                    <mx:Button x="73.5" y="257" label="Save" />

                    <mx:Button x="143.5" y="257" label="Delete" />

                    <mx:Button x="224.5" y="257" label="Cancel" />

                </mx:Panel>

        </mx:HBox>

        <mx:ApplicationControlBar width="100%" borderColor="#408080" fillColors="[#408080, #408080]" fillAlphas="[0.55, 0.31]">

            <mx:Button label="Refresh List" click="getList(event)"/>

            <mx:Button label="Delete Selected Employee" id="button2"/>

            <mx:Button label="Add New Employee" id="button1" />

        </mx:ApplicationControlBar>

    </mx:VBox>
    
</mx:Application>