Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / java / Inspector / InspectorAddon.java
blobb001d17b5a46009080687b27c1a47118f872ce14
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
35 import com.sun.star.beans.XPropertySet;
36 import com.sun.star.frame.DispatchDescriptor;
37 import com.sun.star.frame.XController;
38 import com.sun.star.frame.XDispatch;
39 import com.sun.star.frame.XDispatchProvider;
40 import com.sun.star.frame.XFrame;
41 import com.sun.star.frame.XModel;
42 import com.sun.star.frame.XStatusListener;
43 import com.sun.star.lang.XInitialization;
44 import com.sun.star.lang.XServiceInfo;
45 import com.sun.star.lang.XSingleComponentFactory;
46 import com.sun.star.lib.uno.helper.WeakBase;
47 import com.sun.star.registry.XRegistryKey;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XComponentContext;
50 import com.sun.star.lib.uno.helper.Factory;
53 public class InspectorAddon {
54 /** This class implements the component. At least the interfaces XServiceInfo,
55 * XTypeProvider, and XInitialization should be provided by the service.
57 public static class InspectorAddonImpl extends WeakBase implements XDispatchProvider, XInitialization, XServiceInfo {
58 private org.openoffice.XInstanceInspector xInstInspector = null;
59 // Dispatcher oDispatcher = null;
60 private XFrame m_xFrame = null;
62 private static final String[] m_serviceNames = {
63 "org.openoffice.InstanceInspectorAddon",
64 "com.sun.star.frame.ProtocolHandler" };
66 private XComponentContext m_xContext = null;
68 /** Creates a new instance of InspectorAddon */
69 public InspectorAddonImpl(XComponentContext _xContext) {
70 m_xContext = _xContext;
73 public XDispatch queryDispatch( /*IN*/com.sun.star.util.URL aURL, /*IN*/String sTargetFrameName, /*IN*/int iSearchFlags ) {
74 XDispatch xRet = null;
75 if ( aURL.Protocol.equals("org.openoffice.Office.addon.Inspector:") ) {
76 if ( aURL.Path.equals( "inspect" ) ){
77 // Todo: Check if the frame is already administered (use hashtable)
78 xRet = new Dispatcher(m_xFrame);
81 return xRet;
85 public XDispatch[] queryDispatches( /*IN*/DispatchDescriptor[] seqDescripts ) {
86 int nCount = seqDescripts.length;
87 XDispatch[] lDispatcher = new XDispatch[nCount];
88 for( int i=0; i<nCount; ++i )
89 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL, seqDescripts[i].FrameName, seqDescripts[i].SearchFlags );
90 return lDispatcher;
94 public void initialize( Object[] object ) throws com.sun.star.uno.Exception {
95 if ( object.length > 0 ){
96 m_xFrame = UnoRuntime.queryInterface(XFrame.class, object[ 0 ] );
100 private class Dispatcher implements XDispatch{
101 private XFrame m_xFrame = null;
102 private XModel xModel = null;
104 private Dispatcher(XFrame _xFrame){
105 m_xFrame = _xFrame;
106 if (m_xFrame != null){
107 XController xController = m_xFrame.getController();
108 if (xController != null){
109 xModel = xController.getModel();
114 // XDispatch
115 public void dispatch( /*IN*/com.sun.star.util.URL _aURL, /*IN*/com.sun.star.beans.PropertyValue[] aArguments ) {
116 try{
117 if ( _aURL.Protocol.equals("org.openoffice.Office.addon.Inspector:") ){
118 if ( _aURL.Path.equals("inspect")){
119 Object oUnoInspectObject = xModel;
120 com.sun.star.lang.XMultiComponentFactory xMCF = m_xContext.getServiceManager();
121 if (xInstInspector == null){
122 Object obj= xMCF.createInstanceWithContext("org.openoffice.InstanceInspector", m_xContext);
123 xInstInspector = UnoRuntime.queryInterface(org.openoffice.XInstanceInspector.class, obj);
125 if ((m_xFrame == null) || (xModel == null)){
126 Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
127 m_xFrame = UnoRuntime.queryInterface(XFrame.class, oDesktop);
128 oUnoInspectObject = m_xFrame;
130 XPropertySet xFramePropertySet = UnoRuntime.queryInterface(XPropertySet.class, m_xFrame);
131 String sTitle = (String) xFramePropertySet.getPropertyValue("Title");
132 String[] sTitleList = sTitle.split(" - ");
133 if (sTitleList.length > 0){
134 sTitle = sTitleList[0];
136 xInstInspector.inspect(oUnoInspectObject, sTitle);
139 } catch( Exception e ) {
140 System.err.println( e + e.getMessage());
141 e.printStackTrace(System.err);
144 public void addStatusListener( /*IN*/XStatusListener xControl, /*IN*/com.sun.star.util.URL aURL ) {
147 public void removeStatusListener( /*IN*/XStatusListener xControl, /*IN*/com.sun.star.util.URL aURL ) {
154 private static String[] getServiceNames() {
155 return m_serviceNames;
158 // Implement the interface XServiceInfo
159 /** Get all supported service names.
160 * @return Supported service names.
162 public String[] getSupportedServiceNames() {
163 return getServiceNames();
166 // Implement the interface XServiceInfo
167 /** Test, if the given service will be supported.
168 * @return Return true, if the service will be supported.
170 public boolean supportsService( String sServiceName ) {
171 int len = m_serviceNames.length;
173 for( int i=0; i < len; i++) {
174 if ( sServiceName.equals( m_serviceNames[i] ) )
175 return true;
178 return false;
181 // Implement the interface XServiceInfo
182 /** Get the implementation name of the component.
183 * @return Implementation name of the component.
185 public String getImplementationName() {
186 return InspectorAddonImpl.class.getName();
193 * Gives a factory for creating the service.
194 * This method is called by the <code>JavaLoader</code>
195 * <p>
196 * @return returns a <code>XSingleComponentFactory</code> for creating
197 * the component
198 * @param sImplName the name of the implementation for which a
199 * service is desired
200 * @see com.sun.star.comp.loader.JavaLoader
202 public static XSingleComponentFactory __getComponentFactory( String sImplName )
204 XSingleComponentFactory xFactory = null;
205 if ( sImplName.equals( InspectorAddonImpl.class.getName() ) )
206 xFactory = Factory.createComponentFactory(InspectorAddonImpl.class, InspectorAddonImpl.getServiceNames());
207 return xFactory;
211 * Writes the service information into the given registry key.
212 * This method is called by the <code>JavaLoader</code>
213 * <p>
214 * @return returns true if the operation succeeded
215 * @param regKey the registryKey
216 * @see com.sun.star.comp.loader.JavaLoader
218 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
219 return Factory.writeRegistryServiceInfo(InspectorAddonImpl.class.getName(), InspectorAddonImpl.getServiceNames(), regKey);
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */