Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DirtiableEditableViewModel.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 using System.Linq;
6 
7 using SiliconStudio.ActionStack;
8 using SiliconStudio.Core.Extensions;
9 using SiliconStudio.Presentation.Services;
10 using SiliconStudio.Presentation.ViewModel.ActionStack;
11 
12 namespace SiliconStudio.Presentation.ViewModel
13 {
14  /// <summary>
15  /// An implementation of the <see cref="EditableViewModel"/> that is also itself an <see cref="IDirtiableViewModel"/>. The <see cref="Dirtiables"/>
16  /// property returns an enumerable containing the instance itself.
17  /// </summary>
19  {
20  private readonly HashSet<ViewModelActionItem> changes = new HashSet<ViewModelActionItem>();
21  private readonly List<IDirtiableViewModel> dependencies = new List<IDirtiableViewModel>();
22  private bool isDirty;
23 
24  /// <summary>
25  /// Initializes a new instance of the <see cref="DirtiableEditableViewModel"/> class.
26  /// </summary>
27  /// <param name="serviceProvider">A service provider that can provide a <see cref="IDispatcherService"/> and an <see cref="ITransactionalActionStack"/> to use for this view model.</param>
29  : base(serviceProvider)
30  {
31  }
32 
33  /// <inheritdoc/>
34  public bool IsDirty { get { return isDirty; } protected set { var oldValue = isDirty; SetValueUncancellable(ref isDirty, value); OnDirtyFlagSet(oldValue, value); } }
35 
36  /// <inheritdoc/>
37  public override IEnumerable<IDirtiableViewModel> Dirtiables { get { return this.Yield(); } }
38 
39  /// <inheritdoc/>
40  public event EventHandler<DirtinessChangedEventArgs> DirtinessChanged;
41 
42  /// <inheritdoc/>
43  public virtual void Dispose()
44  {
45  foreach (var dependency in dependencies)
46  dependency.DirtinessChanged -= DependencyDirtinessChanged;
47  }
48 
49  /// <inheritdoc/>
50  public virtual void RegisterActionItem(ViewModelActionItem actionItem)
51  {
52  if (changes.Contains(actionItem)) throw new ArgumentException(@"The given action item is already registered.", "actionItem");
53  changes.Add(actionItem);
54  UpdateDirtiness();
55  }
56 
57  /// <inheritdoc/>
58  public virtual void DiscardActionItem(ViewModelActionItem actionItem)
59  {
60  bool removed = changes.Remove(actionItem);
61  if (!removed) throw new ArgumentException(@"The given action item was not registered.", "actionItem");
62  UpdateDirtiness();
63  }
64 
65  /// <inheritdoc/>
66  public virtual void NotifyActionStackChange(ActionStackChange change)
67  {
68  UpdateDirtiness();
69  }
70 
71  /// <inheritdoc/>
73  {
74  if (dependencies.Contains(dirtiable)) throw new ArgumentException(@"The given dirtiable object is already registered as a dependency.", "dirtiable");
75  dependencies.Add(dirtiable);
76  dirtiable.DirtinessChanged += DependencyDirtinessChanged;
77  }
78 
79  /// <inheritdoc/>
81  {
82  dirtiable.DirtinessChanged -= DependencyDirtinessChanged;
83  bool removed = dependencies.Remove(dirtiable);
84  if (!removed) throw new ArgumentException(@"The given dirtiable object was not registered as a dependency.", "dirtiable");
85  }
86 
87  protected virtual void OnDirtyFlagSet(bool oldValue, bool newValue)
88  {
89  // intentionally do nothing
90  }
91 
92  private void DependencyDirtinessChanged(object sender, EventArgs e)
93  {
94  UpdateDirtiness();
95  }
96 
97  private void UpdateDirtiness()
98  {
99  bool previousValue = IsDirty;
100  IsDirty = changes.Any(x => x.IsSaved != x.IsDone) || dependencies.Any(x => x.IsDirty);
101  var handler = DirtinessChanged;
102  if (previousValue != IsDirty && handler != null)
103  {
104  handler(this, new DirtinessChangedEventArgs(IsDirty));
105  }
106  }
107  }
108 }
An interface that represents an object which can be in a dirty state (modified since last save)...
virtual void RegisterActionItem(ViewModelActionItem actionItem)
Register a ViewModelActionItem object to this dirtiable object. A registered action item can modify t...
virtual void DiscardActionItem(ViewModelActionItem actionItem)
Discard a previously registered ViewModelActionItem. The action item to discard.The given action item...
DirtiableEditableViewModel(IViewModelServiceProvider serviceProvider)
Initializes a new instance of the DirtiableEditableViewModel class.
virtual void NotifyActionStackChange(ActionStackChange change)
Notify the IDirtiableViewModel that a registered action item has been modified and thus the dirty sta...
void UnregisterDirtiableDependency(IDirtiableViewModel dirtiable)
Unregister a IDirtiableViewModel as a dependency of the current object. The dirtiable object to unreg...
An abstact class that inherits from ActionItem and can be used to manage actions related to an IDirti...
This class is an implementation of the DispatcherViewModel class that supports undo/redo of property ...
void RegisterDirtiableDependency(IDirtiableViewModel dirtiable)
Register a IDirtiableViewModel as a dependency of the current object. When a registered dependency ob...
An implementation of the EditableViewModel that is also itself an IDirtiableViewModel. The Dirtiables property returns an enumerable containing the instance itself.