Began implementing the ProgressionTracker

git-svn-id: file:///srv/devel/repo-conversion/nusu@34 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2007-07-05 20:02:02 +00:00
parent 46c0ac68af
commit 344e5fac53
11 changed files with 363 additions and 66 deletions

View file

@ -0,0 +1,45 @@
#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 {
/// <summary>Event arguments for an idle state change notification</summary>
public class IdleStateEventArgs : EventArgs {
/// <summary>Initializes the idle state change notification</summary>
/// <param name="idle">The new idle state</param>
public IdleStateEventArgs(bool idle) {
this.idle = idle;
}
/// <summary>Current idle state</summary>
public bool Idle {
get { return this.idle; }
}
/// <summary>Current idle state</summary>
private bool idle;
}
} // namespace Nuclex.Support.Tracking

View file

@ -27,7 +27,7 @@ namespace Nuclex.Support.Tracking {
/// <typeparam name="ProgressionType">
/// Type of the progression that is being observed
/// </typeparam>
internal class ObservedProgression<ProgressionType> : IDisposable
internal class ObservedWeightedProgression<ProgressionType> : IDisposable
where ProgressionType : Progression {
/// <summary>Delegate for reporting progress updates</summary>
@ -41,7 +41,7 @@ namespace Nuclex.Support.Tracking {
/// <param name="endedCallback">
/// Callback to invoke when the progression has ended
/// </param>
internal ObservedProgression(
internal ObservedWeightedProgression(
WeightedProgression<ProgressionType> weightedProgression,
ReportDelegate progressUpdateCallback,
ReportDelegate endedCallback

View file

@ -48,14 +48,14 @@ namespace Nuclex.Support.Tracking {
/// </remarks>
internal class WeightedProgressionWrapperCollection<ProgressionType> :
TransformingReadOnlyCollection<
ObservedProgression<ProgressionType>, WeightedProgression<ProgressionType>
ObservedWeightedProgression<ProgressionType>, WeightedProgression<ProgressionType>
>
where ProgressionType : Progression {
/// <summary>Initializes a new weighted progression collection wrapper</summary>
/// <param name="items">Items to be exposed as weighted progressions</param>
internal WeightedProgressionWrapperCollection(
IList<ObservedProgression<ProgressionType>> items
IList<ObservedWeightedProgression<ProgressionType>> items
)
: base(items) { }
@ -69,7 +69,7 @@ namespace Nuclex.Support.Tracking {
/// not cache otherwise store the transformed items.
/// </remarks>
protected override WeightedProgression<ProgressionType> Transform(
ObservedProgression<ProgressionType> item
ObservedWeightedProgression<ProgressionType> item
) {
return item.WeightedProgression;
}

View file

@ -22,15 +22,97 @@ using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Tracking {
/*
public abstract class ProgressionTracker {
public void Track() {}
/// <summary>Helps tracking the progress of one or more progressions</summary>
/// <remarks>
/// <para>
/// 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.
/// </para>
/// <para>
/// 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).
/// </para>
/// </remarks>
public class ProgressionTracker : IDisposable {
protected virtual void OnStartTracking();
protected virtual void OnEndTracking();
protected virtual void OnProgressUpdated();
/// <summary>Triggered when the idle state of the tracker changes</summary>
/// <remarks>
/// 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.
/// </remarks>
public event EventHandler<IdleStateEventArgs> AsyncIdleStateChanged;
/// <summary>Triggered when the total progress has changed</summary>
public event EventHandler<ProgressUpdateEventArgs> AsyncProgressUpdated;
/// <summary>Immediately releases all resources owned by the instance</summary>
public void Dispose() {
// TODO: Untrack all
}
/// <summary>Begins tracking the specified progression</summary>
/// <param name="progression">Progression to be tracked</param>
public void Track(Progression progression) {
Track(progression, 1.0f);
}
/// <summary>Begins tracking the specified progression</summary>
/// <param name="progression">Progression to be tracked</param>
/// <param name="weight">Weight to assign to this progression</param>
public void Track(Progression progression, float weight) {
this.trackedProgressions.Add(
new WeightedProgression<Progression>(progression, weight)
);
this.totalWeight += weight;
recalculateProgress();
}
/// <summary>Stops tracking the specified progression</summary>
/// <param name="progression">Progression to stop tracking of</param>
public void Untrack(Progression progression) { }
/// <summary>Fires the AsyncIdleStateChanged event</summary>
/// <param name="idle">New idle state to report</param>
protected virtual void OnAsyncIdleStateChanged(bool idle) {
EventHandler<IdleStateEventArgs> copy = AsyncIdleStateChanged;
if(copy != null)
copy(this, new IdleStateEventArgs(idle));
}
/// <summary>Fires the AsyncProgressUpdated event</summary>
/// <param name="progress">New progress to report</param>
protected virtual void OnAsyncProgressUpdated(float progress) {
EventHandler<ProgressUpdateEventArgs> copy = AsyncProgressUpdated;
if(copy != null)
copy(this, new ProgressUpdateEventArgs(progress));
}
/// <summary>Recalculates the total progress of the tracker</summary>
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(
}
/// <summary>Total weight of all progressions being tracked</summary>
private float totalWeight;
/// <summary>Progressions being tracked by this tracker</summary>
private List<ObservedWeightedProgression<ProgressionType>> trackedProgressions;
}
*/
} // namespace Nuclex.Support.Tracking

View file

@ -32,7 +32,7 @@ namespace Nuclex.Support.Tracking {
/// <summary>Performs common initialization for the public constructors</summary>
private SetProgression() {
this.childs = new List<ObservedProgression<ProgressionType>>();
this.childs = new List<ObservedWeightedProgression<ProgressionType>>();
}
/// <summary>Initializes a new set progression</summary>
@ -47,10 +47,10 @@ namespace Nuclex.Support.Tracking {
// progression and wrap it in an ObservedProgression
foreach(ProgressionType progression in childs) {
this.childs.Add(
new ObservedProgression<ProgressionType>(
new ObservedWeightedProgression<ProgressionType>(
new WeightedProgression<ProgressionType>(progression),
new ObservedProgression<ProgressionType>.ReportDelegate(asyncProgressUpdated),
new ObservedProgression<ProgressionType>.ReportDelegate(asyncEnded)
new ObservedWeightedProgression<ProgressionType>.ReportDelegate(asyncProgressUpdated),
new ObservedWeightedProgression<ProgressionType>.ReportDelegate(asyncEnded)
)
);
}
@ -71,10 +71,10 @@ namespace Nuclex.Support.Tracking {
// Construct an ObservedProgression around each of the WeightedProgressions
foreach(WeightedProgression<ProgressionType> progression in childs) {
this.childs.Add(
new ObservedProgression<ProgressionType>(
new ObservedWeightedProgression<ProgressionType>(
progression,
new ObservedProgression<ProgressionType>.ReportDelegate(asyncProgressUpdated),
new ObservedProgression<ProgressionType>.ReportDelegate(asyncEnded)
new ObservedWeightedProgression<ProgressionType>.ReportDelegate(asyncProgressUpdated),
new ObservedWeightedProgression<ProgressionType>.ReportDelegate(asyncEnded)
)
);
@ -110,8 +110,8 @@ namespace Nuclex.Support.Tracking {
// the Childs collection.
if(this.wrapper == null) {
// This doesn't need a lock because it's only a stateless wrapper. If it
// is constructed twice, then so be it.
// This doesn't need a lock because it's a stateless wrapper.
// If it is constructed twice, then so be it, no problem at all.
this.wrapper = new WeightedProgressionWrapperCollection<ProgressionType>(
this.childs
);
@ -127,10 +127,10 @@ namespace Nuclex.Support.Tracking {
/// Called when the progress of one of the observed progressions changes
/// </summary>
private void asyncProgressUpdated() {
float totalProgress = 0.0f;
// Calculate the sum of the progress reported by our child progressions,
// scaled to the weight each progression has assigned to it.
float totalProgress = 0.0f;
for(int index = 0; index < this.childs.Count; ++index) {
totalProgress +=
this.childs[index].Progress * this.childs[index].WeightedProgression.Weight;
@ -142,7 +142,6 @@ namespace Nuclex.Support.Tracking {
// Send out the progress update
OnAsyncProgressUpdated(totalProgress);
}
/// <summary>
@ -162,7 +161,7 @@ namespace Nuclex.Support.Tracking {
}
/// <summary>Progressions being managed in the set</summary>
private List<ObservedProgression<ProgressionType>> childs;
private List<ObservedWeightedProgression<ProgressionType>> childs;
/// <summary>
/// Wrapper collection for exposing the child progressions under the
/// WeightedProgression interface