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