1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.State
7 using System.Diagnostics;
8 using System.Diagnostics.CodeAnalysis;
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.
16 internal class CategoryState : PersistedState
19 private const bool DefaultCategoryExpanded = true;
20 private const bool DefaultAdvancedSectionExpanded = true;
22 private string _categoryName;
24 private bool _categoryExpanded = DefaultCategoryExpanded;
25 private bool _advancedSectionExpanded = DefaultAdvancedSectionExpanded;
30 // <param name="categoryName">Name of the contained category</param>
31 [SuppressMessage("Microsoft.Performance", "CA1805:DoNotInitializeUnnecessarily")]
32 public CategoryState(string categoryName)
34 Fx.Assert(!string.IsNullOrEmpty(categoryName), "Expected a full category name");
35 _categoryName = categoryName;
39 // We key these state objects by the category names
41 public override object Key
43 get { return _categoryName; }
47 // Returns true if any of the contained values differ from the default
49 public override bool IsSignificant
51 get { return _categoryExpanded != DefaultCategoryExpanded || _advancedSectionExpanded != DefaultAdvancedSectionExpanded; }
55 // Gets or sets a flag indicating whether this category should be expanded or collapsed
57 public bool CategoryExpanded
59 get { return _categoryExpanded; }
60 set { _categoryExpanded = value; }
64 // Gets or sets a flag indicating whether this category should have its advanced section
65 // expanded or collapsed
67 public bool AdvancedSectionExpanded
69 get { return _advancedSectionExpanded; }
70 set { _advancedSectionExpanded = value; }
74 // Serializes this object into a simple string (AppDomains like strings).
76 // Format: CategoryName,CategoryExpanded,AdvancedExpanded;NextCategoryName,CategoryExpanded,AdvancedExpanded;...
77 // Where bools are recorded as 0 = false and 1 = true and ';' and ',' are escaped
79 // <returns>Serialized version of this state object (may be null)</returns>
80 protected override string SerializeCore()
83 PersistedStateUtilities.Escape(_categoryName),
85 PersistedStateUtilities.BoolToDigit(_categoryExpanded),
87 PersistedStateUtilities.BoolToDigit(_advancedSectionExpanded));
91 // Attempts to deserialize a string into a CategoryState object
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)
97 string[] args = categoryStateString.Split(',');
98 if (args == null || args.Length != 3)
103 bool? categoryExpanded = PersistedStateUtilities.DigitToBool(args[1]);
104 bool? advancedSectionExpanded = PersistedStateUtilities.DigitToBool(args[2]);
105 if (categoryExpanded == null || advancedSectionExpanded == null)
110 string categoryName = PersistedStateUtilities.Unescape(args[0]);
111 if (string.IsNullOrEmpty(categoryName))
116 CategoryState categoryState = new CategoryState(categoryName);
117 categoryState.CategoryExpanded = (bool)categoryExpanded;
118 categoryState.AdvancedSectionExpanded = (bool)advancedSectionExpanded;
119 return categoryState;