uno grid a11y: Use ImplInheritanceHelper
[LibreOffice.git] / ridljar / com / sun / star / lib / uno / helper / ComponentBase.java
blobd886ef7020d584729309c03a788201b630285959
1 /*
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()
40 super();
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.
60 public void dispose()
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;
66 synchronized (this)
68 if ( ! bInDispose && ! bDisposed)
70 bDoDispose= true;
71 bInDispose= true;
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.
77 if (bDoDispose)
79 try
81 preDisposing();
82 listenerContainer.disposeAndClear(new EventObject(this));
83 //notify subclasses that disposing is in progress
84 postDisposing();
86 finally
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.
90 synchronized (this)
92 bDisposed= true;
93 bInDispose= false;
97 else
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.
101 if (DEBUG)
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;
116 synchronized (this)
118 if (bDisposed || bInDispose)
119 bDoDispose= true;
120 else
121 listenerContainer.addInterface(EVT_LISTENER_TYPE, listener);
123 if (bDoDispose )
125 listener.disposing( new EventObject(this));
129 @Override
130 protected void finalize() throws Throwable
132 if ( ! bInDispose && ! bDisposed)
133 dispose();
134 super.finalize();