mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-17 22:11:48 +08:00
Refactored drag-n-drop code to its own class
This commit is contained in:
48
mRemoteV1/Tree/ConnectionTreeDragAndDropHandler.cs
Normal file
48
mRemoteV1/Tree/ConnectionTreeDragAndDropHandler.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
using BrightIdeasSoftware;
|
||||
using mRemoteNG.Connection;
|
||||
using mRemoteNG.Container;
|
||||
using mRemoteNG.Tree.Root;
|
||||
|
||||
namespace mRemoteNG.Tree
|
||||
{
|
||||
internal class ConnectionTreeDragAndDropHandler
|
||||
{
|
||||
internal void OnModelCanDrop(object sender, ModelDropEventArgs e)
|
||||
{
|
||||
e.Effect = DragDropEffects.None;
|
||||
var draggedObject = e.SourceModels.Cast<ConnectionInfo>().First();
|
||||
var dropTarget = e.TargetModel as ContainerInfo;
|
||||
if (AncestorDraggingOntoChild(draggedObject, dropTarget))
|
||||
e.InfoMessage = "Cannot drag parent node onto child.";
|
||||
else if (!NodeIsDraggable(draggedObject))
|
||||
e.InfoMessage = "This node is not draggable";
|
||||
else
|
||||
e.Effect = DragDropEffects.Move;
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private bool NodeIsDraggable(ConnectionInfo node)
|
||||
{
|
||||
if (node == null || node is RootNodeInfo || node is PuttySessionInfo) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool AncestorDraggingOntoChild(ConnectionInfo source, ConnectionInfo target)
|
||||
{
|
||||
var sourceAsContainer = source as ContainerInfo;
|
||||
return sourceAsContainer != null && sourceAsContainer.GetRecursiveChildList().Contains(target);
|
||||
}
|
||||
|
||||
internal void OnModelDropped(object sender, ModelDropEventArgs e)
|
||||
{
|
||||
var draggedObject = (IHasParent)e.SourceModels[0];
|
||||
var dropTarget = e.TargetModel as ContainerInfo;
|
||||
if (dropTarget != null)
|
||||
draggedObject.SetParent(dropTarget);
|
||||
e.Handled = true;
|
||||
e.RefreshObjects();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ namespace mRemoteNG.UI.Window
|
||||
public partial class ConnectionTreeWindow
|
||||
{
|
||||
private ConnectionTreeModel _connectionTreeModel;
|
||||
private ConnectionTreeDragAndDropHandler _dragAndDropHandler = new ConnectionTreeDragAndDropHandler();
|
||||
|
||||
private ToolTip DescriptionTooltip { get; }
|
||||
private ConnectionInfo SelectedNode => (ConnectionInfo) olvConnections.SelectedObject;
|
||||
@@ -105,13 +106,10 @@ namespace mRemoteNG.UI.Window
|
||||
olvConnections.CellClick += tvConnections_NodeMouseSingleClick;
|
||||
olvConnections.CellClick += tvConnections_NodeMouseDoubleClick;
|
||||
olvConnections.CellToolTipShowing += tvConnections_CellToolTipShowing;
|
||||
olvConnections.ModelCanDrop += OlvConnections_OnModelCanDrop;
|
||||
olvConnections.ModelDropped += OlvConnections_OnModelDropped;
|
||||
olvConnections.ModelCanDrop += _dragAndDropHandler.OnModelCanDrop;
|
||||
olvConnections.ModelDropped += _dragAndDropHandler.OnModelDropped;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void PopulateTreeView()
|
||||
{
|
||||
olvConnections.Roots = ConnectionTreeModel.RootNodes;
|
||||
@@ -442,82 +440,44 @@ namespace mRemoteNG.UI.Window
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Drag and Drop
|
||||
private void OlvConnections_OnModelCanDrop(object sender, ModelDropEventArgs e)
|
||||
{
|
||||
var draggedObject = e.SourceModels[0] as ConnectionInfo;
|
||||
if (!NodeIsDraggable(draggedObject)) return;
|
||||
var dropTarget = e.TargetModel as ContainerInfo;
|
||||
if (dropTarget != null)
|
||||
e.Effect = DragDropEffects.Move;
|
||||
else
|
||||
{
|
||||
e.Effect = DragDropEffects.None;
|
||||
}
|
||||
}
|
||||
|
||||
private bool NodeIsDraggable(ConnectionInfo node)
|
||||
{
|
||||
if (node == null || node is RootNodeInfo || node is PuttySessionInfo) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OlvConnections_OnModelDropped(object sender, ModelDropEventArgs e)
|
||||
{
|
||||
var draggedObject = (IHasParent)e.SourceModels[0];
|
||||
var dropTarget = e.TargetModel as ContainerInfo;
|
||||
if (dropTarget != null)
|
||||
draggedObject.SetParent(dropTarget);
|
||||
e.RefreshObjects();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Tree Context Menu
|
||||
//TODO Fix for TreeListView
|
||||
private void cMenTreeAddConnection_Click(object sender, EventArgs e)
|
||||
{
|
||||
AddConnection();
|
||||
Runtime.SaveConnectionsBG();
|
||||
}
|
||||
|
||||
//TODO Fix for TreeListView
|
||||
private void cMenTreeAddFolder_Click(object sender, EventArgs e)
|
||||
{
|
||||
AddFolder();
|
||||
Runtime.SaveConnectionsBG();
|
||||
}
|
||||
|
||||
//TODO Fix for TreeListView
|
||||
private void cMenTreeConnect_Click(object sender, EventArgs e)
|
||||
{
|
||||
Runtime.OpenConnection(ConnectionInfo.Force.DoNotJump);
|
||||
}
|
||||
|
||||
//TODO Fix for TreeListView
|
||||
private void cMenTreeConnectWithOptionsConnectToConsoleSession_Click(object sender, EventArgs e)
|
||||
{
|
||||
Runtime.OpenConnection(ConnectionInfo.Force.UseConsoleSession | ConnectionInfo.Force.DoNotJump);
|
||||
}
|
||||
|
||||
//TODO Fix for TreeListView
|
||||
private void cMenTreeConnectWithOptionsNoCredentials_Click(object sender, EventArgs e)
|
||||
{
|
||||
Runtime.OpenConnection(ConnectionInfo.Force.NoCredentials);
|
||||
}
|
||||
|
||||
//TODO Fix for TreeListView
|
||||
private void cMenTreeConnectWithOptionsDontConnectToConsoleSession_Click(object sender, EventArgs e)
|
||||
{
|
||||
Runtime.OpenConnection(ConnectionInfo.Force.DontUseConsoleSession | ConnectionInfo.Force.DoNotJump);
|
||||
}
|
||||
|
||||
//TODO Fix for TreeListView
|
||||
private void cMenTreeConnectWithOptionsConnectInFullscreen_Click(object sender, EventArgs e)
|
||||
{
|
||||
Runtime.OpenConnection(ConnectionInfo.Force.Fullscreen | ConnectionInfo.Force.DoNotJump);
|
||||
}
|
||||
|
||||
//TODO Fix for TreeListView
|
||||
private void cMenTreeConnectWithOptionsChoosePanelBeforeConnecting_Click(object sender, EventArgs e)
|
||||
{
|
||||
Runtime.OpenConnection(ConnectionInfo.Force.OverridePanel | ConnectionInfo.Force.DoNotJump);
|
||||
@@ -525,7 +485,7 @@ namespace mRemoteNG.UI.Window
|
||||
|
||||
private void cMenTreeDisconnect_Click(object sender, EventArgs e)
|
||||
{
|
||||
DisconnectConnection();
|
||||
DisconnectConnection(SelectedNode);
|
||||
}
|
||||
|
||||
private void cMenTreeToolsTransferFile_Click(object sender, EventArgs e)
|
||||
@@ -665,36 +625,29 @@ namespace mRemoteNG.UI.Window
|
||||
}
|
||||
|
||||
//TODO Fix for TreeListView
|
||||
private void DisconnectConnection()
|
||||
private void DisconnectConnection(ConnectionInfo connectionInfo)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (tvConnections.SelectedNode != null)
|
||||
{
|
||||
if (tvConnections.SelectedNode.Tag is ConnectionInfo)
|
||||
{
|
||||
ConnectionInfo conI = (ConnectionInfo)tvConnections.SelectedNode.Tag;
|
||||
for (int i = 0; i <= conI.OpenConnections.Count - 1; i++)
|
||||
{
|
||||
conI.OpenConnections[i].Disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
if (tvConnections.SelectedNode.Tag is ContainerInfo)
|
||||
{
|
||||
foreach (TreeNode n in tvConnections.SelectedNode.Nodes)
|
||||
{
|
||||
if (n.Tag is ConnectionInfo)
|
||||
{
|
||||
ConnectionInfo conI = (ConnectionInfo)n.Tag;
|
||||
for (int i = 0; i <= conI.OpenConnections.Count - 1; i++)
|
||||
{
|
||||
conI.OpenConnections[i].Disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (connectionInfo == null) return;
|
||||
var nodeAsContainer = connectionInfo as ContainerInfo;
|
||||
if (nodeAsContainer != null)
|
||||
{
|
||||
foreach (var child in nodeAsContainer.Children)
|
||||
{
|
||||
for (var i = 0; i <= child.OpenConnections.Count - 1; i++)
|
||||
{
|
||||
child.OpenConnections[i].Disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i <= connectionInfo.OpenConnections.Count - 1; i++)
|
||||
{
|
||||
connectionInfo.OpenConnections[i].Disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -194,6 +194,7 @@
|
||||
<Compile Include="Tools\Sorting\TreeNodeSorter.cs" />
|
||||
<Compile Include="Tools\Sorting\Sortable.cs" />
|
||||
<Compile Include="Tree\ConnectionTree.cs" />
|
||||
<Compile Include="Tree\ConnectionTreeDragAndDropHandler.cs" />
|
||||
<Compile Include="Tree\ConnectionTreeModel.cs" />
|
||||
<Compile Include="Tree\NodeType.cs" />
|
||||
<Compile Include="Tree\Root\RootNodeTypeEnum.cs" />
|
||||
|
||||
Reference in New Issue
Block a user