extracted the code for moving treenodes into its own class

This commit is contained in:
David Sparer
2016-05-06 19:13:00 -06:00
parent 24663a1944
commit b33f1a988c
5 changed files with 113 additions and 69 deletions

View File

@@ -340,7 +340,7 @@ namespace mRemoteNG.Connection
public void TurnOffInheritanceCompletely()
{
SetAllValues(false);
//SetAllValues(false);
}
#endregion

View File

@@ -6,6 +6,7 @@ using System;
using System.Windows.Forms;
using mRemoteNG.Messages;
using mRemoteNG.Root.PuttySessions;
using mRemoteNG.Tree.Root;
namespace mRemoteNG.Tree
{
@@ -63,7 +64,7 @@ namespace mRemoteNG.Tree
if (treeNode.Tag is PuttySessionsNodeInfo)
return TreeNodeType.PuttyRoot;
else if (treeNode.Tag is Root.RootNodeInfo)
else if (treeNode.Tag is RootNodeInfo)
return TreeNodeType.Root;
else if (treeNode.Tag is ContainerInfo)
return TreeNodeType.Container;

View File

@@ -0,0 +1,93 @@
using mRemoteNG.App;
using mRemoteNG.Connection;
using mRemoteNG.Container;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace mRemoteNG.Tree
{
public class TreeNodeMover
{
TreeNode _nodeBeingMoved;
public TreeNodeMover (TreeNode NodeBeingMoved)
{
_nodeBeingMoved = NodeBeingMoved;
}
public void MoveNode(TreeNode TargetNode)
{
if (WeAreAllowedToMoveThisNode(TargetNode))
{
RemoveNodeFromCurrentLocation();
AddNodeToNewLocation(TargetNode);
UpdateParentReferences();
SelectTheNewNode();
Runtime.SaveConnectionsBG();
}
}
private bool WeAreAllowedToMoveThisNode(TreeNode targetNode)
{
bool weShouldMoveThisNode = true;
if (_nodeBeingMoved == targetNode)
weShouldMoveThisNode = false;
if (ConnectionTreeNode.GetNodeType(_nodeBeingMoved) == TreeNodeType.Root)
weShouldMoveThisNode = false;
if (_nodeBeingMoved == targetNode.Parent)
weShouldMoveThisNode = false;
return weShouldMoveThisNode;
}
private void RemoveNodeFromCurrentLocation()
{
_nodeBeingMoved.Remove();
}
private void AddNodeToNewLocation(TreeNode targetNode)
{
//If there is no targetNode add dropNode to the bottom of
//the TreeView root nodes, otherwise add it to the end of
//the dropNode child nodes
if (ConnectionTreeNode.GetNodeType(targetNode) == TreeNodeType.Root | ConnectionTreeNode.GetNodeType(targetNode) == TreeNodeType.Container)
targetNode.Nodes.Insert(0, _nodeBeingMoved);
else
targetNode.Parent.Nodes.Insert(targetNode.Index + 1, _nodeBeingMoved);
}
private void UpdateParentReferences()
{
if (ConnectionTreeNode.GetNodeType(_nodeBeingMoved) == TreeNodeType.Connection | ConnectionTreeNode.GetNodeType(_nodeBeingMoved) == TreeNodeType.Container)
{
if (ConnectionTreeNode.GetNodeType(_nodeBeingMoved.Parent) == TreeNodeType.Container)
{
((ContainerInfo)_nodeBeingMoved.Tag).Parent = (ContainerInfo)_nodeBeingMoved.Parent.Tag;
}
else if (ConnectionTreeNode.GetNodeType(_nodeBeingMoved.Parent) == TreeNodeType.Root)
{
if (ConnectionTreeNode.GetNodeType(_nodeBeingMoved) == TreeNodeType.Connection)
{
((ConnectionInfo)_nodeBeingMoved.Tag).Parent = null;
((ConnectionInfo)_nodeBeingMoved.Tag).Inherit.TurnOffInheritanceCompletely();
}
else if (ConnectionTreeNode.GetNodeType(_nodeBeingMoved) == TreeNodeType.Container)
{
((ContainerInfo)_nodeBeingMoved.Tag).Parent = null;
((ContainerInfo)_nodeBeingMoved.Tag).ConnectionInfo.Inherit.TurnOffInheritanceCompletely();
}
}
}
}
private void SelectTheNewNode()
{
_nodeBeingMoved.EnsureVisible();
_nodeBeingMoved.TreeView.SelectedNode = _nodeBeingMoved;
}
}
}

View File

@@ -385,78 +385,27 @@ namespace mRemoteNG.UI.Window
static public void tvConnections_DragDrop(object sender, DragEventArgs e)
{
try
{
//Check that there is a TreeNode being dragged
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", true) == false)
{
return;
}
//Get the TreeView raising the event (in case multiple on form)
TreeView selectedTreeview = (TreeView) sender;
//Get the TreeNode being dragged
TreeNode dropNode = (TreeNode) (e.Data.GetData("System.Windows.Forms.TreeNode"));
//The target node should be selected from the DragOver event
TreeNode targetNode = selectedTreeview.SelectedNode;
if (dropNode == targetNode)
return;
if (ConnectionTreeNode.GetNodeType(dropNode) == TreeNodeType.Root)
return;
if (dropNode == targetNode.Parent)
return;
//Remove the drop node from its current location
dropNode.Remove();
//If there is no targetNode add dropNode to the bottom of
//the TreeView root nodes, otherwise add it to the end of
//the dropNode child nodes
if (ConnectionTreeNode.GetNodeType(targetNode) == TreeNodeType.Root | ConnectionTreeNode.GetNodeType(targetNode) == TreeNodeType.Container)
targetNode.Nodes.Insert(0, dropNode);
else
targetNode.Parent.Nodes.Insert(targetNode.Index + 1, dropNode);
if (ConnectionTreeNode.GetNodeType(dropNode) == TreeNodeType.Connection | ConnectionTreeNode.GetNodeType(dropNode) == TreeNodeType.Container)
{
if (ConnectionTreeNode.GetNodeType(dropNode.Parent) == TreeNodeType.Container)
{
((ContainerInfo)dropNode.Tag).Parent = (ContainerInfo)dropNode.Parent.Tag;
}
else if (ConnectionTreeNode.GetNodeType(dropNode.Parent) == TreeNodeType.Root)
{
if (ConnectionTreeNode.GetNodeType(dropNode) == TreeNodeType.Connection)
{
((ConnectionInfo)dropNode.Tag).Parent = null;
((ConnectionInfo)dropNode.Tag).Inherit.TurnOffInheritanceCompletely();
}
else if (ConnectionTreeNode.GetNodeType(dropNode) == TreeNodeType.Container)
{
((ContainerInfo)dropNode.Tag).Parent = null;
((ContainerInfo)dropNode.Tag).ConnectionInfo.Inherit.TurnOffInheritanceCompletely();
}
}
}
//Ensure the newly created node is visible to
//the user and select it
dropNode.EnsureVisible();
selectedTreeview.SelectedNode = dropNode;
{
//Check that there is a TreeNode being dragged
if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", true) == false)
return;
Runtime.SaveConnectionsBG();
}
catch (Exception ex)
TreeView treeviewThatSentTheEvent = (TreeView)sender;
TreeNode nodeBeingDragged = (TreeNode)(e.Data.GetData("System.Windows.Forms.TreeNode"));
TreeNode nodeBeingTargetedByDragOverEvent = treeviewThatSentTheEvent.SelectedNode;
TreeNodeMover treeNodeMover = new TreeNodeMover(nodeBeingDragged);
treeNodeMover.MoveNode(nodeBeingTargetedByDragOverEvent);
}
catch (Exception ex)
{
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, "tvConnections_DragDrop (UI.Window.Tree) failed" + Environment.NewLine + ex.Message, true);
}
}
static public void tvConnections_DragEnter(object sender, DragEventArgs e)
static public void tvConnections_DragEnter(object sender, DragEventArgs e)
{
try
{

View File

@@ -225,6 +225,7 @@
<Compile Include="Tree\ConnectionTree.cs" />
<Compile Include="Tree\NodeType.cs" />
<Compile Include="Tree\Root\RootNodeTypeEnum.cs" />
<Compile Include="Tree\TreeNodeMover.cs" />
<Compile Include="UI\Controls\ListView.cs">
<SubType>Component</SubType>
</Compile>