2004-01-19 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / nunit20 / util / ProjectConfigCollection.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         /// Summary description for ProjectConfigCollection.
37         /// </summary>
38         public class ProjectConfigCollection : CollectionBase
39         {
40                 protected NUnitProject project;
41
42                 public ProjectConfigCollection( NUnitProject project ) 
43                 { 
44                         this.project = project;
45                 }
46
47                 #region Properties
48
49                 public NUnitProject Project
50                 {
51                         get { return project; }
52                 }
53
54                 public ArrayList Names
55                 {
56                         get
57                         {
58                                 ArrayList names = new ArrayList();
59                                 
60                                 foreach( ProjectConfig config in InnerList )
61                                         names.Add( config.Name );
62
63                                 return names;
64                         }
65                 }
66
67                 public ProjectConfig this[int index]
68                 {
69                         get { return (ProjectConfig)InnerList[index]; }
70                 }
71
72                 public ProjectConfig this[string name]
73                 {
74                         get 
75                         { 
76                                 int index = IndexOf( name );
77                                 return index >= 0 ? (ProjectConfig)InnerList[index]: null;
78                         }
79                 }
80                 #endregion
81
82                 #region Methods
83
84                 public void Add( ProjectConfig config )
85                 {
86                         List.Add( config );
87                         config.Project = this.Project;
88                 }
89
90                 public void Add( string name )
91                 {
92                         Add( new ProjectConfig( name ) );
93                 }
94
95                 public void Remove( ProjectConfig config )
96                 {
97                         string name = config.Name;
98                         bool wasActive = name == this.Project.ActiveConfigName;
99                         List.Remove( config );
100                 }
101
102                 public void Remove( string name )
103                 {
104                         int index = IndexOf( name );
105                         if ( index >= 0 )
106                         {
107                                 bool wasActive = name == this.Project.ActiveConfigName;
108                                 RemoveAt( index );
109                         }
110                 }
111
112                 public int IndexOf( ProjectConfig config )
113                 {
114                         return InnerList.IndexOf( config );
115                 }
116
117                 public int IndexOf( string name )
118                 {
119                         for( int index = 0; index < InnerList.Count; index++ )
120                         {
121                                 ProjectConfig config = (ProjectConfig)InnerList[index];
122                                 if( config.Name == name )
123                                         return index;
124                         }
125
126                         return -1;
127                 }
128
129                 public bool Contains( ProjectConfig config )
130                 {
131                         return InnerList.Contains( config );
132                 }
133
134                 public bool Contains( string name )
135                 {
136                         return IndexOf( name ) >= 0;
137                 }
138
139                 protected override void OnRemoveComplete( int index, object obj )
140                 {
141                         ProjectConfig config = obj as ProjectConfig;
142                         this.Project.OnProjectChange( ProjectChangeType.RemoveConfig, config.Name );
143                 }
144
145                 protected override void OnInsertComplete( int index, object obj )
146                 {
147                         ProjectConfig config = obj as ProjectConfig;
148                         project.OnProjectChange( ProjectChangeType.AddConfig, config.Name );
149                         config.Changed += new EventHandler( OnConfigChanged );
150                 }
151
152                 protected override void OnSetComplete( int index, object oldValue, object newValue )
153                 {
154                         ProjectConfig oldConfig = oldValue as ProjectConfig;
155                         ProjectConfig newConfig = newValue as ProjectConfig;
156                         bool active = oldConfig.Name == project.ActiveConfigName;
157                         
158                         project.OnProjectChange( ProjectChangeType.UpdateConfig, newConfig.Name );
159                 }
160
161                 private void OnConfigChanged( object sender, EventArgs e )
162                 {
163                         ProjectConfig config = sender as ProjectConfig;
164                         project.OnProjectChange( ProjectChangeType.UpdateConfig, config.Name );
165                 }
166
167                 #endregion
168         }
169 }