home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Puzzle / filler.swf / scripts / mx / managers / ToolTipManagerImpl.as < prev   
Encoding:
Text File  |  2008-09-02  |  19.9 KB  |  669 lines

  1. package mx.managers
  2. {
  3.    import flash.display.DisplayObject;
  4.    import flash.events.Event;
  5.    import flash.events.EventDispatcher;
  6.    import flash.events.MouseEvent;
  7.    import flash.events.TimerEvent;
  8.    import flash.geom.Point;
  9.    import flash.geom.Rectangle;
  10.    import flash.utils.Timer;
  11.    import mx.controls.ToolTip;
  12.    import mx.core.Application;
  13.    import mx.core.IInvalidating;
  14.    import mx.core.IToolTip;
  15.    import mx.core.IUIComponent;
  16.    import mx.core.mx_internal;
  17.    import mx.effects.Effect;
  18.    import mx.effects.EffectManager;
  19.    import mx.events.EffectEvent;
  20.    import mx.events.ToolTipEvent;
  21.    import mx.styles.IStyleClient;
  22.    import mx.validators.IValidatorListener;
  23.    
  24.    use namespace mx_internal;
  25.    
  26.    public class ToolTipManagerImpl extends EventDispatcher implements IToolTipManager
  27.    {
  28.       
  29.       mx_internal static const VERSION:String = "2.0.1.0";
  30.       
  31.       private static var instance:IToolTipManager;
  32.        
  33.       
  34.       private var _enabled:Boolean = true;
  35.       
  36.       private var _showDelay:Number = 500;
  37.       
  38.       private var _hideEffect:Effect;
  39.       
  40.       private var hideTimer:Timer;
  41.       
  42.       private var _toolTipClass:Class;
  43.       
  44.       private var _scrubDelay:Number = 100;
  45.       
  46.       private var showTimer:Timer;
  47.       
  48.       private var _currentToolTip:IToolTip;
  49.       
  50.       private var currentText:String;
  51.       
  52.       private var scrubTimer:Timer;
  53.       
  54.       private var previousTarget:DisplayObject;
  55.       
  56.       private var _currentTarget:DisplayObject;
  57.       
  58.       private var _showEffect:Effect;
  59.       
  60.       private var initialized:Boolean = false;
  61.       
  62.       private var _hideDelay:Number = 10000;
  63.       
  64.       private var isError:Boolean;
  65.       
  66.       public function ToolTipManagerImpl()
  67.       {
  68.          initialized = false;
  69.          _enabled = true;
  70.          _hideDelay = 10000;
  71.          _scrubDelay = 100;
  72.          _showDelay = 500;
  73.          _toolTipClass = ToolTip;
  74.          super();
  75.          if(instance)
  76.          {
  77.             throw new Error("Instance already exists.");
  78.          }
  79.       }
  80.       
  81.       public static function getInstance() : IToolTipManager
  82.       {
  83.          if(!instance)
  84.          {
  85.             instance = new ToolTipManagerImpl();
  86.          }
  87.          return instance;
  88.       }
  89.       
  90.       private function toolTipMouseOverHandler(param1:MouseEvent) : void
  91.       {
  92.          checkIfTargetChanged(DisplayObject(param1.target));
  93.       }
  94.       
  95.       private function showTimer_timerHandler(param1:TimerEvent) : void
  96.       {
  97.          if(currentTarget)
  98.          {
  99.             createTip();
  100.             initializeTip();
  101.             positionTip();
  102.             showTip();
  103.          }
  104.       }
  105.       
  106.       private function systemManager_mouseDownHandler(param1:MouseEvent) : void
  107.       {
  108.          reset();
  109.       }
  110.       
  111.       public function set showDelay(param1:Number) : void
  112.       {
  113.          _showDelay = param1;
  114.       }
  115.       
  116.       private function hideEffectEnded() : void
  117.       {
  118.          var _loc1_:ToolTipEvent = null;
  119.          reset();
  120.          if(previousTarget)
  121.          {
  122.             _loc1_ = new ToolTipEvent(ToolTipEvent.TOOL_TIP_END);
  123.             _loc1_.toolTip = currentToolTip;
  124.             previousTarget.dispatchEvent(_loc1_);
  125.          }
  126.       }
  127.       
  128.       public function set scrubDelay(param1:Number) : void
  129.       {
  130.          _scrubDelay = param1;
  131.       }
  132.       
  133.       public function get currentToolTip() : IToolTip
  134.       {
  135.          return _currentToolTip;
  136.       }
  137.       
  138.       private function toolTipMouseOutHandler(param1:MouseEvent) : void
  139.       {
  140.          checkIfTargetChanged(param1.relatedObject);
  141.       }
  142.       
  143.       public function get enabled() : Boolean
  144.       {
  145.          return _enabled;
  146.       }
  147.       
  148.       public function createToolTip(param1:String, param2:Number, param3:Number, param4:String = null, param5:IUIComponent = null) : IToolTip
  149.       {
  150.          var _loc6_:ToolTip = null;
  151.          var _loc7_:ISystemManager = null;
  152.          _loc6_ = new ToolTip();
  153.          (_loc7_ = !!param5 ? param5.systemManager : Application.application.systemManager).toolTipChildren.addChild(_loc6_);
  154.          if(param4)
  155.          {
  156.             _loc6_.setStyle("styleName","errorTip");
  157.             _loc6_.setStyle("borderStyle",param4);
  158.          }
  159.          _loc6_.text = param1;
  160.          sizeTip(_loc6_);
  161.          _loc6_.move(param2,param3);
  162.          return _loc6_ as IToolTip;
  163.       }
  164.       
  165.       private function reset() : void
  166.       {
  167.          var _loc1_:ISystemManager = null;
  168.          showTimer.reset();
  169.          hideTimer.reset();
  170.          if(currentToolTip)
  171.          {
  172.             if(Boolean(showEffect) || Boolean(hideEffect))
  173.             {
  174.                currentToolTip.removeEventListener(EffectEvent.EFFECT_END,effectEndHandler);
  175.             }
  176.             EffectManager.endEffectsForTarget(currentToolTip);
  177.             _loc1_ = getSystemManager(previousTarget);
  178.             _loc1_.toolTipChildren.removeChild(DisplayObject(currentToolTip));
  179.             currentToolTip = null;
  180.             scrubTimer.delay = scrubDelay;
  181.             scrubTimer.reset();
  182.             if(scrubDelay > 0)
  183.             {
  184.                scrubTimer.delay = scrubDelay;
  185.                scrubTimer.start();
  186.             }
  187.          }
  188.       }
  189.       
  190.       public function set currentToolTip(param1:IToolTip) : void
  191.       {
  192.          _currentToolTip = param1;
  193.       }
  194.       
  195.       private function showTip() : void
  196.       {
  197.          var _loc1_:ToolTipEvent = null;
  198.          var _loc2_:ISystemManager = null;
  199.          _loc1_ = new ToolTipEvent(ToolTipEvent.TOOL_TIP_SHOW);
  200.          _loc1_.toolTip = currentToolTip;
  201.          currentTarget.dispatchEvent(_loc1_);
  202.          if(isError)
  203.          {
  204.             currentTarget.addEventListener("change",changeHandler);
  205.          }
  206.          else
  207.          {
  208.             _loc2_ = getSystemManager(currentTarget);
  209.             _loc2_.addEventListener(MouseEvent.MOUSE_DOWN,systemManager_mouseDownHandler);
  210.          }
  211.          currentToolTip.visible = true;
  212.          if(!showEffect)
  213.          {
  214.             showEffectEnded();
  215.          }
  216.       }
  217.       
  218.       private function effectEndHandler(param1:EffectEvent) : void
  219.       {
  220.          if(param1.effectInstance.effect == showEffect)
  221.          {
  222.             showEffectEnded();
  223.          }
  224.          else if(param1.effectInstance.effect == hideEffect)
  225.          {
  226.             hideEffectEnded();
  227.          }
  228.       }
  229.       
  230.       public function get hideDelay() : Number
  231.       {
  232.          return _hideDelay;
  233.       }
  234.       
  235.       public function get toolTipClass() : Class
  236.       {
  237.          return _toolTipClass;
  238.       }
  239.       
  240.       public function get currentTarget() : DisplayObject
  241.       {
  242.          return _currentTarget;
  243.       }
  244.       
  245.       public function get hideEffect() : Effect
  246.       {
  247.          return _hideEffect;
  248.       }
  249.       
  250.       private function showEffectEnded() : void
  251.       {
  252.          var _loc1_:ToolTipEvent = null;
  253.          if(hideDelay == 0)
  254.          {
  255.             hideTip();
  256.          }
  257.          else if(hideDelay < Infinity)
  258.          {
  259.             hideTimer.delay = hideDelay;
  260.             hideTimer.start();
  261.          }
  262.          if(currentTarget)
  263.          {
  264.             _loc1_ = new ToolTipEvent(ToolTipEvent.TOOL_TIP_SHOWN);
  265.             _loc1_.toolTip = currentToolTip;
  266.             currentTarget.dispatchEvent(_loc1_);
  267.          }
  268.       }
  269.       
  270.       public function set enabled(param1:Boolean) : void
  271.       {
  272.          _enabled = param1;
  273.       }
  274.       
  275.       private function errorTipMouseOverHandler(param1:MouseEvent) : void
  276.       {
  277.          checkIfTargetChanged(DisplayObject(param1.target));
  278.       }
  279.       
  280.       public function get scrubDelay() : Number
  281.       {
  282.          return _scrubDelay;
  283.       }
  284.       
  285.       public function get showDelay() : Number
  286.       {
  287.          return _showDelay;
  288.       }
  289.       
  290.       public function registerErrorString(param1:DisplayObject, param2:String) : void
  291.       {
  292.          var _loc3_:String = null;
  293.          _loc3_ = IValidatorListener(param1).errorString;
  294.          if(!_loc3_ && Boolean(param2))
  295.          {
  296.             param1.addEventListener(MouseEvent.MOUSE_OVER,errorTipMouseOverHandler);
  297.             param1.addEventListener(MouseEvent.MOUSE_OUT,errorTipMouseOutHandler);
  298.          }
  299.          else if(Boolean(_loc3_) && !param2)
  300.          {
  301.             param1.removeEventListener(MouseEvent.MOUSE_OVER,errorTipMouseOverHandler);
  302.             param1.removeEventListener(MouseEvent.MOUSE_OUT,errorTipMouseOutHandler);
  303.          }
  304.       }
  305.       
  306.       private function initialize() : void
  307.       {
  308.          if(!showTimer)
  309.          {
  310.             showTimer = new Timer(0,1);
  311.             showTimer.addEventListener(TimerEvent.TIMER,showTimer_timerHandler);
  312.          }
  313.          if(!hideTimer)
  314.          {
  315.             hideTimer = new Timer(0,1);
  316.             hideTimer.addEventListener(TimerEvent.TIMER,hideTimer_timerHandler);
  317.          }
  318.          if(!scrubTimer)
  319.          {
  320.             scrubTimer = new Timer(0,1);
  321.          }
  322.          initialized = true;
  323.       }
  324.       
  325.       public function destroyToolTip(param1:IToolTip) : void
  326.       {
  327.          var _loc2_:ISystemManager = null;
  328.          _loc2_ = param1.systemManager;
  329.          _loc2_.toolTipChildren.removeChild(DisplayObject(param1));
  330.       }
  331.       
  332.       private function checkIfTargetChanged(param1:DisplayObject) : void
  333.       {
  334.          if(!enabled)
  335.          {
  336.             return;
  337.          }
  338.          findTarget(param1);
  339.          if(currentTarget != previousTarget)
  340.          {
  341.             targetChanged();
  342.             previousTarget = currentTarget;
  343.          }
  344.       }
  345.       
  346.       private function changeHandler(param1:Event) : void
  347.       {
  348.          reset();
  349.       }
  350.       
  351.       public function set toolTipClass(param1:Class) : void
  352.       {
  353.          _toolTipClass = param1;
  354.       }
  355.       
  356.       private function getGlobalBounds(param1:DisplayObject) : Rectangle
  357.       {
  358.          var _loc2_:Point = null;
  359.          _loc2_ = new Point(0,0);
  360.          _loc2_ = param1.localToGlobal(_loc2_);
  361.          return new Rectangle(_loc2_.x,_loc2_.y,param1.width,param1.height);
  362.       }
  363.       
  364.       private function positionTip() : void
  365.       {
  366.          var _loc1_:Number = NaN;
  367.          var _loc2_:Number = NaN;
  368.          var _loc3_:Number = NaN;
  369.          var _loc4_:Number = NaN;
  370.          var _loc5_:Rectangle = null;
  371.          var _loc6_:Point = null;
  372.          var _loc7_:IToolTip = null;
  373.          var _loc8_:Number = NaN;
  374.          var _loc9_:Number = NaN;
  375.          var _loc10_:Number = NaN;
  376.          var _loc11_:Number = NaN;
  377.          _loc3_ = currentToolTip.screen.width;
  378.          _loc4_ = currentToolTip.screen.height;
  379.          if(isError)
  380.          {
  381.             _loc1_ = (_loc5_ = getGlobalBounds(currentTarget)).right + 4;
  382.             _loc2_ = _loc5_.top - 1;
  383.             if(_loc1_ + currentToolTip.width > _loc3_)
  384.             {
  385.                _loc8_ = NaN;
  386.                _loc9_ = NaN;
  387.                _loc1_ = _loc5_.left - 2;
  388.                if(_loc1_ + currentToolTip.width + 4 > _loc3_)
  389.                {
  390.                   _loc8_ = _loc3_ - _loc1_ - 4;
  391.                   _loc9_ = Number(Object(toolTipClass).maxWidth);
  392.                   Object(toolTipClass).maxWidth = _loc8_;
  393.                   if(currentToolTip is IStyleClient)
  394.                   {
  395.                      IStyleClient(currentToolTip).setStyle("borderStyle","errorTipAbove");
  396.                   }
  397.                   currentToolTip["text"] = currentToolTip["text"];
  398.                   Object(toolTipClass).maxWidth = _loc9_;
  399.                }
  400.                else
  401.                {
  402.                   if(currentToolTip is IStyleClient)
  403.                   {
  404.                      IStyleClient(currentToolTip).setStyle("borderStyle","errorTipAbove");
  405.                   }
  406.                   currentToolTip["text"] = currentToolTip["text"];
  407.                }
  408.                if(currentToolTip.height + 2 < _loc5_.top)
  409.                {
  410.                   _loc2_ = _loc5_.top - (currentToolTip.height + 2);
  411.                }
  412.                else
  413.                {
  414.                   _loc2_ = _loc5_.bottom + 2;
  415.                   if(!isNaN(_loc8_))
  416.                   {
  417.                      Object(toolTipClass).maxWidth = _loc8_;
  418.                   }
  419.                   if(currentToolTip is IStyleClient)
  420.                   {
  421.                      IStyleClient(currentToolTip).setStyle("borderStyle","errorTipBelow");
  422.                   }
  423.                   currentToolTip["text"] = currentToolTip["text"];
  424.                   if(!isNaN(_loc9_))
  425.                   {
  426.                      Object(toolTipClass).maxWidth = _loc9_;
  427.                   }
  428.                }
  429.             }
  430.             sizeTip(currentToolTip);
  431.             _loc6_ = new Point(_loc1_,_loc2_);
  432.             _loc7_ = currentToolTip;
  433.             _loc1_ = (_loc6_ = DisplayObject(_loc7_).root.globalToLocal(_loc6_)).x;
  434.             _loc2_ = _loc6_.y;
  435.          }
  436.          else
  437.          {
  438.             _loc1_ = Application.application.mouseX + 11;
  439.             _loc2_ = Application.application.mouseY + 22;
  440.             _loc10_ = Number(currentToolTip.width);
  441.             if(_loc1_ + _loc10_ > _loc3_)
  442.             {
  443.                _loc1_ = _loc3_ - _loc10_;
  444.             }
  445.             _loc11_ = Number(currentToolTip.height);
  446.             if(_loc2_ + _loc11_ > _loc4_)
  447.             {
  448.                _loc2_ = _loc4_ - _loc11_;
  449.             }
  450.          }
  451.          currentToolTip.move(_loc1_,_loc2_);
  452.       }
  453.       
  454.       private function errorTipMouseOutHandler(param1:MouseEvent) : void
  455.       {
  456.          checkIfTargetChanged(param1.relatedObject);
  457.       }
  458.       
  459.       private function findTarget(param1:DisplayObject) : void
  460.       {
  461.          while(param1)
  462.          {
  463.             if(param1 is IValidatorListener)
  464.             {
  465.                currentText = IValidatorListener(param1).errorString;
  466.                if(currentText != null && currentText != "")
  467.                {
  468.                   currentTarget = param1;
  469.                   isError = true;
  470.                   return;
  471.                }
  472.             }
  473.             if(param1 is IToolTipManagerClient)
  474.             {
  475.                currentText = IToolTipManagerClient(param1).toolTip;
  476.                if(currentText != null)
  477.                {
  478.                   currentTarget = param1;
  479.                   isError = false;
  480.                   return;
  481.                }
  482.             }
  483.             param1 = param1.parent;
  484.          }
  485.          currentText = null;
  486.          currentTarget = null;
  487.       }
  488.       
  489.       public function registerToolTip(param1:DisplayObject, param2:String) : void
  490.       {
  491.          var _loc3_:String = null;
  492.          if(param1 is IToolTipManagerClient)
  493.          {
  494.             _loc3_ = IToolTipManagerClient(param1).toolTip;
  495.          }
  496.          if(!_loc3_ && Boolean(param2))
  497.          {
  498.             param1.addEventListener(MouseEvent.MOUSE_OVER,toolTipMouseOverHandler);
  499.             param1.addEventListener(MouseEvent.MOUSE_OUT,toolTipMouseOutHandler);
  500.          }
  501.          else if(Boolean(_loc3_) && !param2)
  502.          {
  503.             param1.removeEventListener(MouseEvent.MOUSE_OVER,toolTipMouseOverHandler);
  504.             param1.removeEventListener(MouseEvent.MOUSE_OUT,toolTipMouseOutHandler);
  505.          }
  506.       }
  507.       
  508.       public function set hideDelay(param1:Number) : void
  509.       {
  510.          _hideDelay = param1;
  511.       }
  512.       
  513.       private function getSystemManager(param1:DisplayObject) : ISystemManager
  514.       {
  515.          return param1 is IUIComponent ? IUIComponent(param1).systemManager : null;
  516.       }
  517.       
  518.       public function set currentTarget(param1:DisplayObject) : void
  519.       {
  520.          _currentTarget = param1;
  521.       }
  522.       
  523.       public function set showEffect(param1:Effect) : void
  524.       {
  525.          _showEffect = param1;
  526.       }
  527.       
  528.       public function sizeTip(param1:IToolTip) : void
  529.       {
  530.          if(param1 is IInvalidating)
  531.          {
  532.             IInvalidating(param1).validateNow();
  533.          }
  534.          param1.setActualSize(param1.getExplicitOrMeasuredWidth(),param1.getExplicitOrMeasuredHeight());
  535.       }
  536.       
  537.       public function set hideEffect(param1:Effect) : void
  538.       {
  539.          _hideEffect = param1;
  540.       }
  541.       
  542.       private function hideTimer_timerHandler(param1:TimerEvent) : void
  543.       {
  544.          hideTip();
  545.       }
  546.       
  547.       private function targetChanged() : void
  548.       {
  549.          var _loc1_:ToolTipEvent = null;
  550.          if(!initialized)
  551.          {
  552.             initialize();
  553.          }
  554.          if(Boolean(previousTarget) && Boolean(currentToolTip))
  555.          {
  556.             _loc1_ = new ToolTipEvent(ToolTipEvent.TOOL_TIP_HIDE);
  557.             _loc1_.toolTip = currentToolTip;
  558.             previousTarget.dispatchEvent(_loc1_);
  559.          }
  560.          reset();
  561.          if(currentTarget)
  562.          {
  563.             if(currentText == "")
  564.             {
  565.                return;
  566.             }
  567.             _loc1_ = new ToolTipEvent(ToolTipEvent.TOOL_TIP_START);
  568.             currentTarget.dispatchEvent(_loc1_);
  569.             if(showDelay == 0 || scrubTimer.running)
  570.             {
  571.                createTip();
  572.                initializeTip();
  573.                positionTip();
  574.                showTip();
  575.             }
  576.             else
  577.             {
  578.                showTimer.delay = showDelay;
  579.                showTimer.start();
  580.             }
  581.          }
  582.       }
  583.       
  584.       private function initializeTip() : void
  585.       {
  586.          if(currentToolTip is ToolTip)
  587.          {
  588.             ToolTip(currentToolTip).text = currentText;
  589.          }
  590.          if(isError && currentToolTip is IStyleClient)
  591.          {
  592.             IStyleClient(currentToolTip).setStyle("styleName","errorTip");
  593.          }
  594.          sizeTip(currentToolTip);
  595.          if(currentToolTip is IStyleClient)
  596.          {
  597.             if(showEffect)
  598.             {
  599.                IStyleClient(currentToolTip).setStyle("showEffect",showEffect);
  600.             }
  601.             if(hideEffect)
  602.             {
  603.                IStyleClient(currentToolTip).setStyle("hideEffect",hideEffect);
  604.             }
  605.          }
  606.          if(Boolean(showEffect) || Boolean(hideEffect))
  607.          {
  608.             currentToolTip.addEventListener(EffectEvent.EFFECT_END,effectEndHandler);
  609.          }
  610.       }
  611.       
  612.       public function get showEffect() : Effect
  613.       {
  614.          return _showEffect;
  615.       }
  616.       
  617.       private function hideTip() : void
  618.       {
  619.          var _loc1_:ToolTipEvent = null;
  620.          var _loc2_:ISystemManager = null;
  621.          if(previousTarget)
  622.          {
  623.             _loc1_ = new ToolTipEvent(ToolTipEvent.TOOL_TIP_HIDE);
  624.             _loc1_.toolTip = currentToolTip;
  625.             previousTarget.dispatchEvent(_loc1_);
  626.          }
  627.          if(currentToolTip)
  628.          {
  629.             currentToolTip.visible = false;
  630.          }
  631.          if(isError)
  632.          {
  633.             if(currentTarget)
  634.             {
  635.                currentTarget.removeEventListener("change",changeHandler);
  636.             }
  637.          }
  638.          else if(previousTarget)
  639.          {
  640.             _loc2_ = getSystemManager(previousTarget);
  641.             _loc2_.removeEventListener(MouseEvent.MOUSE_DOWN,systemManager_mouseDownHandler);
  642.          }
  643.          if(!hideEffect)
  644.          {
  645.             hideEffectEnded();
  646.          }
  647.       }
  648.       
  649.       private function createTip() : void
  650.       {
  651.          var _loc1_:ToolTipEvent = null;
  652.          var _loc2_:ISystemManager = null;
  653.          _loc1_ = new ToolTipEvent(ToolTipEvent.TOOL_TIP_CREATE);
  654.          currentTarget.dispatchEvent(_loc1_);
  655.          if(_loc1_.toolTip)
  656.          {
  657.             currentToolTip = _loc1_.toolTip;
  658.          }
  659.          else
  660.          {
  661.             currentToolTip = new toolTipClass();
  662.          }
  663.          currentToolTip.visible = false;
  664.          _loc2_ = getSystemManager(currentTarget);
  665.          _loc2_.toolTipChildren.addChild(DisplayObject(currentToolTip));
  666.       }
  667.    }
  668. }
  669.