home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / mx / controls / treeClasses / HierarchicalViewCursor.as < prev    next >
Encoding:
Text File  |  2010-06-23  |  19.0 KB  |  658 lines

  1. package mx.controls.treeClasses
  2. {
  3.    import flash.events.EventDispatcher;
  4.    import flash.utils.Dictionary;
  5.    import mx.collections.CursorBookmark;
  6.    import mx.collections.ICollectionView;
  7.    import mx.collections.IViewCursor;
  8.    import mx.core.mx_internal;
  9.    import mx.events.CollectionEvent;
  10.    import mx.events.CollectionEventKind;
  11.    
  12.    use namespace mx_internal;
  13.    
  14.    public class HierarchicalViewCursor extends EventDispatcher implements IViewCursor
  15.    {
  16.       mx_internal static const VERSION:String = "3.5.0.12683";
  17.       
  18.       private var itemToUID:Function;
  19.       
  20.       private var openNodes:Object;
  21.       
  22.       private var collection:HierarchicalCollectionView;
  23.       
  24.       private var childIndexStack:Array = [];
  25.       
  26.       private var parentNodes:Array = [];
  27.       
  28.       private var _currentDepth:int = 1;
  29.       
  30.       private var model:ICollectionView;
  31.       
  32.       private var dataDescriptor:ITreeDataDescriptor;
  33.       
  34.       private var more:Boolean;
  35.       
  36.       private var currentIndex:int = 0;
  37.       
  38.       private var modelCursor:IViewCursor;
  39.       
  40.       private var childNodes:Object = [];
  41.       
  42.       private var currentChildIndex:int = 0;
  43.       
  44.       public function HierarchicalViewCursor(param1:HierarchicalCollectionView, param2:ICollectionView, param3:ITreeDataDescriptor, param4:Function, param5:Object)
  45.       {
  46.          super();
  47.          this.collection = param1;
  48.          this.model = param2;
  49.          this.dataDescriptor = param3;
  50.          this.itemToUID = param4;
  51.          this.openNodes = param5;
  52.          param1.addEventListener(CollectionEvent.COLLECTION_CHANGE,collectionChangeHandler,false,0,true);
  53.          modelCursor = param2.createCursor();
  54.          if(param2.length > 1)
  55.          {
  56.             more = true;
  57.          }
  58.          else
  59.          {
  60.             more = false;
  61.          }
  62.       }
  63.       
  64.       private function isNodeBefore(param1:Object, param2:Object) : Boolean
  65.       {
  66.          var _loc3_:int = 0;
  67.          var _loc4_:int = 0;
  68.          var _loc5_:ICollectionView = null;
  69.          var _loc6_:Object = null;
  70.          var _loc9_:Object = null;
  71.          var _loc10_:Object = null;
  72.          var _loc11_:Object = null;
  73.          if(param2 == null)
  74.          {
  75.             return false;
  76.          }
  77.          var _loc7_:Array = getParentStack(param1);
  78.          var _loc8_:Array = getParentStack(param2);
  79.          while(Boolean(_loc7_.length) && Boolean(_loc8_.length))
  80.          {
  81.             _loc9_ = _loc7_.shift();
  82.             _loc10_ = _loc8_.shift();
  83.             if(_loc9_ != _loc10_)
  84.             {
  85.                _loc6_ = collection.getParentItem(_loc9_);
  86.                if(_loc6_ != null && dataDescriptor.isBranch(_loc6_,model) && dataDescriptor.hasChildren(_loc6_,model))
  87.                {
  88.                   _loc5_ = dataDescriptor.getChildren(_loc6_,model);
  89.                }
  90.                else
  91.                {
  92.                   _loc5_ = model;
  93.                }
  94.                _loc3_ = _loc5_.length;
  95.                _loc11_ = _loc5_[_loc4_];
  96.                if(_loc11_ == _loc10_)
  97.                {
  98.                   return false;
  99.                }
  100.                if(_loc11_ == _loc9_)
  101.                {
  102.                   return true;
  103.                }
  104.             }
  105.          }
  106.          if(_loc7_.length)
  107.          {
  108.             param1 = _loc7_.shift();
  109.          }
  110.          if(_loc8_.length)
  111.          {
  112.             param2 = _loc8_.shift();
  113.          }
  114.          _loc5_ = model;
  115.          _loc3_ = _loc5_.length;
  116.          _loc4_ = 0;
  117.          while(_loc4_ < _loc3_)
  118.          {
  119.             _loc11_ = _loc5_[_loc4_];
  120.             if(_loc11_ == param2)
  121.             {
  122.                return false;
  123.             }
  124.             if(_loc11_ == param1)
  125.             {
  126.                return true;
  127.             }
  128.             _loc4_++;
  129.          }
  130.          return false;
  131.       }
  132.       
  133.       public function findFirst(param1:Object) : Boolean
  134.       {
  135.          return findAny(param1);
  136.       }
  137.       
  138.       public function remove() : Object
  139.       {
  140.          return null;
  141.       }
  142.       
  143.       public function get afterLast() : Boolean
  144.       {
  145.          return currentIndex >= collection.length && current == null;
  146.       }
  147.       
  148.       public function collectionChangeHandler(param1:CollectionEvent) : void
  149.       {
  150.          var _loc2_:int = 0;
  151.          var _loc3_:int = 0;
  152.          var _loc4_:Object = null;
  153.          var _loc5_:Object = null;
  154.          var _loc6_:Object = null;
  155.          var _loc7_:Array = null;
  156.          var _loc8_:Dictionary = null;
  157.          var _loc10_:int = 0;
  158.          var _loc9_:Boolean = false;
  159.          _loc7_ = getParentStack(current);
  160.          _loc8_ = new Dictionary();
  161.          _loc3_ = int(_loc7_.length);
  162.          _loc2_ = 0;
  163.          while(_loc2_ < _loc3_ - 1)
  164.          {
  165.             _loc8_[_loc7_[_loc2_]] = _loc2_ + 1;
  166.             _loc2_++;
  167.          }
  168.          _loc6_ = _loc7_[_loc7_.length - 1];
  169.          if(param1.kind == CollectionEventKind.ADD)
  170.          {
  171.             _loc3_ = int(param1.items.length);
  172.             if(param1.location <= currentIndex)
  173.             {
  174.                currentIndex += _loc3_;
  175.                _loc9_ = true;
  176.             }
  177.             _loc2_ = 0;
  178.             while(_loc2_ < _loc3_)
  179.             {
  180.                _loc4_ = param1.items[_loc2_];
  181.                if(_loc9_)
  182.                {
  183.                   _loc5_ = collection.getParentItem(_loc4_);
  184.                   if(_loc5_ == _loc6_)
  185.                   {
  186.                      ++currentChildIndex;
  187.                   }
  188.                   else if(_loc8_[_loc5_] != null)
  189.                   {
  190.                      ++childIndexStack[_loc8_[_loc5_]];
  191.                   }
  192.                }
  193.                _loc2_++;
  194.             }
  195.          }
  196.          else if(param1.kind == CollectionEventKind.REMOVE)
  197.          {
  198.             _loc3_ = int(param1.items.length);
  199.             if(param1.location <= currentIndex)
  200.             {
  201.                if(param1.location + _loc3_ >= currentIndex)
  202.                {
  203.                   _loc10_ = param1.location;
  204.                   moveToFirst();
  205.                   seek(CursorBookmark.FIRST,_loc10_);
  206.                   _loc2_ = 0;
  207.                   while(_loc2_ < _loc3_)
  208.                   {
  209.                      _loc4_ = param1.items[_loc2_];
  210.                      delete collection.parentMap[itemToUID(_loc4_)];
  211.                      _loc2_++;
  212.                   }
  213.                   return;
  214.                }
  215.                currentIndex -= _loc3_;
  216.                _loc9_ = true;
  217.             }
  218.             _loc2_ = 0;
  219.             while(_loc2_ < _loc3_)
  220.             {
  221.                _loc4_ = param1.items[_loc2_];
  222.                if(_loc9_)
  223.                {
  224.                   _loc5_ = collection.getParentItem(_loc4_);
  225.                   if(_loc5_ == _loc6_)
  226.                   {
  227.                      --currentChildIndex;
  228.                   }
  229.                   else if(_loc8_[_loc5_] != null)
  230.                   {
  231.                      --childIndexStack[_loc8_[_loc5_]];
  232.                   }
  233.                }
  234.                delete collection.parentMap[itemToUID(_loc4_)];
  235.                _loc2_++;
  236.             }
  237.          }
  238.       }
  239.       
  240.       public function get view() : ICollectionView
  241.       {
  242.          return model;
  243.       }
  244.       
  245.       public function get bookmark() : CursorBookmark
  246.       {
  247.          return new CursorBookmark(currentIndex.toString());
  248.       }
  249.       
  250.       public function insert(param1:Object) : void
  251.       {
  252.       }
  253.       
  254.       private function getParentStack(param1:Object) : Array
  255.       {
  256.          var _loc2_:Array = [];
  257.          var _loc3_:Object = collection.getParentItem(param1);
  258.          while(_loc3_ != null)
  259.          {
  260.             _loc2_.unshift(_loc3_);
  261.             _loc3_ = collection.getParentItem(_loc3_);
  262.          }
  263.          return _loc2_;
  264.       }
  265.       
  266.       public function get currentDepth() : int
  267.       {
  268.          return _currentDepth;
  269.       }
  270.       
  271.       private function isItemVisible(param1:Object) : Boolean
  272.       {
  273.          var _loc2_:Object = collection.getParentItem(param1);
  274.          while(_loc2_ != null)
  275.          {
  276.             if(openNodes[itemToUID(_loc2_)] == null)
  277.             {
  278.                return false;
  279.             }
  280.             _loc2_ = collection.getParentItem(_loc2_);
  281.          }
  282.          return true;
  283.       }
  284.       
  285.       public function moveToLast() : void
  286.       {
  287.          var _loc3_:Object = null;
  288.          childNodes = [];
  289.          childIndexStack = [];
  290.          _currentDepth = 1;
  291.          parentNodes = [];
  292.          var _loc1_:Boolean = false;
  293.          modelCursor.seek(CursorBookmark.LAST,0);
  294.          var _loc2_:Object = modelCursor.current;
  295.          while(openNodes[itemToUID(_loc2_)] && dataDescriptor.isBranch(_loc2_,model) && dataDescriptor.hasChildren(_loc2_,model))
  296.          {
  297.             _loc3_ = childNodes;
  298.             childNodes = dataDescriptor.getChildren(_loc2_,model);
  299.             if(!(childNodes != null && childNodes.length > 0))
  300.             {
  301.                childNodes = _loc3_;
  302.                break;
  303.             }
  304.             parentNodes.push(_loc2_);
  305.             childIndexStack.push(currentChildIndex);
  306.             _loc2_ = childNodes[childNodes.length - 1];
  307.             currentChildIndex = childNodes.length - 1;
  308.             ++_currentDepth;
  309.          }
  310.          currentIndex = collection.length - 1;
  311.       }
  312.       
  313.       public function movePrevious() : Boolean
  314.       {
  315.          var grandParent:Object = null;
  316.          var previousChildNodes:Object = null;
  317.          var currentNode:Object = current;
  318.          if(currentNode == null)
  319.          {
  320.             return false;
  321.          }
  322.          if(Boolean(parentNodes) && parentNodes.length > 0)
  323.          {
  324.             if(currentChildIndex == 0)
  325.             {
  326.                currentNode = parentNodes.pop();
  327.                currentChildIndex = childIndexStack.pop();
  328.                grandParent = parentNodes[parentNodes.length - 1];
  329.                if(grandParent != null && dataDescriptor.isBranch(grandParent,model) && dataDescriptor.hasChildren(grandParent,model))
  330.                {
  331.                   childNodes = dataDescriptor.getChildren(grandParent,model);
  332.                }
  333.                else
  334.                {
  335.                   childNodes = [];
  336.                }
  337.                --_currentDepth;
  338.                --currentIndex;
  339.                return true;
  340.             }
  341.             try
  342.             {
  343.                currentNode = childNodes[--currentChildIndex];
  344.             }
  345.             catch(e:RangeError)
  346.             {
  347.                return false;
  348.             }
  349.          }
  350.          else
  351.          {
  352.             more = modelCursor.movePrevious();
  353.             if(!more)
  354.             {
  355.                currentIndex = -1;
  356.                currentNode = null;
  357.                return false;
  358.             }
  359.             currentNode = modelCursor.current;
  360.          }
  361.          while(openNodes[itemToUID(currentNode)] && dataDescriptor.isBranch(currentNode,model) && dataDescriptor.hasChildren(currentNode,model))
  362.          {
  363.             previousChildNodes = childNodes;
  364.             childNodes = dataDescriptor.getChildren(currentNode,model);
  365.             if(childNodes.length <= 0)
  366.             {
  367.                childNodes = previousChildNodes;
  368.                break;
  369.             }
  370.             childIndexStack.push(currentChildIndex);
  371.             parentNodes.push(currentNode);
  372.             currentChildIndex = childNodes.length - 1;
  373.             currentNode = childNodes[currentChildIndex];
  374.             ++_currentDepth;
  375.          }
  376.          --currentIndex;
  377.          return true;
  378.       }
  379.       
  380.       public function moveNext() : Boolean
  381.       {
  382.          var uid:String;
  383.          var previousChildNodes:Object = null;
  384.          var grandParent:Object = null;
  385.          var currentNode:Object = current;
  386.          if(currentNode == null)
  387.          {
  388.             return false;
  389.          }
  390.          uid = itemToUID(currentNode);
  391.          if(!collection.parentMap.hasOwnProperty(uid))
  392.          {
  393.             collection.parentMap[uid] = parentNodes[parentNodes.length - 1];
  394.          }
  395.          if(openNodes[uid] && dataDescriptor.isBranch(currentNode,model) && dataDescriptor.hasChildren(currentNode,model))
  396.          {
  397.             previousChildNodes = childNodes;
  398.             childNodes = dataDescriptor.getChildren(currentNode,model);
  399.             if(childNodes.length > 0)
  400.             {
  401.                childIndexStack.push(currentChildIndex);
  402.                parentNodes.push(currentNode);
  403.                currentChildIndex = 0;
  404.                currentNode = childNodes[0];
  405.                ++currentIndex;
  406.                ++_currentDepth;
  407.                return true;
  408.             }
  409.             childNodes = previousChildNodes;
  410.          }
  411.          while(childNodes != null && childNodes.length > 0 && currentChildIndex >= Math.max(childNodes.length - 1,0))
  412.          {
  413.             if(childIndexStack.length < 1 && !more)
  414.             {
  415.                currentNode = null;
  416.                ++currentIndex;
  417.                _currentDepth = 1;
  418.                return false;
  419.             }
  420.             currentNode = parentNodes.pop();
  421.             grandParent = parentNodes[parentNodes.length - 1];
  422.             if(grandParent != null && dataDescriptor.isBranch(grandParent,model) && dataDescriptor.hasChildren(grandParent,model))
  423.             {
  424.                childNodes = dataDescriptor.getChildren(grandParent,model);
  425.             }
  426.             else
  427.             {
  428.                childNodes = [];
  429.             }
  430.             currentChildIndex = childIndexStack.pop();
  431.             --_currentDepth;
  432.          }
  433.          if(childIndexStack.length == 0)
  434.          {
  435.             _currentDepth = 1;
  436.             more = modelCursor.moveNext();
  437.             if(!more)
  438.             {
  439.                _currentDepth = 1;
  440.                ++currentIndex;
  441.                currentNode = null;
  442.                return false;
  443.             }
  444.             currentNode = modelCursor.current;
  445.          }
  446.          else
  447.          {
  448.             try
  449.             {
  450.                currentNode = childNodes[++currentChildIndex];
  451.             }
  452.             catch(e:RangeError)
  453.             {
  454.                return false;
  455.             }
  456.          }
  457.          ++currentIndex;
  458.          return true;
  459.       }
  460.       
  461.       public function get index() : int
  462.       {
  463.          return currentIndex;
  464.       }
  465.       
  466.       public function findLast(param1:Object) : Boolean
  467.       {
  468.          var _loc3_:Object = null;
  469.          var _loc4_:Boolean = false;
  470.          var _loc5_:String = null;
  471.          seek(CursorBookmark.LAST);
  472.          var _loc2_:Boolean = false;
  473.          while(true)
  474.          {
  475.             if(_loc2_)
  476.             {
  477.                return false;
  478.             }
  479.             _loc3_ = current;
  480.             _loc4_ = true;
  481.             for(_loc5_ in param1)
  482.             {
  483.                if(_loc3_[_loc5_] != param1[_loc5_])
  484.                {
  485.                   _loc4_ = false;
  486.                   break;
  487.                }
  488.             }
  489.             if(_loc4_)
  490.             {
  491.                break;
  492.             }
  493.             _loc2_ = movePrevious();
  494.          }
  495.          return true;
  496.       }
  497.       
  498.       public function get beforeFirst() : Boolean
  499.       {
  500.          return currentIndex <= collection.length && current == null;
  501.       }
  502.       
  503.       public function get current() : Object
  504.       {
  505.          try
  506.          {
  507.             if(childIndexStack.length == 0)
  508.             {
  509.                return modelCursor.current;
  510.             }
  511.             return childNodes[currentChildIndex];
  512.          }
  513.          catch(e:RangeError)
  514.          {
  515.          }
  516.          return null;
  517.       }
  518.       
  519.       public function findAny(param1:Object) : Boolean
  520.       {
  521.          var _loc3_:Object = null;
  522.          var _loc4_:Boolean = false;
  523.          var _loc5_:String = null;
  524.          seek(CursorBookmark.FIRST);
  525.          var _loc2_:Boolean = false;
  526.          while(true)
  527.          {
  528.             if(_loc2_)
  529.             {
  530.                return false;
  531.             }
  532.             _loc3_ = dataDescriptor.getData(current);
  533.             _loc4_ = true;
  534.             for(_loc5_ in param1)
  535.             {
  536.                if(_loc3_[_loc5_] != param1[_loc5_])
  537.                {
  538.                   _loc4_ = false;
  539.                   break;
  540.                }
  541.             }
  542.             if(_loc4_)
  543.             {
  544.                break;
  545.             }
  546.             _loc2_ = moveNext();
  547.          }
  548.          return true;
  549.       }
  550.       
  551.       private function moveToFirst() : void
  552.       {
  553.          childNodes = [];
  554.          modelCursor.seek(CursorBookmark.FIRST,0);
  555.          if(model.length > 1)
  556.          {
  557.             more = true;
  558.          }
  559.          else
  560.          {
  561.             more = false;
  562.          }
  563.          currentChildIndex = 0;
  564.          parentNodes = [];
  565.          childIndexStack = [];
  566.          currentIndex = 0;
  567.          _currentDepth = 1;
  568.       }
  569.       
  570.       public function seek(param1:CursorBookmark, param2:int = 0, param3:int = 0) : void
  571.       {
  572.          var _loc4_:int = 0;
  573.          if(param1 == CursorBookmark.FIRST)
  574.          {
  575.             _loc4_ = 0;
  576.          }
  577.          else if(param1 == CursorBookmark.CURRENT)
  578.          {
  579.             _loc4_ = currentIndex;
  580.          }
  581.          else if(param1 == CursorBookmark.LAST)
  582.          {
  583.             _loc4_ = collection.length - 1;
  584.          }
  585.          else
  586.          {
  587.             _loc4_ = int(param1.value);
  588.          }
  589.          _loc4_ = Math.max(Math.min(_loc4_ + param2,collection.length),0);
  590.          var _loc5_:int = Math.abs(currentIndex - _loc4_);
  591.          var _loc6_:int = Math.abs(collection.length - _loc4_);
  592.          var _loc7_:* = true;
  593.          if(_loc5_ < _loc4_)
  594.          {
  595.             if(_loc6_ < _loc5_)
  596.             {
  597.                moveToLast();
  598.                if(_loc6_ == 0)
  599.                {
  600.                   moveNext();
  601.                   _loc4_ = 0;
  602.                }
  603.                else
  604.                {
  605.                   _loc4_ = _loc6_ - 1;
  606.                }
  607.                _loc7_ = false;
  608.             }
  609.             else
  610.             {
  611.                _loc7_ = currentIndex < _loc4_;
  612.                _loc4_ = _loc5_;
  613.                if(currentIndex == collection.length)
  614.                {
  615.                   _loc4_--;
  616.                   moveToLast();
  617.                }
  618.             }
  619.          }
  620.          else if(_loc6_ < _loc4_)
  621.          {
  622.             moveToLast();
  623.             if(_loc6_ == 0)
  624.             {
  625.                moveNext();
  626.                _loc4_ = 0;
  627.             }
  628.             else
  629.             {
  630.                _loc4_ = _loc6_ - 1;
  631.             }
  632.             _loc7_ = false;
  633.          }
  634.          else
  635.          {
  636.             moveToFirst();
  637.          }
  638.          if(_loc7_)
  639.          {
  640.             while(Boolean(_loc4_) && _loc4_ > 0)
  641.             {
  642.                moveNext();
  643.                _loc4_--;
  644.             }
  645.          }
  646.          else
  647.          {
  648.             while(Boolean(_loc4_) && _loc4_ > 0)
  649.             {
  650.                movePrevious();
  651.                _loc4_--;
  652.             }
  653.          }
  654.       }
  655.    }
  656. }
  657.  
  658.