fa0d09caec9f5e28085d3cb998e09cea16ebbf21
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / State / PropertyActiveEditModeStateContainer.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.State 
5 {
6     using System.Collections;
7     using System.Text;
8     using System.Activities.Presentation.PropertyEditing;
9     using System.Activities.Presentation.Internal.PropertyEditing.Model;
10
11     // <summary>
12     // Container storing the names of properties the value editors of which
13     // are in ExtendedPinned ActiveEditMode.  We persist this information
14     // across domain reloads and view changes.
15     // </summary>
16     internal class PropertyActiveEditModeStateContainer : IStateContainer 
17     {
18
19         public static readonly PropertyActiveEditModeStateContainer Instance = new PropertyActiveEditModeStateContainer();
20
21         private Hashtable _expandedPropertyEditors = new Hashtable();
22         private PropertyActiveEditModeStateContainer() 
23         {
24         }
25
26         // <summary>
27         // Gets the last stored ActiveEditMode for the specified property
28         // </summary>
29         // <param name="property">PropertyEntry to look up</param>
30         // <returns>Last stored ActiveEditMode for the specified property</returns>
31         public PropertyContainerEditMode GetActiveEditMode(PropertyEntry property) 
32         {
33             return _expandedPropertyEditors.ContainsKey(ModelUtilities.GetCachedSubPropertyHierarchyPath(property)) ?
34                 PropertyContainerEditMode.ExtendedPinned :
35                 PropertyContainerEditMode.Inline;
36         }
37
38         // <summary>
39         // Stores the current ActiveEditMode for the specified property
40         // </summary>
41         // <param name="property">Property to key off of</param>
42         // <param name="editMode">ActiveEditMode value to store</param>
43         public void StoreActiveEditMode(PropertyEntry property, PropertyContainerEditMode editMode) 
44         {
45             string path = ModelUtilities.GetCachedSubPropertyHierarchyPath(property);
46
47             // We only care about storing the ExtendedPinned state.  Everything
48             // else is transitory and shouldn't be persisted.
49             //
50             if (editMode == PropertyContainerEditMode.ExtendedPinned)
51             {
52                 _expandedPropertyEditors[path] = null;
53             }
54             else
55             {
56                 _expandedPropertyEditors.Remove(path);
57             }
58         }
59
60         // IStateContainer
61
62         public object RetrieveState() 
63         {
64             if (_expandedPropertyEditors.Count == 0)
65             {
66                 return null;
67             }
68
69             bool firstTime = true;
70             StringBuilder sb = new StringBuilder();
71             foreach (string propertyPath in _expandedPropertyEditors) 
72             {
73                 sb.Append(propertyPath);
74                 if (!firstTime) 
75                 {
76                     sb.Append(';');
77                 }
78                 firstTime = false;
79             }
80
81             return sb.ToString();
82         }
83
84         public void RestoreState(object state) 
85         {
86             string stateString = state as string;
87             if (stateString == null) 
88             {
89                 return;
90             }
91
92             string[] paths = stateString.Split(';');
93             foreach (string path in paths) 
94             {
95                 if (string.IsNullOrEmpty(path)) 
96                 {
97                     continue;
98                 }
99                 _expandedPropertyEditors[path] = null;
100             }
101         }
102
103     }
104 }