Index: /branches/eraser6/CodeReview/Eraser/LogForm.cs
===================================================================
--- /branches/eraser6/CodeReview/Eraser/LogForm.cs	(revision 1525)
+++ /branches/eraser6/CodeReview/Eraser/LogForm.cs	(revision 1526)
@@ -27,9 +27,9 @@
 using System.Text;
 using System.Windows.Forms;
+using System.Globalization;
+using System.IO;
 
 using Eraser.Manager;
-using System.Globalization;
 using Eraser.Util;
-using System.IO;
 
 namespace Eraser
@@ -56,16 +56,17 @@
 
 			//Display the log entries
-			this.task = task;
+			Task = task;
 			RefreshMessages();
 			EnableButtons();
 
 			//Register our event handler to get live log messages
-			task.Log.Logged += task_Logged;
-			task.Log.NewSession += task_NewSession;
+			Task.Log.Logged += task_Logged;
+			Task.Log.NewSession += task_NewSession;
 		}
 
 		private void LogForm_FormClosed(object sender, FormClosedEventArgs e)
 		{
-			task.Log.Logged -= task_Logged;
+			Task.Log.NewSession -= task_NewSession;
+			Task.Log.Logged -= task_Logged;
 		}
 
@@ -83,5 +84,5 @@
 			}
 
-			filterSessionCombobox.Items.Add(task.Log.LastSession);
+			filterSessionCombobox.Items.Add(Task.Log.LastSession);
 		}
 
@@ -98,5 +99,5 @@
 			//display this entry when the session in question is the last one.
 			if (filterSessionCombobox.SelectedItem == null ||
-				(DateTime)filterSessionCombobox.SelectedItem != task.Log.LastSession ||
+				(DateTime)filterSessionCombobox.SelectedItem != Task.Log.LastSession ||
 				!MeetsCriteria(e.LogEntry))
 			{
@@ -105,5 +106,5 @@
 
 			//Add it to the cache and increase our virtual list size.
-			entryCache.Add(e.LogEntry);
+			EntryCache.Add(e.LogEntry);
 			++log.VirtualListSize;
 
@@ -114,5 +115,5 @@
 		private void log_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
 		{
-			LogEntry entry = entryCache[e.ItemIndex];
+			LogEntry entry = EntryCache[e.ItemIndex];
 			e.Item = new ListViewItem(entry.Timestamp.ToString("F", CultureInfo.CurrentCulture));
 			e.Item.SubItems.Add(entry.Level.ToString());
@@ -137,10 +138,9 @@
 			if (e.IsSelected)
 			{
-				if (!selectedEntries.ContainsKey(e.ItemIndex))
-					selectedEntries.Add(e.ItemIndex, null);
+				SelectedEntries.Add(e.ItemIndex, EntryCache[e.ItemIndex]);
 			}
 			else
 			{
-				selectedEntries.Remove(e.ItemIndex);
+				SelectedEntries.Remove(e.ItemIndex);
 			}
 		}
@@ -152,10 +152,9 @@
 				if (e.IsSelected)
 				{
-					if (!selectedEntries.ContainsKey(i))
-						selectedEntries.Add(i, null);
+					SelectedEntries.Add(i, EntryCache[i]);
 				}
 				else
 				{
-					selectedEntries.Remove(i);
+					SelectedEntries.Remove(i);
 				}
 			}
