/// <summary> /// Adds a field to the parent <see cref="ObjectFieldCollection"/>. /// </summary> /// <param name="field">The field to add.</param> private void AddNewEntry(ObjectField field) { object newValue = GetNewValue(field); ObjectFieldDictionary parentDictionary = field as ObjectFieldDictionary; if (parentDictionary != null) { // Find a new key value string key; for (int i = 1; ; ++i) { key = "Key" + i; if (!parentDictionary.Items.ContainsKey(key)) { break; } } ObjectField newField = ModelCreator.CreateObjectField("[" + key + "]", newValue, parentDictionary); newField.NameTag = key; parentDictionary.Add(newField); return; } ObjectFieldList parentList = field as ObjectFieldList; if (parentList != null) { ObjectField newField = ModelCreator.CreateObjectField("[" + (parentList.Items.Count + 1) + "]", newValue, parentList); parentList.Add(newField); } }
private ObjectFieldDictionary CreateObjectFieldDictionary(string name, Type type, PropertyInfo property, IDictionary dictionary) { ObjectFieldDictionary field = new ObjectFieldDictionary { Name = name, IsNullable = true, PropertyInfo = property, Type = type, Value = dictionary, Count = dictionary != null ? dictionary.Count : 0 }; field.ItemType = field.Type.GetProperty("Item").PropertyType; if (dictionary != null) { foreach (object key in dictionary.Keys) { ObjectField itemField = CreateObjectField("[" + key + "]", field.ItemType, dictionary[key], null); if (itemField == null) { continue; } itemField.Parent = field; itemField.NameTag = key; field.Items.Add(key, itemField); } } field.ItemAdded += ItemAddedMethod; field.ItemRemoved += ItemRemovedMethod; return(field); }
/// <summary> /// Applies the changes mede by the user to the model. /// </summary> /// <param name="node">The changed node.</param> /// <param name="editor">The editor used for changing.</param> protected override void DoApplyChanges(TreeNodeAdv node, Control editor) { ObjectField field = node.Tag as ObjectField; if (field != null && field.Parent is ObjectFieldDictionary && field.NameTag is string) { ObjectFieldDictionary dictionaryField = field.Parent as ObjectFieldDictionary; IDictionary dictionary = dictionaryField.Value as IDictionary; if (dictionary != null) { dictionaryField.Items.Remove(field.NameTag); dictionary.Remove(field.NameTag); field.NameTag = editor.Text; field.Name = "[" + editor.Text + "]"; dictionaryField.Items.Add(field.NameTag, field); dictionary.Add(field.NameTag, field.Value); editor.Text = field.Name; } } base.DoApplyChanges(node, editor); }
/// <summary> /// Gets the <see cref="ObjectField"/> which is eqivalent to the given field /// within this tree. /// </summary> /// <param name="field">The field to search the equivalent for.</param> /// <returns>The found field or null.</returns> public ObjectField GetEquivalentField(ObjectField field) { if (field == null || field.Parent == null) { return(TreeModel.Root); } ObjectField equivalentParentField = GetEquivalentField(field.Parent); ObjectField equivalentField = null; ObjectFieldClass equivalentParentFieldClass = equivalentParentField as ObjectFieldClass; if (equivalentParentFieldClass != null) { equivalentField = equivalentParentFieldClass.ChildFields.FirstOrDefault(childField => childField.Name == field.Name); } ObjectFieldList equivalentParentFieldList = equivalentParentField as ObjectFieldList; if (equivalentParentFieldList != null) { equivalentField = equivalentParentFieldList.Items.FirstOrDefault(item => item.Name == field.Name); } ObjectFieldDictionary equivalentParentFieldDictionary = equivalentParentField as ObjectFieldDictionary; if (equivalentParentFieldDictionary != null) { equivalentField = equivalentParentFieldDictionary.Items.Values.FirstOrDefault(item => item.Name == field.Name); } return(equivalentField); }
/// <summary> /// Gets called when an item is dropped over the treeview. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="e">More information about the event.</param> private void trvTree_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(TreeNodeAdv[])) && trvTree.DropPosition.Node != null) { TreeNodeAdv[] nodes = e.Data.GetData(typeof(TreeNodeAdv[])) as TreeNodeAdv[]; TreeNodeAdv parent = trvTree.DropPosition.Node; if (nodes != null && nodes.Length == 1 && parent != null) { ObjectField draggedField = nodes[0].Tag as ObjectField; ObjectField destField = parent.Tag as ObjectField; if (draggedField != null && destField != null) { bool ownNode = trvTree.FindNodeByTag(draggedField) != null; if (ownNode) { ObjectFieldList parentList = destField.Parent as ObjectFieldList; if (parentList != null) { int index = parentList.Items.IndexOf(destField); index = trvTree.DropPosition.Position == NodePosition.After ? index + 1 : index; bool expanded = nodes[0].IsExpanded; parentList.Move(draggedField, index); trvTree.FindNodeByTag(draggedField).IsExpanded = expanded; } } else { ObjectFieldDictionary parentDestDict = destField.Parent as ObjectFieldDictionary; ObjectFieldList parentDestList = destField.Parent as ObjectFieldList; ObjectField clonedField = draggedField.Clone() as ObjectField; if (clonedField != null) { clonedField.Parent = destField.Parent; //TODO: The cloned field still wraps the uncloned value. Cloning this is not easy since // we can't use Clone() on it since it could not be implemented. if (parentDestDict != null) { parentDestDict.Add(clonedField); } else if (parentDestList != null) { int index = parentDestList.Items.IndexOf(destField); index += trvTree.DropPosition.Position == NodePosition.After ? 1 : 0; parentDestList.Insert(index, clonedField); } } } } } } }
/// <summary> /// Removes an entry from the parent <see cref="ObjectFieldCollection"/>. /// </summary> /// <param name="field">The field to remove.</param> private void RemoveEntry(ObjectField field) { ObjectFieldDictionary parentDictionary = field.Parent as ObjectFieldDictionary; if (parentDictionary != null) { parentDictionary.Remove(field); return; } ObjectFieldList parentList = field.Parent as ObjectFieldList; if (parentList != null) { parentList.Remove(field); } }
// ======================================================================== // Methods #region === Methods /// <summary> /// Returns the children of a node at a specified path. /// </summary> /// <param name="treePath">The path.</param> /// <returns>The children.</returns> public override IEnumerable GetChildren(TreePath treePath) { ObjectField field; if (treePath.IsEmpty()) { if (DisplayRoot == null) { return(null); } field = DisplayRoot; } else { field = treePath.LastNode as ObjectField; } ObjectFieldClass objectFieldClass = field as ObjectFieldClass; if (objectFieldClass != null) { return(objectFieldClass.ChildFields); } ObjectFieldList objectFieldList = field as ObjectFieldList; if (objectFieldList != null) { return(objectFieldList.Items); } ObjectFieldDictionary objectFieldDictionary = field as ObjectFieldDictionary; if (objectFieldDictionary != null) { return(objectFieldDictionary.Items.Values); } return(null); }