Refactored node searching to a new class

This commit is contained in:
David Sparer
2016-09-14 11:43:40 -06:00
parent 7d882c3f74
commit a992b41e26
3 changed files with 63 additions and 9 deletions

View File

@@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.Linq;
using mRemoteNG.Connection;
namespace mRemoteNG.Tree
{
internal class NodeSearcher
{
private readonly ConnectionTreeModel _connectionTreeModel;
private ConnectionInfo _currentMatch;
public List<ConnectionInfo> Matches { get; private set; }
public NodeSearcher(ConnectionTreeModel connectionTreeModel)
{
_connectionTreeModel = connectionTreeModel;
}
internal IEnumerable<ConnectionInfo> SearchByName(string searchText)
{
if (searchText == "")
ResetMatches();
else
{
Matches = (List<ConnectionInfo>)_connectionTreeModel.GetRecursiveChildList().Where(node => node.Name.Contains(searchText));
_currentMatch = Matches.First();
}
return Matches;
}
internal ConnectionInfo NextMatch()
{
var currentMatchIndex = Matches.IndexOf(_currentMatch);
if (currentMatchIndex < Matches.Count-1)
_currentMatch = Matches[currentMatchIndex + 1];
return _currentMatch;
}
internal ConnectionInfo PreviousMatch()
{
var currentMatchIndex = Matches.IndexOf(_currentMatch);
if (currentMatchIndex > 0)
_currentMatch = Matches[currentMatchIndex - 1];
return _currentMatch;
}
private void ResetMatches()
{
Matches = new List<ConnectionInfo>();
_currentMatch = null;
}
}
}

View File

@@ -19,6 +19,7 @@ namespace mRemoteNG.UI.Window
{
private ConnectionTreeModel _connectionTreeModel;
private ConnectionTreeDragAndDropHandler _dragAndDropHandler = new ConnectionTreeDragAndDropHandler();
private NodeSearcher _nodeSearcher;
public ConnectionInfo SelectedNode => (ConnectionInfo) olvConnections.SelectedObject;
@@ -135,6 +136,7 @@ namespace mRemoteNG.UI.Window
private void PopulateTreeView()
{
olvConnections.Roots = ConnectionTreeModel.RootNodes;
_nodeSearcher = new NodeSearcher(ConnectionTreeModel);
ExpandPreviouslyOpenedFolders();
ExpandRootConnectionNode();
OpenConnectionsFromLastSession();
@@ -817,11 +819,13 @@ namespace mRemoteNG.UI.Window
}
else if (e.KeyCode == Keys.Up)
{
tvConnections.SelectedNode = tvConnections.SelectedNode.PrevVisibleNode;
//tvConnections.SelectedNode = tvConnections.SelectedNode.PrevVisibleNode;
olvConnections.SelectObject(_nodeSearcher.PreviousMatch());
}
else if (e.KeyCode == Keys.Down)
{
tvConnections.SelectedNode = tvConnections.SelectedNode.NextVisibleNode;
//tvConnections.SelectedNode = tvConnections.SelectedNode.NextVisibleNode;
olvConnections.SelectObject(_nodeSearcher.NextMatch());
}
else
{
@@ -837,13 +841,8 @@ namespace mRemoteNG.UI.Window
//TODO Fix for TreeListView
private void txtSearch_TextChanged(object sender, EventArgs e)
{
try
{
tvConnections.SelectedNode = ConnectionTree.Find(tvConnections.Nodes[0], txtSearch.Text);
}
catch (Exception)
{
}
var matches = _nodeSearcher.SearchByName(txtSearch.Text);
olvConnections.SelectObject(matches.First());
}
//TODO Fix for TreeListView

View File

@@ -196,6 +196,7 @@
<Compile Include="Tree\ConnectionTree.cs" />
<Compile Include="Tree\ConnectionTreeDragAndDropHandler.cs" />
<Compile Include="Tree\ConnectionTreeModel.cs" />
<Compile Include="Tree\NodeSearcher.cs" />
<Compile Include="Tree\NodeType.cs" />
<Compile Include="Tree\Root\RootNodeTypeEnum.cs" />
<Compile Include="Tree\TreeNodeMover.cs" />