Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Inspector / ProtocolHandlerAddon.java
blob80e52db58f9aead3bd7273d04134e5f5794a7536
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
35 import com.sun.star.uno.XComponentContext;
36 import com.sun.star.lib.uno.helper.Factory;
37 import com.sun.star.lang.XSingleComponentFactory;
38 import com.sun.star.lib.uno.helper.WeakBase;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.registry.XRegistryKey;
41 import com.sun.star.lang.XInitialization;
42 import com.sun.star.lang.XServiceInfo;
43 import com.sun.star.frame.XStatusListener;
44 import com.sun.star.frame.XDispatchProvider;
45 import com.sun.star.frame.XDispatch;
46 import com.sun.star.frame.XFrame;
47 import com.sun.star.frame.DispatchDescriptor;
48 import com.sun.star.awt.XToolkit;
49 import com.sun.star.awt.XWindowPeer;
50 import com.sun.star.awt.XMessageBox;
51 import com.sun.star.awt.WindowAttribute;
52 import com.sun.star.awt.WindowClass;
53 import com.sun.star.awt.WindowDescriptor;
54 import com.sun.star.awt.Rectangle;
56 public class ProtocolHandlerAddon {
57 /** This class implements the component. At least the interfaces XServiceInfo,
58 * XTypeProvider, and XInitialization should be provided by the service.
60 public static class ProtocolHandlerAddonImpl extends WeakBase implements
61 XDispatchProvider,
62 XDispatch,
63 XInitialization,
64 XServiceInfo {
66 /** The service name, that must be used to get an instance of this service.
68 static private final String[] m_serviceNames = { "com.sun.star.frame.ProtocolHandler" };
70 /** The component context, that gives access to the service manager and all registered services.
72 private XComponentContext m_xCmpCtx;
74 /** The toolkit, that we can create UNO dialogs.
76 private XToolkit m_xToolkit;
78 /** The frame where the addon depends on.
80 private XFrame m_xFrame;
83 /** The constructor of the inner class has a XMultiServiceFactory parameter.
84 * @param xComponentContext A special service factory
85 * could be introduced while initializing.
87 public ProtocolHandlerAddonImpl( XComponentContext xComponentContext ) {
88 m_xCmpCtx = xComponentContext;
91 /** This method is a member of the interface for initializing an object
92 * directly after its creation.
93 * @param object This array of arbitrary objects will be passed to the
94 * component after its creation.
95 * @throws com.sun.star.uno.Exception Every exception will not be handled, but will be
96 * passed to the caller.
98 public void initialize( Object[] object )
99 throws com.sun.star.uno.Exception {
101 if ( object.length > 0 )
103 m_xFrame = UnoRuntime.queryInterface(
104 XFrame.class, object[ 0 ] );
107 // Create the toolkit to have access to it later
108 m_xToolkit = UnoRuntime.queryInterface(
109 XToolkit.class,
110 m_xCmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit",
111 m_xCmpCtx));
114 /** This method returns an array of all supported service names.
115 * @return Array of supported service names.
117 public String[] getSupportedServiceNames() {
118 return getServiceNames();
121 private static String[] getServiceNames() {
122 return m_serviceNames;
125 /** This method returns true, if the given service will be
126 * supported by the component.
127 * @param sService Service name.
128 * @return True, if the given service name will be supported.
130 public boolean supportsService( String sService ) {
131 int len = m_serviceNames.length;
133 for( int i=0; i < len; i++) {
134 if ( sService.equals( m_serviceNames[i] ) )
135 return true;
138 return false;
141 /** Return the class name of the component.
142 * @return Class name of the component.
144 public String getImplementationName() {
145 return ProtocolHandlerAddonImpl.class.getName();
148 // XDispatchProvider
149 public XDispatch queryDispatch( /*IN*/com.sun.star.util.URL aURL,
150 /*IN*/String sTargetFrameName,
151 /*IN*/int iSearchFlags ) {
152 XDispatch xRet = null;
153 if ( aURL.Protocol.equals("org.openoffice.Office.addon.example:") ) {
154 if ( aURL.Path.equals( "Function1" ) )
155 xRet = this;
156 if ( aURL.Path.equals( "Function2" ) )
157 xRet = this;
158 if ( aURL.Path.equals( "Help" ) )
159 xRet = this;
161 return xRet;
164 public XDispatch[] queryDispatches( /*IN*/DispatchDescriptor[] seqDescripts ) {
165 int nCount = seqDescripts.length;
166 XDispatch[] lDispatcher = new XDispatch[nCount];
168 for( int i=0; i<nCount; ++i )
169 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL,
170 seqDescripts[i].FrameName,
171 seqDescripts[i].SearchFlags );
173 return lDispatcher;
176 // XDispatch
177 public void dispatch( /*IN*/com.sun.star.util.URL aURL,
178 /*IN*/com.sun.star.beans.PropertyValue[] aArguments ) {
180 if ( aURL.Protocol.equals("org.openoffice.Office.addon.example:") )
182 if ( aURL.Path.equals( "Function1" ) )
184 showMessageBox("SDK DevGuide Add-On example", "Function 1 activated");
186 if ( aURL.Path.equals( "Function2" ) )
188 showMessageBox("SDK DevGuide Add-On example", "Function 2 activated");
190 if ( aURL.Path.equals( "Help" ) )
192 showMessageBox("About SDK DevGuide Add-On example", "This is the SDK Add-On example");
197 public void addStatusListener( /*IN*/XStatusListener xControl,
198 /*IN*/com.sun.star.util.URL aURL ) {
201 public void removeStatusListener( /*IN*/XStatusListener xControl,
202 /*IN*/com.sun.star.util.URL aURL ) {
205 private void showMessageBox(String sTitle, String sMessage) {
206 if ( null != m_xFrame && null != m_xToolkit ) {
208 // describe window properties.
209 WindowDescriptor aDescriptor = new WindowDescriptor();
210 aDescriptor.Type = WindowClass.MODALTOP;
211 aDescriptor.WindowServiceName = "infobox";
212 aDescriptor.ParentIndex = -1;
213 aDescriptor.Parent = UnoRuntime.queryInterface(
214 XWindowPeer.class, m_xFrame.getContainerWindow());
215 aDescriptor.Bounds = new Rectangle(0,0,300,200);
216 aDescriptor.WindowAttributes = WindowAttribute.BORDER |
217 WindowAttribute.MOVEABLE |
218 WindowAttribute.CLOSEABLE;
220 XWindowPeer xPeer = m_xToolkit.createWindow( aDescriptor );
221 if ( null != xPeer ) {
222 XMessageBox xMsgBox = UnoRuntime.queryInterface(
223 XMessageBox.class, xPeer);
224 if ( null != xMsgBox )
226 xMsgBox.setCaptionText( sTitle );
227 xMsgBox.setMessageText( sMessage );
228 xMsgBox.execute();
236 /** Gives a factory for creating the service.
237 * This method is called by the <code>JavaLoader</code>
238 * <p>
239 * @return Returns a <code>XSingleServiceFactory</code> for creating the
240 * component.
241 * @see com.sun.star.comp.loader.JavaLoader
242 * @param sImplementationName The implementation name of the component.
244 public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
245 XSingleComponentFactory xFactory = null;
247 if ( sImplementationName.equals( ProtocolHandlerAddonImpl.class.getName() ) )
248 xFactory = Factory.createComponentFactory(ProtocolHandlerAddonImpl.class,
249 ProtocolHandlerAddonImpl.getServiceNames());
251 return xFactory;
254 /** Writes the service information into the given registry key.
255 * This method is called by the <code>JavaLoader</code>.
256 * @return returns true if the operation succeeded
257 * @see com.sun.star.comp.loader.JavaLoader
258 * @see com.sun.star.lib.uno.helper.Factory
259 * @param xRegistryKey Makes structural information (except regarding tree
260 * structures) of a single
261 * registry key accessible.
263 public static boolean __writeRegistryServiceInfo(
264 XRegistryKey xRegistryKey ) {
265 return Factory.writeRegistryServiceInfo(
266 ProtocolHandlerAddonImpl.class.getName(),
267 ProtocolHandlerAddonImpl.getServiceNames(),
268 xRegistryKey );