Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Klocman committed Dec 14, 2016
1 parent d6305a2 commit cd596e2
Showing 1 changed file with 84 additions and 74 deletions.
158 changes: 84 additions & 74 deletions MainApplication/Controls/FilterBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace TextToScreen.Controls
public partial class FilterBox : UserControl
{
private static readonly LocalisedEnumWrapper[] FilteringOptions;
private Regex _compiledRegex;

static FilterBox()
{
Expand All @@ -35,44 +36,109 @@ public FilterBox()
FilteringOptions.Single(x => (ComparisonMethod) x.TargetEnum == ComparisonMethod.Contains));
}

public bool SearchStringIsEmpty => string.IsNullOrEmpty(searchBox1.SearchString);

public Control SearchBox => searchBox1;

public bool SearchStringIsEmpty => string.IsNullOrEmpty(searchBox1.SearchString);

private ComparisonMethod SelectedComparisonMethod
=> (ComparisonMethod) ((LocalisedEnumWrapper) comboBoxCompareMethod.SelectedItem).TargetEnum;

public void ClearSearchBox()
public event EventHandler FilterChanged;

private void button1_Click(object sender, EventArgs e)
{
searchBox1.ClearSearchBox();
}

public void SetGroups(IEnumerable<SongFileEntry> entries)
private bool CheckGroupMatch(SongFileEntry entry)
{
groupFilterComboBox.SelectedIndex = 0;
while (groupFilterComboBox.Items.Count > 2)
groupFilterComboBox.Items.RemoveAt(2);
if (groupFilterComboBox.SelectedIndex <= 1) return true;

var query = entries.GroupBy(x => x.Group)
.Select(group => string.IsNullOrEmpty(group.Key) ? Localisation.DefaultGroupName : group.Key)
.OrderBy(x => x);
var filter = groupFilterComboBox.SelectedItem as string;
if (filter == null || filter.Equals(Localisation.DefaultGroupName))
filter = string.Empty;
return entry.Group.Equals(filter, StringComparison.CurrentCulture);
}

groupFilterComboBox.Items.AddRange(query.Cast<object>().ToArray());
public void ClearSearchBox()
{
searchBox1.ClearSearchBox();
}

public void FocusSearchbox()
{
searchBox1.FocusSearchBox();
}

private bool CheckGroupMatch(SongFileEntry entry)
private RegexOptions GetRegexOptions()
{
if (groupFilterComboBox.SelectedIndex <= 1) return true;
// TODO Check if there are enough entries to search through to warrant compiling?
var options = RegexOptions.Compiled;

var filter = groupFilterComboBox.SelectedItem as string;
if (filter == null || filter.Equals(Localisation.DefaultGroupName))
filter = string.Empty;
return entry.Group.Equals(filter, StringComparison.CurrentCulture);
if (!checkBoxExact.Checked)
options |= RegexOptions.IgnoreCase | RegexOptions.CultureInvariant;

// Names and comments are all single line.
options |= searchInsideFilesCheckBox.Checked ? RegexOptions.Multiline : RegexOptions.Singleline;

return options;
}

private void groupFilterComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (groupFilterComboBox.SelectedIndex == 1)
groupFilterComboBox.SelectedIndex = 0;

OnFilterChanged(sender, e, true);
}

private void OnFilterChanged(object sender, EventArgs eventArgs)
{
OnFilterChanged(sender, eventArgs, false);
}

private void OnFilterChanged(object sender, EventArgs eventArgs, bool forceUpdate)
{
// If there is no search string entered, skip processing non-important changes.
if (!forceUpdate && SearchStringIsEmpty)
return;

if (SelectedComparisonMethod == ComparisonMethod.Regex)
{
try
{
// Compile the Regex expression now to check if it's valid.
_compiledRegex = new Regex(searchBox1.SearchString, GetRegexOptions());
}
catch (ArgumentException)
{
_compiledRegex = null;
}
}

FilterChanged?.Invoke(sender, eventArgs);
}

private void searchBox1_SearchTextChanged(SearchBox arg1, EventArgs arg2)
{
//TODO Reposition and enable the clear button?
//var enableClear = !SearchStringIsEmpty;
//buttonClear.Visible = enableClear;
//buttonClear.Enabled = enableClear;
OnFilterChanged(arg1, arg2, true);
}

public void SetGroups(IEnumerable<SongFileEntry> entries)
{
groupFilterComboBox.SelectedIndex = 0;
while (groupFilterComboBox.Items.Count > 2)
groupFilterComboBox.Items.RemoveAt(2);

var query = entries.GroupBy(x => x.Group)
.Select(group => string.IsNullOrEmpty(group.Key) ? Localisation.DefaultGroupName : group.Key)
.OrderBy(x => x);

groupFilterComboBox.Items.AddRange(query.Cast<object>().ToArray());
}

/// <summary>
Expand Down Expand Up @@ -158,64 +224,8 @@ private bool CheckGroupMatch(SongFileEntry entry)

if (!result.HasValue) return null;

// Invert value if needed
return checkBoxInvert.Checked ? !result.Value : result.Value;
}

private void button1_Click(object sender, EventArgs e)
{
searchBox1.ClearSearchBox();
}

private void groupFilterComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (groupFilterComboBox.SelectedIndex == 1)
groupFilterComboBox.SelectedIndex = 0;

OnFilterChanged(sender, e);
}

public event EventHandler FilterChanged;

private Regex _compiledRegex;

private void OnFilterChanged(object sender, EventArgs eventArgs)
{
if (SelectedComparisonMethod == ComparisonMethod.Regex)
{
try
{
// Compile the Regex expression now to check if it's valid.
_compiledRegex = new Regex(searchBox1.SearchString, GetRegexOptions());
}
catch (ArgumentException)
{
_compiledRegex = null;
}
}
FilterChanged?.Invoke(sender, eventArgs);
}

private RegexOptions GetRegexOptions()
{
// TODO Check if there are enough entries to search through to warrant compiling?
var options = RegexOptions.Compiled;

if (!checkBoxExact.Checked)
options |= RegexOptions.IgnoreCase | RegexOptions.CultureInvariant;

// Names and comments are all single line.
options |= searchInsideFilesCheckBox.Checked ? RegexOptions.Multiline : RegexOptions.Singleline;

return options;
}

private void searchBox1_SearchTextChanged(SearchBox arg1, EventArgs arg2)
{
//TODO Reposition and enable the clear button?
//var enableClear = !SearchStringIsEmpty;
//buttonClear.Visible = enableClear;
//buttonClear.Enabled = enableClear;
OnFilterChanged(arg1, arg2);
}
}
}

0 comments on commit cd596e2

Please sign in to comment.