PageRenderTime 38ms CodeModel.GetById 19ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/util/SettingsGroup.cs

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