1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
.registry
.XRegistryKey
;
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
66 /** The service name, that must be used to get an instance of this service.
68 static private 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 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
;
81 private XStatusListener m_xStatusListener
;
84 /** The constructor of the inner class has a XMultiServiceFactory parameter.
85 * @param xmultiservicefactoryInitialization A special service factory
86 * could be introduced while initializing.
88 public ProtocolHandlerAddonImpl( XComponentContext xComponentContext
) {
89 m_xCmpCtx
= xComponentContext
;
92 /** This method is a member of the interface for initializing an object
93 * directly after its creation.
94 * @param object This array of arbitrary objects will be passed to the
95 * component after its creation.
96 * @throws Exception Every exception will not be handled, but will be
97 * passed to the caller.
99 public void initialize( Object
[] object
)
100 throws com
.sun
.star
.uno
.Exception
{
102 if ( object
.length
> 0 )
104 m_xFrame
= UnoRuntime
.queryInterface(
105 XFrame
.class, object
[ 0 ] );
108 // Create the toolkit to have access to it later
109 m_xToolkit
= UnoRuntime
.queryInterface(
111 m_xCmpCtx
.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit",
115 /** This method returns an array of all supported service names.
116 * @return Array of supported service names.
118 public String
[] getSupportedServiceNames() {
119 return getServiceNames();
122 public static String
[] getServiceNames() {
123 return m_serviceNames
;
126 /** This method returns true, if the given service will be
127 * supported by the component.
128 * @param stringService Service name.
129 * @return True, if the given service name will be supported.
131 public boolean supportsService( String sService
) {
132 int len
= m_serviceNames
.length
;
134 for( int i
=0; i
< len
; i
++) {
135 if ( sService
.equals( m_serviceNames
[i
] ) )
142 /** Return the class name of the component.
143 * @return Class name of the component.
145 public String
getImplementationName() {
146 return ProtocolHandlerAddonImpl
.class.getName();
150 public XDispatch
queryDispatch( /*IN*/com
.sun
.star
.util
.URL aURL
,
151 /*IN*/String sTargetFrameName
,
152 /*IN*/int iSearchFlags
) {
153 XDispatch xRet
= null;
154 if ( aURL
.Protocol
.compareTo("org.openoffice.Office.addon.example:") == 0 ) {
155 if ( aURL
.Path
.compareTo( "Function1" ) == 0 )
157 if ( aURL
.Path
.compareTo( "Function2" ) == 0 )
159 if ( aURL
.Path
.compareTo( "Help" ) == 0 )
165 public XDispatch
[] queryDispatches( /*IN*/DispatchDescriptor
[] seqDescripts
) {
166 int nCount
= seqDescripts
.length
;
167 XDispatch
[] lDispatcher
= new XDispatch
[nCount
];
169 for( int i
=0; i
<nCount
; ++i
)
170 lDispatcher
[i
] = queryDispatch( seqDescripts
[i
].FeatureURL
,
171 seqDescripts
[i
].FrameName
,
172 seqDescripts
[i
].SearchFlags
);
178 public void dispatch( /*IN*/com
.sun
.star
.util
.URL aURL
,
179 /*IN*/com
.sun
.star
.beans
.PropertyValue
[] aArguments
) {
181 if ( aURL
.Protocol
.compareTo("org.openoffice.Office.addon.example:") == 0 )
183 if ( aURL
.Path
.compareTo( "Function1" ) == 0 )
185 showMessageBox("SDK DevGuide Add-On example", "Function 1 activated");
187 if ( aURL
.Path
.compareTo( "Function2" ) == 0 )
189 showMessageBox("SDK DevGuide Add-On example", "Function 2 activated");
191 if ( aURL
.Path
.compareTo( "Help" ) == 0 )
193 showMessageBox("About SDK DevGuide Add-On example", "This is the SDK Add-On example");
198 public void addStatusListener( /*IN*/XStatusListener xControl
,
199 /*IN*/com
.sun
.star
.util
.URL aURL
) {
202 public void removeStatusListener( /*IN*/XStatusListener xControl
,
203 /*IN*/com
.sun
.star
.util
.URL aURL
) {
206 public void showMessageBox(String sTitle
, String sMessage
) {
207 if ( null != m_xFrame
&& null != m_xToolkit
) {
209 // describe window properties.
210 WindowDescriptor aDescriptor
= new WindowDescriptor();
211 aDescriptor
.Type
= WindowClass
.MODALTOP
;
212 aDescriptor
.WindowServiceName
= new String( "infobox" );
213 aDescriptor
.ParentIndex
= -1;
214 aDescriptor
.Parent
= UnoRuntime
.queryInterface(
215 XWindowPeer
.class, m_xFrame
.getContainerWindow());
216 aDescriptor
.Bounds
= new Rectangle(0,0,300,200);
217 aDescriptor
.WindowAttributes
= WindowAttribute
.BORDER
|
218 WindowAttribute
.MOVEABLE
|
219 WindowAttribute
.CLOSEABLE
;
221 XWindowPeer xPeer
= m_xToolkit
.createWindow( aDescriptor
);
222 if ( null != xPeer
) {
223 XMessageBox xMsgBox
= UnoRuntime
.queryInterface(
224 XMessageBox
.class, xPeer
);
225 if ( null != xMsgBox
)
227 xMsgBox
.setCaptionText( sTitle
);
228 xMsgBox
.setMessageText( sMessage
);
237 /** Gives a factory for creating the service.
238 * This method is called by the <code>JavaLoader</code>
240 * @return Returns a <code>XSingleServiceFactory</code> for creating the
242 * @see com.sun.star.comp.loader.JavaLoader#
243 * @param stringImplementationName The implementation name of the component.
244 * @param xmultiservicefactory The service manager, who gives access to every
246 * @param xregistrykey Makes structural information (except regarding tree
247 * structures) of a single
248 * registry key accessible.
250 public static XSingleComponentFactory
__getComponentFactory( String sImplementationName
) {
251 XSingleComponentFactory xFactory
= null;
253 if ( sImplementationName
.equals( ProtocolHandlerAddonImpl
.class.getName() ) )
254 xFactory
= Factory
.createComponentFactory(ProtocolHandlerAddonImpl
.class,
255 ProtocolHandlerAddonImpl
.getServiceNames());
260 /** Writes the service information into the given registry key.
261 * This method is called by the <code>JavaLoader</code>.
262 * @return returns true if the operation succeeded
263 * @see com.sun.star.comp.loader.JavaLoader#
264 * @see com.sun.star.lib.uno.helper.Factory#
265 * @param xregistrykey Makes structural information (except regarding tree
266 * structures) of a single
267 * registry key accessible.
269 public static boolean __writeRegistryServiceInfo(
270 XRegistryKey xRegistryKey
) {
271 return Factory
.writeRegistryServiceInfo(
272 ProtocolHandlerAddonImpl
.class.getName(),
273 ProtocolHandlerAddonImpl
.getServiceNames(),