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