(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / nunit20 / util / RecentFileSettings.cs
blob17216c304aa72f6dc86b520b781823048611d310
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
4 ' Copyright 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright 2000-2002 Philip A. Craig
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.
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:
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.
19 ' Portions Copyright 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright 2000-2002 Philip A. Craig
22 ' 2. Altered source versions must be plainly marked as such, and must not be
23 ' misrepresented as being the original software.
25 ' 3. This notice may not be removed or altered from any source distribution.
27 '***********************************************************************************/
28 #endregion
30 using System;
31 using System.Collections;
33 namespace NUnit.Util
35 /// <summary>
36 /// Base class for settings that hold lists of recent files
37 /// </summary>
38 public abstract class RecentFileSettings : SettingsGroup
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;
47 public static readonly int MinSize = 1;
49 public static readonly int MaxSize = 24;
51 public static readonly int DefaultSize = 5;
53 public RecentFileSettings( string name ) : base ( name, UserSettings.GetStorageImpl( name ) )
55 LoadFiles();
58 public RecentFileSettings( string name, SettingsStorage storage ) : base( name, storage )
60 LoadFiles();
63 public RecentFileSettings( string name, SettingsGroup parent ) : base( name, parent )
65 LoadFiles();
68 public int MaxFiles
70 get
72 int size = LoadIntSetting( "MaxFiles", DefaultSize );
74 if ( size < MinSize ) size = MinSize;
75 if ( size > MaxSize ) size = MaxSize;
77 return size;
79 set
81 int oldSize = MaxFiles;
82 int newSize = value;
84 if ( newSize < MinSize ) newSize = MinSize;
85 if ( newSize > MaxSize ) newSize = MaxSize;
87 SaveIntSetting( "MaxFiles", newSize );
88 if ( newSize < oldSize ) SaveSettings();
92 protected void LoadFiles()
94 fileEntries = new ArrayList();
95 for ( int index = 1; index <= MaxFiles; index++ )
97 string fileName = LoadStringSetting( ValueName( index ) );
98 if ( fileName != null )
99 fileEntries.Add( fileName );
103 public override void Clear()
105 base.Clear();
106 fileEntries = new ArrayList();
109 public IList GetFiles()
111 LoadFiles();
112 return fileEntries;
115 public string RecentFile
117 get
119 LoadFiles();
120 if( fileEntries.Count > 0 )
121 return (string)fileEntries[0];
123 return null;
127 LoadFiles();
129 int index = fileEntries.IndexOf(value);
131 if(index == 0) return;
133 if(index != -1)
135 fileEntries.RemoveAt(index);
138 fileEntries.Insert( 0, value );
139 if( fileEntries.Count > MaxFiles )
140 fileEntries.RemoveAt( MaxFiles );
142 SaveSettings();
146 public void Remove( string fileName )
148 LoadFiles();
149 fileEntries.Remove( fileName );
150 SaveSettings();
153 private void SaveSettings()
155 while( fileEntries.Count > MaxFiles )
156 fileEntries.RemoveAt( fileEntries.Count - 1 );
158 for( int index = 0; index < MaxSize; index++ )
160 string valueName = ValueName( index + 1 );
161 if ( index < fileEntries.Count )
162 SaveSetting( valueName, fileEntries[index] );
163 else
164 RemoveSetting( valueName );
168 private string ValueName( int index )
170 return string.Format( "File{0}", index );