Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ObservableViewModelService.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 
6 using SiliconStudio.Core.Extensions;
7 
8 namespace SiliconStudio.Presentation.Quantum
9 {
10  /// <summary>
11  /// A class that provides various services to <see cref="ObservableViewModel"/> objects
12  /// </summary>
14  {
15  private readonly List<Action<IObservableNode, IDictionary<string, object>>> associatedDataProviders = new List<Action<IObservableNode, IDictionary<string, object>>>();
16 
17  /// <summary>
18  /// Initializes a new instance of the <see cref="ObservableViewModelService"/> class.
19  /// </summary>
21  : this(x => null)
22  {
23  }
24 
25  /// <summary>
26  /// Initializes a new instance of the <see cref="ObservableViewModelService"/> class.
27  /// </summary>
28  /// <param name="viewModelProvider">A function that returns an <see cref="ObservableViewModel"/> for an given <see cref="ObservableViewModelIdentifier"/>.</param>
29  public ObservableViewModelService(Func<ObservableViewModelIdentifier, ObservableViewModel> viewModelProvider)
30  {
31  if (viewModelProvider == null) throw new ArgumentNullException("viewModelProvider");
32  ViewModelProvider = viewModelProvider;
33  }
34 
35  /// <summary>
36  /// Gets or sets a method that retrieves the currently active <see cref="ObservableViewModel"/>. This method is used to get the current observable
37  /// view model matching a Quantum object when using undo/redo features, since observable objects can be destroyed and recreated frequently.
38  /// </summary>
39  public Func<ObservableViewModelIdentifier, ObservableViewModel> ViewModelProvider { get; set; }
40 
41  /// <summary>
42  /// Register a method that will associate additional data to an instance of <see cref="IObservableNode"/>.
43  /// </summary>
44  /// <param name="provider">The method that will associate additional data to an instance of <see cref="IObservableNode"/>.</param>
46  {
47  associatedDataProviders.Add(provider);
48  }
49 
50  /// <summary>
51  /// Unregister a previoulsy registered method that was associating additional data to an instance of <see cref="IObservableNode"/>.
52  /// </summary>
53  /// <param name="provider">The previoulsy registered method that was associating additional additional data to an instance of <see cref="IObservableNode"/>.</param>
55  {
56  associatedDataProviders.Remove(provider);
57  }
58 
59  internal IDictionary<string, object> RequestAssociatedData(IObservableNode node, bool updatingData)
60  {
61  var mergedResult = new Dictionary<string, object>();
62  foreach (var provider in associatedDataProviders)
63  {
64  var data = new Dictionary<string, object>();
65  provider(node, data);
66  // We use the Add method the first time to prevent unspotted key collision.
67  if (updatingData)
68  data.ForEach(x => mergedResult.Add(x.Key, x.Value));
69  else
70  data.ForEach(x => mergedResult[x.Key] = x.Value);
71  }
72  return mergedResult;
73  }
74  }
75 }
void UnregisterAssociatedDataProvider(Action< IObservableNode, IDictionary< string, object >> provider)
Unregister a previoulsy registered method that was associating additional data to an instance of IObs...
ObservableViewModelService(Func< ObservableViewModelIdentifier, ObservableViewModel > viewModelProvider)
Initializes a new instance of the ObservableViewModelService class.
void RegisterAssociatedDataProvider(Action< IObservableNode, IDictionary< string, object >> provider)
Register a method that will associate additional data to an instance of IObservableNode.
A class that provides various services to ObservableViewModel objects
ObservableViewModelService()
Initializes a new instance of the ObservableViewModelService class.