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