diff --git a/mRemoteV1/App/Runtime.cs b/mRemoteV1/App/Runtime.cs index c5d7008e3..a30e612f9 100644 --- a/mRemoteV1/App/Runtime.cs +++ b/mRemoteV1/App/Runtime.cs @@ -2,7 +2,6 @@ using mRemoteNG.App.Info; using mRemoteNG.Config.Connections; using mRemoteNG.Connection; using mRemoteNG.Connection.Protocol; -using mRemoteNG.Connection.Protocol.RDP; using mRemoteNG.Container; using mRemoteNG.Credential; using mRemoteNG.Messages; diff --git a/mRemoteV1/Connection/ConnectionInfoComparer.cs b/mRemoteV1/Connection/ConnectionInfoComparer.cs new file mode 100644 index 000000000..0783c2b1a --- /dev/null +++ b/mRemoteV1/Connection/ConnectionInfoComparer.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; + + +namespace mRemoteNG.Connection +{ + public class ConnectionInfoComparer : IComparer where TProperty : IComparable + { + private readonly Func _sortExpression; + public ListSortDirection SortDirection { get; set; } = ListSortDirection.Ascending; + + public ConnectionInfoComparer(Func sortExpression) + { + _sortExpression = sortExpression; + } + + public int Compare(ConnectionInfo x, ConnectionInfo y) + { + return SortDirection == ListSortDirection.Ascending ? CompareAscending(x, y) : CompareDescending(x, y); + } + + private int CompareAscending(ConnectionInfo x, ConnectionInfo y) + { + return _sortExpression(x).CompareTo(_sortExpression(y)); + } + + private int CompareDescending(ConnectionInfo x, ConnectionInfo y) + { + return _sortExpression(y).CompareTo(_sortExpression(x)); + } + } +} \ No newline at end of file diff --git a/mRemoteV1/Container/ContainerInfo.cs b/mRemoteV1/Container/ContainerInfo.cs index b1fc714cf..790e7439f 100644 --- a/mRemoteV1/Container/ContainerInfo.cs +++ b/mRemoteV1/Container/ContainerInfo.cs @@ -1,6 +1,10 @@ +using System; +using System.Collections; using System.Collections.Generic; using mRemoteNG.Connection; using System.ComponentModel; +using System.Globalization; +using System.Linq; using mRemoteNG.Connection.Protocol; using mRemoteNG.Tools; using mRemoteNG.Tree; @@ -103,6 +107,34 @@ namespace mRemoteNG.Container SetChildPosition(child, originalIndex + 1); } + public void Sort(ListSortDirection sortDirection = ListSortDirection.Ascending) + { + SortOn(connectionInfo => connectionInfo.Name, sortDirection); + } + + public void SortOn(Func propertyToCompare, ListSortDirection sortDirection = ListSortDirection.Ascending) + where TProperty : IComparable + { + var connectionComparer = new ConnectionInfoComparer(propertyToCompare) + { + SortDirection = sortDirection + }; + Children.Sort(connectionComparer); + } + + public void SortRecursive(ListSortDirection sortDirection = ListSortDirection.Ascending) + { + SortOnRecursive(connectionInfo => connectionInfo.Name, sortDirection); + } + + public void SortOnRecursive(Func propertyToCompare, ListSortDirection sortDirection = ListSortDirection.Ascending) + where TProperty : IComparable + { + foreach (var child in Children.OfType()) + child.SortOnRecursive(propertyToCompare, sortDirection); + SortOn(propertyToCompare, sortDirection); + } + public override void Dispose() { var tempChildList = Children.ToArray(); diff --git a/mRemoteV1/UI/Window/ConnectionTreeWindow.Designer.cs b/mRemoteV1/UI/Window/ConnectionTreeWindow.Designer.cs index 5bec8207d..82865b44a 100644 --- a/mRemoteV1/UI/Window/ConnectionTreeWindow.Designer.cs +++ b/mRemoteV1/UI/Window/ConnectionTreeWindow.Designer.cs @@ -188,7 +188,6 @@ namespace mRemoteNG.UI.Window this.mMenSortAscending.Image = global::mRemoteNG.Resources.Sort_AZ; this.mMenSortAscending.Name = "mMenSortAscending"; this.mMenSortAscending.Size = new System.Drawing.Size(28, 20); - this.mMenSortAscending.Click += new System.EventHandler(this.mMenSortAscending_Click); // // ConnectionTreeWindow // diff --git a/mRemoteV1/UI/Window/ConnectionTreeWindow.cs b/mRemoteV1/UI/Window/ConnectionTreeWindow.cs index 4cf01423a..580660a73 100644 --- a/mRemoteV1/UI/Window/ConnectionTreeWindow.cs +++ b/mRemoteV1/UI/Window/ConnectionTreeWindow.cs @@ -3,6 +3,7 @@ using mRemoteNG.Connection; using mRemoteNG.Container; using mRemoteNG.Tree; using System; +using System.ComponentModel; using System.Drawing; using System.Linq; using System.Windows.Forms; @@ -18,9 +19,9 @@ namespace mRemoteNG.UI.Window public partial class ConnectionTreeWindow { private ConnectionTreeModel _connectionTreeModel; - private ConnectionTreeDragAndDropHandler _dragAndDropHandler = new ConnectionTreeDragAndDropHandler(); + private readonly ConnectionTreeDragAndDropHandler _dragAndDropHandler = new ConnectionTreeDragAndDropHandler(); private NodeSearcher _nodeSearcher; - private ConnectionContextMenu _contextMenu = new ConnectionContextMenu(); + private readonly ConnectionContextMenu _contextMenu = new ConnectionContextMenu(); public ConnectionInfo SelectedNode => (ConnectionInfo) olvConnections.SelectedObject; @@ -145,8 +146,8 @@ namespace mRemoteNG.UI.Window _contextMenu.ExportFileClicked += (sender, args) => Export.ExportToFile(Windows.treeForm.tvConnections.Nodes[0], Windows.treeForm.tvConnections.SelectedNode, Runtime.ConnectionTreeModel); _contextMenu.AddConnectionClicked += cMenTreeAddConnection_Click; _contextMenu.AddFolderClicked += cMenTreeAddFolder_Click; - _contextMenu.SortAscendingClicked += cMenTreeToolsSortAscending_Click; - _contextMenu.SortDescendingClicked += cMenTreeToolsSortDescending_Click; + _contextMenu.SortAscendingClicked += (sender, args) => SortNodes(ListSortDirection.Ascending); + _contextMenu.SortDescendingClicked += (sender, args) => SortNodes(ListSortDirection.Descending); ; _contextMenu.MoveUpClicked += cMenTreeMoveUp_Click; _contextMenu.MoveDownClicked += cMenTreeMoveDown_Click; _contextMenu.ExternalToolClicked += (sender, args) => StartExternalApp((ExternalTool)((ToolStripMenuItem)sender).Tag); @@ -160,7 +161,8 @@ namespace mRemoteNG.UI.Window olvConnections.CollapseAll(); olvConnections.Expand(GetRootConnectionNode()); }; - } + mMenSortAscending.Click += (sender, args) => SortNodesRecursive(GetRootConnectionNode(), ListSortDirection.Ascending); + } private void PopulateTreeView() { @@ -404,32 +406,23 @@ namespace mRemoteNG.UI.Window Runtime.SaveConnectionsBG(); } - //TODO Fix for TreeListView - private void mMenSortAscending_Click(object sender, EventArgs e) - { - tvConnections.BeginUpdate(); - ConnectionTree.Sort(tvConnections.Nodes[0], SortOrder.Ascending); - tvConnections.EndUpdate(); + private void SortNodes(ListSortDirection sortDirection) + { + var selectedNodeAsContainer = SelectedNode as ContainerInfo; + if (selectedNodeAsContainer != null) + selectedNodeAsContainer.Sort(sortDirection); + else + SelectedNode.Parent.Sort(sortDirection); + olvConnections.RefreshObject(SelectedNode); Runtime.SaveConnectionsBG(); - } + } - //TODO Fix for TreeListView - private void cMenTreeToolsSortAscending_Click(object sender, EventArgs e) - { - tvConnections.BeginUpdate(); - ConnectionTree.Sort(tvConnections.SelectedNode, SortOrder.Ascending); - tvConnections.EndUpdate(); + private void SortNodesRecursive(ContainerInfo rootSortTarget, ListSortDirection sortDirection) + { + rootSortTarget.SortRecursive(sortDirection); + olvConnections.RefreshObject(rootSortTarget); Runtime.SaveConnectionsBG(); - } - - //TODO Fix for TreeListView - private void cMenTreeToolsSortDescending_Click(object sender, EventArgs e) - { - tvConnections.BeginUpdate(); - ConnectionTree.Sort(tvConnections.SelectedNode, SortOrder.Descending); - tvConnections.EndUpdate(); - Runtime.SaveConnectionsBG(); - } + } private void cMenTreeMoveUp_Click(object sender, EventArgs e) { diff --git a/mRemoteV1/mRemoteV1.csproj b/mRemoteV1/mRemoteV1.csproj index d37c93220..8b9906ce2 100644 --- a/mRemoteV1/mRemoteV1.csproj +++ b/mRemoteV1/mRemoteV1.csproj @@ -169,6 +169,7 @@ +