[runtime] Fix corlib out of date error with disabled COM
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / State / CategoryState.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.Diagnostics;
8     using System.Diagnostics.CodeAnalysis;
9     using System.Runtime;
10
11     // <summary>
12     // Simple category state object that knows how to remember
13     // two boolean flags: the expansion state of the category itself and the
14     // expansion state of its advanced section.
15     // </summary>
16     internal class CategoryState : PersistedState 
17     {
18
19         private const bool DefaultCategoryExpanded = true;
20         private const bool DefaultAdvancedSectionExpanded = true;
21
22         private string _categoryName;
23
24         private bool _categoryExpanded = DefaultCategoryExpanded;
25         private bool _advancedSectionExpanded = DefaultAdvancedSectionExpanded;
26
27         // <summary>
28         // Basic ctor
29         // </summary>
30         // <param name="categoryName">Name of the contained category</param>
31         [SuppressMessage("Microsoft.Performance", "CA1805:DoNotInitializeUnnecessarily")]
32         public CategoryState(string categoryName) 
33         {
34             Fx.Assert(!string.IsNullOrEmpty(categoryName), "Expected a full category name");
35             _categoryName = categoryName;
36         }
37
38         // <summary>
39         // We key these state objects by the category names
40         // </summary>
41         public override object Key 
42         {
43             get { return _categoryName; }
44         }
45
46         // <summary>
47         // Returns true if any of the contained values differ from the default
48         // </summary>
49         public override bool IsSignificant 
50         {
51             get { return _categoryExpanded != DefaultCategoryExpanded || _advancedSectionExpanded != DefaultAdvancedSectionExpanded; }
52         }
53
54         // <summary>
55         // Gets or sets a flag indicating whether this category should be expanded or collapsed
56         // </summary>
57         public bool CategoryExpanded 
58         {
59             get { return _categoryExpanded; }
60             set { _categoryExpanded = value; }
61         }
62
63         // <summary>
64         // Gets or sets a flag indicating whether this category should have its advanced section
65         // expanded or collapsed
66         // </summary>
67         public bool AdvancedSectionExpanded 
68         {
69             get { return _advancedSectionExpanded; }
70             set { _advancedSectionExpanded = value; }
71         }
72
73         // <summary>
74         // Serializes this object into a simple string (AppDomains like strings).
75         //
76         // Format: CategoryName,CategoryExpanded,AdvancedExpanded;NextCategoryName,CategoryExpanded,AdvancedExpanded;...
77         // Where bools are recorded as 0 = false and 1 = true and ';' and ',' are escaped
78         // </summary>
79         // <returns>Serialized version of this state object (may be null)</returns>
80         protected override string SerializeCore() 
81         {
82             return string.Concat(
83                 PersistedStateUtilities.Escape(_categoryName),
84                 ',',
85                 PersistedStateUtilities.BoolToDigit(_categoryExpanded),
86                 ',',
87                 PersistedStateUtilities.BoolToDigit(_advancedSectionExpanded));
88         }
89
90         // <summary>
91         // Attempts to deserialize a string into a CategoryState object
92         // </summary>
93         // <param name="categoryStateString">String to deserialize</param>
94         // <returns>Instance of CategoryState if the serialized string was valid, null otherwise.</returns>
95         public static CategoryState Deserialize(string categoryStateString) 
96         {
97             string[] args = categoryStateString.Split(',');
98             if (args == null || args.Length != 3)
99             {
100                 return null;
101             }
102
103             bool? categoryExpanded = PersistedStateUtilities.DigitToBool(args[1]);
104             bool? advancedSectionExpanded = PersistedStateUtilities.DigitToBool(args[2]);
105             if (categoryExpanded == null || advancedSectionExpanded == null)
106             {
107                 return null;
108             }
109
110             string categoryName = PersistedStateUtilities.Unescape(args[0]);
111             if (string.IsNullOrEmpty(categoryName))
112             {
113                 return null;
114             }
115
116             CategoryState categoryState = new CategoryState(categoryName);
117             categoryState.CategoryExpanded = (bool)categoryExpanded;
118             categoryState.AdvancedSectionExpanded = (bool)advancedSectionExpanded;
119             return categoryState;
120         }
121     }
122 }