2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / nunit20 / util / RecentFileSettings.cs
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
3 '
4 ' Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright  2000-2002 Philip A. Craig
6 '
7 ' This software is provided 'as-is', without any express or implied warranty. In no 
8 ' event will the authors be held liable for any damages arising from the use of this 
9 ' software.
10
11 ' Permission is granted to anyone to use this software for any purpose, including 
12 ' commercial applications, and to alter it and redistribute it freely, subject to the 
13 ' following restrictions:
14 '
15 ' 1. The origin of this software must not be misrepresented; you must not claim that 
16 ' you wrote the original software. If you use this software in a product, an 
17 ' acknowledgment (see the following) in the product documentation is required.
18 '
19 ' Portions Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright  2000-2002 Philip A. Craig
21 '
22 ' 2. Altered source versions must be plainly marked as such, and must not be 
23 ' misrepresented as being the original software.
24 '
25 ' 3. This notice may not be removed or altered from any source distribution.
26 '
27 '***********************************************************************************/
28 #endregion
29
30 using System;
31 using System.Collections;
32
33 namespace NUnit.Util
34 {
35         /// <summary>
36         /// Base class for settings that hold lists of recent files
37         /// </summary>
38         public abstract class RecentFileSettings : SettingsGroup
39         {
40                 // TODO: This class does more loading and
41                 // storing than it should but this is the
42                 // current simplest solution to having
43                 // multiple recentfiles objects around.
44                 // We can fix this by using a singleton.
45                 private IList fileEntries;
46
47                 public static readonly int MinSize = 1;
48
49                 public static readonly int MaxSize = 24;
50
51                 public static readonly int DefaultSize = 5;
52
53                 public RecentFileSettings( string name ) : base ( name, UserSettings.GetStorageImpl( name ) )
54                 {
55                         LoadFiles();
56                 }
57
58                 public RecentFileSettings( string name, SettingsStorage storage ) : base( name, storage ) 
59                 {
60                         LoadFiles();
61                 }
62
63                 public RecentFileSettings( string name, SettingsGroup parent ) : base( name, parent ) 
64                 { 
65                         LoadFiles();
66                 }
67
68                 public int MaxFiles
69                 {
70                         get 
71                         { 
72                                 int size = LoadIntSetting( "MaxFiles", DefaultSize );
73                                 
74                                 if ( size < MinSize ) size = MinSize;
75                                 if ( size > MaxSize ) size = MaxSize;
76                                 
77                                 return size;
78                         }
79                         set 
80                         { 
81                                 int oldSize = MaxFiles;
82                                 int newSize = value;
83                                 
84                                 if ( newSize < MinSize ) newSize = MinSize;
85                                 if ( newSize > MaxSize ) newSize = MaxSize;
86
87                                 SaveIntSetting( "MaxFiles", newSize );
88                                 if ( newSize < oldSize ) SaveSettings();
89                         }
90                 }
91
92                 protected void LoadFiles()
93                 {
94                         fileEntries = new ArrayList();
95                         for ( int index = 1; index <= MaxFiles; index++ )
96                         {
97                                 string fileName = LoadStringSetting( ValueName( index ) );
98                                 if ( fileName != null )
99                                         fileEntries.Add( fileName );
100                         }
101                 }
102
103                 public override void Clear()
104                 {
105                         base.Clear();
106                         fileEntries = new ArrayList();
107                 }
108
109                 public IList GetFiles()
110                 {
111                         LoadFiles();
112                         return fileEntries;
113                 }
114                 
115                 public string RecentFile
116                 {
117                         get 
118                         { 
119                                 LoadFiles();
120                                 if( fileEntries.Count > 0 )
121                                         return (string)fileEntries[0];
122
123                                 return null;
124                         }
125                         set
126                         {
127                                 LoadFiles();
128
129                                 int index = fileEntries.IndexOf(value);
130
131                                 if(index == 0) return;
132
133                                 if(index != -1)
134                                 {
135                                         fileEntries.RemoveAt(index);
136                                 }
137
138                                 fileEntries.Insert( 0, value );
139                                 if( fileEntries.Count > MaxFiles )
140                                         fileEntries.RemoveAt( MaxFiles );
141
142                                 SaveSettings();                 
143                         }
144                 }
145
146                 public void Remove( string fileName )
147                 {
148                         LoadFiles();
149                         fileEntries.Remove( fileName );
150                         SaveSettings();
151                 }
152
153                 private void SaveSettings()
154                 {
155                         while( fileEntries.Count > MaxFiles )
156                                 fileEntries.RemoveAt( fileEntries.Count - 1 );
157
158                         for( int index = 0; index < MaxSize; index++ ) 
159                         {
160                                 string valueName = ValueName( index + 1 );
161                                 if ( index < fileEntries.Count )
162                                         SaveSetting( valueName, fileEntries[index] );
163                                 else
164                                         RemoveSetting( valueName );
165                         }
166                 }
167
168                 private string ValueName( int index )
169                 {
170                         return string.Format( "File{0}", index );
171                 }
172         }
173 }