1 /*************************************************************************
3 * $RCSfile: ViewContainer.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:40:33 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 // __________ Imports __________
43 import com
.sun
.star
.uno
.UnoRuntime
;
48 // __________ Implementation __________
51 * It's implement a static container which hold
52 * all opened documents and her views alive.
53 * It's possible to register/deregister such views,
54 * to get information about these and it provides
55 * some global functionality - like termination of
56 * this demo application.
58 * @author Andreas Schlüns
59 * @created 01.03.2002 08:42
61 public class ViewContainer
extends Thread
63 // ____________________
66 * provides a singleton view container
67 * Neccessary for terminate(9 functionality to be able
68 * to call Runtime.runFinilization().
70 * @return a reference to the singleton ViewContainer instance
72 public static synchronized ViewContainer
getGlobalContainer()
74 if (maSingleton
==null)
75 maSingleton
=new ViewContainer();
79 // ____________________
83 * It's private - because nobody should create any instance
84 * expect the only global one, which wil be created by ourself!
86 private ViewContainer()
88 mlViews
= new Vector();
89 mlListener
= new Vector();
90 mbShutdownActive
= false ;
91 Runtime
.getRuntime().addShutdownHook(this);
94 // ____________________
97 * This register a new view inside this global container
98 * (if it doesnt already exist).
100 * @param aView view which whish to be registered inside this container
102 public void addView(Object aView
)
104 synchronized(mlViews
)
106 if(mlViews
.contains(aView
)==false)
111 // ____________________
114 * This deregister a view from this global container.
115 * Normaly it should be the last reference to the view
116 * and her finalize() method should be called.
117 * If last view will be closed here - we terminate these
118 * java application too. Because there is no further
119 * visible frame anymore.
122 * view object which wish to be deregistered
124 public void removeView(Object aView
)
127 synchronized(mlViews
)
129 if(mlViews
.contains(aView
)==true)
130 mlViews
.remove(aView
);
132 nViewCount
= mlViews
.size();
137 // If this view is a registered shutdown listener on this view container
138 // too, we must call his interface and forget him as possible listener.
139 // It's neccessary to guarantee his dead ...
140 boolean bShutdownView
= false;
141 synchronized(mlListener
)
143 bShutdownView
= mlListener
.contains(aView
);
144 if (bShutdownView
==true)
145 mlListener
.remove(aView
);
147 if (bShutdownView
==true)
148 ((IShutdownListener
)aView
).shutdown();
150 // We use a system.exit() to finish the whole application.
151 // And further we have registered THIS instance as a possible shutdown
152 // hook at the runtime class. So our run() method will be called.
153 // Teh our view container should be empty - but
154 // our listener container can include some references.
155 // These objects wich to be informed then and release e.g. some
156 // remote references.
159 boolean bNeccessary
= false;
162 bNeccessary
= ! mbShutdownActive
;
164 if (bNeccessary
==true)
166 System
.out
.println("call exit(0)!");
172 // ____________________
175 * add/remove listener for possibe shutdown events
177 public void addListener( IShutdownListener rListener
)
179 synchronized(mlListener
)
181 if ( ! mlListener
.contains(rListener
) )
182 mlListener
.add(rListener
);
186 // ____________________
188 public void removeListener( IShutdownListener rListener
)
190 synchronized(mlListener
)
192 if ( mlListener
.contains(rListener
) )
193 mlListener
.remove(rListener
);
197 // ____________________
200 * Is called from current runtime system of the java machine
201 * on shutdown. We inform all current registered listener and
202 * views. They should deinitialize her internal things then.
208 if (mbShutdownActive
)
210 mbShutdownActive
=true;
215 IShutdownListener aListener
= null;
216 synchronized(mlListener
)
219 aListener
= (IShutdownListener
)mlListener
.firstElement();
220 } catch(java
.util
.NoSuchElementException exEmpty
) {}
225 aListener
.shutdown();
226 // May this listener has dergeistered himself.
227 // But if not we must do it for him. Our own
228 // method "removeListener()" ignore requests for
229 // already gone listener objects.
230 removeListener(aListener
);
235 synchronized(mlViews
)
242 if (mlListener
!=null)
244 synchronized(mlListener
)
252 // ____________________
255 * @const BASICNAME it's used to create uinque names for all regieterd views
257 private static final String BASICNAME
= "Document View ";
259 // ____________________
262 * @member mbInplace indicates using of inplace office frames instead of outplace ones
263 * @member maSingleton singleton instance of this view container
264 * @member mlViews list of all currently registered document views
265 * @member mlListener list of all currently registered shutdown listener
266 * @member mbShutdownActive if this shutdown hook already was started it's not a good idea to
267 * call System.exit() again for other conditions.
268 * We supress it by using this variable!
270 public static boolean mbInplace
= false ;
271 private static ViewContainer maSingleton
= null ;
272 private Vector mlViews
;
273 private Vector mlListener
;
274 private boolean mbShutdownActive
;