using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SolutionOpen.ToolWindow
{
public partial class ChooseFile : Form
{
public ChooseFile()
{
InitializeComponent();
LoadFromRegistry();
RebuildListViewItems();
}
#region Members
private readonly static String RegistryKeyName = "Software\\JeffGrills\\SolutionOpen";
private readonly static String LastSearchRegistryValueName = "LastSearch";
private readonly static String DisplayLimitRegistryValueName = "DisplayLimit";
#endregion
#region Methods
private void Abort()
{
SaveToRegistry();
DialogResult = DialogResult.Abort;
Close();
}
private void Accept()
{
SaveToRegistry();
DialogResult = DialogResult.OK;
Close();
}
private void LoadFromRegistry()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryKeyName);
if (key != null)
{
object value = key.GetValue(LastSearchRegistryValueName);
if (value != null)
{
string text = value as string;
if (text != null)
searchTokens.Text = text;
}
value = key.GetValue(DisplayLimitRegistryValueName);
if (value != null)
{
string text = value as string;
if (text != null)
displayLimit.Text = text;
}
}
}
private void SaveToRegistry()
{
RegistryKey key = Registry.CurrentUser.CreateSubKey(RegistryKeyName);
if (key != null)
{
key.SetValue(LastSearchRegistryValueName, searchTokens.Text);
key.SetValue(DisplayLimitRegistryValueName, displayLimit.Text);
}
}
private void RebuildListViewItems()
{
// build the patterns to match against
string text = searchTokens.Text.ToLower();
string []tokens = text.Split(' ');
List<Regex> regularExpressions = new List<Regex>();
foreach (string token in tokens)
{
if (token != "")
{
try
{
regularExpressions.Add(new System.Text.RegularExpressions.Regex(token));
}
catch
{
}
}
}
// get the limit
int maximum = -1;
try
{
maximum = System.Convert.ToInt32(displayLimit.Text, 10);
}
catch
{
}
// build the list of items to add
List<ListViewItem> newItems = new List<ListViewItem>();
foreach (string pathName in SolutionOpenPackage.FilePaths.Values)
{
int slash = pathName.LastIndexOf('\\');
if (slash > 0)
{
// separate the file name and the path
string fileNameMixedCase = pathName.Substring(slash + 1);
string fileNameLowerCase = fileNameMixedCase.ToLower();
string path = pathName.Substring(0, slash);
// validate this file matches all search parameters
bool pass = true;
foreach (Regex regex in regularExpressions)
{
if (!regex.IsMatch(fileNameLowerCase))
{
pass = false;
break;
}
}
// add the item to the list if it passed all the search parameters
if (pass && (maximum < 0 || newItems.Count < maximum))
newItems.Add(new ListViewItem(new string[] { fileNameMixedCase, path }));
}
}
matchingFiles.BeginUpdate();
try
{
matchingFiles.Items.Clear();
matchingFiles.SelectedItems.Clear();
numberOfMatchingFiles.Text = newItems.Count.ToString();
matchingFiles.Items.AddRange(newItems.ToArray());
// auto select the first element if any elements passed the search
if (matchingFiles.Items.Count > 0)
{
matchingFiles.Items[0].Selected = true;
matchingFiles.FocusedItem = matchingFiles.Items[0];
}
}
finally
{
matchingFiles.EndUpdate();
}
}
public void OpenSelectedFiles()
{
// open all the selected items
foreach (ListViewItem item in matchingFiles.SelectedItems)
{
// recompile the path and file name to get the path name
string pathName = "";
foreach (ListViewItem.ListViewSubItem piece in item.SubItems)
{
if (pathName.Length == 0)
{
pathName = piece.Text;
}
else
{
pathName = piece.Text + "\\" + pathName;
}
}
SolutionOpenPackage.ApplicationObject.ItemOperations.OpenFile(pathName, EnvDTE.Constants.vsViewKindPrimary);
}
}
#endregion
#region Events
private void searchTokens_TextChanged(object sender, EventArgs e)
{
RebuildListViewItems();
}
private void searchTokens_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData.Equals(System.Windows.Forms.Keys.Down))
{
e.Handled = true;
matchingFiles.Focus();
}
else
{
if (e.KeyData.Equals(System.Windows.Forms.Keys.Enter))
{
e.Handled = true;
Accept();
}
else
{
if (e.KeyData.Equals(System.Windows.Forms.Keys.Escape))
{
e.Handled = true;
Abort();
}
}
}
}
private void openButton_Click(object sender, EventArgs e)
{
Accept();
}
private void cancelButton_Click(object sender, EventArgs e)
{
Abort();
}
private void displayLimit_TextChanged(object sender, EventArgs e)
{
RebuildListViewItems();
}
private void matchingFiles_DoubleClick(object sender, EventArgs e)
{
Accept();
}
private void matchingFiles_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData.Equals(System.Windows.Forms.Keys.Enter))
{
e.Handled = true;
Accept();
}
else
{
if (e.KeyData.Equals(System.Windows.Forms.Keys.Escape))
{
e.Handled = true;
Abort();
}
}
}
#endregion
}
}
# |
Change |
User |
Description |
Committed |
|
#1
|
15412 |
brandon_m_bare |
Adding the latest SolutionOpen code (converted from an AddIn to an extension). |
|
|