#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2007 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Tracking {
/// Helps tracking the progress of one or more progressions
///
///
/// This is useful if you want to display a progress bar for multiple
/// progressions but can not guarantee that no additional progressions
/// will appear inmidst of execution.
///
///
/// This class does not implement the IProgression interface itself in
/// order to not violate the design principles of progressions which
/// guarantee that a progression will only finish once (whereas the
/// progression tracking might finish any number of times).
///
///
public class ProgressionTracker : IDisposable {
/// Triggered when the idle state of the tracker changes
///
/// The tracker is idle when no progressions are being tracked in it. If you're
/// using this class to feed a progress bar, this would be the event to use for
/// showing or hiding the progress bar. The tracker starts off as idle because,
/// upon construction, its list of progressions will be empty.
///
public event EventHandler AsyncIdleStateChanged;
/// Triggered when the total progress has changed
public event EventHandler AsyncProgressUpdated;
/// Immediately releases all resources owned by the instance
public void Dispose() {
// TODO: Untrack all
}
/// Begins tracking the specified progression
/// Progression to be tracked
public void Track(Progression progression) {
Track(progression, 1.0f);
}
/// Begins tracking the specified progression
/// Progression to be tracked
/// Weight to assign to this progression
public void Track(Progression progression, float weight) {
this.trackedProgressions.Add(
new WeightedProgression(progression, weight)
);
this.totalWeight += weight;
recalculateProgress();
}
/// Stops tracking the specified progression
/// Progression to stop tracking of
public void Untrack(Progression progression) { }
/// Fires the AsyncIdleStateChanged event
/// New idle state to report
protected virtual void OnAsyncIdleStateChanged(bool idle) {
EventHandler copy = AsyncIdleStateChanged;
if(copy != null)
copy(this, new IdleStateEventArgs(idle));
}
/// Fires the AsyncProgressUpdated event
/// New progress to report
protected virtual void OnAsyncProgressUpdated(float progress) {
EventHandler copy = AsyncProgressUpdated;
if(copy != null)
copy(this, new ProgressUpdateEventArgs(progress));
}
/// Recalculates the total progress of the tracker
private void recalculateProgress() {
float totalProgress;
for(int index = 0; index < trackedProgressions.Count; ++index) {
float weight = this.trackedProgressions[index].WeightedProgression;
totalProgress = this.trackedProgressions[index].Progress * weight;
}
totalProgress /= this.totalWeight;
//OnAsyncProgressUpdated(
}
/// Total weight of all progressions being tracked
private float totalWeight;
/// Progressions being tracked by this tracker
private List> trackedProgressions;
}
} // namespace Nuclex.Support.Tracking