<?xml version="1.0" encoding="utf-8"?>
<!-- 
 *
 *  XMLResourceLocalizationSample
 *  Copyright (c) 2009 Robert Savage <www.savage7.com>
 *  Your reuse is governed by the Creative Commons Attribution 3.0 License
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 *
-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  
                xmlns:myControls="com.savage7.controls.*"
                layout="absolute" 
                viewSourceURL="srcview/index.html" 
                backgroundGradientAlphas="[1.0, 1.0]" 
                backgroundGradientColors="[#BDBDBD, #FFFDE3]" 
                creationComplete="init();" 
                currentState="Start">
    <mx:states>
        <mx:State name="Start">
            <mx:RemoveChild target="{titlewindow0}"/>
        </mx:State>
        <mx:State name="Loading">
            <mx:RemoveChild target="{titlewindow0}"/>
            <mx:RemoveChild target="{combobox1}"/>
            <mx:SetProperty target="{label7}" name="text" value="Loading XML Resources ..."/>
        </mx:State>
        <mx:State name="Ready">
        </mx:State>
    </mx:states>
    <mx:TitleWindow height="136" layout="absolute" title="XML Resource Localization" left="10" top="10" id="titlewindow1" width="231">
        <mx:Label text="Select Locale: " fontWeight="bold" id="label7" x="10" y="24"/>
        <mx:ComboBox dataProvider="{supportedLocales.locale}" labelField="@label" change="selectLocale(event);" id="combobox1" x="10" y="50" width="191"></mx:ComboBox>
    </mx:TitleWindow>
    <mx:TitleWindow layout="absolute" title="Event Log" left="10" right="10" bottom="10" top="154" id="titlewindow2">
        <mx:TextArea right="10" left="10" top="10" bottom="10" id="txtLog" wordWrap="false" enabled="true" editable="false" fontFamily="Courier New" fontSize="11"/>
    </mx:TitleWindow>
    <mx:TitleWindow layout="absolute" title="Localization Results" id="titlewindow0" left="249" right="10" top="10" verticalScrollPolicy="off" horizontalScrollPolicy="off">
        <mx:HBox x="0" y="0" width="100%" verticalAlign="middle" height="96" horizontalScrollPolicy="off" verticalScrollPolicy="off">
            <mx:Canvas height="100">
                <mx:Label text="Locale Chain:" fontWeight="bold" id="label3" x="10" y="3"/>
                <mx:Label text="...locale chain..." id="labelLocaleChain" x="117" y="3"/>
                <mx:Label text="Resource Origin:" fontWeight="bold" id="label2" x="10" y="24"/>
                <mx:Label text="{resourceManager.getString('myResources', 'myResourceOrigin')}" id="label1" x="117" y="24"/>
                <mx:Label text="Localized Label:" fontWeight="bold" id="label6" x="10" y="44"/>
                <mx:Label text="{resourceManager.getString('myResources', 'myLocalizedLabel')}" id="label5" x="118" y="44"/>
                <mx:Label text="Localized Button:" fontWeight="bold" id="label4" x="10" y="68"/>
                <mx:Button label="{resourceManager.getString('myResources', 'myLocalizedButton')}" id="button1" x="121" y="66"/>
            </mx:Canvas>
            <mx:Image source="{resourceManager.getString('myResources', 'myResourceFlagImage')}" id="image1"/>
            <mx:TextArea id="txtResourceFiles" borderStyle="none" wordWrap="false" editable="false" enabled="true" width="100%" verticalScrollPolicy="off" horizontalScrollPolicy="off" height="95%"/>
        </mx:HBox>
    </mx:TitleWindow>
    <mx:Script>
        <![CDATA[
            import mx.logging.targets.TraceTarget;
            import mx.logging.LogEventLevel;
            import mx.logging.LogEvent;
            import com.savage7.logging.targets.EventRaisingLoggingTarget;
            import mx.logging.Log;
            import com.savage7.localization.XmlResourceLoader;
            import mx.collections.XMLListCollection;
            import mx.controls.Alert;
            
            // create XML resource loader
            private var xmlResourceLoader:XmlResourceLoader = new XmlResourceLoader();
            private var loggingTarget:EventRaisingLoggingTarget = new EventRaisingLoggingTarget();

            [Bindable]
            private var supportedLocales:XML =
                 
                <locales>
                    <locale id='en' label='English'>
                        <localeChain>
                            <item>en</item> 
                        </localeChain>
                    </locale>
                    <locale id='en_US' label='English (United States)'>
                        <localeChain>
                            <item>en_US</item>
                            <item>en</item> 
                        </localeChain>
                    </locale>
                    <locale id='en_GB' label='English (Great Britain)'>
                        <localeChain>
                            <item>en_GB</item> 
                            <item>en</item> 
                        </localeChain>
                    </locale>
                    <locale id='es' label='Spanish'>
                        <localeChain>
                            <item>es</item> 
                            <item>en</item> 
                        </localeChain>
                    </locale>
                    <locale id='es_MX' label='Spanish (Mexico)'>
                        <localeChain>
                            <item>es_MX</item>
                            <item>es</item> 
                            <item>en</item> 
                        </localeChain>
                    </locale>
                    <locale id='fr' label='French'>
                        <localeChain>
                            <item>fr</item> 
                            <item>en</item> 
                        </localeChain>
                    </locale>
                    <locale id='fr_CA' label='French (Canada)'>
                        <localeChain>
                            <item>fr_CA</item>
                            <item>fr</item> 
                            <item>en</item> 
                        </localeChain>
                    </locale>
                </locales>
        

            /**
             * iniitalize application 
             */ 
            private function init():void
            {
                // register logging target to listen for log events
                loggingTarget.addEventListener(LogEvent.LOG,logEvent_Handler);
                loggingTarget.includeCategory = true;
                loggingTarget.includeDate = false;
                loggingTarget.includeLevel = true;
                loggingTarget.includeTime = true;
                mx.logging.Log.addTarget(loggingTarget);
                
                // listen for resource loading events
                xmlResourceLoader.addEventListener(XmlResourceLoader.COMPLETED, xmlResourceLoader_CompletedHandler);
                xmlResourceLoader.addEventListener(XmlResourceLoader.STARTED, xmlResourceLoader_StartedHandler);
            }
            
            private function xmlResourceLoader_StartedHandler(event:Event):void
            {
                currentState = "Loading";                
            }

            private function xmlResourceLoader_CompletedHandler(event:Event):void            
            {
                currentState = "Ready";
                
                // update locale chain
                labelLocaleChain.text = resourceManager.localeChain.toString();
                
                // clear resource list
                txtResourceFiles.htmlText = "";
                txtResourceFiles.htmlText = "<font color='#0000FF'>";
                
                
                // display resource file used
                for each (var resourceFile:String in xmlResourceLoader.resourceFiles)
                {
                    txtResourceFiles.htmlText = txtResourceFiles.htmlText + "<p><a href='" + resourceFile + "' target='_blank'><u>" + resourceFile + "</u></a></p>"
                } 
                txtResourceFiles.htmlText = txtResourceFiles.htmlText + "</font>";
            }

            /**
             * handles locale combo box change event when 
             * user selects a different locale to display
             */ 
            private function selectLocale(event:Event):void
            {
                // clear logged event text area
                txtLog.htmlText = "";
                 
                // get the locale chain from the XML 
                // configuration data defined above in the 
                // supportedLocales XML object
                var localeChain:XMLListCollection = new XMLListCollection(event.target.selectedItem.localeChain.item);
                
                // load the chain of localized resources from the corresponding XML files
                // the xml resource loader will apply the locale chain after all the 
                // resource have been loaded.
                xmlResourceLoader.load(localeChain.toArray());
            }
            
            
            /**
             * event handler for listening to logging events 
             */
            private function logEvent_Handler(event:LogEvent):void
            {
                var color:String = "black";

                switch(event.level)
                { 
                    case LogEventLevel.ALL:
                    {
                        color="#000000";                   
                        break;
                    }   
                    case LogEventLevel.DEBUG:
                    {
                        color="#000000";
                        break;
                    }   
                    case LogEventLevel.INFO:
                    {
                        color="#008810";
                        break;
                    }   
                    case LogEventLevel.WARN:
                    {
                        color="#0000FF";
                        break;
                    }   
                    case LogEventLevel.ERROR:
                    {
                        color="#FF0000";
                        break;
                    }   
                    case LogEventLevel.FATAL:
                    {
                        color="#FF0000";
                        break;
                    }
                }
                
                txtLog.htmlText = txtLog.htmlText + "<p><font color=\"" + color  + "\">" + event.message + "</font></p>";
            }


        ]]>
    </mx:Script>        
</mx:Application>