//moveTree.mxml文件
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="15"
xmlns:comp="com.components.moveTree.*" xmlns:component="com.component.*">
<mx:Script>
<![CDATA[
import mx.events.DragEvent;
import mx.managers.DragManager;
import mx.core.DragSource;
import mx.events.DragEvent;
private function dropDrag(event:DragEvent):void {
tree.hideDropFeedback(event); //取消默认画线
}
]]>
</mx:Script>
<mx:XMLListCollection id="xmlColl" source="{xmlList}"/>
<mx:XMLList id="xmlList" xmlns="">
<rootdata name="北京XX总局" position="教授" isBranch="true">
<subdata name="大队1" isBranch="true" >
<data name="中队1-1"/>
<data name="中队1-2"/>
<data name="中队1-3"/>
</subdata>
<subdata name="大队2" isBranch="true" >
<data name="中队2-1"/>
</subdata>
</rootdata>
<rootdata name="信息分类" isBranch="true">
<subdata id="01" name="Flash" isBranch="true">
<data id="0101" name="Flash1" />
<data id="0102" name="Flash2" />
</subdata>
<subdata id="01" name="相册" isBranch="true">
<data id="0104" name="相册1" />
<data id="0104" name="相册2" />
</subdata>
<subdata id="02" name="服务" isBranch="true">
<data id="0201" name="GIS服务" />
<data id="0202" name="arcgis服务" />
</subdata>
</rootdata>
</mx:XMLList>
<mx:HBox horizontalAlign="center" verticalAlign="middle" paddingLeft="15" paddingRight="15" dropShadowVisible="true" height="90%" width="100%">
<mx:HBox width="100%" height="100%" horizontalAlign="left" verticalAlign="middle">
<mx:Panel width="250" height="100%" verticalAlign="middle" horizontalAlign="center" title="结构" chromeColor="#020202" color="#FFFFFF">
<component:moveTree
id="tree" labelField="@name" width="100%"
disclosureOpenIcon="@Embed(source='img/disclosure-open.png')"
disclosureClosedIcon="@Embed(source='img/disclosure-closed.png')"
height="100%" dragEnabled="true" dropEnabled="true"
dragMoveEnabled="true" dragDrop="dropDrag(event);" dataProvider="{xmlColl}" dropShadowVisible="true" borderVisible="false" color="#010101"/>
</mx:Panel>
</mx:HBox>
</mx:HBox>
</mx:Application>
//moveTree.as
package com.component
{
import mx.collections.ICollectionView;
import mx.collections.IViewCursor;
import mx.controls.Tree;
import mx.core.mx_internal;
import mx.events.DragEvent;
import mx.managers.DragManager;
use namespace mx_internal; //命名空间
public class moveTree extends Tree {
public function moveTree() {
super();
}
/**
* @private
* Returns the stack of parents from a child item.
*/
private function getParentStack(item:Object):Array {
var stack:Array = [];
if (item == null)
return stack;
var parent:* = getParentItem(item);
while (parent) {
stack.push(parent);
parent = getParentItem(parent);
}
return stack;
}
override protected function dragDropHandler(event:DragEvent):void {
if (event.dragSource.hasFormat("treeItems")) {
var items:Array = event.dragSource.dataForFormat("treeItems") as Array;
var i:int;
var n:int;
if (event.action == DragManager.MOVE && dragMoveEnabled) {
if (event.dragInitiator == this) {
calculateDropIndex(event);
var index:int;
var parent:*;
var parentItem:*;
var dropIndex:int = _dropData.index;
// 获得 拖动目标 项的父节点 。
var dropParentStack:Array = getParentStack(_dropData.parent);
dropParentStack.unshift(_dropData.parent); //将父节点添加到数组的开头
n = items.length;
for (i = 0;i < n;i++) {
parent = getParentItem(items[i]); //获得父节点
index = getChildIndexInParent(parent,items[i]); //获得父节点的索引号
var item:* = items[i];
var itemName:String = item.localName().toString(); //
var targetParent:*;
if(itemName == "rootdata"){
removeChildItem(parent,items[i],index);
if (parent == _dropData.parent && index < _dropData.index)
dropIndex--;
addChildItem(targetParent,items[i],dropIndex);
}
for each (parentItem in dropParentStack) {
// 不能把节点放到自身的子节点下面
if (items[i] === parentItem)
return;
if (parentItem) {
var targetParentName:String = parentItem.localName().toString();
if (itemName == "data" && targetParentName == "subdata") {
targetParent = parentItem;
break;
}
if (itemName == "subdata" && targetParentName == "rootdata") {
targetParent = parentItem;
break;
}
}
}
if (targetParent) {
removeChildItem(parent,items[i],index);
if (parent == _dropData.parent && index < _dropData.index)
dropIndex--;
addChildItem(targetParent,items[i],dropIndex);
}
}
return;
}
}
lastDragEvent = null;
}
}
/**
* @private
* Finds the index distance between a parent and child
*/
private function getChildIndexInParent(parent:Object,child:Object):int {
var index:int = 0;
if (!parent) {
var cursor:IViewCursor = ICollectionView(iterator.view).createCursor();
while (!cursor.afterLast) {
if (child === cursor.current)
break;
index++;
cursor.moveNext();
}
} else {
if (parent != null && _dataDescriptor.isBranch(parent,iterator.view) && _dataDescriptor.hasChildren(parent,
iterator.view)) {
var children:ICollectionView = getChildren(parent,iterator.view);
if (children.contains(child)) {
cursor = children.createCursor();
while (!cursor.afterLast) {
if (child === cursor.current)
break;
cursor.moveNext();
index++;
}
} else {
}
}
}
return index;
}
}
}