1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import com.sun.star.uno.UnoRuntime;
25 import com.sun.star.uno.XComponentContext;
26 import com.sun.star.lang.XMultiComponentFactory;
27 import com.sun.star.frame.XDesktop;
28 
29 /**
30  *
31  * @author  dschulten
32  */
33 public class TerminationTest extends java.lang.Object {
34 
35     private static boolean atWork = false;
36     /**
37      * @param args the command line arguments
38      */
main(String[] args)39     public static void main(String[] args) {
40 
41         XComponentContext xRemoteContext = null;
42         XMultiComponentFactory xRemoteServiceManager = null;
43         XDesktop xDesktop = null;
44 
45         try {
46             // get the remote office context. If necessary a new office
47             // process is started
48             xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
49             System.out.println("Connected to a running office ...");
50             xRemoteServiceManager = xRemoteContext.getServiceManager();
51 
52             Object desktop = xRemoteServiceManager.createInstanceWithContext(
53                 "com.sun.star.frame.Desktop", xRemoteContext);
54             xDesktop = (XDesktop)UnoRuntime.queryInterface(XDesktop.class, desktop);
55 
56             TerminateListener terminateListener = new TerminateListener();
57             xDesktop.addTerminateListener(terminateListener);
58 
59             atWork = true;
60             // try to terminate while we are at work
61             boolean terminated = xDesktop.terminate();
62             System.out.println("The Office " +
63                 (terminated == true ?
64                  "has been terminated" :
65                  "is still running, we are at work"));
66 
67             // no longer at work
68             atWork = false;
69             // once more: try to terminate
70             terminated = xDesktop.terminate();
71             System.out.println("The Office " +
72                 (terminated == true ?
73                     "has been terminated" :
74                     "is still running. Someone else prevents termination, " +
75                     "e.g. the quickstarter"));
76         }
77         catch (java.lang.Exception e){
78             e.printStackTrace();
79         }
80         finally {
81             System.exit(0);
82         }
83 
84 
85     }
isAtWork()86     public static boolean isAtWork() {
87         return atWork;
88     }
89 
90 }
91