1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import java.awt.event.ActionListener;
36*cdf0e10cSrcweir import javax.swing.*;
37*cdf0e10cSrcweir import java.awt.*;
38*cdf0e10cSrcweir import java.util.*;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir import com.sun.star.awt.XFocusListener;
41*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
42*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
43*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
44*cdf0e10cSrcweir import com.sun.star.accessibility.*;
45*cdf0e10cSrcweir import com.sun.star.awt.XExtendedToolkit;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir /** This timer task tries to connect to a running Office application in regular
49*cdf0e10cSrcweir     intervals until a connection can be successfully established.
50*cdf0e10cSrcweir */
51*cdf0e10cSrcweir class ConnectionTask
52*cdf0e10cSrcweir     extends TimerTask
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir     public ConnectionTask (EventListenerProxy xListener)
55*cdf0e10cSrcweir     {
56*cdf0e10cSrcweir         Init (xListener);
57*cdf0e10cSrcweir     }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     private void Init (EventListenerProxy xListener)
60*cdf0e10cSrcweir     {
61*cdf0e10cSrcweir         mxListener = xListener;
62*cdf0e10cSrcweir         mbInitialized = false;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir         maTimer = new java.util.Timer ();
65*cdf0e10cSrcweir         maTimer.schedule (this, 0, mnPeriod);
66*cdf0e10cSrcweir     }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     /** This method is run every time the task is executed.  It tries to
70*cdf0e10cSrcweir         connect to and register the listener at an Office application.  If it
71*cdf0e10cSrcweir         can establish a connection it terminates the timer task.  Otherwise it
72*cdf0e10cSrcweir         waits until the next activation.
73*cdf0e10cSrcweir     */
74*cdf0e10cSrcweir     public void run ()
75*cdf0e10cSrcweir     {
76*cdf0e10cSrcweir         if (registerListeners())
77*cdf0e10cSrcweir         {
78*cdf0e10cSrcweir             // Focus listener was successfully registered so we can cancel this task.
79*cdf0e10cSrcweir             MessageArea.println ("\nconnected successfully to office");
80*cdf0e10cSrcweir             cancel ();
81*cdf0e10cSrcweir             maTimer = null;
82*cdf0e10cSrcweir         }
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     /** Try to register the listener.
89*cdf0e10cSrcweir     */
90*cdf0e10cSrcweir     private boolean registerListeners ()
91*cdf0e10cSrcweir     {
92*cdf0e10cSrcweir         // Get toolkit.
93*cdf0e10cSrcweir         XExtendedToolkit xToolkit = getToolkit ();
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         // Register at toolkit as focus event listener.
96*cdf0e10cSrcweir         if (xToolkit != null)
97*cdf0e10cSrcweir         {
98*cdf0e10cSrcweir             xToolkit.addTopWindowListener (mxListener);
99*cdf0e10cSrcweir             int nTopWindowCount = xToolkit.getTopWindowCount();
100*cdf0e10cSrcweir             try
101*cdf0e10cSrcweir             {
102*cdf0e10cSrcweir                 com.sun.star.lang.EventObject aEvent = new com.sun.star.lang.EventObject();
103*cdf0e10cSrcweir                 for (int i=0; i<nTopWindowCount; i++)
104*cdf0e10cSrcweir                 {
105*cdf0e10cSrcweir                     XAccessible xAccessible = (XAccessible) UnoRuntime.queryInterface(
106*cdf0e10cSrcweir                         XAccessible.class,
107*cdf0e10cSrcweir                         xToolkit.getTopWindow(i));
108*cdf0e10cSrcweir                     XAccessibleContext xContext = xAccessible.getAccessibleContext();
109*cdf0e10cSrcweir                     if (xContext.getAccessibleName().length() > 0)
110*cdf0e10cSrcweir                     {
111*cdf0e10cSrcweir                         // Simulate an event that leads to registration the
112*cdf0e10cSrcweir                         // listener at the window.
113*cdf0e10cSrcweir                         aEvent.Source = xToolkit.getTopWindow(i);
114*cdf0e10cSrcweir                         mxListener.windowOpened (aEvent);
115*cdf0e10cSrcweir                     }
116*cdf0e10cSrcweir                 }
117*cdf0e10cSrcweir             }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir             catch (com.sun.star.lang.IndexOutOfBoundsException aException)
120*cdf0e10cSrcweir             {
121*cdf0e10cSrcweir                 // This exception signals that the number of top windows has
122*cdf0e10cSrcweir                 // changed since our last call to getTopWindowCount().
123*cdf0e10cSrcweir             }
124*cdf0e10cSrcweir             return true;
125*cdf0e10cSrcweir         }
126*cdf0e10cSrcweir         else
127*cdf0e10cSrcweir             return false;
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     /** Get the toolkit from an Office which can then be used to register
134*cdf0e10cSrcweir         the listener.
135*cdf0e10cSrcweir     */
136*cdf0e10cSrcweir     private XExtendedToolkit getToolkit ()
137*cdf0e10cSrcweir     {
138*cdf0e10cSrcweir         XMultiServiceFactory xFactory = connectToOffice();
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         // Get toolkit.
141*cdf0e10cSrcweir         XExtendedToolkit xToolkit = null;
142*cdf0e10cSrcweir         try
143*cdf0e10cSrcweir         {
144*cdf0e10cSrcweir             if (xFactory != null)
145*cdf0e10cSrcweir             {
146*cdf0e10cSrcweir                 xToolkit = (XExtendedToolkit) UnoRuntime.queryInterface(
147*cdf0e10cSrcweir                     XExtendedToolkit.class,
148*cdf0e10cSrcweir                     xFactory.createInstance ("stardiv.Toolkit.VCLXToolkit"));
149*cdf0e10cSrcweir             }
150*cdf0e10cSrcweir         }
151*cdf0e10cSrcweir         catch (com.sun.star.uno.Exception aException)
152*cdf0e10cSrcweir         {
153*cdf0e10cSrcweir             MessageArea.println ("caught exception while creating extended toolkit");
154*cdf0e10cSrcweir             // Ignored.
155*cdf0e10cSrcweir         }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir         return xToolkit;
158*cdf0e10cSrcweir     }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     /** Connect to a running (Star|Open)Office application
164*cdf0e10cSrcweir     */
165*cdf0e10cSrcweir     private XMultiServiceFactory connectToOffice ()
166*cdf0e10cSrcweir     {
167*cdf0e10cSrcweir         // connect to a running office and get the ServiceManager
168*cdf0e10cSrcweir         try
169*cdf0e10cSrcweir         {
170*cdf0e10cSrcweir             com.sun.star.uno.XComponentContext xCmpContext = null;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir             // get the remote office component context
173*cdf0e10cSrcweir             xCmpContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
174*cdf0e10cSrcweir             if( xCmpContext != null )
175*cdf0e10cSrcweir                 System.out.println("Connected to a running office ...");
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir             // get the remote office service manager
178*cdf0e10cSrcweir             com.sun.star.lang.XMultiComponentFactory xMCF =
179*cdf0e10cSrcweir                 xCmpContext.getServiceManager();
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir             return (XMultiServiceFactory) UnoRuntime.queryInterface (
182*cdf0e10cSrcweir                 XMultiServiceFactory.class, xMCF);
183*cdf0e10cSrcweir         }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir         catch (Exception e)
186*cdf0e10cSrcweir         {
187*cdf0e10cSrcweir             if ( ! mbInitialized)
188*cdf0e10cSrcweir                 MessageArea.println ("Could not connect to office");
189*cdf0e10cSrcweir             else
190*cdf0e10cSrcweir                 MessageArea.print (".");
191*cdf0e10cSrcweir         }
192*cdf0e10cSrcweir         mbInitialized = true;
193*cdf0e10cSrcweir         return null;
194*cdf0e10cSrcweir     }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     /** Time in milliseconds between two attempts to connect to an Office
197*cdf0e10cSrcweir          application.
198*cdf0e10cSrcweir     */
199*cdf0e10cSrcweir     private int mnPeriod = 1000;
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     private EventListenerProxy mxListener;
202*cdf0e10cSrcweir     private boolean mbInitialized;
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     /** This timer is used for the registration loop to retry to connect to
205*cdf0e10cSrcweir         the Office until a connection can be established.
206*cdf0e10cSrcweir     */
207*cdf0e10cSrcweir     private java.util.Timer maTimer;
208*cdf0e10cSrcweir }
209