1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  *  The Contents of this file are made available subject to the terms of
5  *  the BSD license.
6  *
7  *  Copyright 2000, 2010 Oracle and/or its affiliates.
8  *  All rights reserved.
9  *
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.
21  *
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.
33  *
34  *************************************************************************/
35 
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;
55 
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.
59      */
60     public static class ProtocolHandlerAddonImpl extends WeakBase implements
61                                                  XDispatchProvider,
62                                                  XDispatch,
63                                                  XInitialization,
64                                                  XServiceInfo {
65 
66         /** The service name, that must be used to get an instance of this service.
67          */
68         private static final String[] m_serviceNames = { "com.sun.star.frame.ProtocolHandler" };
69 
70         /** The component context, that gives access to the service manager and all registered services.
71          */
72         private final XComponentContext m_xCmpCtx;
73 
74         /** The toolkit, that we can create UNO dialogs.
75          */
76         private XToolkit m_xToolkit;
77 
78         /** The frame where the addon depends on.
79          */
80         private XFrame m_xFrame;
81 
82 
83         /** The constructor of the inner class has a XMultiServiceFactory parameter.
84          * @param xComponentContext A special service factory
85          * could be introduced while initializing.
86          */
ProtocolHandlerAddonImpl( XComponentContext xComponentContext )87         public ProtocolHandlerAddonImpl( XComponentContext xComponentContext ) {
88             m_xCmpCtx = xComponentContext;
89         }
90 
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.
97          */
initialize( Object[] object )98         public void initialize( Object[] object )
99             throws com.sun.star.uno.Exception {
100 
101             if ( object.length > 0 )
102             {
103                 m_xFrame = UnoRuntime.queryInterface(
104                     XFrame.class, object[ 0 ] );
105             }
106 
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));
112         }
113 
114         /** This method returns an array of all supported service names.
115          * @return Array of supported service names.
116          */
getSupportedServiceNames()117         public String[] getSupportedServiceNames() {
118             return getServiceNames();
119         }
120 
getServiceNames()121         public static String[] getServiceNames() {
122             return m_serviceNames;
123         }
124 
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.
128          */
supportsService( String sService )129         public boolean supportsService( String sService ) {
130             int len = m_serviceNames.length;
131 
132             for( int i=0; i < len; i++) {
133                 if ( sService.equals( m_serviceNames[i] ) )
134                     return true;
135             }
136 
137             return false;
138         }
139 
140         /** Return the class name of the component.
141          * @return Class name of the component.
142          */
getImplementationName()143         public String getImplementationName() {
144             return ProtocolHandlerAddonImpl.class.getName();
145         }
146 
147         // XDispatchProvider
queryDispatch( com.sun.star.util.URL aURL, String sTargetFrameName, int iSearchFlags )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;
159             }
160             return xRet;
161         }
162 
queryDispatches( DispatchDescriptor[] seqDescripts )163         public XDispatch[] queryDispatches( /*IN*/DispatchDescriptor[] seqDescripts ) {
164             int nCount = seqDescripts.length;
165             XDispatch[] lDispatcher = new XDispatch[nCount];
166 
167             for( int i=0; i<nCount; ++i )
168                 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL,
169                                                 seqDescripts[i].FrameName,
170                                                 seqDescripts[i].SearchFlags );
171 
172             return lDispatcher;
173         }
174 
175         // XDispatch
dispatch( com.sun.star.util.URL aURL, com.sun.star.beans.PropertyValue[] aArguments )176         public void dispatch( /*IN*/com.sun.star.util.URL aURL,
177                               /*IN*/com.sun.star.beans.PropertyValue[] aArguments ) {
178 
179             if ( aURL.Protocol.equals("org.openoffice.Office.addon.example:") )
180             {
181                 if ( aURL.Path.equals( "Function1" ) )
182                 {
183                     showMessageBox("SDK DevGuide Add-On example", "Function 1 activated");
184                 }
185                 if ( aURL.Path.equals( "Function2" ) )
186                 {
187                     showMessageBox("SDK DevGuide Add-On example", "Function 2 activated");
188                 }
189                 if ( aURL.Path.equals( "Help" ) )
190                 {
191                     showMessageBox("About SDK DevGuide Add-On example", "This is the SDK Add-On example");
192                 }
193             }
194         }
195 
addStatusListener( XStatusListener xControl, com.sun.star.util.URL aURL )196         public void addStatusListener( /*IN*/XStatusListener xControl,
197                                        /*IN*/com.sun.star.util.URL aURL ) {
198         }
199 
removeStatusListener( XStatusListener xControl, com.sun.star.util.URL aURL )200         public void removeStatusListener( /*IN*/XStatusListener xControl,
201                                           /*IN*/com.sun.star.util.URL aURL ) {
202         }
203 
showMessageBox(String sTitle, String sMessage)204         public void showMessageBox(String sTitle, String sMessage) {
205             if ( null == m_xFrame || null == m_xToolkit ) {
206                 return;
207             }
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;
219 
220             XWindowPeer xPeer = m_xToolkit.createWindow( aDescriptor );
221             if ( null != xPeer ) {
222                 XMessageBox xMsgBox = UnoRuntime.queryInterface(
223                     XMessageBox.class, xPeer);
224                 if ( null != xMsgBox )
225                 {
226                     xMsgBox.setCaptionText( sTitle );
227                     xMsgBox.setMessageText( sMessage );
228                     xMsgBox.execute();
229                 }
230             }
231         }
232     }
233 
234 
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.
242      */
__getComponentFactory( String sImplementationName )243     public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
244         XSingleComponentFactory xFactory = null;
245 
246         if ( sImplementationName.equals( ProtocolHandlerAddonImpl.class.getName() ) )
247             xFactory = Factory.createComponentFactory(ProtocolHandlerAddonImpl.class,
248                                                       ProtocolHandlerAddonImpl.getServiceNames());
249 
250         return xFactory;
251     }
252 }
253 
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
255