1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
25cdf0e10cSrcweir import com.sun.star.lib.uno.helper.Factory;
26cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory;
27cdf0e10cSrcweir import com.sun.star.lib.uno.helper.WeakBase;
28cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
29cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
30cdf0e10cSrcweir import com.sun.star.lang.XInitialization;
31cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
32cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
33cdf0e10cSrcweir import com.sun.star.uno.Type;
34cdf0e10cSrcweir import	com.sun.star.frame.XStatusListener;
35cdf0e10cSrcweir import	com.sun.star.frame.XDispatchProvider;
36cdf0e10cSrcweir import	com.sun.star.frame.XDispatch;
37cdf0e10cSrcweir import	com.sun.star.frame.XModel;
38cdf0e10cSrcweir import	com.sun.star.frame.XFrame;
39cdf0e10cSrcweir import	com.sun.star.frame.DispatchDescriptor;
40cdf0e10cSrcweir import com.sun.star.awt.XToolkit;
41cdf0e10cSrcweir import com.sun.star.awt.XWindowPeer;
42cdf0e10cSrcweir import com.sun.star.awt.XMessageBox;
43cdf0e10cSrcweir import com.sun.star.awt.WindowAttribute;
44cdf0e10cSrcweir import com.sun.star.awt.WindowClass;
45cdf0e10cSrcweir import com.sun.star.awt.WindowDescriptor;
46cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir public class ProtocolHandlerAddon {
49cdf0e10cSrcweir     /** This class implements the component. At least the interfaces XServiceInfo,
50cdf0e10cSrcweir      * XTypeProvider, and XInitialization should be provided by the service.
51cdf0e10cSrcweir      */
52cdf0e10cSrcweir     public static class ProtocolHandlerAddonImpl extends WeakBase implements
53cdf0e10cSrcweir                                                  XDispatchProvider,
54cdf0e10cSrcweir                                                  XDispatch,
55cdf0e10cSrcweir                                                  XInitialization,
56cdf0e10cSrcweir                                                  XServiceInfo {
57cdf0e10cSrcweir 
58cdf0e10cSrcweir         /** The service name, that must be used to get an instance of this service.
59cdf0e10cSrcweir          */
60cdf0e10cSrcweir         static private final String[] m_serviceNames = { "com.sun.star.frame.ProtocolHandler" };
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         /** The component context, that gives access to the service manager and all registered services.
63cdf0e10cSrcweir          */
64cdf0e10cSrcweir         private XComponentContext m_xCmpCtx;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         /** The toolkit, that we can create UNO dialogs.
67cdf0e10cSrcweir          */
68cdf0e10cSrcweir         private XToolkit m_xToolkit;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         /** The frame where the addon depends on.
71cdf0e10cSrcweir          */
72cdf0e10cSrcweir         private XFrame m_xFrame;
73cdf0e10cSrcweir         private XStatusListener m_xStatusListener;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         /** The constructor of the inner class has a XMultiServiceFactory parameter.
77cdf0e10cSrcweir          * @param xmultiservicefactoryInitialization A special service factory
78cdf0e10cSrcweir          * could be introduced while initializing.
79cdf0e10cSrcweir          */
80cdf0e10cSrcweir         public ProtocolHandlerAddonImpl( XComponentContext xComponentContext ) {
81cdf0e10cSrcweir             m_xCmpCtx = xComponentContext;
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         /** This method is a member of the interface for initializing an object
85cdf0e10cSrcweir          * directly after its creation.
86cdf0e10cSrcweir          * @param object This array of arbitrary objects will be passed to the
87cdf0e10cSrcweir          * component after its creation.
88cdf0e10cSrcweir          * @throws Exception Every exception will not be handled, but will be
89cdf0e10cSrcweir          * passed to the caller.
90cdf0e10cSrcweir          */
91cdf0e10cSrcweir         public void initialize( Object[] object )
92cdf0e10cSrcweir             throws com.sun.star.uno.Exception {
93cdf0e10cSrcweir 
94cdf0e10cSrcweir             if ( object.length > 0 )
95cdf0e10cSrcweir             {
96cdf0e10cSrcweir                 m_xFrame = ( XFrame ) UnoRuntime.queryInterface(
97cdf0e10cSrcweir                     XFrame.class, object[ 0 ] );
98cdf0e10cSrcweir             }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir             // Create the toolkit to have access to it later
101cdf0e10cSrcweir             m_xToolkit = (XToolkit) UnoRuntime.queryInterface(
102cdf0e10cSrcweir                 XToolkit.class,
103cdf0e10cSrcweir                 m_xCmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit",
104cdf0e10cSrcweir                                                                         m_xCmpCtx));
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         /** This method returns an array of all supported service names.
108cdf0e10cSrcweir          * @return Array of supported service names.
109cdf0e10cSrcweir          */
110cdf0e10cSrcweir         public String[] getSupportedServiceNames() {
111cdf0e10cSrcweir             return getServiceNames();
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         public static String[] getServiceNames() {
115cdf0e10cSrcweir             return m_serviceNames;
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         /** This method returns true, if the given service will be
119cdf0e10cSrcweir          * supported by the component.
120cdf0e10cSrcweir          * @param stringService Service name.
121cdf0e10cSrcweir          * @return True, if the given service name will be supported.
122cdf0e10cSrcweir          */
123cdf0e10cSrcweir         public boolean supportsService( String sService ) {
124cdf0e10cSrcweir             int len = m_serviceNames.length;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir             for( int i=0; i < len; i++) {
127cdf0e10cSrcweir                 if ( sService.equals( m_serviceNames[i] ) )
128cdf0e10cSrcweir                     return true;
129cdf0e10cSrcweir             }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir             return false;
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         /** Return the class name of the component.
135cdf0e10cSrcweir          * @return Class name of the component.
136cdf0e10cSrcweir          */
137cdf0e10cSrcweir         public String getImplementationName() {
138cdf0e10cSrcweir             return ProtocolHandlerAddonImpl.class.getName();
139cdf0e10cSrcweir         }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         // XDispatchProvider
142cdf0e10cSrcweir         public XDispatch queryDispatch( /*IN*/com.sun.star.util.URL aURL,
143cdf0e10cSrcweir                                         /*IN*/String sTargetFrameName,
144cdf0e10cSrcweir                                         /*IN*/int iSearchFlags ) {
145cdf0e10cSrcweir             XDispatch xRet = null;
146cdf0e10cSrcweir             if ( aURL.Protocol.compareTo("org.openoffice.Office.addon.example:") == 0 ) {
147cdf0e10cSrcweir                 if ( aURL.Path.compareTo( "Function1" ) == 0 )
148cdf0e10cSrcweir                     xRet = this;
149cdf0e10cSrcweir                 if ( aURL.Path.compareTo( "Function2" ) == 0 )
150cdf0e10cSrcweir                     xRet = this;
151cdf0e10cSrcweir                 if ( aURL.Path.compareTo( "Help" ) == 0 )
152cdf0e10cSrcweir                     xRet = this;
153cdf0e10cSrcweir             }
154cdf0e10cSrcweir             return xRet;
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         public XDispatch[] queryDispatches( /*IN*/DispatchDescriptor[] seqDescripts ) {
158cdf0e10cSrcweir             int nCount = seqDescripts.length;
159cdf0e10cSrcweir             XDispatch[] lDispatcher = new XDispatch[nCount];
160cdf0e10cSrcweir 
161cdf0e10cSrcweir             for( int i=0; i<nCount; ++i )
162cdf0e10cSrcweir                 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL,
163cdf0e10cSrcweir                                                 seqDescripts[i].FrameName,
164cdf0e10cSrcweir                                                 seqDescripts[i].SearchFlags );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir             return lDispatcher;
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         // XDispatch
170cdf0e10cSrcweir         public void dispatch( /*IN*/com.sun.star.util.URL aURL,
171cdf0e10cSrcweir                               /*IN*/com.sun.star.beans.PropertyValue[] aArguments ) {
172cdf0e10cSrcweir 
173cdf0e10cSrcweir             if ( aURL.Protocol.compareTo("org.openoffice.Office.addon.example:") == 0 )
174cdf0e10cSrcweir             {
175cdf0e10cSrcweir                 if ( aURL.Path.compareTo( "Function1" ) == 0 )
176cdf0e10cSrcweir                 {
177cdf0e10cSrcweir                     showMessageBox("SDK DevGuide Add-On example", "Function 1 activated");
178cdf0e10cSrcweir                 }
179cdf0e10cSrcweir                 if ( aURL.Path.compareTo( "Function2" ) == 0 )
180cdf0e10cSrcweir                 {
181cdf0e10cSrcweir                     showMessageBox("SDK DevGuide Add-On example", "Function 2 activated");
182cdf0e10cSrcweir                 }
183cdf0e10cSrcweir                 if ( aURL.Path.compareTo( "Help" ) == 0 )
184cdf0e10cSrcweir                 {
185cdf0e10cSrcweir                     showMessageBox("About SDK DevGuide Add-On example", "This is the SDK Add-On example");
186cdf0e10cSrcweir                 }
187cdf0e10cSrcweir             }
188cdf0e10cSrcweir         }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir         public void addStatusListener( /*IN*/XStatusListener xControl,
191cdf0e10cSrcweir                                        /*IN*/com.sun.star.util.URL aURL ) {
192cdf0e10cSrcweir         }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         public void removeStatusListener( /*IN*/XStatusListener xControl,
195cdf0e10cSrcweir                                           /*IN*/com.sun.star.util.URL aURL ) {
196cdf0e10cSrcweir         }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir         public void showMessageBox(String sTitle, String sMessage) {
199cdf0e10cSrcweir             try {
200cdf0e10cSrcweir                 if ( null != m_xFrame && null != m_xToolkit ) {
201cdf0e10cSrcweir 
202cdf0e10cSrcweir                     // describe window properties.
203cdf0e10cSrcweir                     WindowDescriptor aDescriptor = new WindowDescriptor();
204cdf0e10cSrcweir                     aDescriptor.Type              = WindowClass.MODALTOP;
205cdf0e10cSrcweir                     aDescriptor.WindowServiceName = new String( "infobox" );
206cdf0e10cSrcweir                     aDescriptor.ParentIndex       = -1;
207cdf0e10cSrcweir                     aDescriptor.Parent            = (XWindowPeer)UnoRuntime.queryInterface(
208cdf0e10cSrcweir                         XWindowPeer.class, m_xFrame.getContainerWindow());
209cdf0e10cSrcweir                     aDescriptor.Bounds            = new Rectangle(0,0,300,200);
210cdf0e10cSrcweir                     aDescriptor.WindowAttributes  = WindowAttribute.BORDER |
211cdf0e10cSrcweir                         WindowAttribute.MOVEABLE |
212cdf0e10cSrcweir                         WindowAttribute.CLOSEABLE;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir                     XWindowPeer xPeer = m_xToolkit.createWindow( aDescriptor );
215cdf0e10cSrcweir                     if ( null != xPeer ) {
216cdf0e10cSrcweir                         XMessageBox xMsgBox = (XMessageBox)UnoRuntime.queryInterface(
217cdf0e10cSrcweir                             XMessageBox.class, xPeer);
218cdf0e10cSrcweir                         if ( null != xMsgBox )
219cdf0e10cSrcweir                         {
220cdf0e10cSrcweir                             xMsgBox.setCaptionText( sTitle );
221cdf0e10cSrcweir                             xMsgBox.setMessageText( sMessage );
222cdf0e10cSrcweir                             xMsgBox.execute();
223cdf0e10cSrcweir                         }
224cdf0e10cSrcweir                     }
225cdf0e10cSrcweir                 }
226cdf0e10cSrcweir             } catch ( com.sun.star.uno.Exception e) {
227cdf0e10cSrcweir                 // do your error handling
228cdf0e10cSrcweir             }
229cdf0e10cSrcweir         }
230cdf0e10cSrcweir     }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 
233cdf0e10cSrcweir     /** Gives a factory for creating the service.
234cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
235cdf0e10cSrcweir      * <p>
236cdf0e10cSrcweir      * @return Returns a <code>XSingleServiceFactory</code> for creating the
237cdf0e10cSrcweir      * component.
238cdf0e10cSrcweir      * @see com.sun.star.comp.loader.JavaLoader#
239cdf0e10cSrcweir      * @param stringImplementationName The implementation name of the component.
240cdf0e10cSrcweir      * @param xmultiservicefactory The service manager, who gives access to every
241cdf0e10cSrcweir      * known service.
242cdf0e10cSrcweir      * @param xregistrykey Makes structural information (except regarding tree
243cdf0e10cSrcweir      * structures) of a single
244cdf0e10cSrcweir      * registry key accessible.
245cdf0e10cSrcweir      */
246cdf0e10cSrcweir     public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
247cdf0e10cSrcweir         XSingleComponentFactory xFactory = null;
248cdf0e10cSrcweir 
249cdf0e10cSrcweir         if ( sImplementationName.equals( ProtocolHandlerAddonImpl.class.getName() ) )
250cdf0e10cSrcweir             xFactory = Factory.createComponentFactory(ProtocolHandlerAddonImpl.class,
251cdf0e10cSrcweir                                                       ProtocolHandlerAddonImpl.getServiceNames());
252cdf0e10cSrcweir 
253cdf0e10cSrcweir         return xFactory;
254cdf0e10cSrcweir     }
255cdf0e10cSrcweir 
256cdf0e10cSrcweir     /** Writes the service information into the given registry key.
257cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>.
258cdf0e10cSrcweir      * @return returns true if the operation succeeded
259cdf0e10cSrcweir      * @see com.sun.star.comp.loader.JavaLoader#
260cdf0e10cSrcweir      * @see com.sun.star.lib.uno.helper.Factory#
261cdf0e10cSrcweir      * @param xregistrykey Makes structural information (except regarding tree
262cdf0e10cSrcweir      * structures) of a single
263cdf0e10cSrcweir      * registry key accessible.
264cdf0e10cSrcweir      */
265cdf0e10cSrcweir     public static boolean __writeRegistryServiceInfo(
266cdf0e10cSrcweir         XRegistryKey xRegistryKey ) {
267cdf0e10cSrcweir         return Factory.writeRegistryServiceInfo(
268cdf0e10cSrcweir             ProtocolHandlerAddonImpl.class.getName(),
269cdf0e10cSrcweir             ProtocolHandlerAddonImpl.getServiceNames(),
270cdf0e10cSrcweir             xRegistryKey );
271cdf0e10cSrcweir   }
272cdf0e10cSrcweir }
273