public DataElement MoveTo(DataElementContainer newParent, int index) { // Locate any fixups so we can update them // Move element DataElement newElem; DataElementContainer oldParent = this.parent; string newName = this.name; for (int i = 0; newParent.ContainsKey(newName); i++) { newName = this.name + "_" + i; } oldParent.RemoveAt(oldParent.IndexOf(this)); if (newName == this.name) { newElem = this; } else { newElem = this.Clone(newName); this.ClearRelations(); } newParent.Insert(index, newElem); foreach (Relation relation in newElem.relations) { if (relation.Of == newElem) { relation.OfName = newElem.fullName; } if (relation.From == newElem) { relation.FromName = newElem.fullName; } } return(newElem); }
private DataElement MoveTo(DataElementContainer newParent, int index) { DataElementContainer oldParent = this.parent; if (oldParent == newParent) { int oldIdx = oldParent.IndexOf(this); oldParent.RemoveAt(oldIdx); if (oldIdx < index) { --index; } newParent.Insert(index, this); return(this); } string newName = this.name; for (int i = 0; newParent.ContainsKey(newName); i++) { newName = this.name + "_" + i; } DataElement newElem; if (newName == this.name) { newElem = this; } else { newElem = this.Clone(newName); // We are "moving" the element, but doing so by cloning // into a new element. The clone will duplicate relations // that reach outside of the element tree, so we need to // clean up all old relations that were inside // the old element tree. ClearBindings(true); } // Save off relations var relations = newElem.relations.Of <Binding>().ToArray(); oldParent.RemoveAt(oldParent.IndexOf(this)); newParent.Insert(index, newElem); // When an element is moved, the name can change. // Additionally, the resolution algorithm might not // be able to locate the proper element, so set // the 'OfName' to the full name of the new element. foreach (var rel in relations) { rel.OfName = newElem.fullName; rel.Resolve(); } return(newElem); }
static void ApplyField(DataElementContainer model, string field, Variant value) { DataElement elem = model; DataElementContainer container = model; var names = field.Split('.'); for (int i = 0; i < names.Length; i++) { string name = names[i]; Match m = Regex.Match(name, @"(.*)\[(-?\d+)\]$"); if (m.Success) { name = m.Groups[1].Value; int index = int.Parse(m.Groups[2].Value); if (!container.ContainsKey(name)) { throw new PeachException("Error, unable to resolve field \"" + field + "\" against \"" + model.fullName + "\"."); } var array = container[name] as Array; if (array == null) { throw new PeachException("Error, cannot use array index syntax on field name unless target element is an array. Field: " + field); } // Are we disabling this array? if (index == -1) { if (array.minOccurs > 0) { throw new PeachException("Error, cannot set array to zero elements when minOccurs > 0. Field: " + field + " Element: " + array.fullName); } // Remove all children array.Clear(); return; } if (array.maxOccurs != -1 && index > array.maxOccurs) { throw new PeachException("Error, index larger that maxOccurs. Field: " + field + " Element: " + array.fullName); } if (!array.hasExpanded && array.origionalElement == null) { array.origionalElement = array[0]; array.RemoveAt(0); } // Add elements upto our index for (int x = array.Count; x <= index; x++) { string itemName = array.origionalElement.name + "_" + x; var item = array.origionalElement.Clone(itemName); array.Add(item); } array.hasExpanded = true; elem = array[index]; container = elem as DataElementContainer; } else if (container is Choice) { elem = null; var choice = container as Choice; if (!choice.choiceElements.TryGetValue(name, out elem)) { throw new PeachException("Error, unable to resolve field \"" + field + "\" against \"" + model.fullName + "\"."); } container = elem as DataElementContainer; choice.SelectedElement = elem; } else { if (!container.ContainsKey(name)) { throw new PeachException("Error, unable to resolve field \"" + field + "\" against \"" + model.fullName + "\"."); } elem = container[name]; container = elem as DataElementContainer; } } if (!(elem is DataElementContainer)) { elem.DefaultValue = value; } }