Click here to Skip to main content
15,891,204 members
Articles / Desktop Programming / WPF

Zip My Code

Rate me:
Please Sign up or sign in to vote.
4.78/5 (17 votes)
20 Dec 2009CPOL3 min read 71.9K   2K   48  
A utility stripping your source code to the essential core and then compressing it to a nice CodeProject article attachment.
using System;
using ZipMyCode.Properties;
using ZipMyCode.Service.Tasks;
using ZipMyCode.UI.Mvvm;

namespace ZipMyCode.UI.Task
{
	/// <summary>
	/// Class acting as ViewModel for a ITask.
	/// </summary>
	class TaskViewModel : ViewModelBase
	{
		private readonly ITask task;


		public TaskViewModel(ITask task)
		{
			if (task == null) throw new ArgumentNullException("task");

			this.task = task;
		}


		/// <summary>
		/// Gets the name of the task.
		/// </summary>
		public string Name
		{
			get { return task.Name; }
		}


		/// <summary>
		/// Gets or sets the status of the task.
		/// </summary>
		public TaskStatus Status
		{
			get { return Property(() => Status); }
			set
			{
				Property(() => Status, value);

				// Trigger depending properties
				OnPropertyChanged(() => StatusText);
				OnPropertyChanged(() => StatusIcon);
			}
		}


		/// <summary>
		/// Gets the status text.
		/// </summary>
		public string StatusText
		{
			get
			{
				switch (Status)
				{
					case TaskStatus.Idle:
						return Resources.TaskViewModel_Idle;

					case TaskStatus.Running:
						return Resources.TaskViewModel_Running;

					case TaskStatus.Finished:
						return Resources.TaskViewModel_Successful;

					case TaskStatus.FinishedWithError:
						return Resources.TaskViewModel_Unsuccessful;

					case TaskStatus.Skipped:
						return Resources.TaskViewModel_Skipped;

					default:
						throw new InvalidOperationException("Unsupported status: " + Status);
				}
			}
		}


		/// <summary>
		/// Gets the status icon.
		/// </summary>
		public Uri StatusIcon
		{
			get
			{
				switch (Status)
				{
					case TaskStatus.Idle:
						return new Uri("pack://application:,,,/ZipMyCode;component/Resources/TaskIdle.png");

					case TaskStatus.Running:
						return new Uri("pack://application:,,,/ZipMyCode;component/Resources/TaskRunning.png");

					case TaskStatus.Finished:
						return new Uri("pack://application:,,,/ZipMyCode;component/Resources/TaskSuccessful.png");

					case TaskStatus.FinishedWithError:
						return new Uri("pack://application:,,,/ZipMyCode;component/Resources/TaskUnsuccessful.png");

					case TaskStatus.Skipped:
						return new Uri("pack://application:,,,/ZipMyCode;component/Resources/TaskSkipped.png");

					default:
						throw new InvalidOperationException("Unsupported status: " + Status);
				}
			}
		}


		/// <summary>
		/// Runs the task.
		/// </summary>
		/// <returns>true if task was successful; otherwise false.</returns>
		public bool Run()
		{
			// Set "task is running" status
			Status = TaskStatus.Running;

			// Set task outcome
			Status = task.Run();

			return Status == TaskStatus.Finished;
		}
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Axis Communications
Sweden Sweden
Got my first computer in the 90's and loved it even though it sounded like a coffeemaker.

Now getting paid for designing cool applications, and drinks the coffee instead of listening to it being made.

Comments and Discussions