1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 import com.sun.star.beans.XPropertySet;
24 import com.sun.star.frame.DispatchDescriptor;
25 import com.sun.star.frame.FrameSearchFlag;
26 import com.sun.star.frame.XController;
27 import com.sun.star.frame.XDesktop;
28 import com.sun.star.frame.XDispatch;
29 import com.sun.star.frame.XDispatchProvider;
30 import com.sun.star.frame.XFrame;
31 import com.sun.star.frame.XModel;
32 import com.sun.star.frame.XStatusListener;
33 import com.sun.star.lang.XInitialization;
34 import com.sun.star.lang.XServiceInfo;
35 import com.sun.star.lang.XSingleComponentFactory;
36 import com.sun.star.lib.uno.helper.WeakBase;
37 import com.sun.star.registry.XRegistryKey;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XComponentContext;
40 import com.sun.star.lib.uno.helper.Factory;
41 
42 
43 public class InspectorAddon {
44     /** This class implements the component. At least the interfaces XServiceInfo,
45      * XTypeProvider, and XInitialization should be provided by the service.
46      */
47     public static class InspectorAddonImpl extends WeakBase implements XDispatchProvider, XInitialization, XServiceInfo {
48     private static XModel xModel = null;
49     org.openoffice.XInstanceInspector xInstInspector = null;
50 //    Dispatcher oDispatcher = null;
51     XFrame m_xFrame = null;
52 
53     private static final String[] m_serviceNames = {
54         "org.openoffice.InstanceInspectorAddon",
55         "com.sun.star.frame.ProtocolHandler" };
56                                                      ;
57         private XComponentContext m_xContext = null;
58 
59     /** Creates a new instance of InspectorAddon */
InspectorAddonImpl(XComponentContext _xContext)60     public InspectorAddonImpl(XComponentContext _xContext) {
61         m_xContext = _xContext;
62     }
63 
queryDispatch( com.sun.star.util.URL aURL, String sTargetFrameName, int iSearchFlags )64         public XDispatch queryDispatch( /*IN*/com.sun.star.util.URL aURL, /*IN*/String sTargetFrameName, /*IN*/int iSearchFlags ) {
65             XDispatch xRet = null;
66             if ( aURL.Protocol.compareTo("org.openoffice.Office.addon.Inspector:") == 0 ) {
67                 if ( aURL.Path.compareTo( "inspect" ) == 0 ){
68                     // Todo: Check if the frame is already administered (use hashtable)
69                     xRet = new Dispatcher(m_xFrame);
70                 }
71             }
72             return xRet;
73         }
74 
75 
queryDispatches( DispatchDescriptor[] seqDescripts )76         public XDispatch[] queryDispatches( /*IN*/DispatchDescriptor[] seqDescripts ) {
77             int nCount = seqDescripts.length;
78             XDispatch[] lDispatcher = new XDispatch[nCount];
79             for( int i=0; i<nCount; ++i )
80                 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL, seqDescripts[i].FrameName, seqDescripts[i].SearchFlags );
81             return lDispatcher;
82         }
83 
84 
initialize( Object[] object )85         public void initialize( Object[] object ) throws com.sun.star.uno.Exception {
86             if ( object.length > 0 ){
87                 m_xFrame = ( XFrame ) UnoRuntime.queryInterface(XFrame.class, object[ 0 ] );
88             }
89         }
90 
91         public class Dispatcher implements XDispatch{
92             private XFrame m_xFrame = null;
93             private XModel xModel = null;
94 
Dispatcher(XFrame _xFrame)95             public Dispatcher(XFrame _xFrame){
96                 m_xFrame = _xFrame;
97                 if (m_xFrame != null){
98                     XController xController = m_xFrame.getController();
99                     if (xController != null){
100                         xModel = xController.getModel();
101                     }
102                 }
103             }
104 
105             // XDispatch
dispatch( com.sun.star.util.URL _aURL, com.sun.star.beans.PropertyValue[] aArguments )106             public void dispatch( /*IN*/com.sun.star.util.URL _aURL, /*IN*/com.sun.star.beans.PropertyValue[] aArguments ) {
107             try{
108                 if ( _aURL.Protocol.compareTo("org.openoffice.Office.addon.Inspector:") == 0 ){
109                     if ( _aURL.Path.equals("inspect")){
110                         Object oUnoInspectObject = xModel;
111                         com.sun.star.lang.XMultiComponentFactory xMCF = m_xContext.getServiceManager();
112                         if (xInstInspector == null){
113                             Object obj= xMCF.createInstanceWithContext("org.openoffice.InstanceInspector", m_xContext);
114                             xInstInspector = (org.openoffice.XInstanceInspector)UnoRuntime.queryInterface(org.openoffice.XInstanceInspector.class, obj);
115                         }
116                         if ((m_xFrame == null) || (xModel == null)){
117                             Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
118                             m_xFrame = (XFrame) UnoRuntime.queryInterface(XFrame.class, oDesktop);
119                             oUnoInspectObject = m_xFrame;
120                         }
121                         XPropertySet xFramePropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, m_xFrame);
122                         String sTitle = (String) xFramePropertySet.getPropertyValue("Title");
123                         String[] sTitleList = sTitle.split(" - ");
124                         if (sTitleList.length > 0){
125                             sTitle = sTitleList[0];
126                         }
127                         xInstInspector.inspect(oUnoInspectObject, sTitle);
128                     }
129                 }
130             } catch( Exception e ) {
131                 System.err.println( e + e.getMessage());
132                 e.printStackTrace(System.out);
133             }}
134 
addStatusListener( XStatusListener xControl, com.sun.star.util.URL aURL )135             public void addStatusListener( /*IN*/XStatusListener xControl, /*IN*/com.sun.star.util.URL aURL ) {
136             }
137 
removeStatusListener( XStatusListener xControl, com.sun.star.util.URL aURL )138             public void removeStatusListener( /*IN*/XStatusListener xControl, /*IN*/com.sun.star.util.URL aURL ) {
139             }
140 
141 
142         }
143 
144 
getServiceNames()145         public static String[] getServiceNames() {
146             return m_serviceNames;
147         }
148 
149         // Implement the interface XServiceInfo
150         /** Get all supported service names.
151          * @return Supported service names.
152          */
getSupportedServiceNames()153         public String[] getSupportedServiceNames() {
154             return getServiceNames();
155         }
156 
157         // Implement the interface XServiceInfo
158         /** Test, if the given service will be supported.
159          * @param sService Service name.
160          * @return Return true, if the service will be supported.
161          */
supportsService( String sServiceName )162         public boolean supportsService( String sServiceName ) {
163             int len = m_serviceNames.length;
164 
165             for( int i=0; i < len; i++) {
166                 if ( sServiceName.equals( m_serviceNames[i] ) )
167                     return true;
168             }
169 
170             return false;
171         }
172 
173         // Implement the interface XServiceInfo
174         /** Get the implementation name of the component.
175          * @return Implementation name of the component.
176          */
getImplementationName()177         public String getImplementationName() {
178             return InspectorAddonImpl.class.getName();
179         }
180 
181     }
182 
183 
184     /**
185      * Gives a factory for creating the service.
186      * This method is called by the <code>JavaLoader</code>
187      * <p>
188      * @return  returns a <code>XSingleComponentFactory</code> for creating
189      *          the component
190      * @param   sImplName the name of the implementation for which a
191      *          service is desired
192      * @see     com.sun.star.comp.loader.JavaLoader
193      */
__getComponentFactory( String sImplName )194     public static XSingleComponentFactory __getComponentFactory( String sImplName )
195     {
196         XSingleComponentFactory xFactory = null;
197         if ( sImplName.equals( InspectorAddonImpl.class.getName() ) )
198             xFactory = Factory.createComponentFactory(InspectorAddonImpl.class, InspectorAddonImpl.getServiceNames());
199         return xFactory;
200     }
201 
202     /**
203      * Writes the service information into the given registry key.
204      * This method is called by the <code>JavaLoader</code>
205      * <p>
206      * @return  returns true if the operation succeeded
207      * @param   regKey the registryKey
208      * @see     com.sun.star.comp.loader.JavaLoader
209      */
__writeRegistryServiceInfo(XRegistryKey regKey)210     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
211         return Factory.writeRegistryServiceInfo(InspectorAddonImpl.class.getName(), InspectorAddonImpl.getServiceNames(), regKey);
212     }
213 
214 //    __create( XComponentContext ){
215 //
216 //    }
217 }
218