merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / Accessibility / ConnectionTask.java
blobcd1398252b893024364850e83e7ae8bf48562f3a
1 /*************************************************************************
3 * $RCSfile: ConnectionTask.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:02:37 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import java.awt.event.ActionListener;
42 import javax.swing.*;
43 import java.awt.*;
44 import java.util.*;
46 import com.sun.star.awt.XFocusListener;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.bridge.XUnoUrlResolver;
49 import com.sun.star.lang.XMultiServiceFactory;
50 import com.sun.star.accessibility.*;
51 import com.sun.star.awt.XExtendedToolkit;
54 /** This timer task tries to connect to a running Office application in regular
55 intervals until a connection can be successfully established.
57 class ConnectionTask
58 extends TimerTask
60 public ConnectionTask (EventListenerProxy xListener)
62 Init (xListener);
65 private void Init (EventListenerProxy xListener)
67 mxListener = xListener;
68 mbInitialized = false;
70 maTimer = new java.util.Timer ();
71 maTimer.schedule (this, 0, mnPeriod);
75 /** This method is run every time the task is executed. It tries to
76 connect to and register the listener at an Office application. If it
77 can establish a connection it terminates the timer task. Otherwise it
78 waits until the next activation.
80 public void run ()
82 if (registerListeners())
84 // Focus listener was successfully registered so we can cancel this task.
85 MessageArea.println ("\nconnected successfully to office");
86 cancel ();
87 maTimer = null;
94 /** Try to register the listener.
96 private boolean registerListeners ()
98 // Get toolkit.
99 XExtendedToolkit xToolkit = getToolkit ();
101 // Register at toolkit as focus event listener.
102 if (xToolkit != null)
104 xToolkit.addTopWindowListener (mxListener);
105 int nTopWindowCount = xToolkit.getTopWindowCount();
108 com.sun.star.lang.EventObject aEvent = new com.sun.star.lang.EventObject();
109 for (int i=0; i<nTopWindowCount; i++)
111 XAccessible xAccessible = (XAccessible) UnoRuntime.queryInterface(
112 XAccessible.class,
113 xToolkit.getTopWindow(i));
114 XAccessibleContext xContext = xAccessible.getAccessibleContext();
115 if (xContext.getAccessibleName().length() > 0)
117 // Simulate an event that leads to registration the
118 // listener at the window.
119 aEvent.Source = xToolkit.getTopWindow(i);
120 mxListener.windowOpened (aEvent);
125 catch (com.sun.star.lang.IndexOutOfBoundsException aException)
127 // This exception signals that the number of top windows has
128 // changed since our last call to getTopWindowCount().
130 return true;
132 else
133 return false;
139 /** Get the toolkit from an Office which can then be used to register
140 the listener.
142 private XExtendedToolkit getToolkit ()
144 XMultiServiceFactory xFactory = connectToOffice();
146 // Get toolkit.
147 XExtendedToolkit xToolkit = null;
150 if (xFactory != null)
152 xToolkit = (XExtendedToolkit) UnoRuntime.queryInterface(
153 XExtendedToolkit.class,
154 xFactory.createInstance ("stardiv.Toolkit.VCLXToolkit"));
157 catch (com.sun.star.uno.Exception aException)
159 MessageArea.println ("caught exception while creating extended toolkit");
160 // Ignored.
163 return xToolkit;
169 /** Connect to a running (Star|Open)Office application
171 private XMultiServiceFactory connectToOffice ()
173 // connect to a running office and get the ServiceManager
176 com.sun.star.uno.XComponentContext xCmpContext = null;
178 // get the remote office component context
179 xCmpContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
180 if( xCmpContext != null )
181 System.out.println("Connected to a running office ...");
183 // get the remote office service manager
184 com.sun.star.lang.XMultiComponentFactory xMCF =
185 xCmpContext.getServiceManager();
187 return (XMultiServiceFactory) UnoRuntime.queryInterface (
188 XMultiServiceFactory.class, xMCF);
191 catch (Exception e)
193 if ( ! mbInitialized)
194 MessageArea.println ("Could not connect to office");
195 else
196 MessageArea.print (".");
198 mbInitialized = true;
199 return null;
202 /** Time in milliseconds between two attempts to connect to an Office
203 application.
205 private int mnPeriod = 1000;
207 private EventListenerProxy mxListener;
208 private boolean mbInitialized;
210 /** This timer is used for the registration loop to retry to connect to
211 the Office until a connection can be established.
213 private java.util.Timer maTimer;