xref: /AOO41X/main/odk/examples/java/Inspector/InspectorAddon.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 import com.sun.star.beans.XPropertySet;
35*cdf0e10cSrcweir import com.sun.star.frame.DispatchDescriptor;
36*cdf0e10cSrcweir import com.sun.star.frame.FrameSearchFlag;
37*cdf0e10cSrcweir import com.sun.star.frame.XController;
38*cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
39*cdf0e10cSrcweir import com.sun.star.frame.XDispatch;
40*cdf0e10cSrcweir import com.sun.star.frame.XDispatchProvider;
41*cdf0e10cSrcweir import com.sun.star.frame.XFrame;
42*cdf0e10cSrcweir import com.sun.star.frame.XModel;
43*cdf0e10cSrcweir import com.sun.star.frame.XStatusListener;
44*cdf0e10cSrcweir import com.sun.star.lang.XInitialization;
45*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
46*cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory;
47*cdf0e10cSrcweir import com.sun.star.lib.uno.helper.WeakBase;
48*cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
49*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
50*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
51*cdf0e10cSrcweir import com.sun.star.lib.uno.helper.Factory;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir public class InspectorAddon {
55*cdf0e10cSrcweir     /** This class implements the component. At least the interfaces XServiceInfo,
56*cdf0e10cSrcweir      * XTypeProvider, and XInitialization should be provided by the service.
57*cdf0e10cSrcweir      */
58*cdf0e10cSrcweir     public static class InspectorAddonImpl extends WeakBase implements XDispatchProvider, XInitialization, XServiceInfo {
59*cdf0e10cSrcweir     private static XModel xModel = null;
60*cdf0e10cSrcweir     org.openoffice.XInstanceInspector xInstInspector = null;
61*cdf0e10cSrcweir //    Dispatcher oDispatcher = null;
62*cdf0e10cSrcweir     XFrame m_xFrame = null;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     private static final String[] m_serviceNames = {
65*cdf0e10cSrcweir         "org.openoffice.InstanceInspectorAddon",
66*cdf0e10cSrcweir         "com.sun.star.frame.ProtocolHandler" };
67*cdf0e10cSrcweir                                                      ;
68*cdf0e10cSrcweir         private XComponentContext m_xContext = null;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     /** Creates a new instance of InspectorAddon */
71*cdf0e10cSrcweir     public InspectorAddonImpl(XComponentContext _xContext) {
72*cdf0e10cSrcweir         m_xContext = _xContext;
73*cdf0e10cSrcweir     }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir         public XDispatch queryDispatch( /*IN*/com.sun.star.util.URL aURL, /*IN*/String sTargetFrameName, /*IN*/int iSearchFlags ) {
76*cdf0e10cSrcweir             XDispatch xRet = null;
77*cdf0e10cSrcweir             if ( aURL.Protocol.compareTo("org.openoffice.Office.addon.Inspector:") == 0 ) {
78*cdf0e10cSrcweir                 if ( aURL.Path.compareTo( "inspect" ) == 0 ){
79*cdf0e10cSrcweir                     // Todo: Check if the frame is already administered (use hashtable)
80*cdf0e10cSrcweir                     xRet = new Dispatcher(m_xFrame);
81*cdf0e10cSrcweir                 }
82*cdf0e10cSrcweir             }
83*cdf0e10cSrcweir             return xRet;
84*cdf0e10cSrcweir         }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir         public XDispatch[] queryDispatches( /*IN*/DispatchDescriptor[] seqDescripts ) {
88*cdf0e10cSrcweir             int nCount = seqDescripts.length;
89*cdf0e10cSrcweir             XDispatch[] lDispatcher = new XDispatch[nCount];
90*cdf0e10cSrcweir             for( int i=0; i<nCount; ++i )
91*cdf0e10cSrcweir                 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL, seqDescripts[i].FrameName, seqDescripts[i].SearchFlags );
92*cdf0e10cSrcweir             return lDispatcher;
93*cdf0e10cSrcweir         }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir         public void initialize( Object[] object ) throws com.sun.star.uno.Exception {
97*cdf0e10cSrcweir             if ( object.length > 0 ){
98*cdf0e10cSrcweir                 m_xFrame = ( XFrame ) UnoRuntime.queryInterface(XFrame.class, object[ 0 ] );
99*cdf0e10cSrcweir             }
100*cdf0e10cSrcweir         }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir         public class Dispatcher implements XDispatch{
103*cdf0e10cSrcweir             private XFrame m_xFrame = null;
104*cdf0e10cSrcweir             private XModel xModel = null;
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir             public Dispatcher(XFrame _xFrame){
107*cdf0e10cSrcweir                 m_xFrame = _xFrame;
108*cdf0e10cSrcweir                 if (m_xFrame != null){
109*cdf0e10cSrcweir                     XController xController = m_xFrame.getController();
110*cdf0e10cSrcweir                     if (xController != null){
111*cdf0e10cSrcweir                         xModel = xController.getModel();
112*cdf0e10cSrcweir                     }
113*cdf0e10cSrcweir                 }
114*cdf0e10cSrcweir             }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir             // XDispatch
117*cdf0e10cSrcweir             public void dispatch( /*IN*/com.sun.star.util.URL _aURL, /*IN*/com.sun.star.beans.PropertyValue[] aArguments ) {
118*cdf0e10cSrcweir             try{
119*cdf0e10cSrcweir                 if ( _aURL.Protocol.compareTo("org.openoffice.Office.addon.Inspector:") == 0 ){
120*cdf0e10cSrcweir                     if ( _aURL.Path.equals("inspect")){
121*cdf0e10cSrcweir                         Object oUnoInspectObject = xModel;
122*cdf0e10cSrcweir                         com.sun.star.lang.XMultiComponentFactory xMCF = m_xContext.getServiceManager();
123*cdf0e10cSrcweir                         if (xInstInspector == null){
124*cdf0e10cSrcweir                             Object obj= xMCF.createInstanceWithContext("org.openoffice.InstanceInspector", m_xContext);
125*cdf0e10cSrcweir                             xInstInspector = (org.openoffice.XInstanceInspector)UnoRuntime.queryInterface(org.openoffice.XInstanceInspector.class, obj);
126*cdf0e10cSrcweir                         }
127*cdf0e10cSrcweir                         if ((m_xFrame == null) || (xModel == null)){
128*cdf0e10cSrcweir                             Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
129*cdf0e10cSrcweir                             m_xFrame = (XFrame) UnoRuntime.queryInterface(XFrame.class, oDesktop);
130*cdf0e10cSrcweir                             oUnoInspectObject = m_xFrame;
131*cdf0e10cSrcweir                         }
132*cdf0e10cSrcweir                         XPropertySet xFramePropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, m_xFrame);
133*cdf0e10cSrcweir                         String sTitle = (String) xFramePropertySet.getPropertyValue("Title");
134*cdf0e10cSrcweir                         String[] sTitleList = sTitle.split(" - ");
135*cdf0e10cSrcweir                         if (sTitleList.length > 0){
136*cdf0e10cSrcweir                             sTitle = sTitleList[0];
137*cdf0e10cSrcweir                         }
138*cdf0e10cSrcweir                         xInstInspector.inspect(oUnoInspectObject, sTitle);
139*cdf0e10cSrcweir                     }
140*cdf0e10cSrcweir                 }
141*cdf0e10cSrcweir             } catch( Exception e ) {
142*cdf0e10cSrcweir                 System.err.println( e + e.getMessage());
143*cdf0e10cSrcweir                 e.printStackTrace(System.out);
144*cdf0e10cSrcweir             }}
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir             public void addStatusListener( /*IN*/XStatusListener xControl, /*IN*/com.sun.star.util.URL aURL ) {
147*cdf0e10cSrcweir             }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir             public void removeStatusListener( /*IN*/XStatusListener xControl, /*IN*/com.sun.star.util.URL aURL ) {
150*cdf0e10cSrcweir             }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir         }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir         public static String[] getServiceNames() {
157*cdf0e10cSrcweir             return m_serviceNames;
158*cdf0e10cSrcweir         }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         // Implement the interface XServiceInfo
161*cdf0e10cSrcweir         /** Get all supported service names.
162*cdf0e10cSrcweir          * @return Supported service names.
163*cdf0e10cSrcweir          */
164*cdf0e10cSrcweir         public String[] getSupportedServiceNames() {
165*cdf0e10cSrcweir             return getServiceNames();
166*cdf0e10cSrcweir         }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         // Implement the interface XServiceInfo
169*cdf0e10cSrcweir         /** Test, if the given service will be supported.
170*cdf0e10cSrcweir          * @param sService Service name.
171*cdf0e10cSrcweir          * @return Return true, if the service will be supported.
172*cdf0e10cSrcweir          */
173*cdf0e10cSrcweir         public boolean supportsService( String sServiceName ) {
174*cdf0e10cSrcweir             int len = m_serviceNames.length;
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir             for( int i=0; i < len; i++) {
177*cdf0e10cSrcweir                 if ( sServiceName.equals( m_serviceNames[i] ) )
178*cdf0e10cSrcweir                     return true;
179*cdf0e10cSrcweir             }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir             return false;
182*cdf0e10cSrcweir         }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir         // Implement the interface XServiceInfo
185*cdf0e10cSrcweir         /** Get the implementation name of the component.
186*cdf0e10cSrcweir          * @return Implementation name of the component.
187*cdf0e10cSrcweir          */
188*cdf0e10cSrcweir         public String getImplementationName() {
189*cdf0e10cSrcweir             return InspectorAddonImpl.class.getName();
190*cdf0e10cSrcweir         }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir     /**
196*cdf0e10cSrcweir      * Gives a factory for creating the service.
197*cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
198*cdf0e10cSrcweir      * <p>
199*cdf0e10cSrcweir      * @return  returns a <code>XSingleComponentFactory</code> for creating
200*cdf0e10cSrcweir      *          the component
201*cdf0e10cSrcweir      * @param   sImplName the name of the implementation for which a
202*cdf0e10cSrcweir      *          service is desired
203*cdf0e10cSrcweir      * @see     com.sun.star.comp.loader.JavaLoader
204*cdf0e10cSrcweir      */
205*cdf0e10cSrcweir     public static XSingleComponentFactory __getComponentFactory( String sImplName )
206*cdf0e10cSrcweir     {
207*cdf0e10cSrcweir         XSingleComponentFactory xFactory = null;
208*cdf0e10cSrcweir         if ( sImplName.equals( InspectorAddonImpl.class.getName() ) )
209*cdf0e10cSrcweir             xFactory = Factory.createComponentFactory(InspectorAddonImpl.class, InspectorAddonImpl.getServiceNames());
210*cdf0e10cSrcweir         return xFactory;
211*cdf0e10cSrcweir     }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir     /**
214*cdf0e10cSrcweir      * Writes the service information into the given registry key.
215*cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
216*cdf0e10cSrcweir      * <p>
217*cdf0e10cSrcweir      * @return  returns true if the operation succeeded
218*cdf0e10cSrcweir      * @param   regKey the registryKey
219*cdf0e10cSrcweir      * @see     com.sun.star.comp.loader.JavaLoader
220*cdf0e10cSrcweir      */
221*cdf0e10cSrcweir     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
222*cdf0e10cSrcweir         return Factory.writeRegistryServiceInfo(InspectorAddonImpl.class.getName(), InspectorAddonImpl.getServiceNames(), regKey);
223*cdf0e10cSrcweir     }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir //    __create( XComponentContext ){
226*cdf0e10cSrcweir //
227*cdf0e10cSrcweir //    }
228*cdf0e10cSrcweir }
229