hackedteam/rcs-console

View on GitHub
src/it/ht/rcs/console/operations/view/evidences/advanced/viewers/Screenshot.mxml

Summary

Maintainability
Test Coverage
<?xml version="1.0" encoding="utf-8"?>
<s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   xmlns:viewers="it.ht.rcs.console.operations.view.evidences.advanced.viewers.*"
                   height="100%"
                   width="100%"
                   borderColor="0xCCCCCC"
                   backgroundColor="0xFFFFFF"
                   backgroundAlpha="1" creationComplete="init()">
  <s:layout>
    <s:VerticalLayout paddingBottom="10"
                      paddingLeft="10"
                      paddingRight="10"
                      paddingTop="10"
                      />
  </s:layout>
  
  <s:states>
    <s:State name="image"/>
    <s:State name="ocr"/>
  </s:states>
  
  <fx:Script>
    <![CDATA[
      
      import flashx.textLayout.conversion.TextConverter;
      
      import it.ht.rcs.console.DB;
      import it.ht.rcs.console.evidence.controller.EvidenceManager;
      import it.ht.rcs.console.evidence.model.Evidence;
      import it.ht.rcs.console.monitor.controller.LicenseManager;
      import it.ht.rcs.console.operations.view.evidences.EvidenceAdvancedViewer;
      import it.ht.rcs.console.operations.view.evidences.EvidencesView;
      import it.ht.rcs.console.monitor.controller.LicenseManager;
      
      import locale.R;
      
      import mx.rpc.events.FaultEvent;
      import mx.rpc.events.ResultEvent;

      private var _evidence:Evidence;
      
      private var _ocrLoaded:Boolean;
      
      private var mainView:EvidencesView;
      
      private var loader:URLLoader=new URLLoader();
      
      [Bindable]
      private var _url:String;
      
      public var target:String;
      
      [Bindable]
      public var idVisible:Boolean;
      
      [Bindable]
      private var ocrContent:String;
      
      [Embed(source="/img/NEW/eyeIcon.png")]
      [Bindable]
      public var eyeIcon:Class;
      
      [Embed(source="/img/NEW/ocrIcon.png")]
      [Bindable]
      public var ocrIcon:Class;
      
      [Bindable]
      private var translation:String;

      [Bindable]
      public function set evidence(value:Evidence):void
      {
        _evidence=value;
        target=EvidenceManager.instance.evidenceFilter.target;
        
        if(evidence.data._grid!=null)
        {
          if(DB.instance.demo)
          {
            _url="grid/" + evidence.data._grid;
          }
          else
          {
            _url=DB.hostAutocomplete(Console.currentSession.server) + "grid/" + evidence.data._grid + "?target_id=" + encodeURIComponent(target);
          }
         
       init()
        }
        
        _ocrLoaded=false;
        currentState="image";
        switchBtn.toolTip="OCR";
        switchBtn.source=ocrIcon;
        
        translation=evidence.data.tr;
        
        switch(evidence.data.tr)
        {  
          case 'TRANS_QUEUED':
            translation='('+R.get('TRANS_QUEUED')+')';
            break;
          case 'TRANS_IN_PROGRESS':
            translation='('+R.get('TRANS_IN_PROGRESS')+')';
            break;
          case 'TRANS_ERROR':
            translation='('+R.get('TRANS_ERROR')+')';
            break;
          case null:
            translation='('+R.get('TRANSLATION_UNAVAILABLE')+')';
            break;
          default:
            translation=evidence.data.tr;
        }
        //onTranslate(null)
      }
      
      private function loadOcrContent():void
      {
        //ocr
        var id:String=encodeURIComponent(evidence._id);
        var target:String=encodeURIComponent(EvidenceManager.instance.evidenceFilter.target);
        var url:String=DB.hostAutocomplete(Console.currentSession.server) + "evidence/body/" + id + "?target=" + target; 
        loader.addEventListener(Event.COMPLETE, onContentLoaded);
        loader.load(new URLRequest(url));
      }
      
      private function onContentLoaded(e:Event):void
      {
        loader.removeEventListener(Event.COMPLETE, onContentLoaded);
        ocrContent=e.currentTarget.data;
        if(ocrContent=="" || ocrContent==null )
          ocrContent = R.get('OCR_NO_DATA');
        ocrTxt.textFlow=TextConverter.importToFlow(ocrContent, TextConverter.PLAIN_TEXT_FORMAT);
        _ocrLoaded=true;
      }
      
      public function get evidence():Evidence
      {
        return _evidence;
      }
      
      private function init():void
      {
        target=EvidenceManager.instance.evidenceFilter.target;
        imageViewer.url=_url;
        
        var container:HGroup=this.owner as HGroup;
        var advancedViewer:EvidenceAdvancedViewer=container.owner as EvidenceAdvancedViewer;
        mainView=advancedViewer.owner as EvidencesView;
        mainView.addEventListener("translate", onTranslate);
      }
      
      private function onTranslate(e:Event):void
      {
        if(currentState=="ocr" && LicenseManager.instance.translation)
        {
          trace("ocr mode");
          
          if (evidence)
          {
            if (mainView.translation)
            {        
              //Show translated OCR content
              switch(evidence.data.tr)
              {
                case 'TRANS_QUEUED':
                  translation='('+R.get('TRANS_QUEUED')+')';
                  break;
                case 'TRANS_IN_PROGRESS':
                  translation='('+R.get('TRANS_IN_PROGRESS')+')';
                  break;
                case 'TRANS_ERROR':
                  translation='('+R.get('TRANS_ERROR')+')';
                  break;
                case null:
                  translation='('+R.get('TRANSLATION_UNAVAILABLE')+')';
                  break;
                default:
                  translation=evidence.data.tr;
              }
              ocrTxt.text=translation;
            }
            else
            { 
              //Show original OCR content
              if(!_ocrLoaded) loadOcrContent()
              else  ocrTxt.text=ocrContent
            }
           
          }
        }
        else
        {
        //image state - do nothing
          trace("image state")
        }
      }
      
      
      public function reset():void
      {
        imageViewer.reset()
      }
      
      private function switchView():void
      {
        if(currentState=="image")
        {
          currentState="ocr"
          switchBtn.toolTip="Image"
          switchBtn.source=eyeIcon;
          if(mainView.translation)
          {
            ocrTxt.text=translation
          }
          else
          {
            if(!_ocrLoaded) loadOcrContent();
          }    
        }
        else
        {
          currentState="image"
          switchBtn.toolTip="OCR"
          switchBtn.source=ocrIcon;
        }
      }
      
    ]]>
  </fx:Script>
  <s:RichText id="idTxt"
              width="100%"
              textFlow="{TextConverter.importToFlow('&lt;b&gt;Id: &lt;/b&gt;'+evidence._id, TextConverter.TEXT_FIELD_HTML_FORMAT)}"
              fontSize="12" visible="{idVisible}" includeInLayout="{idVisible}"/>
  <s:Line width="100%" visible="{idVisible}" includeInLayout="{idVisible}">
    <s:stroke>
      <s:SolidColorStroke color="0xCCCCCC"/>
    </s:stroke>
  </s:Line>
  <s:RichText id="programTxt" textFlow="{TextConverter.importToFlow('&lt;b&gt;'+R.get('PROGRAM')+': &lt;/b&gt;'+evidence.data.program, TextConverter.TEXT_FIELD_HTML_FORMAT)}" fontSize="12"/>
  <s:Line width="100%" >
    <s:stroke>
      <s:SolidColorStroke color="0xCCCCCC"/>
    </s:stroke>
  </s:Line>
  <s:HGroup width="100%" verticalAlign="middle">
  <s:RichText id="windowTxt" textFlow="{TextConverter.importToFlow('&lt;b&gt;'+R.get('WINDOW')+': &lt;/b&gt;'+evidence.data.window, TextConverter.TEXT_FIELD_HTML_FORMAT)}" fontSize="12" width="100%"/>
  <s:Image source="{ocrIcon}" toolTip="OCR" click="switchView()" id="switchBtn" useHandCursor="true" buttonMode="true" visible="{LicenseManager.instance.ocr}"/>
  </s:HGroup>
  <s:Line width="100%" >
    <s:stroke>
      <s:SolidColorStroke color="0xCCCCCC"/>
    </s:stroke>
  </s:Line>
  <viewers:ImageViewer id="imageViewer" url="{_url}" includeIn="image"/>
  <s:Scroller height="100%"
              width="100%" id="scroller" includeIn="ocr">
  <s:RichEditableText id="ocrTxt" textFlow="{TextConverter.importToFlow('OCR data', TextConverter.TEXT_FIELD_HTML_FORMAT)}" fontSize="12" width="100%" editable="false" selectable="true" />
  </s:Scroller>
</s:BorderContainer>