Click here to Skip to main content
15,895,740 members
Articles / Programming Languages / C#

Simple Source Line Counter in C# for C#

Rate me:
Please Sign up or sign in to vote.
4.25/5 (28 votes)
24 Jan 20053 min read 102.7K   3.4K   28  
A simple source code line counter written in C#.
using System;
using blix;

namespace CSharpLineCounter
{
	public class LineCounter
	{
		#region Fields
		private ApplicationForm _Form;
		private CodeFileModel _Model;
		private DirectoryPicker _DirectoryPicker;
		private CountDisplay _CountDisplay;
		private CodeFileDisplay _CodeFileDisplay;
		private ContainerCell _DirectoryPickerCell;
		private ContainerCell _CounterCell;
		private ContainerCell _CodeFileCell;
		private string _ExecutableDirectory;
		private LineCounterState _LineCounterState;
		#endregion

		#region LineCounter(aApplicationForm)
		public LineCounter(ApplicationForm aApplicationForm)
		{
			_Model = new CodeFileModel();

			Simpleton simpleton = new Simpleton();

			simpleton.CountBlankLines = false;
			simpleton.CountMultiLineComments = false;
			simpleton.CountRegions = false;
			simpleton.CountSingleLineComments = false;
			simpleton.CountXMLSummaries = false;

			_Model.Brain = simpleton;


			_Form = aApplicationForm;
			_Form.ControlOverlay = CreateGridBag();

			_DirectoryPicker = new DirectoryPicker(_DirectoryPickerCell);

			_CountDisplay = new CountDisplay(_CounterCell);

			_CodeFileDisplay = new CodeFileDisplay(_CodeFileCell, _Model);

			Hook();

			ReadDirectory();

			RestoreFormSizeAndPosition();
		}
		#endregion

		#region CreateGridBag
		private GridBag CreateGridBag()
		{
			GridBag gridBag = new GridBag();

			_DirectoryPickerCell = (ContainerCell) gridBag.WorkspaceCell.Divide("DirectoryPicker", typeof(ContainerCell), CellSide.Top, 50);
			_CounterCell = (ContainerCell) gridBag.WorkspaceCell.Divide("Counter", typeof(ContainerCell), CellSide.Top, 40);
			_CodeFileCell = gridBag.WorkspaceCell;

			return gridBag;
		}
		#endregion
		#region Hook
		private void Hook()
		{
			_Form.WindowsForm.SizeChanged += new EventHandler(WindowsFormSizeChangedHandler);
			_Form.WindowsForm.LocationChanged += new EventHandler(WindowsFormLocationChangedHandler);

			_Model.SourceFileFound += new ModelSourceFileFoundDelegate(ModelSourceFileFoundHandler);
			
			_DirectoryPicker.Picked += new DirectoryPickerPickedDelegate(DirectoryPickerPickedHandler);
		
			_CountDisplay.Count += new CountDisplayCountDelegate(CountDisplayCountHandler);
		}
		#endregion
		#region ReadDirectory
		public void ReadDirectory()
		{
			string executablePath = System.Windows.Forms.Application.ExecutablePath;

			_ExecutableDirectory = System.IO.Path.GetFullPath(executablePath);

			_ExecutableDirectory = System.IO.Path.GetDirectoryName(_ExecutableDirectory);

			_LineCounterState = Memory.Deserialize(_ExecutableDirectory);
			
			if ((_LineCounterState != null) && (System.IO.Directory.Exists(_LineCounterState.Directory)))
			{
				_DirectoryPicker.Directory = _LineCounterState.Directory;
			}
			else
			{
				_DirectoryPicker.Directory = "Select a folder";

				_LineCounterState = new LineCounterState();

				_LineCounterState.Directory = _DirectoryPicker.Directory;

				_LineCounterState.FormLocation = _Form.Bounds.Location;
				_LineCounterState.FormSize = _Form.Bounds.Size;

				_CountDisplay.GoButton.Enabled = false;
			}
		}
		#endregion
		#region RestoreFormSizeAndPosition
		public void RestoreFormSizeAndPosition()
		{
			System.Drawing.Rectangle workingArea = System.Windows.Forms.SystemInformation.WorkingArea;

			System.Drawing.Rectangle formArea = new System.Drawing.Rectangle(_LineCounterState.FormLocation, _LineCounterState.FormSize);
		
			if ((formArea.Right <= workingArea.Right) && (formArea.Bottom <= workingArea.Bottom))
			{
				_Form.Bounds.Location = formArea.Location;
				_Form.Bounds.Size = formArea.Size;
			}
		}
		#endregion

		#region DirectoryPickerPickedHandler
		private void DirectoryPickerPickedHandler()
		{
			_CountDisplay.GoButton.Enabled = true;

			_LineCounterState.Directory = _DirectoryPicker.Directory;

			_CodeFileDisplay.Clear();
			_CountDisplay.Clear();

			_Form.PaintDeck.Lock(this);
			_Form.Cursor = blix.Cursor.WaitCursor;

			try
			{
				_Model.Clear();

				Memory.Serialize(_ExecutableDirectory, _LineCounterState);
			}
			finally
			{
				_Form.PaintDeck.Unlock(this);

				_Form.Refresh();

				_Form.Cursor = blix.Cursor.Default;
			}
		}
		#endregion
		#region CountDisplayCountHandler
		private void CountDisplayCountHandler()
		{
			_Form.PaintDeck.Lock(this);
			_Form.Cursor = blix.Cursor.WaitCursor;

			try
			{
				_Model.Clear();

				_Model.Populate(_DirectoryPicker.Directory);

				_CountDisplay.Update(_Model);
			}
			finally
			{
				_Form.PaintDeck.Unlock(this);

				_Form.Refresh();

				_Form.Cursor = blix.Cursor.Default;
			}
		}
		#endregion
		#region ModelSourceFileFoundHandler
		private void ModelSourceFileFoundHandler(FileStat aFileStat)
		{
			_Form.PaintDeck.Unlock(this);

			try
			{
				_CountDisplay.Update(_Model);
			}
			finally
			{
				_Form.PaintDeck.Lock(this);
			}
		}
		#endregion

		#region WindowsFormLocationChangedHandler
		private void WindowsFormLocationChangedHandler(object aSender, EventArgs ea)
		{
			_LineCounterState.FormLocation = _Form.Bounds.Location;

			Memory.Serialize(_ExecutableDirectory, _LineCounterState);
		}
		#endregion
		#region WindowsFormSizeChangedHandler
		private void WindowsFormSizeChangedHandler(object aSender, EventArgs ea)
		{
			_LineCounterState.FormSize = _Form.Bounds.Size;

			Memory.Serialize(_ExecutableDirectory, _LineCounterState);
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO Sagerion, LLC
United States United States
I read About Face by Alan Cooper in 1995 and immediately recognized it as a founding document for the future of software. I also recognized we had a long, long way to go - and yes, even with the advent of iOS, we are still not there yet.

At my company, Sagerion (say-jair-ee-on), we can take a look at your planned or existing software and suggest ways of making it better - lots better. We can develop down-to-the-pixel blueprints showing exactly what our suggestions mean. We can help manage on-going development to make sure the top-notch user-experience we've suggested really does get built. Now, honestly, how often have you ever seen all those things happen?

You may or may not already have great development going on - but what does that matter if you don't have great design driving it?

Feel free to contact me at tom@sagerion.com, I would love to hear about your next ground-breaking project.

Comments and Discussions