/*
* 2006.6.27
* getKeydList
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
using IReaper.FileData;
using IReaper;
namespace IReaper.CourseData{
public delegate void NotifyBindingListItemRemoveEventHandler(int index);
public class CourseCollection : SortableBindingList<Course>
{
public event NotifyBindingListItemRemoveEventHandler NotifyRemove;
static TimeComparer compare = new TimeComparer();
CourseCollectionType type;
Dictionary<string, Course> dic;
bool isAutoAdded;
/// <summary>
///
/// </summary>
public CourseCollection(CourseCollectionType Type)
{
dic = new Dictionary<string, Course>();
type = Type;
isAutoAdded = true;
CourseFileData.StatueChanged += new FileDataStatueChangedEventHandler(CourseFileData_StatueChange);
Course.OnCourseUpdated += new CourseUpdatedEventHandler(Course_OnCourseUpdated);
}
/// <summary>
/// CourseID
/// </summary>
/// <returns></returns>
public Dictionary<string, Course> KeydList
{
get { return dic; }
}
/// <summary>
/// (Common)(Completed)
/// </summary>
public CourseCollectionType Type
{
get { return type; }
set { type = value; }
}
/// <summary>
///
/// </summary>
/// <param name="course"></param>
public new void Add(Course course)
{
if (this.Contains(course))
return;
dic.Add(course.Id, course);
base.Add(course);
}
/// <summary>
/// ID
/// </summary>
/// <param name="course"></param>
/// <returns></returns>
public new bool Contains(Course course)
{
if (base.Contains(course))
return true;
return dic.ContainsKey(course.Id);
}
/// <summary>
/// CoursesCollection
/// </summary>
/// <returns></returns>
public virtual CourseCollection GetTypedCoursesCollection()
{
CourseCollection courses = new CourseCollection(this.type);
courses.isAutoAdded = false;
return courses;
}
/// <summary>
/// Course
/// </summary>
public new void Clear()
{
base.Clear();
base.OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted,-1));
if (this.dic != null)
this.dic.Clear();
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
protected override void RemoveItem(int index)
{
if (NotifyRemove != null)
NotifyRemove(index);
Course c = this[index];
dic.Remove(c.Id);
base.RemoveItem(index);
}
[MethodImpl(MethodImplOptions.Synchronized)]
private void Course_OnCourseUpdated(Course course)
{
int index = -1;
if ((index = this.IndexOf(course)) >= 0)
{
this.ResetItem(index);
}
}
/// <summary>
/// CourseFileData
/// </summary>
/// <param name="Data"></param>
[MethodImpl(MethodImplOptions.Synchronized)]
private void CourseFileData_StatueChange(CourseFileData Data)
{
int index = this.IndexOf(Data.Owner);
bool ppt, video, code, qa, wmv, mp3, mp4,completed, idle;
ppt = CourseFileData.IsCourseFileFinished(Data.Owner.PPT);
video = CourseFileData.IsCourseFileFinished(Data.Owner.Video);
code = CourseFileData.IsCourseFileFinished(Data.Owner.Code);
qa = CourseFileData.IsCourseFileFinished(Data.Owner.QA);
wmv = CourseFileData.IsCourseFileFinished(Data.Owner.WMV);
mp3 = CourseFileData.IsCourseFileFinished(Data.Owner.MP3);
mp4 = CourseFileData.IsCourseFileFinished(Data.Owner.MP4);
completed = CourseFileData.IsCourseFileFinished(Data);
idle = Data.RunState == RunningStatue.Created;
if (index >= 0)//
{
if (!ppt && !video && !code && !qa && !mp3 && !mp4 && !wmv && type == CourseCollectionType.Completed) //
{
this.RemoveItem(index);
return;
}
//
else if (completed || idle)//
{
this.ResetItem(index);
return;
}
}
else if (isAutoAdded && completed && type == CourseCollectionType.Completed)
{
this.Add(Data.Owner);//
}
}
/// <summary>
///
/// </summary>
public void SortByTime()
{
List<Course> list = this.Items as List<Course>;
list.Sort(compare);
base.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset,-1));
}
}
class TimeComparer : IComparer<Course>
{
#region IComparer<Course>
public int Compare(Course x, Course y)
{
if (x.Time < y.Time)
return 1;
else if (x.Time == y.Time)
return 0;
else
return -1;
}
#endregion
}
/// <summary>
/// CourseCollection
/// CourseCollection
///
/// </summary>
public enum CourseCollectionType
{
/// <summary>
///
/// </summary>
Completed,
/// <summary>
///
/// </summary>
Common,
}
}
|