PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/util/SettingsGroup.cs

#
C# | 314 lines | 168 code | 46 blank | 100 comment | 40 complexity | fc63cb850c14fa5dc5a0dc8a32fdd4c2 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2002-2003, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org
  5. // ****************************************************************
  6. namespace NUnit.Util
  7. {
  8. using System;
  9. using System.Drawing;
  10. using System.Globalization;
  11. using System.ComponentModel;
  12. /// <summary>
  13. /// SettingsGroup is the base class representing a group
  14. /// of user or system settings. All storge of settings
  15. /// is delegated to a SettingsStorage.
  16. /// </summary>
  17. public class SettingsGroup : ISettings, IDisposable
  18. {
  19. #region Instance Fields
  20. protected ISettingsStorage storage;
  21. #endregion
  22. #region Constructors
  23. /// <summary>
  24. /// Construct a settings group.
  25. /// </summary>
  26. /// <param name="storage">Storage for the group settings</param>
  27. public SettingsGroup( ISettingsStorage storage )
  28. {
  29. this.storage = storage;
  30. }
  31. /// <summary>
  32. /// Protected constructor for use by derived classes that
  33. /// set the storage themselves or don't use a storage.
  34. /// </summary>
  35. protected SettingsGroup()
  36. {
  37. }
  38. #endregion
  39. #region Properties
  40. public event SettingsEventHandler Changed;
  41. /// <summary>
  42. /// The storage used for the group settings
  43. /// </summary>
  44. public ISettingsStorage Storage
  45. {
  46. get { return storage; }
  47. }
  48. #endregion
  49. #region ISettings Members
  50. /// <summary>
  51. /// Load the value of one of the group's settings
  52. /// </summary>
  53. /// <param name="settingName">Name of setting to load</param>
  54. /// <returns>Value of the setting or null</returns>
  55. public object GetSetting( string settingName )
  56. {
  57. return storage.GetSetting( settingName );
  58. }
  59. /// <summary>
  60. /// Load the value of one of the group's settings or return a default value
  61. /// </summary>
  62. /// <param name="settingName">Name of setting to load</param>
  63. /// <param name="defaultValue">Value to return if the seeting is not present</param>
  64. /// <returns>Value of the setting or the default</returns>
  65. public object GetSetting( string settingName, object defaultValue )
  66. {
  67. object result = GetSetting(settingName );
  68. if ( result == null )
  69. result = defaultValue;
  70. return result;
  71. }
  72. /// <summary>
  73. /// Load the value of one of the group's integer settings
  74. /// in a type-safe manner or return a default value
  75. /// </summary>
  76. /// <param name="settingName">Name of setting to load</param>
  77. /// <param name="defaultValue">Value to return if the seeting is not present</param>
  78. /// <returns>Value of the setting or the default</returns>
  79. public int GetSetting(string settingName, int defaultValue)
  80. {
  81. object result = GetSetting(settingName);
  82. if (result == null)
  83. return defaultValue;
  84. if (result is int)
  85. return (int)result;
  86. try
  87. {
  88. return Int32.Parse(result.ToString());
  89. }
  90. catch
  91. {
  92. return defaultValue;
  93. }
  94. }
  95. /// <summary>
  96. /// Load the value of one of the group's float settings
  97. /// in a type-safe manner or return a default value
  98. /// </summary>
  99. /// <param name="settingName">Name of setting to load</param>
  100. /// <param name="defaultValue">Value to return if the setting is not present</param>
  101. /// <returns>Value of the setting or the default</returns>
  102. public float GetSetting(string settingName, float defaultValue)
  103. {
  104. object result = GetSetting(settingName);
  105. if (result == null)
  106. return defaultValue;
  107. if (result is float)
  108. return (float)result;
  109. try
  110. {
  111. return float.Parse(result.ToString());
  112. }
  113. catch
  114. {
  115. return defaultValue;
  116. }
  117. }
  118. /// <summary>
  119. /// Load the value of one of the group's boolean settings
  120. /// in a type-safe manner.
  121. /// </summary>
  122. /// <param name="settingName">Name of setting to load</param>
  123. /// <param name="defaultValue">Value of the setting or the default</param>
  124. /// <returns>Value of the setting</returns>
  125. public bool GetSetting( string settingName, bool defaultValue )
  126. {
  127. object result = GetSetting(settingName );
  128. if ( result == null )
  129. return defaultValue;
  130. // Handle legacy formats
  131. // if ( result is int )
  132. // return (int)result == 1;
  133. //
  134. // if ( result is string )
  135. // {
  136. // if ( (string)result == "1" ) return true;
  137. // if ( (string)result == "0" ) return false;
  138. // }
  139. if ( result is bool )
  140. return (bool) result ;
  141. try
  142. {
  143. return Boolean.Parse( result.ToString() );
  144. }
  145. catch
  146. {
  147. return defaultValue;
  148. }
  149. }
  150. /// <summary>
  151. /// Load the value of one of the group's string settings
  152. /// in a type-safe manner or return a default value
  153. /// </summary>
  154. /// <param name="settingName">Name of setting to load</param>
  155. /// <param name="defaultValue">Value to return if the setting is not present</param>
  156. /// <returns>Value of the setting or the default</returns>
  157. public string GetSetting( string settingName, string defaultValue )
  158. {
  159. object result = GetSetting(settingName );
  160. if ( result == null )
  161. return defaultValue;
  162. if ( result is string )
  163. return (string) result;
  164. else
  165. return result.ToString();
  166. }
  167. /// <summary>
  168. /// Load the value of one of the group's enum settings
  169. /// in a type-safe manner or return a default value
  170. /// </summary>
  171. /// <param name="settingName">Name of setting to load</param>
  172. /// <param name="defaultValue">Value to return if the setting is not present</param>
  173. /// <returns>Value of the setting or the default</returns>
  174. public System.Enum GetSetting(string settingName, System.Enum defaultValue)
  175. {
  176. object result = GetSetting(settingName);
  177. if (result == null)
  178. return defaultValue;
  179. if (result is System.Enum)
  180. return (System.Enum)result;
  181. try
  182. {
  183. return (System.Enum)System.Enum.Parse(defaultValue.GetType(), result.ToString(), true);
  184. }
  185. catch
  186. {
  187. return defaultValue;
  188. }
  189. }
  190. /// <summary>
  191. /// Load the value of one of the group's Font settings
  192. /// in a type-safe manner or return a default value
  193. /// </summary>
  194. /// <param name="settingName">Name of setting to load</param>
  195. /// <param name="defaultFont">Value to return if the setting is not present</param>
  196. /// <returns>Value of the setting or the default</returns>
  197. public Font GetSetting(string settingName, Font defaultFont)
  198. {
  199. object result = GetSetting(settingName);
  200. if (result == null)
  201. return defaultFont;
  202. if (result is Font)
  203. return (Font)result;
  204. try
  205. {
  206. TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font));
  207. return (Font)converter.ConvertFrom(null, CultureInfo.InvariantCulture, result.ToString());
  208. }
  209. catch
  210. {
  211. return defaultFont;
  212. }
  213. }
  214. /// <summary>
  215. /// Remove a setting from the group
  216. /// </summary>
  217. /// <param name="settingName">Name of the setting to remove</param>
  218. public void RemoveSetting( string settingName )
  219. {
  220. storage.RemoveSetting( settingName );
  221. if ( Changed != null )
  222. Changed( this, new SettingsEventArgs( settingName ) );
  223. }
  224. /// <summary>
  225. /// Remove a group of settings
  226. /// </summary>
  227. /// <param name="GroupName"></param>
  228. public void RemoveGroup( string groupName )
  229. {
  230. storage.RemoveGroup( groupName );
  231. }
  232. /// <summary>
  233. /// Save the value of one of the group's settings
  234. /// </summary>
  235. /// <param name="settingName">Name of the setting to save</param>
  236. /// <param name="settingValue">Value to be saved</param>
  237. public void SaveSetting( string settingName, object settingValue )
  238. {
  239. object oldValue = storage.GetSetting( settingName );
  240. // Avoid signaling "changes" when there is not really a change
  241. if ( oldValue != null )
  242. {
  243. if( oldValue is string && settingValue is string && (string)oldValue == (string)settingValue ||
  244. oldValue is int && settingValue is int && (int)oldValue == (int)settingValue ||
  245. oldValue is bool && settingValue is bool && (bool)oldValue == (bool)settingValue ||
  246. oldValue is Enum && settingValue is Enum && oldValue.Equals(settingValue) )
  247. return;
  248. }
  249. storage.SaveSetting( settingName, settingValue );
  250. if ( Changed != null )
  251. Changed( this, new SettingsEventArgs( settingName ) );
  252. }
  253. #endregion
  254. #region IDisposable Members
  255. /// <summary>
  256. /// Dispose of this group by disposing of it's storage implementation
  257. /// </summary>
  258. public void Dispose()
  259. {
  260. if ( storage != null )
  261. {
  262. storage.Dispose();
  263. storage = null;
  264. }
  265. }
  266. #endregion
  267. }
  268. }