1 /*************************************************************************
3 * $RCSfile: ProtocolHandlerAddon.java,v $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:12:42 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com
.sun
.star
.uno
.XComponentContext
;
42 import com
.sun
.star
.lib
.uno
.helper
.Factory
;
43 import com
.sun
.star
.lang
.XSingleComponentFactory
;
44 import com
.sun
.star
.lib
.uno
.helper
.WeakBase
;
45 import com
.sun
.star
.uno
.UnoRuntime
;
46 import com
.sun
.star
.registry
.XRegistryKey
;
47 import com
.sun
.star
.lang
.XInitialization
;
48 import com
.sun
.star
.lang
.XTypeProvider
;
49 import com
.sun
.star
.lang
.XServiceInfo
;
50 import com
.sun
.star
.uno
.Type
;
51 import com
.sun
.star
.frame
.XStatusListener
;
52 import com
.sun
.star
.frame
.XDispatchProvider
;
53 import com
.sun
.star
.frame
.XDispatch
;
54 import com
.sun
.star
.frame
.XModel
;
55 import com
.sun
.star
.frame
.XFrame
;
56 import com
.sun
.star
.frame
.DispatchDescriptor
;
57 import com
.sun
.star
.awt
.XToolkit
;
58 import com
.sun
.star
.awt
.XWindowPeer
;
59 import com
.sun
.star
.awt
.XMessageBox
;
60 import com
.sun
.star
.awt
.WindowAttribute
;
61 import com
.sun
.star
.awt
.WindowClass
;
62 import com
.sun
.star
.awt
.WindowDescriptor
;
63 import com
.sun
.star
.awt
.Rectangle
;
65 public class ProtocolHandlerAddon
{
66 /** This class implements the component. At least the interfaces XServiceInfo,
67 * XTypeProvider, and XInitialization should be provided by the service.
69 public static class ProtocolHandlerAddonImpl
extends WeakBase
implements
75 /** The service name, that must be used to get an instance of this service.
77 static private final String
[] m_serviceNames
= { "com.sun.star.frame.ProtocolHandler" };
79 /** The component context, that gives access to the service manager and all registered services.
81 private XComponentContext m_xCmpCtx
;
83 /** The toolkit, that we can create UNO dialogs.
85 private XToolkit m_xToolkit
;
87 /** The frame where the addon depends on.
89 private XFrame m_xFrame
;
90 private XStatusListener m_xStatusListener
;
93 /** The constructor of the inner class has a XMultiServiceFactory parameter.
94 * @param xmultiservicefactoryInitialization A special service factory
95 * could be introduced while initializing.
97 public ProtocolHandlerAddonImpl( XComponentContext xComponentContext
) {
98 m_xCmpCtx
= xComponentContext
;
101 /** This method is a member of the interface for initializing an object
102 * directly after its creation.
103 * @param object This array of arbitrary objects will be passed to the
104 * component after its creation.
105 * @throws Exception Every exception will not be handled, but will be
106 * passed to the caller.
108 public void initialize( Object
[] object
)
109 throws com
.sun
.star
.uno
.Exception
{
111 if ( object
.length
> 0 )
113 m_xFrame
= ( XFrame
) UnoRuntime
.queryInterface(
114 XFrame
.class, object
[ 0 ] );
117 // Create the toolkit to have access to it later
118 m_xToolkit
= (XToolkit
) UnoRuntime
.queryInterface(
120 m_xCmpCtx
.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit",
124 /** This method returns an array of all supported service names.
125 * @return Array of supported service names.
127 public String
[] getSupportedServiceNames() {
128 return getServiceNames();
131 public static String
[] getServiceNames() {
132 return m_serviceNames
;
135 /** This method returns true, if the given service will be
136 * supported by the component.
137 * @param stringService Service name.
138 * @return True, if the given service name will be supported.
140 public boolean supportsService( String sService
) {
141 int len
= m_serviceNames
.length
;
143 for( int i
=0; i
< len
; i
++) {
144 if ( sService
.equals( m_serviceNames
[i
] ) )
151 /** Return the class name of the component.
152 * @return Class name of the component.
154 public String
getImplementationName() {
155 return ProtocolHandlerAddonImpl
.class.getName();
159 public XDispatch
queryDispatch( /*IN*/com
.sun
.star
.util
.URL aURL
,
160 /*IN*/String sTargetFrameName
,
161 /*IN*/int iSearchFlags
) {
162 XDispatch xRet
= null;
163 if ( aURL
.Protocol
.compareTo("org.openoffice.Office.addon.example:") == 0 ) {
164 if ( aURL
.Path
.compareTo( "Function1" ) == 0 )
166 if ( aURL
.Path
.compareTo( "Function2" ) == 0 )
168 if ( aURL
.Path
.compareTo( "Help" ) == 0 )
174 public XDispatch
[] queryDispatches( /*IN*/DispatchDescriptor
[] seqDescripts
) {
175 int nCount
= seqDescripts
.length
;
176 XDispatch
[] lDispatcher
= new XDispatch
[nCount
];
178 for( int i
=0; i
<nCount
; ++i
)
179 lDispatcher
[i
] = queryDispatch( seqDescripts
[i
].FeatureURL
,
180 seqDescripts
[i
].FrameName
,
181 seqDescripts
[i
].SearchFlags
);
187 public void dispatch( /*IN*/com
.sun
.star
.util
.URL aURL
,
188 /*IN*/com
.sun
.star
.beans
.PropertyValue
[] aArguments
) {
190 if ( aURL
.Protocol
.compareTo("org.openoffice.Office.addon.example:") == 0 )
192 if ( aURL
.Path
.compareTo( "Function1" ) == 0 )
194 showMessageBox("SDK DevGuide Add-On example", "Function 1 activated");
196 if ( aURL
.Path
.compareTo( "Function2" ) == 0 )
198 showMessageBox("SDK DevGuide Add-On example", "Function 2 activated");
200 if ( aURL
.Path
.compareTo( "Help" ) == 0 )
202 showMessageBox("About SDK DevGuide Add-On example", "This is the SDK Add-On example");
207 public void addStatusListener( /*IN*/XStatusListener xControl
,
208 /*IN*/com
.sun
.star
.util
.URL aURL
) {
211 public void removeStatusListener( /*IN*/XStatusListener xControl
,
212 /*IN*/com
.sun
.star
.util
.URL aURL
) {
215 public void showMessageBox(String sTitle
, String sMessage
) {
217 if ( null != m_xFrame
&& null != m_xToolkit
) {
219 // describe window properties.
220 WindowDescriptor aDescriptor
= new WindowDescriptor();
221 aDescriptor
.Type
= WindowClass
.MODALTOP
;
222 aDescriptor
.WindowServiceName
= new String( "infobox" );
223 aDescriptor
.ParentIndex
= -1;
224 aDescriptor
.Parent
= (XWindowPeer
)UnoRuntime
.queryInterface(
225 XWindowPeer
.class, m_xFrame
.getContainerWindow());
226 aDescriptor
.Bounds
= new Rectangle(0,0,300,200);
227 aDescriptor
.WindowAttributes
= WindowAttribute
.BORDER
|
228 WindowAttribute
.MOVEABLE
|
229 WindowAttribute
.CLOSEABLE
;
231 XWindowPeer xPeer
= m_xToolkit
.createWindow( aDescriptor
);
232 if ( null != xPeer
) {
233 XMessageBox xMsgBox
= (XMessageBox
)UnoRuntime
.queryInterface(
234 XMessageBox
.class, xPeer
);
235 if ( null != xMsgBox
)
237 xMsgBox
.setCaptionText( sTitle
);
238 xMsgBox
.setMessageText( sMessage
);
243 } catch ( com
.sun
.star
.uno
.Exception e
) {
244 // do your error handling
250 /** Gives a factory for creating the service.
251 * This method is called by the <code>JavaLoader</code>
253 * @return Returns a <code>XSingleServiceFactory</code> for creating the
255 * @see com.sun.star.comp.loader.JavaLoader#
256 * @param stringImplementationName The implementation name of the component.
257 * @param xmultiservicefactory The service manager, who gives access to every
259 * @param xregistrykey Makes structural information (except regarding tree
260 * structures) of a single
261 * registry key accessible.
263 public static XSingleComponentFactory
__getComponentFactory( String sImplementationName
) {
264 XSingleComponentFactory xFactory
= null;
266 if ( sImplementationName
.equals( ProtocolHandlerAddonImpl
.class.getName() ) )
267 xFactory
= Factory
.createComponentFactory(ProtocolHandlerAddonImpl
.class,
268 ProtocolHandlerAddonImpl
.getServiceNames());
273 /** Writes the service information into the given registry key.
274 * This method is called by the <code>JavaLoader</code>.
275 * @return returns true if the operation succeeded
276 * @see com.sun.star.comp.loader.JavaLoader#
277 * @see com.sun.star.lib.uno.helper.Factory#
278 * @param xregistrykey Makes structural information (except regarding tree
279 * structures) of a single
280 * registry key accessible.
282 public static boolean __writeRegistryServiceInfo(
283 XRegistryKey xRegistryKey
) {
284 return Factory
.writeRegistryServiceInfo(
285 ProtocolHandlerAddonImpl
.class.getName(),
286 ProtocolHandlerAddonImpl
.getServiceNames(),