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: ComponentBase.java,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 ************************************************************************/
31 package com
.sun
.star
.lib
.uno
.helper
;
32 import com
.sun
.star
.lang
.XComponent
;
33 import com
.sun
.star
.lang
.XEventListener
;
34 import com
.sun
.star
.lang
.EventObject
;
35 import com
.sun
.star
.uno
.Type
;
37 /** This class can be used as the base class for UNO components. In addition to the functionality ,which
38 * is inherited from WeakBase, it implements com.sun.star.lang.XComponent.
40 public class ComponentBase
extends WeakBase
implements XComponent
42 private final boolean DEBUG
= false;
43 protected MultiTypeInterfaceContainer listenerContainer
;
44 protected boolean bInDispose
= false;
45 protected boolean bDisposed
= false;
46 static final Type EVT_LISTENER_TYPE
= new Type(XEventListener
.class);
49 /** Creates a new instance of CompBase */
50 public ComponentBase()
53 listenerContainer
= new MultiTypeInterfaceContainer();
56 /** Override to perform extra clean-up work. Provided for subclasses. It is
57 called during dispose()
59 protected void preDisposing()
62 /** Override to become notified right before the disposing action is performed.
64 protected void postDisposing()
69 /** Method of XComponent. It is called by the owning client when the component is not needed
70 * anymore. The registered listeners are notified that this method has been called.
74 // Determine in a thread-safe way if this is the first call to this method.
75 // Only then we proceed with the notification of event listeners.
76 // It is an error to call this method more then once.
77 boolean bDoDispose
= false;
80 if ( ! bInDispose
&& ! bDisposed
)
86 // The notification occures in an unsynchronized block in order to avoid
87 // deadlocks if one of the listeners calls back in a different thread on
88 // a synchronized method which uses the same object.
94 listenerContainer
.disposeAndClear(new EventObject(this));
95 //notify subclasses that disposing is in progress
100 // finally makes sure that the flags are set even if a RuntimeException is thrown.
101 // That ensures that this function is only called once.
108 // in a multithreaded environment, it can't be avoided, that dispose is called twice.
109 // However this condition is traced, because it MAY indicate an error.
111 System
.out
.println("OComponentHelper::dispose() - dispose called twice" );
115 /** Method of XComponent.
117 public void removeEventListener(XEventListener xEventListener
)
119 listenerContainer
.removeInterface( EVT_LISTENER_TYPE
, xEventListener
);
122 public void addEventListener(XEventListener listener
)
124 boolean bDoDispose
= false;
127 if (bDisposed
|| bInDispose
)
130 listenerContainer
.addInterface(EVT_LISTENER_TYPE
, listener
);
134 listener
.disposing( new EventObject(this));
138 protected void finalize() throws Throwable
140 if ( ! bInDispose
&& ! bDisposed
)