#if FRAMEWORK20 using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Windows.Forms; namespace DevComponents.DotNetBar.Schedule { public class TimeIndicatorCollection : Collection, IDisposable { #region Events /// /// Occurs when the TimeIndicator collection has changed /// [Description("Occurs when the TimeIndicator collection has changed.")] public event EventHandler TimeIndicatorCollectionChanged; /// /// Occurs when a TimeIndicator time has changed /// [Description("Occurs when a TimeIndicator time has changed.")] public event EventHandler TimeIndicatorTimeChanged; /// /// Occurs when a TimeIndicator Color has changed /// [Description("Occurs when a TimeIndicator Color has changed.")] public event EventHandler TimeIndicatorColorChanged; #endregion #region Private variables private int _UpdateCount; private Timer _Timer; #endregion #region AddRange /// /// Adds a range of TimeIndicators to the collection /// /// Array of items to add public void AddRange(TimeIndicator[] items) { try { BeginUpdate(); for (int i = 0; i < items.Length; i++) Add(items[i]); } finally { EndUpdate(); } } #endregion #region RemoveItem /// /// Processes list RemoveItem calls /// /// Index to remove protected override void RemoveItem(int index) { if (Items[index].IsProtected == false) { Items[index].TimeIndicatorChanged -= IndicatorCollectionChanged; Items[index].TimeIndicatorTimeChanged -= IndicatorTimeChanged; Items[index].TimeIndicatorColorChanged -= IndicatorColorChanged; base.RemoveItem(index); OnCollectionChanged(); } } #endregion #region InsertItem /// /// Processes list InsertItem calls /// /// Index to add /// TimeIndicator to add protected override void InsertItem(int index, TimeIndicator item) { if (item != null) { item.TimeIndicatorChanged += IndicatorCollectionChanged; item.TimeIndicatorTimeChanged += IndicatorTimeChanged; item.TimeIndicatorColorChanged += IndicatorColorChanged; base.InsertItem(index, item); OnCollectionChanged(); } } #endregion #region SetItem /// /// Processes list SetItem calls (e.g. replace) /// /// Index to replace /// TimeIndicator to replace protected override void SetItem(int index, TimeIndicator newItem) { if (Items[index].IsProtected == false) { if (newItem != null) { Items[index].TimeIndicatorChanged -= IndicatorCollectionChanged; Items[index].TimeIndicatorTimeChanged -= IndicatorTimeChanged; Items[index].TimeIndicatorColorChanged -= IndicatorColorChanged; newItem.TimeIndicatorChanged += IndicatorCollectionChanged; newItem.TimeIndicatorTimeChanged += IndicatorTimeChanged; newItem.TimeIndicatorColorChanged += IndicatorColorChanged; base.SetItem(index, newItem); OnCollectionChanged(); } } } #endregion #region ClearItems /// /// Processes list Clear calls (e.g. remove all) /// protected override void ClearItems() { try { BeginUpdate(); for (int i = Count - 1; i>=0 ; i--) { if (Items[i].IsProtected == false) { Items[i].TimeIndicatorChanged -= IndicatorCollectionChanged; Items[i].TimeIndicatorTimeChanged -= IndicatorTimeChanged; Items[i].TimeIndicatorColorChanged -= IndicatorColorChanged; RemoveAt(i); } } } finally { EndUpdate(); } } #endregion #region Events #region IndicatorCollectionChanged /// /// IndicatorCollectionChanged /// /// /// void IndicatorCollectionChanged(object sender, EventArgs e) { OnCollectionChanged(); } #endregion #region IndicatorColorChanged /// /// IndicatorColorChanged /// /// /// void IndicatorColorChanged(object sender, TimeIndicatorColorChangedEventArgs e) { OnTimeIndicatorColorChanged(e); } #endregion #region IndicatorTimeChanged /// /// IndicatorTimeChanged /// /// /// void IndicatorTimeChanged(object sender, TimeIndicatorTimeChangedEventArgs e) { OnTimeIndicatorTimeChanged(e); } #endregion #endregion #region OnCollectionChanged /// /// Propagates TimeIndicatorCollectionChanged events /// protected virtual void OnCollectionChanged() { if (_UpdateCount == 0) { UpdateTimerUse(); if (TimeIndicatorCollectionChanged != null) TimeIndicatorCollectionChanged(this, EventArgs.Empty); } } #endregion #region OnTimeIndicatorColorChanged /// /// Propagates OnTimeIndicatorColorChanged events /// /// protected virtual void OnTimeIndicatorColorChanged(TimeIndicatorColorChangedEventArgs e) { if (_UpdateCount == 0) { if (TimeIndicatorColorChanged != null) TimeIndicatorColorChanged(this, e); } } #endregion #region OnTimeIndicatorTimeChanged /// /// Propagates OnTimeIndicatorTimeChanged events /// /// protected virtual void OnTimeIndicatorTimeChanged(TimeIndicatorTimeChangedEventArgs e) { if (_UpdateCount == 0) { if (TimeIndicatorTimeChanged != null) TimeIndicatorTimeChanged(this, e); } } #endregion #region System Timer support #region UpdateTimerUse /// /// Updates our system timer use /// private void UpdateTimerUse() { // If we need a timer, then allocate it // and initialize it to fire approx every minute if (TimerNeeded() == true) { if (_Timer == null) { _Timer = new Timer(); _Timer.Tick += Timer_Tick; DateTime now = DateTime.Now; _Timer.Interval = (60 - now.Second) * 1000 + (1050 - now.Millisecond); _Timer.Enabled = true; } } else { if (_Timer != null) { _Timer.Enabled = false; _Timer.Tick -= Timer_Tick; _Timer.Dispose(); _Timer = null; } } } #endregion #region TimerNeeded /// /// Determines if a system timer is needed /// /// true if needed private bool TimerNeeded() { for (int i = 0; i < Items.Count; i++) { TimeIndicator ti = Items[i]; if (ti.IsDesignMode == true) return (false); if (ti.Enabled == true && ti.IndicatorSource == eTimeIndicatorSource.SystemTime) { return (true); } } return (false); } #endregion #region Timer_Tick /// /// Handles our timer tick events /// /// /// void Timer_Tick(object sender, EventArgs e) { DateTime now = DateTime.Now; _Timer.Interval = (60 - now.Second) * 1000 + (1050 - now.Millisecond); for (int i = 0; i < Items.Count; i++) { TimeIndicator ti = Items[i]; if (ti.Enabled == true && ti.IndicatorSource == eTimeIndicatorSource.SystemTime) { ti.IndicatorTime = now; } } } #endregion #endregion #region Begin/EndUpdate /// /// Begins Update block /// public void BeginUpdate() { _UpdateCount++; } /// /// Ends update block /// public void EndUpdate() { if (_UpdateCount == 0) { throw new InvalidOperationException( "EndUpdate must be called After BeginUpdate"); } _UpdateCount--; if (_UpdateCount == 0) OnCollectionChanged(); } #endregion #region IDisposable public void Dispose() { for (int i = Count - 1; i >= 0; i--) { Items[i].TimeIndicatorChanged -= IndicatorCollectionChanged; Items[i].TimeIndicatorTimeChanged -= IndicatorTimeChanged; Items[i].TimeIndicatorColorChanged -= IndicatorColorChanged; } } #endregion } #region TimeIndicatorTimeChangedEventArgs /// /// TimeIndicatorTimeChangedEventArgs /// public class TimeIndicatorTimeChangedEventArgs : EventArgs { #region Private variables private TimeIndicator _TimeIndicator; private DateTime _OldTime; private DateTime _NewTime; #endregion public TimeIndicatorTimeChangedEventArgs( TimeIndicator timeIndicator, DateTime oldTime, DateTime newTime) { _TimeIndicator = timeIndicator; _OldTime = oldTime; _NewTime = newTime; } #region Public properties /// /// Gets the TimeIndicator being affected /// public TimeIndicator TimeIndicator { get { return (_TimeIndicator); } } /// /// Gets the old DateTime /// public DateTime OldTime { get { return (_OldTime); } } /// /// Gets the new DateTime /// public DateTime NewTime { get { return (_NewTime); } } #endregion } #endregion #region TimeIndicatorColorChangedEventArgs /// /// TimeIndicatorColorChangedEventArgs /// public class TimeIndicatorColorChangedEventArgs : EventArgs { #region Private variables private TimeIndicator _TimeIndicator; #endregion public TimeIndicatorColorChangedEventArgs(TimeIndicator timeIndicator) { _TimeIndicator = timeIndicator; } #region Public properties /// /// Gets the TimeIndicator being affected /// public TimeIndicator TimeIndicator { get { return (_TimeIndicator); } } #endregion } #endregion } #endif