Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Components / Addons / ProtocolHandlerAddon_java / ProtocolHandlerAddon.java
bloba969253bc9c4f57cbba68c8d6115563f72cd9d59
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.lang.XInitialization;
41 import com.sun.star.lang.XServiceInfo;
42 import com.sun.star.frame.XStatusListener;
43 import com.sun.star.frame.XDispatchProvider;
44 import com.sun.star.frame.XDispatch;
45 import com.sun.star.frame.XFrame;
46 import com.sun.star.frame.DispatchDescriptor;
47 import com.sun.star.awt.XToolkit;
48 import com.sun.star.awt.XWindowPeer;
49 import com.sun.star.awt.XMessageBox;
50 import com.sun.star.awt.WindowAttribute;
51 import com.sun.star.awt.WindowClass;
52 import com.sun.star.awt.WindowDescriptor;
53 import com.sun.star.awt.Rectangle;
55 public class ProtocolHandlerAddon {
56 /** This class implements the component. At least the interfaces XServiceInfo,
57 * XTypeProvider, and XInitialization should be provided by the service.
59 public static class ProtocolHandlerAddonImpl extends WeakBase implements
60 XDispatchProvider,
61 XDispatch,
62 XInitialization,
63 XServiceInfo {
65 /** The service name, that must be used to get an instance of this service.
67 static private final String[] m_serviceNames = { "com.sun.star.frame.ProtocolHandler" };
69 /** The component context, that gives access to the service manager and all registered services.
71 private final XComponentContext m_xCmpCtx;
73 /** The toolkit, that we can create UNO dialogs.
75 private XToolkit m_xToolkit;
77 /** The frame where the addon depends on.
79 private XFrame m_xFrame;
82 /** The constructor of the inner class has a XMultiServiceFactory parameter.
83 * @param xComponentContext A special service factory
84 * could be introduced while initializing.
86 public ProtocolHandlerAddonImpl( XComponentContext xComponentContext ) {
87 m_xCmpCtx = xComponentContext;
90 /** This method is a member of the interface for initializing an object
91 * directly after its creation.
92 * @param object This array of arbitrary objects will be passed to the
93 * component after its creation.
94 * @throws com.sun.star.uno.Exception Every exception will not be handled, but will be
95 * passed to the caller.
97 public void initialize( Object[] object )
98 throws com.sun.star.uno.Exception {
100 if ( object.length > 0 )
102 m_xFrame = UnoRuntime.queryInterface(
103 XFrame.class, object[ 0 ] );
106 // Create the toolkit to have access to it later
107 m_xToolkit = UnoRuntime.queryInterface(
108 XToolkit.class,
109 m_xCmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit",
110 m_xCmpCtx));
113 /** This method returns an array of all supported service names.
114 * @return Array of supported service names.
116 public String[] getSupportedServiceNames() {
117 return getServiceNames();
120 public static String[] getServiceNames() {
121 return m_serviceNames;
124 /** This method returns true, if the given service will be
125 * supported by the component.
126 * @return True, if the given service name will be supported.
128 public boolean supportsService( String sService ) {
129 int len = m_serviceNames.length;
131 for( int i=0; i < len; i++) {
132 if ( sService.equals( m_serviceNames[i] ) )
133 return true;
136 return false;
139 /** Return the class name of the component.
140 * @return Class name of the component.
142 public String getImplementationName() {
143 return ProtocolHandlerAddonImpl.class.getName();
146 // XDispatchProvider
147 public XDispatch queryDispatch( /*IN*/com.sun.star.util.URL aURL,
148 /*IN*/String sTargetFrameName,
149 /*IN*/int iSearchFlags ) {
150 XDispatch xRet = null;
151 if ( aURL.Protocol.equals("org.openoffice.Office.addon.example:") ) {
152 if ( aURL.Path.equals( "Function1" ) )
153 xRet = this;
154 if ( aURL.Path.equals( "Function2" ) )
155 xRet = this;
156 if ( aURL.Path.equals( "Help" ) )
157 xRet = this;
159 return xRet;
162 public XDispatch[] queryDispatches( /*IN*/DispatchDescriptor[] seqDescripts ) {
163 int nCount = seqDescripts.length;
164 XDispatch[] lDispatcher = new XDispatch[nCount];
166 for( int i=0; i<nCount; ++i )
167 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL,
168 seqDescripts[i].FrameName,
169 seqDescripts[i].SearchFlags );
171 return lDispatcher;
174 // XDispatch
175 public void dispatch( /*IN*/com.sun.star.util.URL aURL,
176 /*IN*/com.sun.star.beans.PropertyValue[] aArguments ) {
178 if ( aURL.Protocol.equals("org.openoffice.Office.addon.example:") )
180 if ( aURL.Path.equals( "Function1" ) )
182 showMessageBox("SDK DevGuide Add-On example", "Function 1 activated");
184 if ( aURL.Path.equals( "Function2" ) )
186 showMessageBox("SDK DevGuide Add-On example", "Function 2 activated");
188 if ( aURL.Path.equals( "Help" ) )
190 showMessageBox("About SDK DevGuide Add-On example", "This is the SDK Add-On example");
195 public void addStatusListener( /*IN*/XStatusListener xControl,
196 /*IN*/com.sun.star.util.URL aURL ) {
199 public void removeStatusListener( /*IN*/XStatusListener xControl,
200 /*IN*/com.sun.star.util.URL aURL ) {
203 public void showMessageBox(String sTitle, String sMessage) {
204 if ( null == m_xFrame || null == m_xToolkit ) {
205 return;
207 // describe window properties.
208 WindowDescriptor aDescriptor = new WindowDescriptor();
209 aDescriptor.Type = WindowClass.MODALTOP;
210 aDescriptor.WindowServiceName = "infobox";
211 aDescriptor.ParentIndex = -1;
212 aDescriptor.Parent = UnoRuntime.queryInterface(
213 XWindowPeer.class, m_xFrame.getContainerWindow());
214 aDescriptor.Bounds = new Rectangle(0,0,300,200);
215 aDescriptor.WindowAttributes = WindowAttribute.BORDER |
216 WindowAttribute.MOVEABLE |
217 WindowAttribute.CLOSEABLE;
219 XWindowPeer xPeer = m_xToolkit.createWindow( aDescriptor );
220 if ( null != xPeer ) {
221 XMessageBox xMsgBox = UnoRuntime.queryInterface(
222 XMessageBox.class, xPeer);
223 if ( null != xMsgBox )
225 xMsgBox.setCaptionText( sTitle );
226 xMsgBox.setMessageText( sMessage );
227 xMsgBox.execute();
234 /** Gives a factory for creating the service.
235 * This method is called by the <code>JavaLoader</code>
236 * <p>
237 * @return Returns a <code>XSingleServiceFactory</code> for creating the
238 * component.
239 * @see com.sun.star.comp.loader.JavaLoader
240 * @param sImplementationName The implementation name of the component.
242 public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
243 XSingleComponentFactory xFactory = null;
245 if ( sImplementationName.equals( ProtocolHandlerAddonImpl.class.getName() ) )
246 xFactory = Factory.createComponentFactory(ProtocolHandlerAddonImpl.class,
247 ProtocolHandlerAddonImpl.getServiceNames());
249 return xFactory;