cid#1640468 Dereference after null check
[LibreOffice.git] / odk / examples / DevelopersGuide / Components / Addons / ProtocolHandlerAddon_java / ProtocolHandlerAddon.java
blobc12c69288f448936247fc6df62850cbf46763848
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.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 private static 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 final 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 public static String[] getServiceNames() {
122 return m_serviceNames;
125 /** This method returns true, if the given service will be
126 * supported by the component.
127 * @return True, if the given service name will be supported.
129 public boolean supportsService( String sService ) {
130 int len = m_serviceNames.length;
132 for( int i=0; i < len; i++) {
133 if ( sService.equals( m_serviceNames[i] ) )
134 return true;
137 return false;
140 /** Return the class name of the component.
141 * @return Class name of the component.
143 public String getImplementationName() {
144 return ProtocolHandlerAddonImpl.class.getName();
147 // XDispatchProvider
148 public XDispatch queryDispatch( /*IN*/com.sun.star.util.URL aURL,
149 /*IN*/String sTargetFrameName,
150 /*IN*/int iSearchFlags ) {
151 XDispatch xRet = null;
152 if ( aURL.Protocol.equals("org.openoffice.Office.addon.example:") ) {
153 if ( aURL.Path.equals( "Function1" ) )
154 xRet = this;
155 if ( aURL.Path.equals( "Function2" ) )
156 xRet = this;
157 if ( aURL.Path.equals( "Help" ) )
158 xRet = this;
160 return xRet;
163 public XDispatch[] queryDispatches( /*IN*/DispatchDescriptor[] seqDescripts ) {
164 int nCount = seqDescripts.length;
165 XDispatch[] lDispatcher = new XDispatch[nCount];
167 for( int i=0; i<nCount; ++i )
168 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL,
169 seqDescripts[i].FrameName,
170 seqDescripts[i].SearchFlags );
172 return lDispatcher;
175 // XDispatch
176 public void dispatch( /*IN*/com.sun.star.util.URL aURL,
177 /*IN*/com.sun.star.beans.PropertyValue[] aArguments ) {
179 if ( aURL.Protocol.equals("org.openoffice.Office.addon.example:") )
181 if ( aURL.Path.equals( "Function1" ) )
183 showMessageBox("SDK DevGuide Add-On example", "Function 1 activated");
185 if ( aURL.Path.equals( "Function2" ) )
187 showMessageBox("SDK DevGuide Add-On example", "Function 2 activated");
189 if ( aURL.Path.equals( "Help" ) )
191 showMessageBox("About SDK DevGuide Add-On example", "This is the SDK Add-On example");
196 public void addStatusListener( /*IN*/XStatusListener xControl,
197 /*IN*/com.sun.star.util.URL aURL ) {
200 public void removeStatusListener( /*IN*/XStatusListener xControl,
201 /*IN*/com.sun.star.util.URL aURL ) {
204 public void showMessageBox(String sTitle, String sMessage) {
205 if ( null == m_xFrame || null == m_xToolkit ) {
206 return;
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();
235 /** Gives a factory for creating the service.
236 * This method is called by the <code>JavaLoader</code>
237 * <p>
238 * @return Returns a <code>XSingleServiceFactory</code> for creating the
239 * component.
240 * @see com.sun.star.comp.loader.JavaLoader
241 * @param sImplementationName The implementation name of the component.
243 public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
244 XSingleComponentFactory xFactory = null;
246 if ( sImplementationName.equals( ProtocolHandlerAddonImpl.class.getName() ) )
247 xFactory = Factory.createComponentFactory(ProtocolHandlerAddonImpl.class,
248 ProtocolHandlerAddonImpl.getServiceNames());
250 return xFactory;
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */