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 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.XTypeProvider; 43 import com.sun.star.lang.XServiceInfo; 44 import com.sun.star.uno.Type; 45 import com.sun.star.frame.XStatusListener; 46 import com.sun.star.frame.XDispatchProvider; 47 import com.sun.star.frame.XDispatch; 48 import com.sun.star.frame.XModel; 49 import com.sun.star.frame.XFrame; 50 import com.sun.star.frame.DispatchDescriptor; 51 import com.sun.star.awt.XToolkit; 52 import com.sun.star.awt.XWindowPeer; 53 import com.sun.star.awt.XMessageBox; 54 import com.sun.star.awt.WindowAttribute; 55 import com.sun.star.awt.WindowClass; 56 import com.sun.star.awt.WindowDescriptor; 57 import com.sun.star.awt.Rectangle; 58 59 public class ProtocolHandlerAddon { 60 /** This class implements the component. At least the interfaces XServiceInfo, 61 * XTypeProvider, and XInitialization should be provided by the service. 62 */ 63 public static class ProtocolHandlerAddonImpl extends WeakBase implements 64 XDispatchProvider, 65 XDispatch, 66 XInitialization, 67 XServiceInfo { 68 69 /** The service name, that must be used to get an instance of this service. 70 */ 71 static private final String[] m_serviceNames = { "com.sun.star.frame.ProtocolHandler" }; 72 73 /** The component context, that gives access to the service manager and all registered services. 74 */ 75 private XComponentContext m_xCmpCtx; 76 77 /** The toolkit, that we can create UNO dialogs. 78 */ 79 private XToolkit m_xToolkit; 80 81 /** The frame where the addon depends on. 82 */ 83 private XFrame m_xFrame; 84 private XStatusListener m_xStatusListener; 85 86 87 /** The constructor of the inner class has a XMultiServiceFactory parameter. 88 * @param xmultiservicefactoryInitialization A special service factory 89 * could be introduced while initializing. 90 */ 91 public ProtocolHandlerAddonImpl( XComponentContext xComponentContext ) { 92 m_xCmpCtx = xComponentContext; 93 } 94 95 /** This method is a member of the interface for initializing an object 96 * directly after its creation. 97 * @param object This array of arbitrary objects will be passed to the 98 * component after its creation. 99 * @throws Exception Every exception will not be handled, but will be 100 * passed to the caller. 101 */ 102 public void initialize( Object[] object ) 103 throws com.sun.star.uno.Exception { 104 105 if ( object.length > 0 ) 106 { 107 m_xFrame = ( XFrame ) UnoRuntime.queryInterface( 108 XFrame.class, object[ 0 ] ); 109 } 110 111 // Create the toolkit to have access to it later 112 m_xToolkit = (XToolkit) UnoRuntime.queryInterface( 113 XToolkit.class, 114 m_xCmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit", 115 m_xCmpCtx)); 116 } 117 118 /** This method returns an array of all supported service names. 119 * @return Array of supported service names. 120 */ 121 public String[] getSupportedServiceNames() { 122 return getServiceNames(); 123 } 124 125 public static String[] getServiceNames() { 126 return m_serviceNames; 127 } 128 129 /** This method returns true, if the given service will be 130 * supported by the component. 131 * @param stringService Service name. 132 * @return True, if the given service name will be supported. 133 */ 134 public boolean supportsService( String sService ) { 135 int len = m_serviceNames.length; 136 137 for( int i=0; i < len; i++) { 138 if ( sService.equals( m_serviceNames[i] ) ) 139 return true; 140 } 141 142 return false; 143 } 144 145 /** Return the class name of the component. 146 * @return Class name of the component. 147 */ 148 public String getImplementationName() { 149 return ProtocolHandlerAddonImpl.class.getName(); 150 } 151 152 // XDispatchProvider 153 public XDispatch queryDispatch( /*IN*/com.sun.star.util.URL aURL, 154 /*IN*/String sTargetFrameName, 155 /*IN*/int iSearchFlags ) { 156 XDispatch xRet = null; 157 if ( aURL.Protocol.compareTo("org.openoffice.Office.addon.example:") == 0 ) { 158 if ( aURL.Path.compareTo( "Function1" ) == 0 ) 159 xRet = this; 160 if ( aURL.Path.compareTo( "Function2" ) == 0 ) 161 xRet = this; 162 if ( aURL.Path.compareTo( "Help" ) == 0 ) 163 xRet = this; 164 } 165 return xRet; 166 } 167 168 public XDispatch[] queryDispatches( /*IN*/DispatchDescriptor[] seqDescripts ) { 169 int nCount = seqDescripts.length; 170 XDispatch[] lDispatcher = new XDispatch[nCount]; 171 172 for( int i=0; i<nCount; ++i ) 173 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL, 174 seqDescripts[i].FrameName, 175 seqDescripts[i].SearchFlags ); 176 177 return lDispatcher; 178 } 179 180 // XDispatch 181 public void dispatch( /*IN*/com.sun.star.util.URL aURL, 182 /*IN*/com.sun.star.beans.PropertyValue[] aArguments ) { 183 184 if ( aURL.Protocol.compareTo("org.openoffice.Office.addon.example:") == 0 ) 185 { 186 if ( aURL.Path.compareTo( "Function1" ) == 0 ) 187 { 188 showMessageBox("SDK DevGuide Add-On example", "Function 1 activated"); 189 } 190 if ( aURL.Path.compareTo( "Function2" ) == 0 ) 191 { 192 showMessageBox("SDK DevGuide Add-On example", "Function 2 activated"); 193 } 194 if ( aURL.Path.compareTo( "Help" ) == 0 ) 195 { 196 showMessageBox("About SDK DevGuide Add-On example", "This is the SDK Add-On example"); 197 } 198 } 199 } 200 201 public void addStatusListener( /*IN*/XStatusListener xControl, 202 /*IN*/com.sun.star.util.URL aURL ) { 203 } 204 205 public void removeStatusListener( /*IN*/XStatusListener xControl, 206 /*IN*/com.sun.star.util.URL aURL ) { 207 } 208 209 public void showMessageBox(String sTitle, String sMessage) { 210 try { 211 if ( null != m_xFrame && null != m_xToolkit ) { 212 213 // describe window properties. 214 WindowDescriptor aDescriptor = new WindowDescriptor(); 215 aDescriptor.Type = WindowClass.MODALTOP; 216 aDescriptor.WindowServiceName = new String( "infobox" ); 217 aDescriptor.ParentIndex = -1; 218 aDescriptor.Parent = (XWindowPeer)UnoRuntime.queryInterface( 219 XWindowPeer.class, m_xFrame.getContainerWindow()); 220 aDescriptor.Bounds = new Rectangle(0,0,300,200); 221 aDescriptor.WindowAttributes = WindowAttribute.BORDER | 222 WindowAttribute.MOVEABLE | 223 WindowAttribute.CLOSEABLE; 224 225 XWindowPeer xPeer = m_xToolkit.createWindow( aDescriptor ); 226 if ( null != xPeer ) { 227 XMessageBox xMsgBox = (XMessageBox)UnoRuntime.queryInterface( 228 XMessageBox.class, xPeer); 229 if ( null != xMsgBox ) 230 { 231 xMsgBox.setCaptionText( sTitle ); 232 xMsgBox.setMessageText( sMessage ); 233 xMsgBox.execute(); 234 } 235 } 236 } 237 } catch ( com.sun.star.uno.Exception e) { 238 // do your error handling 239 } 240 } 241 } 242 243 244 /** Gives a factory for creating the service. 245 * This method is called by the <code>JavaLoader</code> 246 * <p> 247 * @return Returns a <code>XSingleServiceFactory</code> for creating the 248 * component. 249 * @see com.sun.star.comp.loader.JavaLoader# 250 * @param stringImplementationName The implementation name of the component. 251 * @param xmultiservicefactory The service manager, who gives access to every 252 * known service. 253 * @param xregistrykey Makes structural information (except regarding tree 254 * structures) of a single 255 * registry key accessible. 256 */ 257 public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) { 258 XSingleComponentFactory xFactory = null; 259 260 if ( sImplementationName.equals( ProtocolHandlerAddonImpl.class.getName() ) ) 261 xFactory = Factory.createComponentFactory(ProtocolHandlerAddonImpl.class, 262 ProtocolHandlerAddonImpl.getServiceNames()); 263 264 return xFactory; 265 } 266 267 /** Writes the service information into the given registry key. 268 * This method is called by the <code>JavaLoader</code>. 269 * @return returns true if the operation succeeded 270 * @see com.sun.star.comp.loader.JavaLoader# 271 * @see com.sun.star.lib.uno.helper.Factory# 272 * @param xregistrykey Makes structural information (except regarding tree 273 * structures) of a single 274 * registry key accessible. 275 */ 276 public static boolean __writeRegistryServiceInfo( 277 XRegistryKey xRegistryKey ) { 278 return Factory.writeRegistryServiceInfo( 279 ProtocolHandlerAddonImpl.class.getName(), 280 ProtocolHandlerAddonImpl.getServiceNames(), 281 xRegistryKey ); 282 } 283 } 284