예제 #1
0
        /// <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);
            }
        }
예제 #2
0
        /// <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);
        }
예제 #3
0
 /// <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);
                         }
                     }
                 }
             }
         }
     }
 }
예제 #4
0
        /// <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);
            }
        }
예제 #5
0
        /// <summary>
        /// Gets called after a new item was added to a collection.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="args">Informations about the event.</param>
        private void field_ItemAdded(object sender, ObjectFieldEventArgs args)
        {
            ObjectFieldCollection collection = args.ObjectField.Parent as ObjectFieldCollection;

            if (collection != null)
            {
                Dirty = true;
                TreePath        parentPath = GetPath(collection);
                ObjectFieldList listField  = collection as ObjectFieldList;
                int             index      = collection.Count;
                if (listField != null)
                {
                    index = listField.Items.IndexOf(args.ObjectField);
                }
                OnNodesInserted(new TreeModelEventArgs(parentPath, new[] { index }, new object[] { args.ObjectField }));
                treeView.FindNode(parentPath).Expand();
                OnItemAdded(args);
            }
        }
예제 #6
0
        // ========================================================================
        // 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);
        }
예제 #7
0
        private ObjectFieldList CreateObjectFieldList(string name, Type type, PropertyInfo property, IList list)
        {
            ObjectFieldList field = new ObjectFieldList
            {
                Name         = name,
                IsNullable   = true,
                PropertyInfo = property,
                Type         = type,
                Value        = list,
                Count        = list != null ? list.Count : 0
            };

            field.ItemType = field.Type.GetProperty("Item").PropertyType;

            int count = 0;

            if (list != null)
            {
                foreach (object item in list)
                {
                    count++;
                    ObjectField itemField = CreateObjectField("[" + count + "]", field.ItemType, item, null);
                    if (itemField == null)
                    {
                        continue;
                    }
                    itemField.Parent = field;
                    field.Items.Add(itemField);
                }
            }

            field.ItemAdded   += ItemAddedMethod;
            field.ItemRemoved += ItemRemovedMethod;

            return(field);
        }