private void treeViewChannel_DragDrop(object sender, DragEventArgs e)
{
// Ensure that the list item index is contained in the data.
if (//this.treeNodeUnderMouseToDrop != null &&
//!this.treeViewChannel.IsSelected(this.treeNodeUnderMouseToDrop) &&
e.Data.GetDataPresent(typeof(ArrayList)))
{
// Perform drag-and-drop, depending upon the effect.
if (e.Effect == DragDropEffects.Copy || e.Effect == DragDropEffects.Move)
{
ArrayList alDraggedChannels = (ArrayList)e.Data.GetData(typeof(ArrayList));
ArrayList draggedChannelsList = new ArrayList();
Hashtable draggedChannelsMap = new Hashtable();
if (e.Effect == DragDropEffects.Copy)
{
foreach (TreeNode tn in alDraggedChannels)
{
Channel channel = (tn.Tag as Channel).MakeCopy();
draggedChannelsMap[channel] = tn;
draggedChannelsList.Add(channel);
}
}
else
{
foreach (TreeNode tn in alDraggedChannels)
{
Channel channel = tn.Tag as Channel;
draggedChannelsMap[channel] = tn;
draggedChannelsList.Add(channel);
}
}
// Compute the drop container and the drop index
ChannelFolder parentChannel;
TreeNodeCollection parentTreeNodeCollection;
int afterChannelListIndex = -1;
int afterTreeNodeIndex = -1;
if (this.treeNodeUnderMouseToDrop == null)
{
parentChannel = this.MainForm.rootChannelFolder;
parentTreeNodeCollection = this.treeViewChannel.Nodes;
afterChannelListIndex = parentChannel.ChannelList.Count;
afterTreeNodeIndex = parentTreeNodeCollection.Count;
//afterChannelListIndex = 0;
//afterTreeNodeIndex = 0;
}
else if (this.treeNodeUnderMouseToDrop.Tag is ChannelFolder)
{
parentChannel = this.treeNodeUnderMouseToDrop.Tag as ChannelFolder;
parentTreeNodeCollection = this.treeNodeUnderMouseToDrop.Nodes;
afterChannelListIndex = 0;
afterTreeNodeIndex = 0;
}
else
{
if (this.treeNodeUnderMouseToDrop.Parent != null)
{
parentChannel = this.treeNodeUnderMouseToDrop.Parent.Tag as ChannelFolder;
parentTreeNodeCollection = this.treeNodeUnderMouseToDrop.Parent.Nodes;
}
else
{
parentChannel = this.MainForm.rootChannelFolder;
parentTreeNodeCollection = this.treeViewChannel.Nodes;
}
afterChannelListIndex = parentChannel.ChannelList.IndexOf(this.treeNodeUnderMouseToDrop.Tag as Channel);
afterTreeNodeIndex = this.treeNodeUnderMouseToDrop.Index;
}
// Remove the old TreeNode
if (e.Effect == DragDropEffects.Move)
{
foreach (TreeNode tn in draggedChannelsMap.Values)
{
// Modify the drop index if some moved elements are before
// the drop target in the same level
if (this.treeNodeUnderMouseToDrop == null ||
(tn.Parent == this.treeNodeUnderMouseToDrop.Parent &&
tn.Index < this.treeNodeUnderMouseToDrop.Index))
{
afterChannelListIndex--;
afterTreeNodeIndex--;
}
// Remove the elements
if (tn.Parent != null)
{
(tn.Parent.Tag as ChannelFolder).ChannelList.Remove(tn.Tag as Channel);
tn.Parent.Nodes.Remove(tn);
}
else
{
(this.treeViewChannel.Tag as ChannelFolder).ChannelList.Remove(tn.Tag as Channel);
this.treeViewChannel.Nodes.Remove(tn);
}
}
}
if (afterChannelListIndex < 0) afterChannelListIndex = 0;
if (afterTreeNodeIndex < 0) afterTreeNodeIndex = 0;
int indexOffset = 0;
foreach (Channel channel in draggedChannelsList)
{
// First level: we add to the existing folder
if (afterChannelListIndex == -1)
parentChannel.Add(channel);
else
{
channel.Parent = parentChannel;
parentChannel.ChannelList.Insert(afterChannelListIndex + indexOffset, channel);
}
TreeNode treeNode = MakeTreeNodeFromChannel(channel);
if (afterTreeNodeIndex == -1)
parentTreeNodeCollection.Add(treeNode);
else
parentTreeNodeCollection.Insert(afterTreeNodeIndex + indexOffset, treeNode);
// Second level: we adjust the parent
if (channel is ChannelFolder)
{
ChannelFolder channelFolder = channel as ChannelFolder;
RecursedFillTree(channelFolder, treeNode.Nodes);
if (channelFolder.Expanded)
treeNode.Expand();
else
treeNode.Collapse();
}
indexOffset++;
}
(alDraggedChannels[0] as TreeNode).EnsureVisible();
}
}
}