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 package integration.extensions;
25 
26 import com.sun.star.uno.*;
27 import com.sun.star.lang.XComponent;
28 
29 public class ConsoleWait implements com.sun.star.lang.XEventListener
30 {
31     private Object  m_disposable;
32 
33     /** a helper class which waits for a console ENTER key event in a dedicated thread,
34         and notifies a ConsoleWait object if this event happened
35      */
36     private class WaitForEnter extends java.lang.Thread
37     {
38         private ConsoleWait m_toNotify;
39         private boolean     m_done;
40 
WaitForEnter( ConsoleWait _toNotify )41         public WaitForEnter( ConsoleWait _toNotify )
42         {
43             m_toNotify = _toNotify;
44             m_done = false;
45         }
46 
isDone()47         public boolean isDone()
48         {
49             return m_done;
50         }
51 
run()52         public void run()
53         {
54             try
55             {
56                 System.out.println( "\npress enter to exit" );
57                 System.in.read();
58 
59                 m_done = true;
60                 // notify that the user pressed the key
61                 synchronized ( m_toNotify )
62                 {
63                     m_toNotify.notify();
64                 }
65             }
66             catch( java.lang.Exception e )
67             {
68                 // not really interested in
69                 System.err.println( e );
70             }
71         }
72     };
73 
74     /** creates a ConsoleWait instance
75      *  @param _disposable
76      *      a component whose disposal should be monitored. When this component dies,
77      *      the ConsoleWait also returns from an waitForConsole call, even if the user
78      *      did not yet press the enter key
79      */
ConsoleWait( Object _disposable )80     public ConsoleWait( Object _disposable )
81     {
82         m_disposable = _disposable;
83         XComponent component = (XComponent)UnoRuntime.queryInterface( XComponent.class, _disposable );
84         if ( component != null )
85             component.addEventListener( this );
86     }
87 
88     /** waits for the user to press the ENTER key (on the console where she started the java program)
89         or the disposable component to be closed by the user.
90         @return
91             TRUE if the user pressed a key on the console, FALSE if she closed the document
92     */
waitForUserInput()93     public boolean waitForUserInput() throws java.lang.Exception
94     {
95         synchronized (this)
96         {
97             WaitForEnter keyWaiter = new WaitForEnter( this );
98             keyWaiter.start();
99             wait();
100 
101             // if the waiter thread is done, the user pressed enter
102             boolean bKeyPressed = keyWaiter.isDone();
103             if ( !bKeyPressed )
104                 keyWaiter.interrupt();
105 
106             return bKeyPressed;
107         }
108     }
109 
110     /* ------------------------------------------------------------------ */
111     /* XEventListener overridables                                        */
112     /* ------------------------------------------------------------------ */
disposing( com.sun.star.lang.EventObject eventObject )113     public void disposing( com.sun.star.lang.EventObject eventObject )
114     {
115         if ( eventObject.Source.equals( m_disposable ) )
116         {
117             // notify ourself that we can stop waiting for user input
118             synchronized (this)
119             {
120                 notify();
121             }
122         }
123     }
124 }