Exemple #1
0
        internal protected virtual void Initialize()
        {
            var targetNode     = GetTargetNode(SourceNode, Index);
            var targetNodePath = SourceNodePath.GetChildPath(SourceNode, targetNode);

            if (targetNodePath == null || !targetNodePath.IsValid)
            {
                throw new InvalidOperationException("Unable to retrieve the path of the given model node.");
            }

            var commandPath = targetNodePath;

            if ((!SourceNode.Content.ShouldProcessReference || targetNode == SourceNode || targetNode == null) && !Index.IsEmpty)
            {
                // When the references are not processed or when the value is null, there is no actual target node.
                // However, the commands need the index to be able to properly set the modified value
                commandPath = targetNodePath.PushElement(Index, GraphNodePath.ElementType.Index);
            }

            if (targetNode != SourceNode && targetNode != null)
            {
                foreach (var command in targetNode.Commands)
                {
                    var commandWrapper = new ModelNodeCommandWrapper(ServiceProvider, command, commandPath);
                    AddCommand(commandWrapper);
                }
            }

            var targetCommandNames = Commands.Select(x => x.Name).ToList();

            foreach (var command in SourceNode.Commands)
            {
                // Add source commands that are not already provided by the target node
                if (!targetCommandNames.Contains(command.Name))
                {
                    var commandWrapper = new ModelNodeCommandWrapper(ServiceProvider, command, commandPath);
                    AddCommand(commandWrapper);
                }
            }

            if (!isPrimitive && targetNode != null)
            {
                GenerateChildren(targetNode, targetNodePath);
            }

            isInitialized = true;

            Owner.ObservableViewModelService?.NotifyNodeInitialized(this);

            FinalizeChildrenInitialization();

            CheckDynamicMemberConsistency();
        }
Exemple #2
0
 private void GenerateChildren(IGraphNode modelNode, GraphNodePath graphNodePath)
 {
     if (modelNode.Content.IsReference && modelNode.Content.ShouldProcessReference)
     {
         var referenceEnumerable = modelNode.Content.Reference as ReferenceEnumerable;
         if (referenceEnumerable != null)
         {
             // If the reference should not be processed, we still need to create an observable node for each entry of the enumerable.
             // These observable nodes will have the same source node that their parent so we use this information to prevent
             // the infinite recursion that could occur due to the fact that these child nodes will have the same model nodes (like primitive types)
             // while holding an enumerable reference.
             //if (modelNode.Content.ShouldProcessReference || ModelNodeParent.sourceNode != modelNode)
             {
                 // Note: we are making a copy of the reference list because it can be updated from the Initialize method of the
                 // observable node in the case of scene objects. Doing this is a hack, but parts of this framework will be redesigned later to improve this
                 foreach (var reference in referenceEnumerable.ToList())
                 {
                     // The type might be a boxed primitive type, such as float, if the collection has object as generic argument.
                     // In this case, we must set the actual type to have type converter working, since they usually can't convert
                     // a boxed float to double for example. Otherwise, we don't want to have a node type that is value-dependent.
                     var  type            = reference.TargetNode != null && reference.TargetNode.Content.IsPrimitive ? reference.TargetNode.Content.Type : reference.Type;
                     bool shouldConstruct = Owner.PropertiesProvider.ShouldConstructNode(modelNode, reference.Index);
                     if (shouldConstruct)
                     {
                         var observableNode = Owner.ObservableViewModelService.ObservableNodeFactory(Owner, null, false, modelNode, graphNodePath, type, reference.Index);
                         AddChild(observableNode);
                         observableNode.Initialize();
                     }
                 }
             }
         }
     }
     else
     {
         var dictionary = modelNode.Content.Descriptor as DictionaryDescriptor;
         var list       = modelNode.Content.Descriptor as CollectionDescriptor;
         if (dictionary != null && modelNode.Content.Value != null)
         {
             // Dictionary of primitive objects
             foreach (var key in dictionary.GetKeys(modelNode.Content.Value))
             {
                 var  index           = new Index(key);
                 bool shouldConstruct = Owner.PropertiesProvider.ShouldConstructNode(modelNode, index);
                 if (shouldConstruct)
                 {
                     var observableChild = Owner.ObservableViewModelService.ObservableNodeFactory(Owner, null, true, modelNode, graphNodePath, dictionary.ValueType, index);
                     AddChild(observableChild);
                     observableChild.Initialize();
                 }
             }
         }
         else if (list != null && modelNode.Content.Value != null)
         {
             // List of primitive objects
             for (int i = 0; i < list.GetCollectionCount(modelNode.Content.Value); ++i)
             {
                 var  index           = new Index(i);
                 bool shouldConstruct = Owner.PropertiesProvider.ShouldConstructNode(modelNode, index);
                 if (shouldConstruct)
                 {
                     var observableChild = Owner.ObservableViewModelService.ObservableNodeFactory(Owner, null, true, modelNode, graphNodePath, list.ElementType, index);
                     AddChild(observableChild);
                     observableChild.Initialize();
                 }
             }
         }
         else
         {
             // Single non-reference primitive object
             foreach (var child in modelNode.Children)
             {
                 bool shouldConstruct = Owner.PropertiesProvider.ShouldConstructNode(child, Index.Empty);
                 if (shouldConstruct)
                 {
                     var childPath       = graphNodePath.GetChildPath(modelNode, child);
                     var observableChild = Owner.ObservableViewModelService.ObservableNodeFactory(Owner, child.Name, child.Content.IsPrimitive, child, childPath, child.Content.Type, Index.Empty);
                     AddChild(observableChild);
                     observableChild.Initialize();
                 }
             }
         }
     }
 }