1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation.View
7 using System.Collections.Generic;
8 using System.Diagnostics.CodeAnalysis;
10 using System.Activities.Presentation.Model;
13 //ViewState is stored as a Dictionary<string, object> on the CFx object.
14 //ModelItem is passed in StoreViewState to get a handle to the CFx object.
15 [Fx.Tag.XamlVisible(false)]
16 public class WorkflowViewStateService : ViewStateService
18 EditingContext context;
20 public override event ViewStateChangedEventHandler ViewStateChanged;
22 public override event ViewStateChangedEventHandler UndoableViewStateChanged;
24 [SuppressMessage(FxCop.Category.Security, FxCop.Rule.DoNotDeclareReadOnlyMutableReferenceTypes)]
25 public static readonly AttachableMemberIdentifier ViewStateName = new AttachableMemberIdentifier(typeof(WorkflowViewStateService), "ViewState");
31 return this.context.Services.GetService<UndoEngine>();
35 public WorkflowViewStateService(EditingContext context)
37 this.context = context;
40 public static Dictionary<string, object> GetViewState(object instance)
44 throw FxTrace.Exception.AsError(new ArgumentNullException("instance"));
46 Dictionary<string, object> viewState;
47 AttachablePropertyServices.TryGetProperty(instance, ViewStateName, out viewState);
51 public static void SetViewState(object instance, Dictionary<string, object> value)
55 throw FxTrace.Exception.AsError(new ArgumentNullException("instance"));
57 AttachablePropertyServices.SetProperty(instance, ViewStateName, value);
61 public override object RetrieveViewState(ModelItem modelItem, string key)
63 if (modelItem == null)
65 throw FxTrace.Exception.AsError(new ArgumentNullException("modelItem"));
69 throw FxTrace.Exception.AsError(new ArgumentNullException("key"));
72 object viewStateObj = null;
73 Dictionary<string, object> viewState = WorkflowViewStateService.GetViewState(modelItem.GetCurrentValue());
74 if (viewState != null)
76 viewState.TryGetValue(key, out viewStateObj);
81 public override void StoreViewState(ModelItem modelItem, string key, object value)
83 if (modelItem == null)
85 throw FxTrace.Exception.AsError(new ArgumentNullException("modelItem"));
89 throw FxTrace.Exception.AsError(new ArgumentNullException("key"));
92 object oldValue = null;
93 Dictionary<string, object> viewState = WorkflowViewStateService.GetViewState(modelItem.GetCurrentValue());
94 if (viewState == null)
96 viewState = new Dictionary<string, object>();
97 WorkflowViewStateService.SetViewState(modelItem.GetCurrentValue(), viewState);
99 viewState.TryGetValue(key, out oldValue);
102 viewState[key] = value;
106 RemoveViewState(modelItem, key);
108 if (this.ViewStateChanged != null && value != oldValue)
110 this.ViewStateChanged(this, new ViewStateChangedEventArgs(modelItem, key, value, oldValue));
114 public override bool RemoveViewState(ModelItem modelItem, string key)
116 if (modelItem == null)
118 throw FxTrace.Exception.AsError(new ArgumentNullException("modelItem"));
122 throw FxTrace.Exception.AsError(new ArgumentNullException("key"));
124 bool itemRemoved = false;
125 Dictionary<string, object> viewState = WorkflowViewStateService.GetViewState(modelItem.GetCurrentValue());
126 if (viewState != null && key != null && viewState.ContainsKey(key))
128 itemRemoved = viewState.Remove(key);
129 if (viewState.Keys.Count == 0)
131 AttachablePropertyServices.RemoveProperty(modelItem.GetCurrentValue(), ViewStateName);
137 public override Dictionary<string, object> RetrieveAllViewState(ModelItem modelItem)
139 if (modelItem == null)
141 throw FxTrace.Exception.AsError(new ArgumentNullException("modelItem"));
143 return WorkflowViewStateService.GetViewState(modelItem.GetCurrentValue());
147 public override void StoreViewStateWithUndo(ModelItem modelItem, string key, object value)
149 object oldValue = RetrieveViewState(modelItem, key);
150 ViewStateChange vsChange = new ViewStateChange(this)
157 ModelTreeManager modelTreeManager = this.context.Services.GetService<ModelTreeManager>();
158 if (modelTreeManager != null)
160 modelTreeManager.AddToCurrentEditingScope(vsChange);
165 void RaiseUndoableViewStateChangedEvent(ModelItem modelItem, string key, object newValue, object oldValue)
167 if (this.UndoableViewStateChanged != null)
169 this.UndoableViewStateChanged(this, new ViewStateChangedEventArgs(modelItem, key, newValue, oldValue));
174 internal class ViewStateChange : Change
176 protected WorkflowViewStateService viewStateService;
178 public ModelItem Item { get; set; }
179 public string Key { get; set; }
180 public object OldValue { get; set; }
181 public object NewValue { get; set; }
183 public ViewStateChange(WorkflowViewStateService viewStateService)
185 this.viewStateService = viewStateService;
189 public override string Description
191 get { return SR.ViewStateUndoUnitDescription; }
194 public override bool Apply()
196 viewStateService.StoreViewState(Item, Key, NewValue);
197 this.viewStateService.RaiseUndoableViewStateChangedEvent(Item, Key, NewValue, OldValue);
201 public override Change GetInverse()
203 return new ViewStateChange(this.viewStateService)
207 OldValue = this.NewValue,
208 NewValue = this.OldValue