using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
// BaileySoft Super Code Search
// Copyright (c) 2008, BSLTD
// nealbailey@hotmail.com - All rights reserved.
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace BSCodeGrep
{
public partial class Form1 : Form
{
#region Constructors
public Form1()
{
InitializeComponent();
InitializeBackgroundWorker();
_labelpg = label1;
_frm2 = new Form2();
}
#endregion
#region Public Properties
public Label LabelPg
{
get { return _labelpg; }
set { _labelpg = value; }
}
#endregion
#region Private Methods
private FileInfo GetSelectedNodeFile()
{
FileInfo fi = null;
string f;
if (treeView1.SelectedNode.Text.IndexOf(":\\") > -1)
{
f = treeView1.SelectedNode.Text;
}
else
{
f = treeView1.SelectedNode.Parent.Text;
}
if (File.Exists(f))
{
fi = new FileInfo(f);
}
return fi;
}
private string CreateReport()
{
string xfile = Path.GetTempFileName().Replace(".tmp", ".txt");
StreamWriter sw = File.AppendText(xfile);
foreach (TreeNode n in treeView1.Nodes)
{
sw.WriteLine(n.Text);
foreach (TreeNode c in n.Nodes)
{
sw.WriteLine("\t{0}", c.Text);
}
if (!_frm2.Uso.NamesOnly)
sw.WriteLine("");
}
sw.Close();
return xfile;
}
#endregion
#region Threading Events
private void InitializeBackgroundWorker()
{
backgroundWorker1.DoWork += new DoWorkEventHandler(OnDoWorkSearch);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(OnWorkCompletedSearch);
}
private void OnDoWorkSearch(object sender, DoWorkEventArgs e)
{
_mThreadWorker = (IOWorker)e.Argument;
_mThreadWorker.CreateIdx();
}
private void OnWorkCompletedSearch(object sender, RunWorkerCompletedEventArgs e)
{
Form1._sIsRunning = false;
for (int i = 0; i < _mThreadWorker.GetSearchResults.Count; i++)
{
TreeNode tn;
TreeNode stn;
treeView1.Nodes.Add(tn = new TreeNode(_mThreadWorker.GetSearchResults[i].FilePath, 5, 5));
for (int z = 0; z < _mThreadWorker.GetSearchResults[i].Lines.Count; z++)
{
tn.Nodes.Add(stn = new TreeNode(_mThreadWorker.GetSearchResults[i].Lines[z], 7, 7));
stn.BackColor = Color.Yellow;
stn.NodeFont = new Font("Courier New", 10, FontStyle.Italic);
}
}
groupBox2.Enabled = true;
label1.Text = "";
DateTime fdt = DateTime.Now;
searchStatusLabel.Text = String.Format("Found {0} results searching {1} files in {2} seconds",
_mThreadWorker.GetSearchResults.Count,
_mThreadWorker.SearchedCount.ToString(),
(fdt - (DateTime)_sdt).Seconds.ToString());
}
private void NotifyStatus()
{
string[] status = new string[] { "S", "e", "a", "r", "c", "h", "i", "n", "g", ".", ".", "." };
List<string> chars = new List<string>();
chars.AddRange(status);
while (Form1._sIsRunning)
{
for (int i = 0; i < chars.Count; i++)
{
Thread.Sleep(100);
threadStatusLabel.Text += status[i];
}
threadStatusLabel.Text = "";
}
threadStatusLabel.Text = "";
}
private void StartSearchThread()
{
treeView1.Nodes.Clear();
if (String.IsNullOrEmpty(_frm2.Uso.SearchPath)
|| _frm2.Uso.SearchFilters.Length < 1
|| String.IsNullOrEmpty(_frm2.Uso.SearchPattern))
{
searchStatusLabel.Text = "Path, extension, and pattern must be specified; Click the setup button to continue";
return;
}
groupBox2.Enabled = false;
_sdt = DateTime.Now;
searchStatusLabel.Text = "";
Form1._sIsRunning = true;
backgroundWorker1.RunWorkerAsync(new IOWorker(_frm2.Uso, this));
Thread _thread = new Thread(new ThreadStart(NotifyStatus));
_thread.Priority = ThreadPriority.Lowest;
_thread.Start();
}
private void StartReplaceUndoThread()
{
List<string> kfiles = new List<string>();
foreach (TreeNode tn in treeView1.Nodes)
kfiles.Add(tn.Text);
IOWorker wkr = new IOWorker(_frm2.Uso, this);
wkr.Index = kfiles;
wkr.UndoReplace();
}
private void StartReplaceThread()
{
List<string> kfiles = new List<string>();
foreach (TreeNode tn in treeView1.Nodes)
kfiles.Add(tn.Text);
IOWorker wkr = new IOWorker(_frm2.Uso, this);
wkr.Index = kfiles;
wkr.ExecuteReplace();
}
#endregion
#region Control Private Events
//Settings Button
private void button1_Click(object sender, EventArgs e)
{
try
{
_frm2.Show();
}
catch (ObjectDisposedException)
{
_frm2 = new Form2();
_frm2.Show();
}
}
//Go Button - Create index & script
private void button2_Click(object sender, EventArgs e)
{
StartSearchThread();
}
//Export Report
private void button3_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("notepad.exe", CreateReport());
}
//Replace Button
private void button4_Click(object sender, EventArgs e)
{
if (treeView1.Nodes.Count == 0)
{
searchStatusLabel.Text = "You must perform a search before you can replace the results";
return;
}
string msg = String.Format("You are about to replace every instance of '{0}' with '{1}' \n"
+ "This change will effect all {2} files in the search results pane. \n\n"
+ "Are you absolutely certain that you want to do this?",
_frm2.Uso.SearchPattern,
_frm2.Uso.Replacement,
treeView1.Nodes.Count);
DialogResult dgr = MessageBox.Show(msg, "Source Code Grep",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dgr == DialogResult.Yes)
{
Thread _thread = new Thread(new ThreadStart(StartReplaceThread));
_thread.Priority = ThreadPriority.Lowest;
_thread.Name = "ThreadReplace";
_thread.Start();
//groupBox2.Enabled = false;
//groupBox2.Enabled = true;
}
}
//ContextMenu Open
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (treeView1.SelectedNode != null)
{
contextMenuStrip1.Enabled = true;
}
else
{
contextMenuStrip1.Enabled = false;
}
}
//ContextMenu Click Events
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
FileInfo f = GetSelectedNodeFile();
if (f != null)
System.Diagnostics.Process.Start(f.FullName);
}
private void openWithNotepadToolStripMenuItem_Click(object sender, EventArgs e)
{
FileInfo f = GetSelectedNodeFile();
if (f != null)
System.Diagnostics.Process.Start("notepad.exe", f.FullName);
}
private void openContainingFolderToolStripMenuItem_Click(object sender, EventArgs e)
{
FileInfo f = GetSelectedNodeFile();
if (f != null)
System.Diagnostics.Process.Start(f.Directory.FullName);
}
private void openProjectSLNFileToolStripMenuItem_Click(object sender, EventArgs e)
{
FileInfo f = GetSelectedNodeFile();
if (f != null)
{
DirectoryInfo di = new DirectoryInfo(f.Directory.Parent.Parent.FullName);
try
{
string[] slnFiles = Directory.GetFiles(di.FullName, "*.sln", SearchOption.AllDirectories);
if (slnFiles.Length > 0)
{
System.Diagnostics.Process.Start(slnFiles[0]);
}
else
{
MessageBox.Show(" No Visual Studio Solutions Found In\r\n This Directory or 2 Directories Up ",
"Alert", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
catch (UnauthorizedAccessException) { }
}
}
#endregion
#region Main Menu Private Events
private void startSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
button2.PerformClick();
}
private void searchReportToolStripMenuItem_Click(object sender, EventArgs e)
{
string rpt = CreateReport();
saveFileDialog1.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
saveFileDialog1.Title = "Save Report As";
saveFileDialog1.DefaultExt = "txt";
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
DialogResult drg = saveFileDialog1.ShowDialog();
if (drg == DialogResult.OK)
{
if (File.Exists(saveFileDialog1.FileName))
File.Delete(saveFileDialog1.FileName);
File.Move(rpt, saveFileDialog1.FileName);
}
}
private void replaceInFiToolStripMenuItem_Click(object sender, EventArgs e)
{
button4.PerformClick();
}
private void undoLastReplaceToolStripMenuItem_Click(object sender, EventArgs e)
{
string msg = String.Format("You are about to replace the original copies of the files in the search result "
+ "with the backup copies created during the 'replace' action. This will effectively undo the most recent 'replace "
+ "in files' action. \n\n Are you absolutely certain that you want to do this?",
_frm2.Uso.SearchPattern,
_frm2.Uso.Replacement,
treeView1.Nodes.Count);
DialogResult dgr = MessageBox.Show(msg, "Source Code Grep",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dgr == DialogResult.Yes)
{
Thread _thread = new Thread(new ThreadStart(StartReplaceUndoThread));
_thread.Priority = ThreadPriority.Lowest;
_thread.Name = "ThreadReplaceUndo";
_thread.Start();
}
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
button1.PerformClick();
}
private void reportToolStripMenuItem_Click(object sender, EventArgs e)
{
button3.PerformClick();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.codeproject.com/KB/macros/BSCodeGrep.aspx");
}
#endregion
#region Private Fields
private Form2 _frm2 = null;
private IOWorker _mThreadWorker = null;
private DateTime? _sdt = null;
private static bool _sIsRunning = false;
private Label _labelpg;
#endregion
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 ab = new AboutBox1();
ab.ShowDialog();
}
}
}