1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WeakComponentBase.cs,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 using unoidl
.com
.sun
.star
.lang
;
37 /** This class can be used as a base class for UNO objects.
38 It implements the capability to be kept weakly
39 (unoidl.com.sun.star.uno.XWeak) and it implements
40 unoidl.com.sun.star.lang.XTypeProvider which is necessary for
41 using the object from StarBasic.
42 In addition, it implements the interface
43 unoidl.com.sun.star.lang.XComponent to be disposed explicitly.
45 public class WeakComponentBase
: WeakBase
, XComponent
47 private delegate void t_disposing( EventObject evt
);
48 private t_disposing m_disposing
= null;
49 private bool m_inDispose
= false;
50 private bool m_disposed
= false;
52 /** Indicates whether object is alrady disposed.
55 true, if object has been disposed
57 protected bool isDisposed()
65 /** Checks whether this object is disposed and throws a DisposedException
66 if it is already disposed.
68 protected void checkUnDisposed()
72 throw new unoidl
.com
.sun
.star
.lang
.DisposedException(
73 "object already disposed!", this );
82 doDispose
= (!m_inDispose
&& !m_disposed
);
90 /** Override to perform extra clean-up work. Provided for subclasses.
91 It is called during dispose()
93 protected void preDisposing()
97 /** Override to become notified right before the disposing action is
100 protected void postDisposing()
105 /** This method is called by the owner of this object to explicitly
106 dispose it. This implementation of dispose() first notifies this object
107 via preDisposing(), then all registered event listeners and
108 finally this object again calling postDisposing().
110 public void dispose()
112 // Determine in a thread-safe way if this is the first call to this
113 // method. Only then we proceed with the notification of event
114 // listeners. It is an error to call this method more then once.
115 bool doDispose
= false;
116 t_disposing call
= null;
119 if (! m_inDispose
&& !m_disposed
)
127 // The notification occures in an unsynchronized block in order to avoid
128 // deadlocks if one of the listeners calls back in a different thread on
129 // a synchronized method which uses the same object.
136 // send disposing notifications to listeners
139 EventObject evt
= new EventObject( this );
147 // finally makes sure that the flags are set ensuring
148 // that this function is only called once.
155 // in a multithreaded environment, it can't be avoided,
156 // that dispose is called twice.
157 // However this condition is traced, because it MAY indicate an
161 "WeakComponentBase.dispose() - dispose called twice" );
163 // Debug.Fail( "WeakComponentBase.dispose() - dispose called twice" );
166 /** Registers an event listener being notified when this object is disposed.
168 @param xListener event listener
170 public void addEventListener( XEventListener xListener
)
175 add = (! m_inDispose
&& !m_disposed
);
177 m_disposing
+= new t_disposing( xListener
.disposing
);
180 xListener
.disposing( new EventObject( this ) );
182 /** Revokes an event listener from being notified when this object
185 @param xListener event listener
187 public void removeEventListener( XEventListener xListener
)
191 if (! m_inDispose
&& !m_disposed
)
192 m_disposing
-= new t_disposing( xListener
.disposing
);