<?xml version="1.0" encoding="utf-8"?>
<s:TextArea xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark" 
            xmlns:mx="library://ns.adobe.com/flex/mx"
            implements="de.dtele.ui.components.viewer.IResourceViewer"
            editable="false"
            fontFamily="Lucida Console, _monospace"
            borderVisible="false"
            width="100%"
            height="100%"
            add="onAdd(event)"
            remove="onRemove(event)">
  
  <fx:Metadata>
    /**
     * Allows for viewing plain text resources
     * 
     * @author Mathias Brodala
     */
  </fx:Metadata>
  
  <fx:Script>
    <![CDATA[
      import de.dtele.data.IResource;
      
      import mx.events.FlexEvent;
      
      public function view(resource:IResource):void {
        
        var loader:URLLoader = new URLLoader(new URLRequest(resource.fullUrl));
        
        loader.addEventListener(Event.COMPLETE, this.onComplete);
        loader.addEventListener(IOErrorEvent.IO_ERROR, this.onIOError);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onSecurityError);
      }
      
      /**
       * Sets the text content after loading the data
       */
      protected function onComplete(e:Event):void {
        
        this.text = (e.target as URLLoader).data;
        this.dispatchEvent(e);
      }
      
      /**
       * Notifies about I/O errors
       */
      protected function onIOError(e:IOErrorEvent):void {
        
        trace("[TextResourceViewer] I/O error encountered");
      }
      
      /**
       * Notifies about security errors
       */
      protected function onSecurityError(e:SecurityErrorEvent):void {
      
        trace("[TextResourceViewer] Security error encountered");
      }
      
      /**
       * Sets up the listener for parent resizes
       */
      protected function onAdd(e:FlexEvent):void {
        
        this.parent.addEventListener(Event.RESIZE, this.onParentResize);
      }
      
      /**
       * Removes the listener for parent resizes
       */
      protected function onRemove(event:FlexEvent):void {
        
        this.parent.removeEventListener(Event.RESIZE, this.onParentResize);
      }
      
      /**
       * Forces layout recalculation upon parent resize
       */
      protected function onParentResize(e:Event):void {
        
        // Re-setting these values forces resizing, manually calling
        // any of the invalidate*() or validate*() methods does not work
        this.percentWidth = this.percentHeight = 100;
      }
      
    ]]>
  </fx:Script>
  
</s:TextArea>