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