[reflection] Coop handles icalls in System.Reflection and System.RuntimeTypeHandle...
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / State / PropertyViewManagerStateContainer.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.State 
5 {
6     using System;
7     using System.Collections.Generic;
8     using System.Diagnostics;
9     using System.Text;
10     using System.Activities.Presentation.Internal.PropertyEditing.Views;
11     using System.Activities.Presentation;
12
13     // <summary>
14     // StateContainer for current selection of IPropertyViewManager
15     // </summary>
16     internal class PropertyViewManagerStateContainer : IStateContainer 
17     {
18
19         public static readonly PropertyViewManagerStateContainer Instance = new PropertyViewManagerStateContainer();
20
21         public const string RootPropertyInspectorPersistenceId = "RootPropertyInspector";
22
23         private Dictionary<string, string> _persistenceIdToManagerTypeNameMap;
24         private PropertyViewManagerStateContainer() 
25         {
26         }
27
28         // <summary>
29         // Event fired when the internal state is scrapped and restored from
30         // some saved value
31         // </summary>
32         public event EventHandler ContentRestored;
33
34         // <summary>
35         // Gets the instance of IPropertyViewManager stored under the specified
36         // persistence ID.
37         // </summary>
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) 
43         {
44             string propertyViewManagerTypeName;
45             if (_persistenceIdToManagerTypeNameMap == null ||
46                 !_persistenceIdToManagerTypeNameMap.TryGetValue(persistenceId, out propertyViewManagerTypeName)) 
47             {
48
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.
52                 //
53                 if (persistenceId != RootPropertyInspectorPersistenceId)
54                 {
55                     return GetPropertyViewManager(RootPropertyInspectorPersistenceId);
56                 }
57                 else
58                 {
59                     return ByCategoryViewManager.Instance;
60                 }
61             }
62
63             if (string.Equals(propertyViewManagerTypeName, typeof(ByCategoryViewManager).Name))
64             {
65                 return ByCategoryViewManager.Instance;
66             }
67             else if (string.Equals(propertyViewManagerTypeName, typeof(AlphabeticalViewManager).Name))
68             {
69                 return AlphabeticalViewManager.Instance;
70             }
71
72             Debug.Fail("Unknown IPropertyViewManager type: " + (propertyViewManagerTypeName ?? "null"));
73             return ByCategoryViewManager.Instance;
74         }
75
76         // <summary>
77         // Stores the specified IPropertyViewManager under the specified persistenceId.
78         // </summary>
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) 
82         {
83             if (persistenceId == null) 
84             {
85                 throw FxTrace.Exception.ArgumentNull("persistenceId");
86             }
87
88             if (manager == null && _persistenceIdToManagerTypeNameMap == null)
89             {
90                 return;
91             }
92
93             if (manager == null) 
94             {
95                 _persistenceIdToManagerTypeNameMap.Remove(persistenceId);
96             }
97             else 
98             {
99                 if (_persistenceIdToManagerTypeNameMap == null)
100                 {
101                     _persistenceIdToManagerTypeNameMap = new Dictionary<string, string>();
102                 }
103
104                 _persistenceIdToManagerTypeNameMap[persistenceId] = manager.GetType().Name;
105             }
106         }
107
108         // IStateContainer Members
109
110         // <summary>
111         // Retrieves all stored IPropertyViewManager types under all persistence IDs
112         // </summary>
113         // <returns>All stored IPropertyViewManager types under all persistence IDs</returns>
114         public object RetrieveState() 
115         {
116             if (_persistenceIdToManagerTypeNameMap == null || _persistenceIdToManagerTypeNameMap.Count == 0) 
117             {
118                 return null;
119             }
120
121             StringBuilder sb = new StringBuilder();
122             foreach (KeyValuePair<string, string> pair in _persistenceIdToManagerTypeNameMap) 
123             {
124                 if (sb.Length > 0)
125                 {
126                     sb.Append(';');
127                 }
128
129                 sb.Append(PersistedStateUtilities.Escape(pair.Key));
130                 sb.Append(';');
131                 sb.Append(pair.Value);
132             }
133
134             return sb.ToString();
135         }
136
137         // <summary>
138         // Attempts to restore the persisted state
139         // </summary>
140         // <param name="state"></param>
141         public void RestoreState(object state) 
142         {
143             if (state == null) 
144             {
145                 return;
146             }
147
148             string serializedState = state as string;
149             if (serializedState == null) 
150             {
151                 Debug.Fail("Invalid PropertyViewManager state: " + state.ToString());
152                 return;
153             }
154
155             string[] items = serializedState.Split(';');
156             if ((items.Length % 2) != 0) 
157             {
158                 Debug.Fail("Invalid PropertyViewManager state: " + state.ToString());
159                 return;
160             }
161
162             if (_persistenceIdToManagerTypeNameMap == null)
163             {
164                 _persistenceIdToManagerTypeNameMap = new Dictionary<string, string>();
165             }
166
167             for (int i = 0; i < items.Length;)
168             {
169                 _persistenceIdToManagerTypeNameMap[items[i++]] = items[i++];
170             }
171
172             if (ContentRestored != null)
173             {
174                 ContentRestored(this, EventArgs.Empty);
175             }
176         }
177
178     }
179 }