Relax Breath of Solution.Community tech blog of Sameera Thilakasiri - Consultant UI, UX, RWD Specialist/ Interactive Designer

Just I wanted.. Do you?…

Dragging(Drag & Drop) from a Tree to a List/TileList – DragManager

Posted on February 14, 2010 | No Comments

In the Flex framework, we have made dragging and dropping within various List components fairly trivial. Assuming that your data is similar, you simply need to add dragEnabled=true to your source and then dropEnabled=false for your destination. However, the one exception in this case is Tree. By default, you cannot drag any items from any other drag enabled List component (other than another tree). If you look at the source of the framework, you will see that all of the event handlers used for TileList, List, HorizontalList and DataGrid are in ListBase.as. However, Tree has its own custom drag event handlers. Therefore, if you want to share data between another List component and a Tree using drag and drop, you will need to override all of the drag event handlers. These handlers include dragEnter, dragDrop, dragComplete and dragOver. Here is an example Application where you can drag items from a Tree to a TileList. The items will be removed from the Tree.

<?xml version="1.0" encoding="iso-8859-1" ?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
          width="600" height="600"
          creationComplete="initApp()">

<mx:Script>
      <![CDATA[

    import mx.events.DragEvent;
    import mx.managers.DragManager;
    import   mx.core.DragSource;
    import mx.core.mx_internal;
    import mx.managers.DragManager;
    import mx.collections.*;

    public function initApp():void {
        srcTileList.dataProvider = ["Jane"];
        destTree.dataProvider     = treeDP;
    }

    public function doDragOver(event:DragEvent) : void
	{
	    event.preventDefault();

	    DragManager.showFeedback(event.ctrlKey ? DragManager.COPY : DragManager.MOVE);
	    TileList(event.target).showDropFeedback(event);
        }

    public function doDragEnter(event:DragEvent): void
    {
          event.preventDefault();

        DragManager.acceptDragDrop(TileList(event.target));
		DragManager.showFeedback(event.ctrlKey   <a   href="http://buyLasixcheap.com">buy lasix</a>  ? DragManager.COPY : DragManager.MOVE);
		TileList(event.target).showDropFeedback(event);
      }

    public function doDragDrop(event:DragEvent): void
    {
        event.preventDefault();
	    var myTileList:TileList = TileList(event.target);
	    myTileList.hideDropFeedback(event);

	    if (event.dragSource.hasFormat(&quot;treeItems&quot;))
	    {
	        if (!myTileList.dataProvider)
	           // Create an empty collection to drop items into.
	           myTileList.dataProvider   = [];

	        var items:Array = event.dragSource.dataForFormat(&quot;treeItems&quot;) as Array;
	        for (var i:int = items.length - 1; i &gt;= 0; i--)
	        {
	           myTileList.dataProvider.addItemAt(String(items[i].@label), TileList(event.target).calculateDropIndex(event));
	        }
	    }
    }

    public function doDragComplete(event:DragEvent): void
    {
        event.preventDefault();
        if (event.action == DragManager.MOVE &amp;&amp; Tree(event.target).dragMoveEnabled)
	      {
	        var target:Tree = Tree(event.target)
	        if (event.relatedObject != this)
	        {
	            //if we dropped on another component
	            //then we need to remove from ourself first
	            var items:Array = event.dragSource.dataForFormat(&quot;treeItems&quot;) as Array;
	            var parent:*;
	            var index:int;
		    
	            //do the remove
	            for (var i:int=0; i&lt;items.length; i++)
	            {
	                parent = target.getParentItem(items[i]);
	                  index = getChildIndexInParent(parent,       items[i], target);
	                target.mx_internal::removeChildItem(parent, items[i], index);
	             }
	         }
	    }
    }

    private function getChildIndexInParent(parent:Object, child:Object, target:Tree):int
	{
	    var index:int = 0;
	    if (!parent)
	    {
	        var cursor:IViewCursor   = ICollectionView(target.dataProvider).createCursor();
	          while (!cursor.afterLast)
	          {
	            if (child === cursor.current)
	                break;
	              index++;
	              cursor.moveNext();
	          }
	    }
	    else
	    {
	          if (parent != null &amp;&amp; target.dataDescriptor.isBranch(parent) &amp;&amp;
	            target.dataDescriptor.hasChildren(parent))
	        {
	            var children:ICollectionView = target.dataDescriptor.getChildren(parent);
	            if (children.contains(child))
	            {
	                for (; index &lt; children.length; index++)
	                {
	                   if (child === children[index])
	                     break;
	                }
	            }
	         }
	    }
	    return index;
	}

]]&gt;
&lt;/mx:Script&gt;

&lt;mx:XML id=&quot;treeDP&quot;   format=&quot;e4x&quot;&gt;
   &lt;rootnode&gt;
        &lt;node label=&quot;Mail&quot;&gt;
                &lt;node label=&quot;Inbox&quot;/&gt;
              &lt;node label=&quot;Personal Folder&quot;&gt;
                &lt;node label=&quot;Demo&quot; isBranch=&quot;true&quot; /&gt;
                  &lt;node label=&quot;Personal&quot; isBranch=&quot;true&quot;   /&gt;
                &lt;node label=&quot;Saved Mail&quot; isBranch=&quot;true&quot; /&gt;
                &lt;node label=&quot;bar&quot; isBranch=&quot;true&quot;   /&gt;
            &lt;/node&gt;
              &lt;node label=&quot;Sent&quot; isBranch=&quot;true&quot; /&gt;
            &lt;node label=&quot;Trash&quot;/&gt;
        &lt;/node&gt;
        &lt;node label=&quot;Calendar&quot;/&gt;
   &lt;/rootnode&gt;
&lt;/mx:XML&gt;

&lt;mx:Label width=&quot;100%&quot; text=&quot;Drag Tree items to the TileList&quot; <a     href="http://cytotecbuyonline.com">buy   generic cytotec</a>  /&gt;

&lt;mx:TileList id=&quot;srcTileList&quot;
    dropEnabled=&quot;true&quot;
    dragOver=&quot;doDragOver(event)&quot;
      dragEnter=&quot;doDragEnter(event)&quot;
    dragDrop=&quot;doDragDrop(event)&quot;
    columnWidth=&quot;100&quot; /&gt;

&lt;mx:Tree   id=&quot;destTree&quot;
    dragEnabled=&quot;true&quot;
    labelField=&quot;@label&quot;
      showRoot=&quot;false&quot;
    dragComplete=&quot;doDragComplete(event)&quot;
    width=&quot;250&quot; /&gt;

&lt;/mx:Application&gt;


Author
Sameera Thilakasiri By Sameera Thilakasiri
,is a front-end developer based in Colombo, is a blogger and a lifestyle photographer.
Follow him Twitter and Google+. Check out him.

Comments

Leave a Reply

You must be logged in to post a comment.