namespace SolutionOpen
{
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using Microsoft.Win32;
public class MyTextBox : System.Windows.Forms.TextBox
{
protected override bool IsInputKey(Keys keyData)
{
if (keyData.Equals(System.Windows.Forms.Keys.Down) || keyData.Equals(System.Windows.Forms.Keys.Escape))
return true;
return base.IsInputKey(keyData);
}
}
public class MyListView : System.Windows.Forms.ListView
{
protected override bool IsInputKey(Keys keyData)
{
if (keyData.Equals(System.Windows.Forms.Keys.Escape))
return true;
return base.IsInputKey(keyData);
}
}
partial class ChooseFile
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
public void Show(ref DTE2 i_applicationObject, ref StringCollection i_pathNames)
{
applicationObject = i_applicationObject;
pathNames = i_pathNames;
LoadSearchFromRegistry();
RebuildListViewItems();
ShowDialog();
}
private void LoadSearchFromRegistry()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(registryKeyName);
if (key != null)
{
object value = key.GetValue(registryValueName);
if (value != null)
{
string text = value as string;
if (text != null)
textBox1.Text = text;
}
}
}
private void SaveSearchToRegistry()
{
RegistryKey key = Registry.CurrentUser.CreateSubKey(registryKeyName);
if (key != null)
key.SetValue(registryValueName, textBox1.Text);
}
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyData.Equals(System.Windows.Forms.Keys.Down))
{
listView1.Focus();
e.Handled = true;
}
else
if (e.KeyData.Equals(System.Windows.Forms.Keys.Enter))
OpenSelectedFiles();
else
if (e.KeyData.Equals(System.Windows.Forms.Keys.Escape))
Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
RebuildListViewItems();
}
private void RebuildListViewItems()
{
// clear the file list. we're going to repopulate it based on the contents of the search text box.
listView1.Items.Clear();
listView1.SelectedItems.Clear();
string text = textBox1.Text;
string []tokens = text.Split(' ');
foreach (string pathName in pathNames)
{
int slash = pathName.LastIndexOf('\\');
if (slash > 0)
{
// separate the file name and the path
string fileName = pathName.Substring(slash + 1);
string path = pathName.Substring(0, slash);
// validate this file matches all search parameters
bool pass = true;
foreach (string token in tokens)
{
if (token != "")
{
if (token.StartsWith("/"))
{
if (!fileName.ToLower().Contains(token.Substring(1).ToLower()))
{
pass = false;
break;
}
}
else
{
if (!fileName.StartsWith(token, StringComparison.OrdinalIgnoreCase))
{
pass = false;
break;
}
}
}
}
// add the item to the list if it passed all the search parameters
if (pass)
listView1.Items.Add(new ListViewItem(new String[] { fileName, path }));
}
}
if (listView1.Items.Count > 0)
listView1.Items[0].Selected = true;
}
private void listBox1_DoubleClick(object sender, System.EventArgs e)
{
OpenSelectedFiles();
}
private void listBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyData.Equals(System.Windows.Forms.Keys.Enter))
{
OpenSelectedFiles();
}
else
if (e.KeyData.Equals(System.Windows.Forms.Keys.Escape))
Close();
}
private void button1_Click(object sender, System.EventArgs e)
{
OpenSelectedFiles();
}
private void button2_Click(object sender, System.EventArgs e)
{
Close();
}
private void OpenSelectedFiles()
{
Close();
SaveSearchToRegistry();
// open all the selected items
foreach (ListViewItem item in listView1.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;
}
}
EnvDTE.Window newWindow = applicationObject.ItemOperations.OpenFile(pathName, "");
if (newWindow != null)
newWindow.Activate();
}
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.textBox1 = new SolutionOpen.MyTextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.listView1 = new SolutionOpen.MyListView();
this.FileName = new System.Windows.Forms.ColumnHeader();
this.Path = new System.Windows.Forms.ColumnHeader();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBox1.Location = new System.Drawing.Point(12, 25);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(631, 22);
this.textBox1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(95, 13);
this.label1.TabIndex = 1;
this.label1.Text = "File name to match:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(658, 25);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(69, 22);
this.button1.TabIndex = 2;
this.button1.Text = "Open";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 65);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(71, 13);
this.label2.TabIndex = 4;
this.label2.Text = "Matching files:";
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.FileName,
this.Path});
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.listView1.Location = new System.Drawing.Point(12, 82);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(715, 273);
this.listView1.Sorting = System.Windows.Forms.SortOrder.Ascending;
this.listView1.TabIndex = 1;
this.listView1.View = System.Windows.Forms.View.Details;
//
// FileName
//
this.FileName.Text = "File Name";
this.FileName.Width = 275;
//
// Path
//
this.Path.Text = "Path";
this.Path.Width = 408;
//
// button2
//
this.button2.Location = new System.Drawing.Point(658, 53);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(69, 22);
this.button2.TabIndex = 3;
this.button2.Text = "Cancel";
//
// ChooseFile
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(740, 367);
this.Controls.Add(this.button2);
this.Controls.Add(this.listView1);
this.Controls.Add(this.label2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Name = "ChooseFile";
this.Text = "Choose File";
this.ResumeLayout(false);
this.PerformLayout();
textBox1.TextChanged += new EventHandler(this.textBox1_TextChanged);
textBox1.KeyDown += new KeyEventHandler(this.textBox1_KeyDown);
listView1.DoubleClick += new EventHandler(this.listBox1_DoubleClick);
listView1.KeyDown += new KeyEventHandler(this.listBox1_KeyDown);
listView1.HideSelection = false;
button1.Click += new EventHandler(this.button1_Click);
button2.Click += new EventHandler(this.button2_Click);
}
#endregion
#region Member Variables
private MyTextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label2;
private MyListView listView1;
private System.Windows.Forms.ColumnHeader FileName;
private System.Windows.Forms.ColumnHeader Path;
private StringCollection pathNames;
private DTE2 applicationObject;
private readonly static String registryKeyName = "Software\\JeffGrills\\SolutionOpen";
private readonly static String registryValueName = "LastSearch";
#endregion
}
}