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