/// <summary> /// Add as many parts as required that we can validly represent in the current setup. This will leave other parts in place. /// </summary> /// <param name="parts"></param> /// <exception cref="System.ApplicationException">Thrown if some parts couldn't be represented in this view.</exception> public void SetParts(IPartViewJunction parts) { // only adding parts, so leave existing Parts alone // for each p in parts // can we copy from p? if so, do it, otherwise try the next part // if we get to the end and the last part we added couldn't be populated, remove it int total = 0; var skipped = new List <IPartView>(); StandardPart <T> part = null; foreach (var p in parts) { total++; if (part == null) { part = AddPart(); } if (part.CanCopyFrom(p)) { part.CopyFrom(p); part = null; } else { skipped.Add(p); } } if (part != null) { this.RemovePart(part); } // if skipped contains values, we should throw an exception here with the remaining parts if (skipped.Count > 0) { var message = new StringBuilder(); message.AppendLine(String.Format("Could not load {0} of {1} parameters:", skipped.Count, total)); foreach (var p in skipped) { message.AppendLine(String.Concat("- ", p.ToString())); } throw new ApplicationException(message.ToString()); } }
public virtual void SetParts(IPartViewJunction parts) { // convert all parts to AdvancedParts // TODO: ensure we don't have any single-item junctions var queue = new Queue <PartLoader>(); queue.Enqueue(new PartLoader(parts)); while (queue.Count > 0) { var load = queue.Dequeue(); if (load.Current is IPartViewJunction) { var junction = CreateJunction(((IPartViewJunction)load.Current).Type); AddHandlers(junction); foreach (var p in (IPartViewJunction)load.Current) { queue.Enqueue(new PartLoader(p, junction)); } if (load.Junction == null) { Part = junction; } else { load.Junction.Add(junction); } } else { var node = CreateNode(load.Current); AddHandlers(node); if (load.Junction == null) { Part = node; } else { load.Junction.Add(node); } } } }
/// <summary> /// /// </summary> /// <param name="parts"></param> /// <exception cref="System.ApplicationException">Thrown if some parts couldn't be represented in this view.</exception> public void SetParts(IPartViewJunction parts) { int total = 0; var skipped = new List <IPartView>(); var myParts = new List <IPartView>(); foreach (var p in Parts) { myParts.Add(p); } foreach (var p in parts) { total++; int index = myParts.IndexOf(p); if (index >= 0) { myParts[index].CopyFrom(p); } else { skipped.Add(p); } } // if skipped contains values, we should throw an exception here with the remaining parts if (skipped.Count > 0) { var message = new StringBuilder(); message.AppendLine(String.Format("Could not load {0} of {1} parameters:", skipped.Count, total)); foreach (var p in skipped) { message.AppendLine(String.Concat("- ", p.ToString())); } throw new ApplicationException(message.ToString()); } }