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 .
19 package com
.sun
.star
.lib
.uno
.helper
;
20 import com
.sun
.star
.lang
.XComponent
;
21 import com
.sun
.star
.lang
.XEventListener
;
22 import com
.sun
.star
.lang
.EventObject
;
23 import com
.sun
.star
.uno
.Type
;
25 /** This class can be used as the base class for UNO components. In addition to the functionality ,which
26 * is inherited from WeakBase, it implements com.sun.star.lang.XComponent.
28 public class ComponentBase
extends WeakBase
implements XComponent
30 private static final boolean DEBUG
= false;
31 protected MultiTypeInterfaceContainer listenerContainer
;
32 protected boolean bInDispose
= false;
33 protected boolean bDisposed
= false;
34 static final Type EVT_LISTENER_TYPE
= new Type(XEventListener
.class);
37 /** Creates a new instance of CompBase */
38 public ComponentBase()
41 listenerContainer
= new MultiTypeInterfaceContainer();
44 /** Override to perform extra clean-up work. Provided for subclasses. It is
45 called during dispose()
47 protected void preDisposing()
50 /** Override to become notified right before the disposing action is performed.
52 protected void postDisposing()
57 /** Method of XComponent. It is called by the owning client when the component is not needed
58 * anymore. The registered listeners are notified that this method has been called.
62 // Determine in a thread-safe way if this is the first call to this method.
63 // Only then we proceed with the notification of event listeners.
64 // It is an error to call this method more than once.
65 boolean bDoDispose
= false;
68 if ( ! bInDispose
&& ! bDisposed
)
74 // The notification occurs in an unsynchronized block in order to avoid
75 // deadlocks if one of the listeners calls back in a different thread on
76 // a synchronized method which uses the same object.
82 listenerContainer
.disposeAndClear(new EventObject(this));
83 //notify subclasses that disposing is in progress
88 // finally makes sure that the flags are set even if a RuntimeException is thrown.
89 // That ensures that this function is only called once.
99 // in a multithreaded environment, it can't be avoided, that dispose is called twice.
100 // However this condition is traced, because it MAY indicate an error.
102 System
.out
.println("OComponentHelper::dispose() - dispose called twice" );
106 /** Method of XComponent.
108 public void removeEventListener(XEventListener xEventListener
)
110 listenerContainer
.removeInterface( EVT_LISTENER_TYPE
, xEventListener
);
113 public void addEventListener(XEventListener listener
)
115 boolean bDoDispose
= false;
118 if (bDisposed
|| bInDispose
)
121 listenerContainer
.addInterface(EVT_LISTENER_TYPE
, listener
);
125 listener
.disposing( new EventObject(this));
130 protected void finalize() throws Throwable
132 if ( ! bInDispose
&& ! bDisposed
)