xref: /AOO41X/main/extensions/qa/integration/extensions/MethodHandler.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package integration.extensions;
29 
30 import com.sun.star.uno.*;
31 import com.sun.star.lang.*;
32 import com.sun.star.beans.*;
33 import com.sun.star.reflection.*;
34 import com.sun.star.inspection.*;
35 
36 /**
37  *
38  * @author fs93730
39  */
40 public class MethodHandler implements XPropertyHandler
41 {
42     private XComponentContext       m_context;
43     private XIntrospection          m_introspection;
44     private XIntrospectionAccess    m_introspectionAccess;
45     private XIdlClass               m_idlClass;
46     private XIdlMethod[]            m_methods;
47     private java.util.HashMap       m_methodsHash;
48 
49     /** Creates a new instance of MethodHandler */
50     public MethodHandler( XComponentContext _context )
51     {
52         m_context = _context;
53         m_methodsHash = new java.util.HashMap();
54 
55         try
56         {
57             m_introspection = (XIntrospection)UnoRuntime.queryInterface( XIntrospection.class,
58                 m_context.getServiceManager().createInstanceWithContext( "com.sun.star.beans.Introspection", m_context )
59             );
60         }
61         catch( com.sun.star.uno.Exception e )
62         {
63             System.err.println( "MethodHandler: could not create a Introspection service, not much functionality will be available." );
64         }
65     }
66 
67     static public XSingleComponentFactory getFactory()
68     {
69         return new ComponentFactory( MethodHandler.class );
70     }
71 
72     public void actuatingPropertyChanged(String _propertyName, Object _newValue, Object _oldValue, com.sun.star.inspection.XObjectInspectorUI _objectInspectorUI, boolean _firstTimeInit) throws com.sun.star.lang.NullPointerException
73     {
74         // not interested in
75     }
76 
77     public void addEventListener(com.sun.star.lang.XEventListener _eventListener)
78     {
79         // ingnoring this
80     }
81 
82     public void addPropertyChangeListener(com.sun.star.beans.XPropertyChangeListener _propertyChangeListener) throws com.sun.star.lang.NullPointerException
83     {
84         // ingnoring this
85     }
86 
87     public Object convertToControlValue(String _propertyName, Object _propertyValue, com.sun.star.uno.Type type) throws com.sun.star.beans.UnknownPropertyException
88     {
89         return _propertyValue;
90     }
91 
92     public Object convertToPropertyValue(String _propertyName, Object _controlValue) throws com.sun.star.beans.UnknownPropertyException
93     {
94         return _controlValue;
95     }
96 
97     public com.sun.star.inspection.LineDescriptor describePropertyLine(String _propertyName, com.sun.star.inspection.XPropertyControlFactory _propertyControlFactory) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.NullPointerException
98     {
99         com.sun.star.inspection.LineDescriptor descriptor = new com.sun.star.inspection.LineDescriptor();
100 
101         descriptor = new LineDescriptor();
102         descriptor.Category = "Methods";
103         descriptor.DisplayName = "has method";
104         descriptor.HasPrimaryButton = descriptor.HasSecondaryButton = false;
105         descriptor.IndentLevel = 0;
106         try
107         {
108             XPropertyControl control = (XPropertyControl)UnoRuntime.queryInterface(
109                     XPropertyControl.class, _propertyControlFactory.createPropertyControl(
110                     PropertyControlType.TextField, true ) );
111 
112             descriptor.Control = control;
113         }
114         catch( com.sun.star.lang.IllegalArgumentException e )
115         {
116         }
117         return descriptor;
118     }
119 
120     public void dispose()
121     {
122         // nothing to do
123     }
124 
125     public String[] getActuatingProperties()
126     {
127         // none
128         return new String[] { };
129     }
130 
131     public com.sun.star.beans.PropertyState getPropertyState(String _propertyName) throws com.sun.star.beans.UnknownPropertyException
132     {
133         return com.sun.star.beans.PropertyState.DIRECT_VALUE;
134     }
135 
136     public Object getPropertyValue(String _propertyName) throws com.sun.star.beans.UnknownPropertyException
137     {
138         XIdlMethod method = impl_getMethod( _propertyName );
139 
140         String signature = new String();
141         signature += method.getReturnType().getName();
142         signature += " ";
143         signature += method.getName();
144 
145         signature += "(";
146 
147         XIdlClass[] parameterTypes = method.getParameterTypes();
148         for ( int param = 0; param<parameterTypes.length; ++param )
149         {
150             signature += ( param == 0 ) ? " " : ", ";
151             signature += parameterTypes[param].getName();
152         }
153 
154         signature += " )";
155         return signature;
156     }
157 
158     public String[] getSupersededProperties()
159     {
160         return new String[] {  };
161     }
162 
163     public com.sun.star.beans.Property[] getSupportedProperties()
164     {
165         Property[] properties = new Property[] { };
166         if ( m_methods != null )
167         {
168             properties = new Property[ m_methods.length ];
169             for ( int i=0; i<m_methods.length; ++i )
170             {
171                 properties[i] = new Property( m_methods[i].getName(), 0, new Type( String.class ), (short)0 );
172                 m_methodsHash.put( m_methods[i].getName(), m_methods[i] );
173             }
174         }
175         return properties;
176     }
177 
178     public void inspect(Object _component) throws com.sun.star.lang.NullPointerException
179     {
180         if ( m_introspection == null )
181             return;
182 
183         m_introspectionAccess = null;
184         m_methods = null;
185         m_methodsHash = new java.util.HashMap();
186 
187         m_introspectionAccess = m_introspection.inspect( _component );
188         if ( m_introspectionAccess == null )
189             return;
190 
191         m_methods = m_introspectionAccess.getMethods( MethodConcept.ALL );
192     }
193 
194     public boolean isComposable(String _propertyName) throws com.sun.star.beans.UnknownPropertyException
195     {
196         return true;
197     }
198 
199     public com.sun.star.inspection.InteractiveSelectionResult onInteractivePropertySelection(String str, boolean param, Object[] obj, com.sun.star.inspection.XObjectInspectorUI xObjectInspectorUI) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.NullPointerException
200     {
201         return InteractiveSelectionResult.Cancelled;
202     }
203 
204     public void removeEventListener(com.sun.star.lang.XEventListener _eventListener)
205     {
206         // ignoring this
207     }
208 
209     public void removePropertyChangeListener(com.sun.star.beans.XPropertyChangeListener _propertyChangeListener)
210     {
211         // ignoring this
212     }
213 
214     public void setPropertyValue(String str, Object obj) throws com.sun.star.beans.UnknownPropertyException
215     {
216         // we declared our properties as readonly
217         throw new java.lang.RuntimeException();
218     }
219 
220     public boolean suspend(boolean param)
221     {
222         return true;
223     }
224 
225     /** returns the descriptor for the method with the given name
226      *  @param _propertyName
227      *      the name of the method whose descriptor should be obtained
228      *  @throws com.sun.star.beans.UnknownPropertyException
229      *      if we don't have a method hash, or the given property name does not denote a method of our inspectee
230      */
231     private XIdlMethod impl_getMethod( String _methodName ) throws UnknownPropertyException
232     {
233         XIdlMethod method = (XIdlMethod)m_methodsHash.get( _methodName );
234         if ( method == null )
235             throw new com.sun.star.beans.UnknownPropertyException();
236 
237         return method;
238     }
239 }
240