Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Inspector / InspectorAddon.java
blobd9236ce2ba05935ec981365ed13b4d50dfc125d0
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.frame.DispatchDescriptor;
36 import com.sun.star.frame.XController;
37 import com.sun.star.frame.XDispatch;
38 import com.sun.star.frame.XDispatchProvider;
39 import com.sun.star.frame.XFrame;
40 import com.sun.star.frame.XModel;
41 import com.sun.star.frame.XStatusListener;
42 import com.sun.star.lang.XInitialization;
43 import com.sun.star.lang.XServiceInfo;
44 import com.sun.star.lang.XSingleComponentFactory;
45 import com.sun.star.lib.uno.helper.WeakBase;
46 import com.sun.star.registry.XRegistryKey;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XComponentContext;
49 import com.sun.star.lib.uno.helper.Factory;
52 public class InspectorAddon {
53 /** This class implements the component. At least the interfaces XServiceInfo,
54 * XTypeProvider, and XInitialization should be provided by the service.
56 public static class InspectorAddonImpl extends WeakBase implements XDispatchProvider, XInitialization, XServiceInfo {
57 private org.openoffice.XInstanceInspector xInstInspector = null;
58 // Dispatcher oDispatcher = null;
59 private XFrame m_xFrame = null;
61 private static final String[] m_serviceNames = {
62 "org.openoffice.InstanceInspectorAddon",
63 "com.sun.star.frame.ProtocolHandler" };
65 private XComponentContext m_xContext = null;
67 /** Creates a new instance of InspectorAddon */
68 public InspectorAddonImpl(XComponentContext _xContext) {
69 m_xContext = _xContext;
72 public XDispatch queryDispatch( /*IN*/com.sun.star.util.URL aURL, /*IN*/String sTargetFrameName, /*IN*/int iSearchFlags ) {
73 XDispatch xRet = null;
74 if ( aURL.Protocol.equals("org.openoffice.Office.addon.Inspector:") ) {
75 if ( aURL.Path.equals( "inspect" ) ){
76 // Todo: Check if the frame is already administered (use hashtable)
77 xRet = new Dispatcher(m_xFrame);
80 return xRet;
84 public XDispatch[] queryDispatches( /*IN*/DispatchDescriptor[] seqDescripts ) {
85 int nCount = seqDescripts.length;
86 XDispatch[] lDispatcher = new XDispatch[nCount];
87 for( int i=0; i<nCount; ++i )
88 lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL, seqDescripts[i].FrameName, seqDescripts[i].SearchFlags );
89 return lDispatcher;
93 public void initialize( Object[] object ) throws com.sun.star.uno.Exception {
94 if ( object.length > 0 ){
95 m_xFrame = UnoRuntime.queryInterface(XFrame.class, object[ 0 ] );
99 private class Dispatcher implements XDispatch{
100 private XFrame m_xFrame = null;
101 private XModel xModel = null;
103 private Dispatcher(XFrame _xFrame){
104 m_xFrame = _xFrame;
105 if (m_xFrame != null){
106 XController xController = m_xFrame.getController();
107 if (xController != null){
108 xModel = xController.getModel();
113 // XDispatch
114 public void dispatch( /*IN*/com.sun.star.util.URL _aURL, /*IN*/com.sun.star.beans.PropertyValue[] aArguments ) {
115 try{
116 if ( _aURL.Protocol.equals("org.openoffice.Office.addon.Inspector:") ){
117 if ( _aURL.Path.equals("inspect")){
118 Object oUnoInspectObject = xModel;
119 com.sun.star.lang.XMultiComponentFactory xMCF = m_xContext.getServiceManager();
120 if (xInstInspector == null){
121 Object obj= xMCF.createInstanceWithContext("org.openoffice.InstanceInspector", m_xContext);
122 xInstInspector = UnoRuntime.queryInterface(org.openoffice.XInstanceInspector.class, obj);
124 if ((m_xFrame == null) || (xModel == null)){
125 Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
126 m_xFrame = UnoRuntime.queryInterface(XFrame.class, oDesktop);
127 oUnoInspectObject = m_xFrame;
129 XPropertySet xFramePropertySet = UnoRuntime.queryInterface(XPropertySet.class, m_xFrame);
130 String sTitle = (String) xFramePropertySet.getPropertyValue("Title");
131 String[] sTitleList = sTitle.split(" - ");
132 if (sTitleList.length > 0){
133 sTitle = sTitleList[0];
135 xInstInspector.inspect(oUnoInspectObject, sTitle);
138 } catch( Exception e ) {
139 System.err.println( e + e.getMessage());
140 e.printStackTrace(System.err);
143 public void addStatusListener( /*IN*/XStatusListener xControl, /*IN*/com.sun.star.util.URL aURL ) {
146 public void removeStatusListener( /*IN*/XStatusListener xControl, /*IN*/com.sun.star.util.URL aURL ) {
153 private static String[] getServiceNames() {
154 return m_serviceNames;
157 // Implement the interface XServiceInfo
158 /** Get all supported service names.
159 * @return Supported service names.
161 public String[] getSupportedServiceNames() {
162 return getServiceNames();
165 // Implement the interface XServiceInfo
166 /** Test, if the given service will be supported.
167 * @return Return true, if the service will be supported.
169 public boolean supportsService( String sServiceName ) {
170 int len = m_serviceNames.length;
172 for( int i=0; i < len; i++) {
173 if ( sServiceName.equals( m_serviceNames[i] ) )
174 return true;
177 return false;
180 // Implement the interface XServiceInfo
181 /** Get the implementation name of the component.
182 * @return Implementation name of the component.
184 public String getImplementationName() {
185 return InspectorAddonImpl.class.getName();
192 * Gives a factory for creating the service.
193 * This method is called by the <code>JavaLoader</code>
194 * <p>
195 * @return returns a <code>XSingleComponentFactory</code> for creating
196 * the component
197 * @param sImplName the name of the implementation for which a
198 * service is desired
199 * @see com.sun.star.comp.loader.JavaLoader
201 public static XSingleComponentFactory __getComponentFactory( String sImplName )
203 XSingleComponentFactory xFactory = null;
204 if ( sImplName.equals( InspectorAddonImpl.class.getName() ) )
205 xFactory = Factory.createComponentFactory(InspectorAddonImpl.class, InspectorAddonImpl.getServiceNames());
206 return xFactory;
210 * Writes the service information into the given registry key.
211 * This method is called by the <code>JavaLoader</code>
212 * <p>
213 * @return returns true if the operation succeeded
214 * @param regKey the registryKey
215 * @see com.sun.star.comp.loader.JavaLoader
217 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
218 return Factory.writeRegistryServiceInfo(InspectorAddonImpl.class.getName(), InspectorAddonImpl.getServiceNames(), regKey);