home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2012 April / ME_04_2012.iso / Video-Tutorial / iPhoto / media / player.swf / scripts / mx / controls / Label.as < prev    next >
Encoding:
Text File  |  2011-11-11  |  15.2 KB  |  500 lines

  1. package mx.controls
  2. {
  3.    import flash.display.DisplayObject;
  4.    import flash.events.Event;
  5.    import flash.geom.Rectangle;
  6.    import flash.text.StyleSheet;
  7.    import flash.text.TextLineMetrics;
  8.    import mx.controls.listClasses.BaseListData;
  9.    import mx.controls.listClasses.IDropInListItemRenderer;
  10.    import mx.controls.listClasses.IListItemRenderer;
  11.    import mx.core.IDataRenderer;
  12.    import mx.core.IFlexModuleFactory;
  13.    import mx.core.IFontContextComponent;
  14.    import mx.core.IUITextField;
  15.    import mx.core.UIComponent;
  16.    import mx.core.UITextField;
  17.    import mx.core.mx_internal;
  18.    import mx.events.FlexEvent;
  19.    
  20.    use namespace mx_internal;
  21.    
  22.    public class Label extends UIComponent implements IDataRenderer, IDropInListItemRenderer, IListItemRenderer, IFontContextComponent
  23.    {
  24.       mx_internal static var createAccessibilityImplementation:Function;
  25.       
  26.       mx_internal static const VERSION:String = "4.5.0.20967";
  27.       
  28.       private var textSet:Boolean;
  29.       
  30.       private var enabledChanged:Boolean = false;
  31.       
  32.       protected var textField:IUITextField;
  33.       
  34.       private var toolTipSet:Boolean = false;
  35.       
  36.       private var _condenseWhite:Boolean = false;
  37.       
  38.       private var condenseWhiteChanged:Boolean = false;
  39.       
  40.       private var _data:Object;
  41.       
  42.       private var _htmlText:String = "";
  43.       
  44.       mx_internal var htmlTextChanged:Boolean = false;
  45.       
  46.       private var explicitHTMLText:String = null;
  47.       
  48.       private var _listData:BaseListData;
  49.       
  50.       private var _selectable:Boolean = false;
  51.       
  52.       private var selectableChanged:Boolean;
  53.       
  54.       private var styleSheetChanged:Boolean = false;
  55.       
  56.       private var _styleSheet:StyleSheet;
  57.       
  58.       private var _tabIndex:int = -1;
  59.       
  60.       private var _text:String = "";
  61.       
  62.       mx_internal var textChanged:Boolean = false;
  63.       
  64.       private var _textHeight:Number;
  65.       
  66.       private var _textWidth:Number;
  67.       
  68.       public var truncateToFit:Boolean = true;
  69.       
  70.       public function Label()
  71.       {
  72.          super();
  73.       }
  74.       
  75.       override public function get baselinePosition() : Number
  76.       {
  77.          if(!mx_internal::validateBaselinePosition())
  78.          {
  79.             return NaN;
  80.          }
  81.          return this.textField.y + this.textField.baselinePosition;
  82.       }
  83.       
  84.       override public function set enabled(param1:Boolean) : void
  85.       {
  86.          if(param1 == enabled)
  87.          {
  88.             return;
  89.          }
  90.          super.enabled = param1;
  91.          this.enabledChanged = true;
  92.          invalidateProperties();
  93.       }
  94.       
  95.       override public function set toolTip(param1:String) : void
  96.       {
  97.          super.toolTip = param1;
  98.          this.toolTipSet = param1 != null;
  99.       }
  100.       
  101.       [Bindable("condenseWhiteChanged")]
  102.       public function get condenseWhite() : Boolean
  103.       {
  104.          return this._condenseWhite;
  105.       }
  106.       
  107.       public function set condenseWhite(param1:Boolean) : void
  108.       {
  109.          if(param1 == this._condenseWhite)
  110.          {
  111.             return;
  112.          }
  113.          this._condenseWhite = param1;
  114.          this.condenseWhiteChanged = true;
  115.          if(this.isHTML)
  116.          {
  117.             this.mx_internal::htmlTextChanged = true;
  118.          }
  119.          invalidateProperties();
  120.          invalidateSize();
  121.          invalidateDisplayList();
  122.          dispatchEvent(new Event("condenseWhiteChanged"));
  123.       }
  124.       
  125.       [Bindable("dataChange")]
  126.       public function get data() : Object
  127.       {
  128.          return this._data;
  129.       }
  130.       
  131.       public function set data(param1:Object) : void
  132.       {
  133.          var _loc2_:* = undefined;
  134.          this._data = param1;
  135.          if(this._listData)
  136.          {
  137.             _loc2_ = this._listData.label;
  138.          }
  139.          else if(this._data != null)
  140.          {
  141.             if(this._data is String)
  142.             {
  143.                _loc2_ = String(this._data);
  144.             }
  145.             else
  146.             {
  147.                _loc2_ = this._data.toString();
  148.             }
  149.          }
  150.          if(_loc2_ !== undefined && !this.textSet)
  151.          {
  152.             this.text = _loc2_;
  153.             this.textSet = false;
  154.          }
  155.          dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
  156.       }
  157.       
  158.       public function get fontContext() : IFlexModuleFactory
  159.       {
  160.          return moduleFactory;
  161.       }
  162.       
  163.       public function set fontContext(param1:IFlexModuleFactory) : void
  164.       {
  165.          this.moduleFactory = param1;
  166.       }
  167.       
  168.       [Bindable("htmlTextChanged")]
  169.       public function get htmlText() : String
  170.       {
  171.          return this._htmlText;
  172.       }
  173.       
  174.       public function set htmlText(param1:String) : void
  175.       {
  176.          this.textSet = true;
  177.          if(!param1)
  178.          {
  179.             param1 = "";
  180.          }
  181.          if(this.isHTML && param1 == this.explicitHTMLText)
  182.          {
  183.             return;
  184.          }
  185.          this._htmlText = param1;
  186.          this.mx_internal::htmlTextChanged = true;
  187.          this._text = null;
  188.          this.explicitHTMLText = param1;
  189.          invalidateProperties();
  190.          invalidateSize();
  191.          invalidateDisplayList();
  192.          dispatchEvent(new Event("htmlTextChanged"));
  193.       }
  194.       
  195.       private function get isHTML() : Boolean
  196.       {
  197.          return this.explicitHTMLText != null;
  198.       }
  199.       
  200.       [Bindable("dataChange")]
  201.       public function get listData() : BaseListData
  202.       {
  203.          return this._listData;
  204.       }
  205.       
  206.       public function set listData(param1:BaseListData) : void
  207.       {
  208.          this._listData = param1;
  209.       }
  210.       
  211.       public function get selectable() : Boolean
  212.       {
  213.          return this._selectable;
  214.       }
  215.       
  216.       public function set selectable(param1:Boolean) : void
  217.       {
  218.          if(param1 == this.selectable)
  219.          {
  220.             return;
  221.          }
  222.          this._selectable = param1;
  223.          this.selectableChanged = true;
  224.          invalidateProperties();
  225.       }
  226.       
  227.       public function get styleSheet() : StyleSheet
  228.       {
  229.          return this._styleSheet;
  230.       }
  231.       
  232.       public function set styleSheet(param1:StyleSheet) : void
  233.       {
  234.          this._styleSheet = param1;
  235.          this.styleSheetChanged = true;
  236.          this.mx_internal::htmlTextChanged = true;
  237.          invalidateProperties();
  238.       }
  239.       
  240.       override public function get tabIndex() : int
  241.       {
  242.          return this._tabIndex;
  243.       }
  244.       
  245.       override public function set tabIndex(param1:int) : void
  246.       {
  247.          this._tabIndex = param1;
  248.          invalidateProperties();
  249.       }
  250.       
  251.       [Bindable("valueCommit")]
  252.       public function get text() : String
  253.       {
  254.          return this._text;
  255.       }
  256.       
  257.       public function set text(param1:String) : void
  258.       {
  259.          this.textSet = true;
  260.          if(!param1)
  261.          {
  262.             param1 = "";
  263.          }
  264.          if(!this.isHTML && param1 == this._text)
  265.          {
  266.             return;
  267.          }
  268.          this._text = param1;
  269.          this.mx_internal::textChanged = true;
  270.          this._htmlText = null;
  271.          this.explicitHTMLText = null;
  272.          invalidateProperties();
  273.          invalidateSize();
  274.          invalidateDisplayList();
  275.          dispatchEvent(new FlexEvent(FlexEvent.VALUE_COMMIT));
  276.       }
  277.       
  278.       public function get textHeight() : Number
  279.       {
  280.          return this._textHeight;
  281.       }
  282.       
  283.       public function get textWidth() : Number
  284.       {
  285.          return this._textWidth;
  286.       }
  287.       
  288.       override protected function initializeAccessibility() : void
  289.       {
  290.          if(Label.mx_internal::createAccessibilityImplementation != null)
  291.          {
  292.             Label.mx_internal::createAccessibilityImplementation(this);
  293.          }
  294.       }
  295.       
  296.       override protected function createChildren() : void
  297.       {
  298.          super.createChildren();
  299.          if(!this.textField)
  300.          {
  301.             this.mx_internal::createTextField(-1);
  302.          }
  303.       }
  304.       
  305.       override protected function commitProperties() : void
  306.       {
  307.          super.commitProperties();
  308.          if(hasFontContextChanged() && this.textField != null)
  309.          {
  310.             this.mx_internal::removeTextField();
  311.             this.condenseWhiteChanged = true;
  312.             this.enabledChanged = true;
  313.             this.selectableChanged = true;
  314.             this.mx_internal::textChanged = true;
  315.          }
  316.          if(!this.textField)
  317.          {
  318.             this.mx_internal::createTextField(-1);
  319.          }
  320.          if(this.condenseWhiteChanged)
  321.          {
  322.             this.textField.condenseWhite = this._condenseWhite;
  323.             this.condenseWhiteChanged = false;
  324.          }
  325.          this.textField.tabIndex = this.tabIndex;
  326.          if(this.enabledChanged)
  327.          {
  328.             this.textField.enabled = enabled;
  329.             this.enabledChanged = false;
  330.          }
  331.          if(this.selectableChanged)
  332.          {
  333.             this.textField.selectable = this._selectable;
  334.             this.selectableChanged = false;
  335.          }
  336.          if(this.styleSheetChanged)
  337.          {
  338.             this.textField.styleSheet = this._styleSheet;
  339.             this.styleSheetChanged = false;
  340.          }
  341.          if(this.mx_internal::textChanged || this.mx_internal::htmlTextChanged)
  342.          {
  343.             if(this.isHTML)
  344.             {
  345.                this.textField.htmlText = this.explicitHTMLText;
  346.             }
  347.             else
  348.             {
  349.                this.textField.text = this._text;
  350.             }
  351.             this.textFieldChanged(false);
  352.             this.mx_internal::textChanged = false;
  353.             this.mx_internal::htmlTextChanged = false;
  354.          }
  355.       }
  356.       
  357.       override protected function measure() : void
  358.       {
  359.          super.measure();
  360.          var _loc1_:String = this.isHTML ? this.explicitHTMLText : this.text;
  361.          _loc1_ = this.mx_internal::getMinimumText(_loc1_);
  362.          var _loc2_:Rectangle = this.measureTextFieldBounds(_loc1_);
  363.          measuredMinWidth = measuredWidth = _loc2_.width + getStyle("paddingLeft") + getStyle("paddingRight");
  364.          measuredMinHeight = measuredHeight = _loc2_.height + getStyle("paddingTop") + getStyle("paddingBottom");
  365.       }
  366.       
  367.       override protected function updateDisplayList(param1:Number, param2:Number) : void
  368.       {
  369.          var _loc9_:* = false;
  370.          super.updateDisplayList(param1,param2);
  371.          var _loc3_:Number = getStyle("paddingLeft");
  372.          var _loc4_:Number = getStyle("paddingTop");
  373.          var _loc5_:Number = getStyle("paddingRight");
  374.          var _loc6_:Number = getStyle("paddingBottom");
  375.          this.textField.setActualSize(param1 - _loc3_ - _loc5_,param2 - _loc4_ - _loc6_);
  376.          this.textField.x = _loc3_;
  377.          this.textField.y = _loc4_;
  378.          var _loc7_:String = this.isHTML ? this.explicitHTMLText : this.text;
  379.          var _loc8_:Rectangle = this.measureTextFieldBounds(_loc7_);
  380.          if(this.truncateToFit)
  381.          {
  382.             if(this.isHTML)
  383.             {
  384.                _loc9_ = _loc8_.width > this.textField.width;
  385.             }
  386.             else
  387.             {
  388.                this.textField.text = this._text;
  389.                _loc9_ = this.textField.truncateToFit();
  390.             }
  391.             if(!this.toolTipSet)
  392.             {
  393.                super.toolTip = _loc9_ ? this.text : null;
  394.             }
  395.          }
  396.       }
  397.       
  398.       mx_internal function createTextField(param1:int) : void
  399.       {
  400.          if(!this.textField)
  401.          {
  402.             this.textField = IUITextField(createInFontContext(UITextField));
  403.             this.textField.enabled = enabled;
  404.             this.textField.ignorePadding = true;
  405.             this.textField.selectable = this.selectable;
  406.             this.textField.styleName = this;
  407.             this.textField.addEventListener("textFieldStyleChange",this.textField_textFieldStyleChangeHandler);
  408.             this.textField.addEventListener("textInsert",this.textField_textModifiedHandler);
  409.             this.textField.addEventListener("textReplace",this.textField_textModifiedHandler);
  410.             this.textField.addEventListener("textFieldWidthChange",this.textField_textFieldWidthChangeHandler);
  411.             if(param1 == -1)
  412.             {
  413.                addChild(DisplayObject(this.textField));
  414.             }
  415.             else
  416.             {
  417.                addChildAt(DisplayObject(this.textField),param1);
  418.             }
  419.          }
  420.       }
  421.       
  422.       mx_internal function removeTextField() : void
  423.       {
  424.          if(this.textField)
  425.          {
  426.             this.textField.removeEventListener("textFieldStyleChange",this.textField_textFieldStyleChangeHandler);
  427.             this.textField.removeEventListener("textInsert",this.textField_textModifiedHandler);
  428.             this.textField.removeEventListener("textReplace",this.textField_textModifiedHandler);
  429.             this.textField.removeEventListener("textFieldWidthChange",this.textField_textFieldWidthChangeHandler);
  430.             removeChild(DisplayObject(this.textField));
  431.             this.textField = null;
  432.          }
  433.       }
  434.       
  435.       public function getLineMetrics(param1:int) : TextLineMetrics
  436.       {
  437.          return !!this.textField ? this.textField.getLineMetrics(param1) : null;
  438.       }
  439.       
  440.       private function textFieldChanged(param1:Boolean) : void
  441.       {
  442.          var _loc2_:* = false;
  443.          var _loc3_:* = false;
  444.          if(!param1)
  445.          {
  446.             _loc2_ = this._text != this.textField.text;
  447.             this._text = this.textField.text;
  448.          }
  449.          _loc3_ = this._htmlText != this.textField.htmlText;
  450.          this._htmlText = this.textField.htmlText;
  451.          if(_loc2_)
  452.          {
  453.             dispatchEvent(new FlexEvent(FlexEvent.VALUE_COMMIT));
  454.          }
  455.          if(_loc3_)
  456.          {
  457.             dispatchEvent(new Event("htmlTextChanged"));
  458.          }
  459.          this._textWidth = this.textField.textWidth;
  460.          this._textHeight = this.textField.textHeight;
  461.       }
  462.       
  463.       private function measureTextFieldBounds(param1:String) : Rectangle
  464.       {
  465.          var _loc2_:TextLineMetrics = this.isHTML ? measureHTMLText(param1) : measureText(param1);
  466.          return new Rectangle(0,0,_loc2_.width + UITextField.mx_internal::TEXT_WIDTH_PADDING,_loc2_.height + UITextField.mx_internal::TEXT_HEIGHT_PADDING);
  467.       }
  468.       
  469.       mx_internal function getTextField() : IUITextField
  470.       {
  471.          return this.textField;
  472.       }
  473.       
  474.       mx_internal function getMinimumText(param1:String) : String
  475.       {
  476.          if(!param1 || param1.length < 2)
  477.          {
  478.             param1 = "Wj";
  479.          }
  480.          return param1;
  481.       }
  482.       
  483.       private function textField_textFieldStyleChangeHandler(param1:Event) : void
  484.       {
  485.          this.textFieldChanged(true);
  486.       }
  487.       
  488.       private function textField_textModifiedHandler(param1:Event) : void
  489.       {
  490.          this.textFieldChanged(false);
  491.       }
  492.       
  493.       private function textField_textFieldWidthChangeHandler(param1:Event) : void
  494.       {
  495.          this.textFieldChanged(true);
  496.       }
  497.    }
  498. }
  499.  
  500.