crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/Settings/XmlSettings.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.Xml;
       
    19 using System.Text;
       
    20 using System.IO;
       
    21 using System.Collections.Generic;
       
    22 using System.Diagnostics;
       
    23 using System.Collections;
       
    24 using System.Windows.Forms;
       
    25 using System.Drawing;
       
    26 using Microsoft.Win32;
       
    27 
       
    28 namespace SymbianUtils.Settings
       
    29 {
       
    30 	public class XmlSettings : IEnumerable<XmlSettingCategory>
       
    31 	{
       
    32 		#region Constructors
       
    33 		public XmlSettings( string aFileName )
       
    34 		{
       
    35 			FullyQualifiedFileName = aFileName;
       
    36 		}
       
    37 		#endregion
       
    38 
       
    39 		#region Store & Restore API
       
    40 		public void Restore()
       
    41 		{
       
    42 			string file = FullyQualifiedFileName;
       
    43 			if (System.IO.File.Exists(file))
       
    44 			{
       
    45 				XmlTextReader reader = new XmlTextReader(file);
       
    46 				try
       
    47 				{
       
    48 					reader.WhitespaceHandling = WhitespaceHandling.None;
       
    49 					reader.MoveToContent();
       
    50 					ReadData(reader);
       
    51 				}
       
    52 				catch (XmlException)
       
    53 				{
       
    54 					// Ignore for now
       
    55 				}
       
    56 				finally
       
    57 				{
       
    58 					reader.Close();
       
    59 				}
       
    60 			}
       
    61 		}
       
    62 
       
    63 		public void Store()
       
    64 		{
       
    65 			string file = FullyQualifiedFileName;
       
    66 			XmlTextWriter writer = new XmlTextWriter(file, System.Text.Encoding.UTF8);
       
    67 			try
       
    68 			{
       
    69 				writer.Formatting = Formatting.Indented;
       
    70 				WriteData(writer);
       
    71 			}
       
    72 			catch (XmlException)
       
    73 			{
       
    74 				// Ignore for now
       
    75 			}
       
    76 			finally
       
    77 			{
       
    78 				writer.Close();
       
    79 			}
       
    80 		}
       
    81 		#endregion
       
    82 
       
    83 		#region API
       
    84         public void Clear()
       
    85         {
       
    86             iCategories.Clear();
       
    87         }
       
    88 
       
    89         public void Add( XmlSettingCategory aCategory )
       
    90         {
       
    91             iCategories.Add( aCategory.Name, aCategory );
       
    92         }
       
    93 
       
    94 		public bool Exists( string aCategory )
       
    95 		{
       
    96             return iCategories.ContainsKey( aCategory );
       
    97 		}
       
    98 
       
    99         public bool Exists( string aCategory, string aKey )
       
   100         {
       
   101             bool ret = false;
       
   102             //
       
   103             if ( Exists( aCategory ) )
       
   104             {
       
   105                 XmlSettingCategory category = this[ aCategory ];
       
   106                 ret = category.Exists( aKey );
       
   107             }
       
   108             //
       
   109             return ret;
       
   110         }
       
   111 
       
   112 		public void Remove( string aCategory )
       
   113 		{
       
   114             if ( Exists( aCategory ) )
       
   115             {
       
   116                 iCategories.Remove( aCategory );
       
   117 			}
       
   118 		}
       
   119 
       
   120 		public void Remove( string aCategory, string aKey )
       
   121 		{
       
   122             if ( Exists( aCategory ) )
       
   123             {
       
   124                 XmlSettingCategory category = this[ aCategory ];
       
   125                 category.Remove( aKey );
       
   126             }
       
   127 		}
       
   128         #endregion
       
   129 
       
   130         #region Loading & Saving
       
   131         public void Save( string aCategory, object aObject )
       
   132         {
       
   133             if ( aObject is TextBox )
       
   134             {
       
   135                 DoSave( aCategory, aObject as TextBox );
       
   136             }
       
   137             else if ( aObject is RadioButton )
       
   138             {
       
   139                 DoSave( aCategory, aObject as RadioButton );
       
   140             }
       
   141             else if ( aObject is CheckBox )
       
   142             {
       
   143                 DoSave( aCategory, aObject as CheckBox );
       
   144             }
       
   145             else if ( aObject is ComboBox )
       
   146             {
       
   147                 DoSave( aCategory, aObject as ComboBox );
       
   148             }
       
   149             else if ( aObject is IXmlSettingsSimple )
       
   150             {
       
   151                 DoSave( aCategory, aObject as IXmlSettingsSimple );
       
   152             }
       
   153             else if ( aObject is IXmlSettingsExtended )
       
   154             {
       
   155                 DoSave( aCategory, aObject as IXmlSettingsExtended );
       
   156             }
       
   157             else if ( aObject is Form )
       
   158             {
       
   159                 DoSave( aCategory, aObject as Form );
       
   160             }
       
   161         }
       
   162 
       
   163         public void Load( string aCategory, object aObject )
       
   164         {
       
   165             if ( aObject is TextBox )
       
   166             {
       
   167                 DoLoad( aCategory, aObject as TextBox );
       
   168             }
       
   169             else if ( aObject is RadioButton )
       
   170             {
       
   171                 DoLoad( aCategory, aObject as RadioButton );
       
   172             }
       
   173             else if ( aObject is CheckBox )
       
   174             {
       
   175                 DoLoad( aCategory, aObject as CheckBox );
       
   176             }
       
   177             else if ( aObject is ComboBox )
       
   178             {
       
   179                 DoLoad( aCategory, aObject as ComboBox );
       
   180             }
       
   181             else if ( aObject is IXmlSettingsSimple )
       
   182             {
       
   183                 DoLoad( aCategory, aObject as IXmlSettingsSimple );
       
   184             }
       
   185             else if ( aObject is IXmlSettingsExtended )
       
   186             {
       
   187                 DoLoad( aCategory, aObject as IXmlSettingsExtended );
       
   188             }
       
   189             else if ( aObject is Form )
       
   190             {
       
   191                 DoLoad( aCategory, aObject as Form );
       
   192             }
       
   193         }
       
   194 
       
   195         #region Basic types
       
   196         public void Save( string aCategory, string aName, int aValue )
       
   197         {
       
   198             this[ aCategory, aName ] = aValue;
       
   199         }
       
   200 
       
   201         public void Save( string aCategory, string aName, uint aValue )
       
   202         {
       
   203             this[ aCategory, aName ] = aValue;
       
   204         }
       
   205 
       
   206         public void Save( string aCategory, string aName, long aValue )
       
   207         {
       
   208             this[ aCategory, aName ] = aValue;
       
   209         }
       
   210 
       
   211         public void Save( string aCategory, string aName, bool aValue )
       
   212         {
       
   213             this[ aCategory, aName ] = aValue;
       
   214         }
       
   215 
       
   216         public void Save( string aCategory, string aName, string aValue )
       
   217         {
       
   218             this[ aCategory, aName ] = aValue;
       
   219         }
       
   220 
       
   221         public void Save( string aCategory, string aName, object aValue )
       
   222         {
       
   223             this[ aCategory, aName ].Value = aValue;
       
   224         }
       
   225 
       
   226         public void Save( string aCategory, string aName, Point aValue )
       
   227         {
       
   228             this[ aCategory, aName ].Value = string.Format( "{0},{1}", aValue.X, aValue.Y );
       
   229         }
       
   230 
       
   231         public void Save( string aCategory, string aName, Size aValue )
       
   232         {
       
   233             this[ aCategory, aName ].Value = string.Format( "{0},{1}", aValue.Width, aValue.Height );
       
   234         }
       
   235 
       
   236         public int LoadInt( string aCategory, string aName )
       
   237         {
       
   238             return Load( aCategory, aName, 0 );
       
   239         }
       
   240 
       
   241         public int Load( string aCategory, string aName, int aDefault )
       
   242         {
       
   243             int ret = aDefault;
       
   244             if ( Exists( aCategory, aName ) )
       
   245             {
       
   246                 ret = this[ aCategory, aName ];
       
   247             }
       
   248             return ret;
       
   249         }
       
   250 
       
   251         public uint LoadUint( string aCategory, string aName )
       
   252         {
       
   253             return Load( aCategory, aName, 0U );
       
   254         }
       
   255 
       
   256         public uint Load( string aCategory, string aName, uint aDefault )
       
   257         {
       
   258             uint ret = aDefault;
       
   259             if ( Exists( aCategory, aName ) )
       
   260             {
       
   261                 ret = this[ aCategory, aName ];
       
   262             }
       
   263             return ret;
       
   264         }
       
   265 
       
   266         public long LoadLong( string aCategory, string aName )
       
   267         {
       
   268             return Load( aCategory, aName, 0L );
       
   269         }
       
   270 
       
   271         public long Load( string aCategory, string aName, long aDefault )
       
   272         {
       
   273             long ret = aDefault;
       
   274             if ( Exists( aCategory, aName ) )
       
   275             {
       
   276                 ret = this[ aCategory, aName ];
       
   277             }
       
   278             return ret;
       
   279         }
       
   280 
       
   281         public bool LoadBool( string aCategory, string aName )
       
   282         {
       
   283             return Load( aCategory, aName, false );
       
   284         }
       
   285 
       
   286         public bool Load( string aCategory, string aName, bool aDefault )
       
   287         {
       
   288             bool ret = aDefault;
       
   289             if ( Exists( aCategory, aName ) )
       
   290             {
       
   291                 ret = this[ aCategory, aName ];
       
   292             }
       
   293             return ret;
       
   294         }
       
   295 
       
   296         public string Load( string aCategory, string aName )
       
   297         {
       
   298             return Load( aCategory, aName, string.Empty );
       
   299         }
       
   300 
       
   301         public string Load( string aCategory, string aName, string aDefault )
       
   302         {
       
   303             string ret = aDefault;
       
   304             if ( Exists( aCategory, aName ) )
       
   305             {
       
   306                 ret = this[ aCategory, aName ];
       
   307             }
       
   308             return ret;
       
   309         }
       
   310 
       
   311         public object LoadObject( string aCategory, string aName )
       
   312         {
       
   313             return Load( aCategory, aName, null );
       
   314         }
       
   315 
       
   316         public object Load( string aCategory, string aName, object aDefault )
       
   317         {
       
   318             object ret = aDefault;
       
   319             if ( Exists( aCategory, aName ) )
       
   320             {
       
   321                 ret = this[ aCategory, aName ].Value;
       
   322             }
       
   323             return ret;
       
   324         }
       
   325 
       
   326         public Point Load( string aCategory, string aName, Point aDefault )
       
   327         {
       
   328             Point ret = aDefault;
       
   329             if ( Exists( aCategory, aName ) )
       
   330             {
       
   331                 ret = this[ aCategory, aName ];
       
   332             }
       
   333             return ret;
       
   334         }
       
   335 
       
   336         public Size Load( string aCategory, string aName, Size aDefault )
       
   337         {
       
   338             Size ret = aDefault;
       
   339             if ( Exists( aCategory, aName ) )
       
   340             {
       
   341                 ret = this[ aCategory, aName ];
       
   342             }
       
   343             return ret;
       
   344         }
       
   345         #endregion
       
   346 
       
   347         #endregion
       
   348 
       
   349 		#region Properties
       
   350         public int Count
       
   351         {
       
   352             get { return iCategories.Count; }
       
   353         }
       
   354 
       
   355 		public string FileName
       
   356 		{
       
   357 			get
       
   358 			{
       
   359 				FileInfo info = new FileInfo(FullyQualifiedFileName);
       
   360 				return info.Name;
       
   361 			}
       
   362 			set
       
   363 			{
       
   364 				FileInfo newNameInfo = new FileInfo(value);
       
   365 				FileInfo fullNameInfo = new FileInfo(FullyQualifiedFileName);
       
   366 				string fileName = fullNameInfo.DirectoryName;
       
   367 				fileName += newNameInfo.Name;
       
   368 				FullyQualifiedFileName = fileName;
       
   369 			}
       
   370 		}
       
   371 
       
   372 		public string FullyQualifiedFileName
       
   373 		{
       
   374 			get { return iFullyQualifiedFileName; }
       
   375 			set
       
   376 			{
       
   377 				string path = string.Empty;
       
   378 				try
       
   379 				{
       
   380 					path = Path.GetDirectoryName(value);
       
   381 
       
   382 				}
       
   383 				catch(ArgumentException)
       
   384 				{
       
   385 				}
       
   386 
       
   387 				if	(path == null || path.Length == 0)
       
   388 				{
       
   389 					// Get the path from the current app domain
       
   390 					iFullyQualifiedFileName = Path.Combine(System.Windows.Forms.Application.StartupPath, Path.GetFileName(value));
       
   391 				}
       
   392 				else
       
   393 				{
       
   394 					iFullyQualifiedFileName = value; 
       
   395 				}
       
   396 			}
       
   397 		}
       
   398 
       
   399 		public static Version CurrentSettingsVersion
       
   400 		{
       
   401 			get { return iCurrentSettingsVersion; }
       
   402 		}
       
   403 		#endregion
       
   404 
       
   405         #region From IEnumerable<XmlSettingCategory>
       
   406         public IEnumerator<XmlSettingCategory> GetEnumerator()
       
   407         {
       
   408             foreach ( KeyValuePair<string, XmlSettingCategory> pair in iCategories )
       
   409             {
       
   410                 yield return pair.Value;
       
   411             }
       
   412         }
       
   413 
       
   414         IEnumerator IEnumerable.GetEnumerator()
       
   415         {
       
   416             foreach ( KeyValuePair<string, XmlSettingCategory> pair in iCategories )
       
   417             {
       
   418                 yield return pair.Value;
       
   419             }
       
   420         }
       
   421         #endregion
       
   422 
       
   423 		#region Indexers
       
   424         public XmlSettingCategory this[ int aIndex ]
       
   425         {
       
   426             get
       
   427             {
       
   428                 string key = iCategories.Keys[ aIndex ];
       
   429                 return iCategories[ key ];
       
   430             }
       
   431         }
       
   432 
       
   433         public XmlSettingCategory this[ string aCategory ]
       
   434         {
       
   435             get
       
   436             {
       
   437                 XmlSettingCategory ret = new XmlSettingCategory( aCategory );
       
   438                 if ( Exists( aCategory ) )
       
   439                 {
       
   440                     ret = iCategories[ aCategory ];
       
   441                 }
       
   442                 else
       
   443                 {
       
   444                     iCategories.Add( aCategory, ret );
       
   445                 }
       
   446                 //
       
   447                 return ret;
       
   448             }
       
   449         }
       
   450         
       
   451 		public XmlSettingsValue this[ string aCategory, string aKey ]
       
   452 		{
       
   453 			get
       
   454 			{
       
   455                 XmlSettingCategory category = this[ aCategory ];
       
   456                 XmlSettingsValue value = category[ aKey ];
       
   457                 return value;
       
   458             }
       
   459 			set
       
   460 			{
       
   461                 XmlSettingCategory category = this[ aCategory ];
       
   462                 category[ aKey ] = value;
       
   463 			}
       
   464 		}
       
   465 
       
   466         public XmlSettingsValue this[ string aCategory, object aObject ]
       
   467         {
       
   468             get
       
   469             {
       
   470                 string key = aObject.ToString();
       
   471                 if ( aObject is System.Windows.Forms.Control )
       
   472                 {
       
   473                     key = ( aObject as Control ).Name;
       
   474                 }
       
   475                 return this[ aCategory, key ];
       
   476             }
       
   477             set
       
   478             {
       
   479                 string key = aObject.ToString();
       
   480                 if ( aObject is System.Windows.Forms.Control )
       
   481                 {
       
   482                     key = ( aObject as Control ).Name;
       
   483                 }
       
   484                 this[ aCategory, key ] = value;
       
   485             }
       
   486         }
       
   487 		#endregion
       
   488 
       
   489         #region Internal write helpers
       
   490         private void WriteData(XmlWriter aWriter)
       
   491 		{
       
   492 			aWriter.WriteStartElement( null, "configuration", null );
       
   493             //
       
   494 			WriteConfigSections( aWriter );
       
   495 			WriteSectionItems( aWriter );
       
   496             //
       
   497 			aWriter.WriteEndElement();
       
   498 		}
       
   499 
       
   500 		private void WriteConfigSections(XmlWriter aWriter)
       
   501 		{
       
   502 			aWriter.WriteStartElement( null, "configSections", null );
       
   503 			//
       
   504             foreach( XmlSettingCategory cat in this )
       
   505 			{
       
   506 				aWriter.WriteStartElement("section", null);
       
   507 				aWriter.WriteAttributeString("name", cat.Name);
       
   508 				aWriter.WriteAttributeString("version", cat.Version.ToString());
       
   509 				aWriter.WriteRaw(" ");
       
   510 				aWriter.WriteEndElement();
       
   511 			}
       
   512             //
       
   513 			aWriter.WriteEndElement();
       
   514 		}
       
   515 
       
   516 		private void WriteSectionItems(XmlWriter aWriter)
       
   517 		{
       
   518 			aWriter.WriteStartElement(null, "sectionItems", null);
       
   519             //
       
   520             foreach ( XmlSettingCategory cat in this )
       
   521 			{
       
   522 				cat.WriteSettings( aWriter );
       
   523 			}
       
   524             //
       
   525 			aWriter.WriteEndElement();
       
   526 		}
       
   527 		#endregion
       
   528 
       
   529 		#region Internal read helpers
       
   530 		private void ReadData(XmlReader aReader)
       
   531 		{
       
   532 			aReader.ReadStartElement("configuration");
       
   533 			//
       
   534 			ReadConfigSections( aReader );
       
   535 			ReadSectionItems( aReader );
       
   536 			//
       
   537 			aReader.ReadEndElement(); // configuration
       
   538 		}
       
   539 
       
   540 		private void ReadConfigSections(XmlReader aReader)
       
   541 		{
       
   542 			aReader.ReadStartElement("configSections");
       
   543 			//
       
   544 			while (!aReader.EOF && aReader.Name == "section")
       
   545 			{
       
   546 				if (aReader.NodeType != XmlNodeType.EndElement)
       
   547 				{
       
   548                     XmlSettingCategory category = new XmlSettingCategory( aReader );
       
   549                     iCategories.Add( category.Name, category );
       
   550 				}
       
   551 				aReader.Read();
       
   552 			}
       
   553             //
       
   554 			aReader.ReadEndElement(); // configSections
       
   555 		}
       
   556 
       
   557 		private void ReadSingleSection( XmlReader aReader )
       
   558 		{
       
   559 			string sectionName = aReader.Name;
       
   560 
       
   561 			// Advance to first section key & pair
       
   562             if ( aReader.IsEmptyElement )
       
   563             {
       
   564                 // It's empty - doesn't contain any content
       
   565             }
       
   566             else
       
   567             {
       
   568                 bool readOkay = aReader.Read();
       
   569 
       
   570                 // Search for the existing section definition
       
   571                 if ( readOkay && Exists( sectionName ) )
       
   572                 {
       
   573                     XmlSettingCategory cat = this[ sectionName ];
       
   574                     cat.ReadSettings( aReader );
       
   575                 }
       
   576             }
       
   577 		}
       
   578 
       
   579 		private void ReadSectionItems(XmlReader aReader)
       
   580 		{
       
   581 			aReader.ReadStartElement( "sectionItems" );
       
   582             //
       
   583             while ( !aReader.EOF && !( aReader.NodeType == XmlNodeType.EndElement && aReader.Name == "sectionItems" ) )
       
   584 			{
       
   585 				ReadSingleSection( aReader );
       
   586 				aReader.Read();
       
   587 			}
       
   588             //
       
   589 			aReader.ReadEndElement(); // sectionItems
       
   590 		}
       
   591 		#endregion
       
   592 
       
   593         #region Internal settings savers
       
   594         private void DoSave( string aCategory, IXmlSettingsSimple aObject )
       
   595         {
       
   596             string name = aObject.XmlSettingsPersistableName;
       
   597             string value = aObject.XmlSettingPersistableValue;
       
   598             this[ aCategory, name ] = value;
       
   599         }
       
   600 
       
   601         private void DoLoad( string aCategory, IXmlSettingsSimple aObject )
       
   602         {
       
   603             string name = aObject.XmlSettingsPersistableName;
       
   604             string value = this[ aCategory, name ];
       
   605             aObject.XmlSettingPersistableValue = value;
       
   606         }
       
   607 
       
   608         private void DoSave( string aCategory, IXmlSettingsExtended aObject )
       
   609         {
       
   610             aObject.XmlSettingsSave( this, aCategory );
       
   611         }
       
   612 
       
   613         private void DoLoad( string aCategory, IXmlSettingsExtended aObject )
       
   614         {
       
   615             aObject.XmlSettingsLoad( this, aCategory );
       
   616         }
       
   617 
       
   618         private void DoSave( string aCategory, TextBox aControl )
       
   619         {
       
   620             this[ aCategory, aControl.Name ] = aControl.Text;
       
   621         }
       
   622 
       
   623         private void DoLoad( string aCategory, TextBox aControl )
       
   624         {
       
   625             DoLoad( aCategory, aControl, string.Empty );
       
   626         }
       
   627 
       
   628         private void DoLoad( string aCategory, TextBox aControl, string aDefault )
       
   629         {
       
   630             string val = this[ aCategory, aControl.Name ];
       
   631             if ( val.Length > 0 )
       
   632             {
       
   633                 aControl.Text = val;
       
   634             }
       
   635             else
       
   636             {
       
   637                 aControl.Text = aDefault;
       
   638             }
       
   639         }
       
   640 
       
   641         private void DoSave( string aCategory, CheckBox aControl )
       
   642         {
       
   643             this[ aCategory, aControl.Name ] = aControl.Checked;
       
   644         }
       
   645 
       
   646         private void DoLoad( string aCategory, CheckBox aControl )
       
   647         {
       
   648             aControl.Checked = this[ aCategory, aControl ];
       
   649         }
       
   650 
       
   651         private void DoSave( string aCategory, RadioButton aControl )
       
   652         {
       
   653             this[ aCategory, aControl.Name ] = aControl.Checked;
       
   654         }
       
   655 
       
   656         private void DoLoad( string aCategory, RadioButton aControl )
       
   657         {
       
   658             aControl.Checked = this[ aCategory, aControl ];
       
   659         }
       
   660 
       
   661         private void DoSave( string aCategory, ComboBox aControl )
       
   662         {
       
   663             XmlSettingCategory category = this[ aCategory ];
       
   664  
       
   665             // Basic prefix
       
   666             string baseName = string.Format( "{0}###Item###", aControl.Name );
       
   667             
       
   668             // Write count, selection
       
   669             category[ baseName + "Count" ] = aControl.Items.Count;
       
   670             category[ baseName + "SelectedIndex" ] = aControl.SelectedIndex;
       
   671 
       
   672             // Write items
       
   673             int itemIndex = 0;
       
   674             foreach ( object item in aControl.Items )
       
   675             {
       
   676                 string itemName = string.Format( "{0}{1:d6}", baseName, itemIndex++ );
       
   677                 category[ itemName ] = item.ToString();
       
   678             }
       
   679         }
       
   680 
       
   681         private void DoLoad( string aCategory, ComboBox aControl )
       
   682         {
       
   683             XmlSettingCategory category = this[ aCategory ];
       
   684 
       
   685             aControl.BeginUpdate();
       
   686 
       
   687             // Basic prefix
       
   688             string baseName = string.Format( "{0}###Item###", aControl.Name );
       
   689 
       
   690             // Get count, selection
       
   691             int count = category[ baseName + "Count" ].ToInt( 0 );
       
   692             int selectedIndex = category[ baseName + "SelectedIndex" ].ToInt( 0 );
       
   693 
       
   694             // Get items
       
   695             for ( int itemIndex = 0; itemIndex < count; itemIndex++ )
       
   696             {
       
   697                 string itemName = string.Format( "{0}{1:d6}", baseName, itemIndex++ );
       
   698                 string itemText = category[ itemName ];
       
   699                 bool alreadyExists = aControl.Items.Contains( itemText );
       
   700                 if ( !alreadyExists )
       
   701                 {
       
   702                     aControl.Items.Add( itemText );
       
   703                 }
       
   704             }
       
   705 
       
   706             // Set selection if possible
       
   707             if ( selectedIndex >= 0 && selectedIndex < aControl.Items.Count )
       
   708             {
       
   709                 aControl.SelectedIndex = selectedIndex;
       
   710             }
       
   711             aControl.EndUpdate();
       
   712         }
       
   713 
       
   714         private void DoSave( string aCategory, Form aForm )
       
   715         {
       
   716             XmlSettingCategory category = this[ aCategory ];
       
   717 
       
   718             // Save location (if relevant)
       
   719             Point location = new Point();
       
   720             if ( aForm.WindowState == FormWindowState.Normal )
       
   721             {
       
   722                 location = aForm.Location;
       
   723             }
       
   724             Save( aCategory, "__#Location#__", location );
       
   725 
       
   726             // Save size 
       
   727             Size size = new Size();
       
   728             if ( aForm.WindowState == FormWindowState.Normal )
       
   729             {
       
   730                 size = aForm.Size;
       
   731             }
       
   732             Save( aCategory, "__#Size#__", size );
       
   733 
       
   734             // Save window state
       
   735             category[ "__#State#__" ] = aForm.WindowState.ToString();
       
   736         }
       
   737 
       
   738         private void DoLoad( string aCategory, Form aForm )
       
   739         {
       
   740             aForm.StartPosition = FormStartPosition.Manual;
       
   741 
       
   742             if ( Exists( aCategory ) )
       
   743             {
       
   744                 XmlSettingCategory category = this[ aCategory ];
       
   745                 
       
   746                 // Get initial string setting values
       
   747                 string stringState = category[ "__#State#__" ];
       
   748                 string stringLocation = category[ "__#Location#__" ];
       
   749                 string stringSize = category[ "__#Size#__" ];
       
   750                 
       
   751                 // Convert to correct types
       
   752                 FormWindowState state = (FormWindowState) System.Enum.Parse( typeof( FormWindowState ), stringState );
       
   753                 Point location = Load( aCategory, "__#Location#__" , new Point( 0, 0 ) );
       
   754                 Size size = Load( aCategory, "__#Size#__", aForm.MinimumSize );
       
   755                 //
       
   756                 aForm.Location = location;
       
   757                 aForm.WindowState = state;
       
   758                 if ( aForm.WindowState == FormWindowState.Normal )
       
   759                 {
       
   760                     aForm.Size = size;
       
   761                 }
       
   762             }
       
   763             else
       
   764             {
       
   765                 // Go with defaults for everything
       
   766                 aForm.WindowState = FormWindowState.Maximized;
       
   767             }
       
   768         }
       
   769         #endregion
       
   770 
       
   771 		#region Data members
       
   772 		private static Version iCurrentSettingsVersion = new Version( 2, 0, 0 );
       
   773         private string iFullyQualifiedFileName = string.Empty;
       
   774         private SortedList<string, XmlSettingCategory> iCategories = new SortedList<string, XmlSettingCategory>();
       
   775 		#endregion
       
   776     }
       
   777 }
       
   778 
       
   779