Remove the NET_2_0 directives from CultureInfoConverter.cs
[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 using System.Runtime.InteropServices;
36
37 namespace System.ComponentModel {
38
39         [DesignerCategory ("Component")]
40 #if NET_2_0
41         [ComVisible (true)]
42         [ClassInterface (ClassInterfaceType.AutoDispatch)]
43 #endif
44         public class Component : MarshalByRefObject, IComponent, IDisposable
45         {
46
47                 private EventHandlerList event_handlers;
48                 private ISite mySite;
49                 private object disposedEvent = new object ();
50
51                 public Component ()
52                 {
53                         event_handlers = null;
54                 }
55
56 #if NET_2_0
57                 protected virtual bool CanRaiseEvents {
58                         get { return false; }
59                 }
60 #endif
61
62                 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
63                 public virtual ISite Site
64                 {
65                         get { return mySite; }
66                         set { mySite = value; }
67                 }
68
69                 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
70                 public IContainer Container {
71                         get {
72                                 if (mySite == null)
73                                         return null;
74                                 return mySite.Container;
75                         }
76                 }
77
78                 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
79                 protected bool DesignMode {
80                         get {
81                                 if (mySite == null)
82                                         return false;
83                                 return mySite.DesignMode;
84                         }
85                 }
86
87                 protected EventHandlerList Events {
88                         get {
89                                 // Note: space vs. time tradeoff
90                                 // We create the object here if it's never be accessed before.  This potentially 
91                                 // saves space. However, we must check each time the propery is accessed to
92                                 // determine whether we need to create the object, which increases overhead.
93                                 // We could put the creation in the contructor, but that would waste space
94                                 // if it were never used.  However, accessing this property would be faster.
95                                 if (null == event_handlers)
96                                         event_handlers = new EventHandlerList();
97
98                                 return event_handlers;
99                         }
100                 }
101
102                 ~Component()
103                 {
104                         Dispose (false);
105                 }
106
107 #if TARGET_JVM
108                 public virtual void Dispose ()
109                 {
110                         Dispose (true);
111                 }
112 #else
113                 public void Dispose ()
114                 {
115                         Dispose (true);
116                         GC.SuppressFinalize (this);
117                 }
118 #endif
119
120                 // <summary>
121                 //   Controls disposal of resources used by this.
122                 // </summary>
123                 //
124                 // <param name="release_all"> Controls which resources are released</param>
125                 //
126                 // <remarks>
127                 //   if release_all is set to true, both managed and unmanaged
128                 //   resources should be released.  If release_all is set to false,
129                 //   only unmanaged resources should be disposed
130                 // </remarks>
131                 protected virtual void Dispose (bool release_all)
132                 {
133                         if (release_all) {
134                                 if (mySite != null && mySite.Container != null)
135                                         mySite.Container.Remove (this);
136                                 EventHandler eh = (EventHandler) Events [disposedEvent];
137                                 if (eh != null)
138                                         eh (this, EventArgs.Empty);
139                         }
140                 }
141
142                 protected virtual object GetService (Type service)
143                 {
144                         if (mySite != null) {
145                                 return mySite.GetService(service); 
146                         }
147                         return null; 
148                 }
149
150                 public override string ToString ()
151                 {
152                         if (mySite == null)
153                                 return GetType ().ToString ();
154                         return String.Format ("{0} [{1}]", mySite.Name, GetType ().ToString ());
155                 }
156
157                 [Browsable (false), EditorBrowsable (EditorBrowsableState.Advanced)]
158                 public event EventHandler Disposed {
159                         add { Events.AddHandler (disposedEvent, value); }
160                         remove { Events.RemoveHandler (disposedEvent, value); }
161                 }
162         }
163         
164 }