From eb020611b18223e163e5198ef270d66c6ef0a3fb Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sat, 10 Nov 2012 14:00:11 -0500 Subject: [PATCH] Separated Winforms from SearchWorker --- NBTExplorer.csproj | 1 + SearchWorker.cs | 34 ++++++++++------------- Windows/MainForm.cs | 6 ++--- Windows/SearchStateWin.cs | 57 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 23 deletions(-) create mode 100644 Windows/SearchStateWin.cs diff --git a/NBTExplorer.csproj b/NBTExplorer.csproj index 5c1d29d..298bba1 100644 --- a/NBTExplorer.csproj +++ b/NBTExplorer.csproj @@ -194,6 +194,7 @@ + CancelSearchForm.cs diff --git a/SearchWorker.cs b/SearchWorker.cs index b424e79..ae2238c 100644 --- a/SearchWorker.cs +++ b/SearchWorker.cs @@ -1,35 +1,32 @@ using System; using System.Collections.Generic; -using System.Windows.Forms; using NBTExplorer.Model; namespace NBTExplorer { - internal class SearchState + internal interface ISearchState { - public DataNode RootNode { get; set; } - public string SearchName { get; set; } - public string SearchValue { get; set; } + DataNode RootNode { get; set; } + string SearchName { get; set; } + string SearchValue { get; set; } - public IEnumerator State { get; set; } + IEnumerator State { get; set; } - public Action DiscoverCallback { get; set; } - public Action ProgressCallback { get; set; } - public Action CollapseCallback { get; set; } - public Action EndCallback { get; set; } + void InvokeDiscoverCallback (DataNode node); + void InvokeProgressCallback (DataNode node); + void InvokeCollapseCallback (DataNode node); + void InvokeEndCallback (DataNode node); } internal class SearchWorker { - private ContainerControl _sender; - private SearchState _state; + private ISearchState _state; private bool _cancel; private object _lock; - public SearchWorker (SearchState state, ContainerControl sender) + public SearchWorker (ISearchState state) { _state = state; - _sender = sender; _lock = new object(); } @@ -102,20 +99,17 @@ namespace NBTExplorer private void InvokeDiscoverCallback (DataNode node) { - if (_sender != null && _state.DiscoverCallback != null) - _sender.BeginInvoke(_state.DiscoverCallback, new object[] { node }); + _state.InvokeDiscoverCallback(node); } private void InvokeCollapseCallback (DataNode node) { - if (_sender != null && _state.CollapseCallback != null) - _sender.BeginInvoke(_state.CollapseCallback, new object[] { node }); + _state.InvokeCollapseCallback(node); } private void InvokeEndCallback () { - if (_sender != null && _state.EndCallback != null) - _sender.BeginInvoke(_state.EndCallback, new object[] { null }); + _state.InvokeEndCallback(null); } } } diff --git a/Windows/MainForm.cs b/Windows/MainForm.cs index 5580722..968bfcb 100644 --- a/Windows/MainForm.cs +++ b/Windows/MainForm.cs @@ -470,7 +470,7 @@ namespace NBTExplorer.Windows } private CancelSearchForm _searchForm; - private SearchState _searchState; + private SearchStateWin _searchState; private void SearchNode (TreeNode node) { @@ -485,7 +485,7 @@ namespace NBTExplorer.Windows if (form.ShowDialog() != DialogResult.OK) return; - _searchState = new SearchState() { + _searchState = new SearchStateWin(this) { RootNode = dataNode, SearchName = form.NameToken, SearchValue = form.ValueToken, @@ -502,7 +502,7 @@ namespace NBTExplorer.Windows if (_searchState == null) return; - SearchWorker worker = new SearchWorker(_searchState, this); + SearchWorker worker = new SearchWorker(_searchState); Thread t = new Thread(new ThreadStart(worker.Run)); t.IsBackground = true; diff --git a/Windows/SearchStateWin.cs b/Windows/SearchStateWin.cs new file mode 100644 index 0000000..8a15581 --- /dev/null +++ b/Windows/SearchStateWin.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Text; +using NBTExplorer.Model; +using System.Windows.Forms; + +namespace NBTExplorer.Windows +{ + internal class SearchStateWin : ISearchState + { + private ContainerControl _sender; + + public SearchStateWin (ContainerControl sender) + { + _sender = sender; + } + + public Action DiscoverCallback { get; set; } + public Action ProgressCallback { get; set; } + public Action CollapseCallback { get; set; } + public Action EndCallback { get; set; } + + #region ISearchState + + public DataNode RootNode { get; set; } + public string SearchName { get; set; } + public string SearchValue { get; set; } + + public IEnumerator State { get; set; } + + public void InvokeDiscoverCallback (DataNode node) + { + if (_sender != null && DiscoverCallback != null) + _sender.BeginInvoke(DiscoverCallback, new object[] { node }); + } + + public void InvokeProgressCallback (DataNode node) + { + if (_sender != null && ProgressCallback != null) + _sender.BeginInvoke(ProgressCallback, new object[] { node }); + } + + public void InvokeCollapseCallback (DataNode node) + { + if (_sender != null && CollapseCallback != null) + _sender.BeginInvoke(CollapseCallback, new object[] { node }); + } + + public void InvokeEndCallback (DataNode node) + { + if (_sender != null && EndCallback != null) + _sender.BeginInvoke(EndCallback, new object[] { node }); + } + + #endregion + } +}