-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from Checkmarx/feature/addFilterToCombobox
Add filter in project, branch and scan ComboBoxes(AST-64166)
- Loading branch information
Showing
12 changed files
with
246 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
ast-visual-studio-extension/CxExtension/Toolbar/ComboboxBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Windows.Controls; | ||
using System.Windows.Input; | ||
|
||
namespace ast_visual_studio_extension.CxExtension.Toolbar | ||
{ | ||
internal abstract class ComboboxBase | ||
{ | ||
protected CxToolbar cxToolbar; | ||
protected ComboBox comboBox; | ||
protected string previousText = string.Empty; | ||
protected bool isFiltered = false; | ||
protected List<ComboBoxItem> allItems; | ||
public ComboboxBase(CxToolbar cxToolbar, ComboBox comboBox) | ||
{ | ||
this.cxToolbar = cxToolbar; | ||
this.comboBox = comboBox; | ||
allItems = new List<ComboBoxItem>(); | ||
} | ||
protected void ResetFilteringState(ComboBoxItem selectedItem) | ||
{ | ||
previousText = selectedItem.Content.ToString(); | ||
if (isFiltered) | ||
{ | ||
Mouse.OverrideCursor = Cursors.Wait; | ||
isFiltered = false; | ||
UpdateCombobox(allItems); | ||
comboBox.SelectedItem = selectedItem; | ||
Mouse.OverrideCursor = null; | ||
} | ||
} | ||
public void OnComboBoxTextChanged(object sender, EventArgs e) | ||
{ | ||
|
||
if (comboBox == null || IsTextUnchanged()) return; | ||
|
||
Mouse.OverrideCursor = Cursors.Wait; | ||
|
||
TextBox textBox = GetTextBoxFromComboBox(); | ||
|
||
if (textBox != null) | ||
{ | ||
string newText = textBox.Text; | ||
int savedSelectionStart = textBox.SelectionStart; | ||
|
||
ResetOthersComboBoxesAndResults(); | ||
|
||
UpdateComboBoxWithFilteredItems(newText); | ||
|
||
comboBox.IsDropDownOpen = true; | ||
RestoreTextBoxState(textBox, savedSelectionStart, newText); | ||
} | ||
Mouse.OverrideCursor = null; | ||
} | ||
private TextBox GetTextBoxFromComboBox() | ||
{ | ||
return (TextBox)comboBox.Template.FindName("PART_EditableTextBox", comboBox); | ||
} | ||
private bool IsTextUnchanged() | ||
{ | ||
|
||
if (comboBox.Text == previousText) return true; | ||
|
||
previousText = comboBox.Text; | ||
return false; | ||
} | ||
private void UpdateComboBoxWithFilteredItems(string newText) | ||
{ | ||
comboBox.SelectedItem = null; | ||
|
||
if (string.IsNullOrEmpty(newText)) | ||
{ | ||
UpdateCombobox(allItems); | ||
} | ||
else | ||
{ | ||
var filteredItems = allItems.Where(item => item.Content.ToString() | ||
.IndexOf(newText, StringComparison.OrdinalIgnoreCase) >= 0) | ||
.ToList(); | ||
UpdateCombobox(filteredItems); | ||
isFiltered = true; | ||
} | ||
} | ||
|
||
private void RestoreTextBoxState(TextBox textBox, int selectionStart, string text) | ||
{ | ||
textBox.Text = text; | ||
textBox.SelectionStart = Math.Min(selectionStart, text.Length); | ||
textBox.SelectionLength = 0; | ||
} | ||
|
||
protected void UpdateCombobox(List<ComboBoxItem> items) | ||
{ | ||
comboBox.Items.Clear(); | ||
foreach (var item in items) | ||
{ | ||
comboBox.Items.Add(item); | ||
} | ||
} | ||
|
||
protected abstract void ResetOthersComboBoxesAndResults(); | ||
} | ||
} |
Oops, something went wrong.