1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.State
7 using System.Collections.Generic;
8 using System.Diagnostics;
10 using System.Activities.Presentation.Internal.PropertyEditing.Views;
11 using System.Activities.Presentation;
14 // StateContainer for current selection of IPropertyViewManager
16 internal class PropertyViewManagerStateContainer : IStateContainer
19 public static readonly PropertyViewManagerStateContainer Instance = new PropertyViewManagerStateContainer();
21 public const string RootPropertyInspectorPersistenceId = "RootPropertyInspector";
23 private Dictionary<string, string> _persistenceIdToManagerTypeNameMap;
24 private PropertyViewManagerStateContainer()
29 // Event fired when the internal state is scrapped and restored from
32 public event EventHandler ContentRestored;
35 // Gets the instance of IPropertyViewManager stored under the specified
38 // <param name="persistenceId">ID to look up</param>
39 // <returns>Instance of IPropertyViewManager stored under the specified
40 // persistence ID. If not found, an instance of the default IPropertyViewManager
41 // is returned.</returns>
42 public IPropertyViewManager GetPropertyViewManager(string persistenceId)
44 string propertyViewManagerTypeName;
45 if (_persistenceIdToManagerTypeNameMap == null ||
46 !_persistenceIdToManagerTypeNameMap.TryGetValue(persistenceId, out propertyViewManagerTypeName))
49 // If we don't have any state stored, default to the value of the
50 // root PropertyInspector, unless that state is not stored either
51 // in which case default to category view.
53 if (persistenceId != RootPropertyInspectorPersistenceId)
55 return GetPropertyViewManager(RootPropertyInspectorPersistenceId);
59 return ByCategoryViewManager.Instance;
63 if (string.Equals(propertyViewManagerTypeName, typeof(ByCategoryViewManager).Name))
65 return ByCategoryViewManager.Instance;
67 else if (string.Equals(propertyViewManagerTypeName, typeof(AlphabeticalViewManager).Name))
69 return AlphabeticalViewManager.Instance;
72 Debug.Fail("Unknown IPropertyViewManager type: " + (propertyViewManagerTypeName ?? "null"));
73 return ByCategoryViewManager.Instance;
77 // Stores the specified IPropertyViewManager under the specified persistenceId.
79 // <param name="persistenceId">ID to store under</param>
80 // <param name="manager">IPropertyViewManager to store</param>
81 public void StorePropertyViewManager(string persistenceId, IPropertyViewManager manager)
83 if (persistenceId == null)
85 throw FxTrace.Exception.ArgumentNull("persistenceId");
88 if (manager == null && _persistenceIdToManagerTypeNameMap == null)
95 _persistenceIdToManagerTypeNameMap.Remove(persistenceId);
99 if (_persistenceIdToManagerTypeNameMap == null)
101 _persistenceIdToManagerTypeNameMap = new Dictionary<string, string>();
104 _persistenceIdToManagerTypeNameMap[persistenceId] = manager.GetType().Name;
108 // IStateContainer Members
111 // Retrieves all stored IPropertyViewManager types under all persistence IDs
113 // <returns>All stored IPropertyViewManager types under all persistence IDs</returns>
114 public object RetrieveState()
116 if (_persistenceIdToManagerTypeNameMap == null || _persistenceIdToManagerTypeNameMap.Count == 0)
121 StringBuilder sb = new StringBuilder();
122 foreach (KeyValuePair<string, string> pair in _persistenceIdToManagerTypeNameMap)
129 sb.Append(PersistedStateUtilities.Escape(pair.Key));
131 sb.Append(pair.Value);
134 return sb.ToString();
138 // Attempts to restore the persisted state
140 // <param name="state"></param>
141 public void RestoreState(object state)
148 string serializedState = state as string;
149 if (serializedState == null)
151 Debug.Fail("Invalid PropertyViewManager state: " + state.ToString());
155 string[] items = serializedState.Split(';');
156 if ((items.Length % 2) != 0)
158 Debug.Fail("Invalid PropertyViewManager state: " + state.ToString());
162 if (_persistenceIdToManagerTypeNameMap == null)
164 _persistenceIdToManagerTypeNameMap = new Dictionary<string, string>();
167 for (int i = 0; i < items.Length;)
169 _persistenceIdToManagerTypeNameMap[items[i++]] = items[i++];
172 if (ContentRestored != null)
174 ContentRestored(this, EventArgs.Empty);