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