@@ -164,22 +163,11 @@
 		private void log_ItemActivate(object sender, EventArgs e)
 		{
-			if (selectedEntries.Count < 1)
-				return;
-
-			int currentEntryIndex = 0;
-			LogEntry selectedEntry = new LogEntry();
-			foreach (LogEntry entry in task.Log.Entries[(DateTime)filterSessionCombobox.SelectedItem])
-			{
-				//Only copy entries which meet the display criteria and that they are selected
-				if (!MeetsCriteria(entry))
-					continue;
-				if (!selectedEntries.ContainsKey(currentEntryIndex++))
-					continue;
-
-				selectedEntry = entry;
-				break;
-			}
-
-			//Decide on the icon
+			if (SelectedEntries.Count < 1)
+				return;
+
+			//Get the selected entry from the entry cache.
+			LogEntry selectedEntry = SelectedEntries.Values[0];
+
+			//Decide on the icon.
 			MessageBoxIcon icon = MessageBoxIcon.None;
 			switch (selectedEntry.Level)
@@ -205,5 +193,5 @@
 		private void logContextMenuStrip_Opening(object sender, CancelEventArgs e)
 		{
-			copySelectedEntriesToolStripMenuItem.Enabled = selectedEntries.Count != 0;
+			copySelectedEntriesToolStripMenuItem.Enabled = SelectedEntries.Count != 0;
 		}
 
@@ -211,10 +199,10 @@
 		{
 			//Ensure we've got stuff to copy.
-			if (selectedEntries.Count == 0)
+			if (SelectedEntries.Count == 0)
 				return;
 
 			StringBuilder csvText = new StringBuilder();
 			StringBuilder rawText = new StringBuilder();
-			LogSessionDictionary logEntries = task.Log.Entries;
+			LogSessionDictionary logEntries = Task.Log.Entries;
 
 			DateTime sessionTime = (DateTime)filterSessionCombobox.SelectedItem;
@@ -222,13 +210,7 @@
 			rawText.AppendLine(S._("Session: {0:F}", sessionTime));
 
-			int currentEntryIndex = 0;
-			foreach (LogEntry entry in logEntries[sessionTime])
-			{
-				//Only copy entries which meet the display criteria and that they are selected
-				if (!MeetsCriteria(entry))
-					continue;
-				if (!selectedEntries.ContainsKey(currentEntryIndex++))
-					continue;
-
+			foreach (LogEntry entry in SelectedEntries.Values)
+			{
+				//Append the entry's contents to our buffer.
 				string timeStamp = entry.Timestamp.ToString("F", CultureInfo.CurrentCulture);
 				string message = entry.Message;
@@ -259,5 +241,5 @@
 		{
 			//Clear the backing store
-			task.Log.Clear();
+			Task.Log.Clear();
 
 			//Reset the list of sessions
@@ -266,6 +248,6 @@
 			//And reset the list-view control
 			log.VirtualListSize = 0;
-			selectedEntries.Clear();
-			entryCache.Clear();
+			SelectedEntries.Clear();
+			EntryCache.Clear();
 
 			//Finally update the UI state.
@@ -314,11 +296,11 @@
 		{
 			//Check if we have a task
-			if (task == null)
+			if (Task == null)
 				return;
 
 			Application.UseWaitCursor = true;
-			LogSessionDictionary log = task.Log.Entries;
-			entryCache.Clear();
-			selectedEntries.Clear();
+			LogSessionDictionary log = Task.Log.Entries;
+			EntryCache.Clear();
+			SelectedEntries.Clear();
 
 			//Iterate over every key
@@ -334,10 +316,10 @@
 					//Check if the entry meets the criteria for viewing
 					if (MeetsCriteria(entry))
-						entryCache.Add(entry);
+						EntryCache.Add(entry);
 				}
 			}
 
 			//Set the list view size and update all the control states
-			this.log.VirtualListSize = entryCache.Count;
+			this.log.VirtualListSize = EntryCache.Count;
 			this.log.Refresh();
 			EnableButtons();
@@ -350,5 +332,5 @@
 		private void EnableButtons()
 		{
-			clear.Enabled = task.Log.Entries.Count > 0;
+			clear.Enabled = Task.Log.Entries.Count > 0;
 		}
 
@@ -356,15 +338,16 @@
 		/// The task which this log is displaying entries for
 		/// </summary>
-		private Task task;
+		private Task Task;
 
 		/// <summary>
 		/// Stores all log entries fulfilling the current criteria for rapid access.
 		/// </summary>
-		private List<LogEntry> entryCache = new List<LogEntry>();
-
-		/// <summary>
-		/// Stores all currently selected list view entry indices.
-		/// </summary>
-		private SortedList<int, object> selectedEntries = new SortedList<int, object>();
+		private List<LogEntry> EntryCache = new List<LogEntry>();
+
+		/// <summary>
+		/// Stores all currently selected list view entry indices. The key is the
+		/// index which is selected.
+		/// </summary>
+		private SortedList<int, LogEntry> SelectedEntries = new SortedList<int, LogEntry>();
 	}
 }
