2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 using unoidl
.com
.sun
.star
.lang
;
25 /** This class can be used as a base class for UNO objects.
26 It implements the capability to be kept weakly
27 (unoidl.com.sun.star.uno.XWeak) and it implements
28 unoidl.com.sun.star.lang.XTypeProvider which is necessary for
29 using the object from StarBasic.
30 In addition, it implements the interface
31 unoidl.com.sun.star.lang.XComponent to be disposed explicitly.
33 public class WeakComponentBase
: WeakBase
, XComponent
35 private delegate void t_disposing( EventObject evt
);
36 private t_disposing m_disposing
= null;
37 private bool m_inDispose
= false;
38 private bool m_disposed
= false;
40 /** Indicates whether object is already disposed.
43 true, if object has been disposed
45 protected bool isDisposed()
53 /** Checks whether this object is disposed and throws a DisposedException
54 if it is already disposed.
56 protected void checkUnDisposed()
60 throw new unoidl
.com
.sun
.star
.lang
.DisposedException(
61 "object already disposed!", this );
70 doDispose
= (!m_inDispose
&& !m_disposed
);
78 /** Override to perform extra clean-up work. Provided for subclasses.
79 It is called during dispose()
81 protected void preDisposing()
85 /** Override to become notified right before the disposing action is
88 protected void postDisposing()
93 /** This method is called by the owner of this object to explicitly
94 dispose it. This implementation of dispose() first notifies this object
95 via preDisposing(), then all registered event listeners and
96 finally this object again calling postDisposing().
100 // Determine in a thread-safe way if this is the first call to this
101 // method. Only then we proceed with the notification of event
102 // listeners. It is an error to call this method more than once.
103 bool doDispose
= false;
104 t_disposing call
= null;
107 if (! m_inDispose
&& !m_disposed
)
115 // The notification occurs in an unsynchronized block in order to avoid
116 // deadlocks if one of the listeners calls back in a different thread on
117 // a synchronized method which uses the same object.
124 // send disposing notifications to listeners
127 EventObject evt
= new EventObject( this );
135 // finally makes sure that the flags are set ensuring
136 // that this function is only called once.
143 // in a multithreaded environment, it can't be avoided,
144 // that dispose is called twice.
145 // However this condition is traced, because it MAY indicate an
149 "WeakComponentBase.dispose() - dispose called twice" );
151 // Debug.Fail( "WeakComponentBase.dispose() - dispose called twice" );
154 /** Registers an event listener being notified when this object is disposed.
156 @param xListener event listener
158 public void addEventListener( XEventListener xListener
)
163 add = (! m_inDispose
&& !m_disposed
);
165 m_disposing
+= new t_disposing( xListener
.disposing
);
168 xListener
.disposing( new EventObject( this ) );
170 /** Revokes an event listener from being notified when this object
173 @param xListener event listener
175 public void removeEventListener( XEventListener xListener
)
179 if (! m_inDispose
&& !m_disposed
)
180 m_disposing
-= new t_disposing( xListener
.disposing
);