1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 // __________ Imports __________
40 // __________ Implementation __________
43 * It's implement a static container which hold
44 * all opened documents and her views alive.
45 * It's possible to register/deregister such views,
46 * to get information about these and it provides
47 * some global functionality - like termination of
48 * this demo application.
51 public class ViewContainer
extends Thread
56 * provides a singleton view container
57 * Necessary for terminate(9 functionality to be able
58 * to call Runtime.runFinilization().
60 * @return a reference to the singleton ViewContainer instance
62 public static synchronized ViewContainer
getGlobalContainer()
64 if (maSingleton
==null)
65 maSingleton
=new ViewContainer();
73 * It's private - because nobody should create any instance
74 * expect the only global one, which will be created by ourself!
76 private ViewContainer()
78 mlViews
= new ArrayList
<Object
>();
79 mlListener
= new ArrayList
<IShutdownListener
>();
80 mbShutdownActive
= false ;
81 Runtime
.getRuntime().addShutdownHook(this);
87 * This register a new view inside this global container
88 * (if it doesn't already exist).
90 * @param aView view which wishes to be registered inside this container
92 public void addView(Object aView
)
96 if(!mlViews
.contains(aView
))
104 * This deregister a view from this global container.
105 * Normally it should be the last reference to the view
106 * and her finalize() method should be called.
107 * If last view will be closed here - we terminate these
108 * java application too. Because there is no further
109 * visible frame anymore.
112 * view object which wishes to be deregistered
114 public void removeView(Object aView
)
117 synchronized(mlViews
)
119 if(mlViews
.contains(aView
))
120 mlViews
.remove(aView
);
122 nViewCount
= mlViews
.size();
127 // If this view is a registered shutdown listener on this view container
128 // too, we must call his interface and forget him as possible listener.
129 // It's necessary to guarantee his dead ...
130 boolean bShutdownView
= false;
131 synchronized(mlListener
)
133 bShutdownView
= mlListener
.contains(aView
);
135 mlListener
.remove(aView
);
138 ((IShutdownListener
)aView
).shutdown();
140 // We use a system.exit() to finish the whole application.
141 // And further we have registered THIS instance as a possible shutdown
142 // hook at the runtime class. So our run() method will be called.
143 // Our view container should be empty - but
144 // our listener container can include some references.
145 // These objects will be informed then and release e.g. some
146 // remote references.
149 boolean bNecessary
= false;
152 bNecessary
= ! mbShutdownActive
;
156 System
.out
.println("call exit(0)!");
165 * add/remove listener for possible shutdown events
167 public void addListener( IShutdownListener rListener
)
169 synchronized(mlListener
)
171 if ( ! mlListener
.contains(rListener
) )
172 mlListener
.add(rListener
);
178 private void removeListener( IShutdownListener rListener
)
180 synchronized(mlListener
)
182 if ( mlListener
.contains(rListener
) )
183 mlListener
.remove(rListener
);
190 * Is called from current runtime system of the java machine
191 * on shutdown. We inform all current registered listener and
192 * views. They should deinitialize her internal things then.
199 if (mbShutdownActive
)
201 mbShutdownActive
=true;
206 IShutdownListener aListener
= null;
207 synchronized(mlListener
)
209 if (!mlListener
.isEmpty())
210 aListener
= mlListener
.get(0);
215 aListener
.shutdown();
216 // May this listener has deregistered himself.
217 // But if not we must do it for him. Our own
218 // method "removeListener()" ignore requests for
219 // already gone listener objects.
220 removeListener(aListener
);
225 synchronized(mlViews
)
232 if (mlListener
!=null)
234 synchronized(mlListener
)
245 * @member mbInplace indicates using of inplace office frames instead of outplace ones
246 * @member maSingleton singleton instance of this view container
247 * @member mlViews list of all currently registered document views
248 * @member mlListener list of all currently registered shutdown listener
249 * @member mbShutdownActive if this shutdown hook already was started it's not a good idea to
250 * call System.exit() again for other conditions.
251 * We suppress it by using this variable!
253 public static boolean mbInplace
= false ;
254 private static ViewContainer maSingleton
= null ;
255 private ArrayList
<Object
> mlViews
;
256 private ArrayList
<IShutdownListener
> mlListener
;
257 private boolean mbShutdownActive
;
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */