New test.
[mono.git] / mcs / class / System / System.ComponentModel / Component.cs
1 //
2 // System.ComponentModel.Component.cs
3 //
4 // Author:
5 //  Miguel de Icaza (miguel@ximian.com)
6 //  Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // (C) 2003 Andreas Nahr
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.ComponentModel;
35
36 namespace System.ComponentModel {
37
38         [DesignerCategory ("Component")]
39         public class Component : MarshalByRefObject, IComponent, IDisposable
40         {
41
42                 private EventHandlerList event_handlers;
43                 private ISite mySite;
44                 private object disposedEvent = new object ();
45
46                 public Component ()
47                 {
48                         event_handlers = null;
49                 }
50
51                 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
52                 public IContainer Container {
53                         get {
54                                 if (mySite == null)
55                                         return null;
56                                 return mySite.Container;
57                         }
58                 }
59
60                 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
61                 protected bool DesignMode {
62                         get {
63                                 if (mySite == null)
64                                         return false;
65                                 return mySite.DesignMode;
66                         }
67                 }
68
69                 protected EventHandlerList Events {
70                         get {
71                                 // Note: space vs. time tradeoff
72                                 // We create the object here if it's never be accessed before.  This potentially 
73                                 // saves space. However, we must check each time the propery is accessed to
74                                 // determine whether we need to create the object, which increases overhead.
75                                 // We could put the creation in the contructor, but that would waste space
76                                 // if it were never used.  However, accessing this property would be faster.
77                                 if (null == event_handlers)
78                                         event_handlers = new EventHandlerList();
79
80                                 return event_handlers;
81                         }
82                 }
83
84                 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
85                 public virtual ISite Site {
86                         get { return mySite; }
87                         set { mySite = value; }
88                 }
89
90                 ~Component()
91                 {
92                         Dispose (false);
93                 }
94
95 #if TARGET_JVM
96                 public virtual void Dispose ()
97                 {
98                         Dispose (true);
99                 }
100 #else
101                 public void Dispose ()
102                 {
103                         Dispose (true);
104                         GC.SuppressFinalize (this);
105                 }
106 #endif
107
108                 // <summary>
109                 //   Controls disposal of resources used by this.
110                 // </summary>
111                 //
112                 // <param name="release_all"> Controls which resources are released</param>
113                 //
114                 // <remarks>
115                 //   if release_all is set to true, both managed and unmanaged
116                 //   resources should be released.  If release_all is set to false,
117                 //   only unmanaged resources should be disposed
118                 // </remarks>
119                 protected virtual void Dispose (bool release_all)
120                 {
121                         if (release_all) {
122                                 EventHandler eh = (EventHandler) Events [disposedEvent];
123                                 if (eh != null)
124                                         eh (this, EventArgs.Empty);
125                         }
126                         mySite = null;
127                 }
128
129                 protected virtual object GetService (Type service)
130                 {
131                         if (mySite != null) {
132                                 return mySite.GetService(service); 
133                         }
134                         return null; 
135                 }
136
137                 public override string ToString ()
138                 {
139                         if (mySite == null)
140                                 return GetType ().ToString ();
141                         return String.Format ("{0} [{1}]", mySite.Name, GetType ().ToString ());
142                 }
143
144                 [Browsable (false), EditorBrowsable (EditorBrowsableState.Advanced)]
145                 public event EventHandler Disposed {
146                         add { Events.AddHandler (disposedEvent, value); }
147                         remove { Events.RemoveHandler (disposedEvent, value); }
148                 }
149         }
150         
151 }