cid#1607171 Data race condition
[LibreOffice.git] / odk / examples / java / Inspector / ProtocolHandlerAddon.java
blob51158c39248f6b2769df96f5f93a1fb567b01406
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import com.sun.star.uno.XComponentContext;
37 import com.sun.star.lib.uno.helper.Factory;
38 import com.sun.star.lang.XSingleComponentFactory;
39 import com.sun.star.lib.uno.helper.WeakBase;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.registry.XRegistryKey;
42 import com.sun.star.lang.XInitialization;
43 import com.sun.star.lang.XServiceInfo;
44 import com.sun.star.frame.XStatusListener;
45 import com.sun.star.frame.XDispatchProvider;
46 import com.sun.star.frame.XDispatch;
47 import com.sun.star.frame.XFrame;
48 import com.sun.star.frame.DispatchDescriptor;
49 import com.sun.star.awt.XToolkit;
50 import com.sun.star.awt.XWindowPeer;
51 import com.sun.star.awt.XMessageBox;
52 import com.sun.star.awt.WindowAttribute;
53 import com.sun.star.awt.WindowClass;
54 import com.sun.star.awt.WindowDescriptor;
55 import com.sun.star.awt.Rectangle;
57 public class ProtocolHandlerAddon {
58 /** This class implements the component. At least the interfaces XServiceInfo,
59 * XTypeProvider, and XInitialization should be provided by the service.
61 public static class ProtocolHandlerAddonImpl extends WeakBase implements
62 XDispatchProvider,
63 XDispatch,
64 XInitialization,
65 XServiceInfo {
67 /** The service name, that must be used to get an instance of this service.
69 private static final String[] m_serviceNames = { "com.sun.star.frame.ProtocolHandler" };
71 /** The component context, that gives access to the service manager and all registered services.
73 private XComponentContext m_xCmpCtx;
75 /** The toolkit, that we can create UNO dialogs.
77 private XToolkit m_xToolkit;
79 /** The frame where the addon depends on.
81 private XFrame m_xFrame;
84 /** The constructor of the inner class has a XMultiServiceFactory parameter.
85 * @param xComponentContext A special service factory
86 * could be introduced while initializing.
88 public ProtocolHandlerAddonImpl( XComponentContext xComponentContext ) {
89 m_xCmpCtx = xComponentContext;
92 /** This method is a member of the interface for initializing an object
93 * directly after its creation.
94 * @param object This array of arbitrary objects will be passed to the
95 * component after its creation.
96 * @throws com.sun.star.uno.Exception Every exception will not be handled, but will be
97 * passed to the caller.
99 public void initialize( Object[] object )
100 throws com.sun.star.uno.Exception {
102 if ( object.length > 0 )
104 m_xFrame = UnoRuntime.queryInterface(
105 XFrame.class, object[ 0 ] );
108 // Create the toolkit to have access to it later
109 m_xToolkit = UnoRuntime.queryInterface(
110 XToolkit.class,
111 m_xCmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit",
112 m_xCmpCtx));
115 /** This method returns an array of all supported service names.
116 * @return Array of supported service names.
118 public String[] getSupportedServiceNames() {
119 return getServiceNames();
122 private static String[] getServiceNames() {
123 return m_serviceNames;
126 /** This method returns true, if the given service will be
127 * supported by the component.
128 * @param sService Service name.
129 * @return True, if the given service name will be supported.
131 public boolean supportsService( String sService ) {
132 int len = m_serviceNames.length;
134 for( int i=0; i < len; i++) {
135 if ( sService.equals( m_serviceNames[i] ) )
136 return true;
139 return false;
142 /** Return the class name of the component.
143 * @return Class name of the component.
145 public String getImplementationName() {
146 return ProtocolHandlerAddonImpl.class.getName();
149 // XDispatchProvider
150 public XDispatch queryDispatch( /*IN*/com.sun.star.util.URL aURL,
151 /*IN*/String sTargetFrameName,
152 /*IN*/int iSearchFlags ) {
153 XDispatch xRet = null;
154 if ( aURL.Protocol.equals("org.openoffice.Office.addon.example:") ) {
155 if ( aURL.Path.equals( "Function1" ) )
156 xRet = this;
157 if ( aURL.Path.equals( "Function2" ) )
158 xRet = this;
159 if ( aURL.Path.equals( "Help" ) )
160 xRet = this;
162 return xRet;
165 public XDispatch[] queryDispatches( /*IN*/DispatchDescriptor[] seqDescripts ) {
166 int nCount = seqDescripts.length;
167 XDispatch[] lDispatcher = new XDispatch[nCount];
169 for( int i=0; i<nCount; ++i )
170 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL,
171 seqDescripts[i].FrameName,
172 seqDescripts[i].SearchFlags );
174 return lDispatcher;
177 // XDispatch
178 public void dispatch( /*IN*/com.sun.star.util.URL aURL,
179 /*IN*/com.sun.star.beans.PropertyValue[] aArguments ) {
181 if ( aURL.Protocol.equals("org.openoffice.Office.addon.example:") )
183 if ( aURL.Path.equals( "Function1" ) )
185 showMessageBox("SDK DevGuide Add-On example", "Function 1 activated");
187 if ( aURL.Path.equals( "Function2" ) )
189 showMessageBox("SDK DevGuide Add-On example", "Function 2 activated");
191 if ( aURL.Path.equals( "Help" ) )
193 showMessageBox("About SDK DevGuide Add-On example", "This is the SDK Add-On example");
198 public void addStatusListener( /*IN*/XStatusListener xControl,
199 /*IN*/com.sun.star.util.URL aURL ) {
202 public void removeStatusListener( /*IN*/XStatusListener xControl,
203 /*IN*/com.sun.star.util.URL aURL ) {
206 private void showMessageBox(String sTitle, String sMessage) {
207 if ( null != m_xFrame && null != m_xToolkit ) {
209 // describe window properties.
210 WindowDescriptor aDescriptor = new WindowDescriptor();
211 aDescriptor.Type = WindowClass.MODALTOP;
212 aDescriptor.WindowServiceName = "infobox";
213 aDescriptor.ParentIndex = -1;
214 aDescriptor.Parent = UnoRuntime.queryInterface(
215 XWindowPeer.class, m_xFrame.getContainerWindow());
216 aDescriptor.Bounds = new Rectangle(0,0,300,200);
217 aDescriptor.WindowAttributes = WindowAttribute.BORDER |
218 WindowAttribute.MOVEABLE |
219 WindowAttribute.CLOSEABLE;
221 XWindowPeer xPeer = m_xToolkit.createWindow( aDescriptor );
222 if ( null != xPeer ) {
223 XMessageBox xMsgBox = UnoRuntime.queryInterface(
224 XMessageBox.class, xPeer);
225 if ( null != xMsgBox )
227 xMsgBox.setCaptionText( sTitle );
228 xMsgBox.setMessageText( sMessage );
229 xMsgBox.execute();
237 /** Gives a factory for creating the service.
238 * This method is called by the <code>JavaLoader</code>
239 * <p>
240 * @return Returns a <code>XSingleServiceFactory</code> for creating the
241 * component.
242 * @see com.sun.star.comp.loader.JavaLoader
243 * @param sImplementationName The implementation name of the component.
245 public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
246 XSingleComponentFactory xFactory = null;
248 if ( sImplementationName.equals( ProtocolHandlerAddonImpl.class.getName() ) )
249 xFactory = Factory.createComponentFactory(ProtocolHandlerAddonImpl.class,
250 ProtocolHandlerAddonImpl.getServiceNames());
252 return xFactory;
255 /** Writes the service information into the given registry key.
256 * This method is called by the <code>JavaLoader</code>.
257 * @return returns true if the operation succeeded
258 * @see com.sun.star.comp.loader.JavaLoader
259 * @see com.sun.star.lib.uno.helper.Factory
260 * @param xRegistryKey Makes structural information (except regarding tree
261 * structures) of a single
262 * registry key accessible.
264 public static boolean __writeRegistryServiceInfo(
265 XRegistryKey xRegistryKey ) {
266 return Factory.writeRegistryServiceInfo(
267 ProtocolHandlerAddonImpl.class.getName(),
268 ProtocolHandlerAddonImpl.getServiceNames(),
269 xRegistryKey );
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */