1*1071dbebSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*1071dbebSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*1071dbebSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*1071dbebSAndrew Rist * distributed with this work for additional information 6*1071dbebSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*1071dbebSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*1071dbebSAndrew Rist * "License"); you may not use this file except in compliance 9*1071dbebSAndrew Rist * with the License. You may obtain a copy of the License at 10*1071dbebSAndrew Rist * 11*1071dbebSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*1071dbebSAndrew Rist * 13*1071dbebSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*1071dbebSAndrew Rist * software distributed under the License is distributed on an 15*1071dbebSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*1071dbebSAndrew Rist * KIND, either express or implied. See the License for the 17*1071dbebSAndrew Rist * specific language governing permissions and limitations 18*1071dbebSAndrew Rist * under the License. 19*1071dbebSAndrew Rist * 20*1071dbebSAndrew Rist *************************************************************/ 21*1071dbebSAndrew Rist 22*1071dbebSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package integration.extensions; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.uno.*; 27cdf0e10cSrcweir import com.sun.star.lang.*; 28cdf0e10cSrcweir import com.sun.star.beans.*; 29cdf0e10cSrcweir import com.sun.star.reflection.*; 30cdf0e10cSrcweir import com.sun.star.inspection.*; 31cdf0e10cSrcweir 32cdf0e10cSrcweir /** 33cdf0e10cSrcweir * 34cdf0e10cSrcweir * @author fs93730 35cdf0e10cSrcweir */ 36cdf0e10cSrcweir public class MethodHandler implements XPropertyHandler 37cdf0e10cSrcweir { 38cdf0e10cSrcweir private XComponentContext m_context; 39cdf0e10cSrcweir private XIntrospection m_introspection; 40cdf0e10cSrcweir private XIntrospectionAccess m_introspectionAccess; 41cdf0e10cSrcweir private XIdlClass m_idlClass; 42cdf0e10cSrcweir private XIdlMethod[] m_methods; 43cdf0e10cSrcweir private java.util.HashMap m_methodsHash; 44cdf0e10cSrcweir 45cdf0e10cSrcweir /** Creates a new instance of MethodHandler */ MethodHandler( XComponentContext _context )46cdf0e10cSrcweir public MethodHandler( XComponentContext _context ) 47cdf0e10cSrcweir { 48cdf0e10cSrcweir m_context = _context; 49cdf0e10cSrcweir m_methodsHash = new java.util.HashMap(); 50cdf0e10cSrcweir 51cdf0e10cSrcweir try 52cdf0e10cSrcweir { 53cdf0e10cSrcweir m_introspection = (XIntrospection)UnoRuntime.queryInterface( XIntrospection.class, 54cdf0e10cSrcweir m_context.getServiceManager().createInstanceWithContext( "com.sun.star.beans.Introspection", m_context ) 55cdf0e10cSrcweir ); 56cdf0e10cSrcweir } 57cdf0e10cSrcweir catch( com.sun.star.uno.Exception e ) 58cdf0e10cSrcweir { 59cdf0e10cSrcweir System.err.println( "MethodHandler: could not create a Introspection service, not much functionality will be available." ); 60cdf0e10cSrcweir } 61cdf0e10cSrcweir } 62cdf0e10cSrcweir getFactory()63cdf0e10cSrcweir static public XSingleComponentFactory getFactory() 64cdf0e10cSrcweir { 65cdf0e10cSrcweir return new ComponentFactory( MethodHandler.class ); 66cdf0e10cSrcweir } 67cdf0e10cSrcweir actuatingPropertyChanged(String _propertyName, Object _newValue, Object _oldValue, com.sun.star.inspection.XObjectInspectorUI _objectInspectorUI, boolean _firstTimeInit)68cdf0e10cSrcweir public void actuatingPropertyChanged(String _propertyName, Object _newValue, Object _oldValue, com.sun.star.inspection.XObjectInspectorUI _objectInspectorUI, boolean _firstTimeInit) throws com.sun.star.lang.NullPointerException 69cdf0e10cSrcweir { 70cdf0e10cSrcweir // not interested in 71cdf0e10cSrcweir } 72cdf0e10cSrcweir addEventListener(com.sun.star.lang.XEventListener _eventListener)73cdf0e10cSrcweir public void addEventListener(com.sun.star.lang.XEventListener _eventListener) 74cdf0e10cSrcweir { 75cdf0e10cSrcweir // ingnoring this 76cdf0e10cSrcweir } 77cdf0e10cSrcweir addPropertyChangeListener(com.sun.star.beans.XPropertyChangeListener _propertyChangeListener)78cdf0e10cSrcweir public void addPropertyChangeListener(com.sun.star.beans.XPropertyChangeListener _propertyChangeListener) throws com.sun.star.lang.NullPointerException 79cdf0e10cSrcweir { 80cdf0e10cSrcweir // ingnoring this 81cdf0e10cSrcweir } 82cdf0e10cSrcweir convertToControlValue(String _propertyName, Object _propertyValue, com.sun.star.uno.Type type)83cdf0e10cSrcweir public Object convertToControlValue(String _propertyName, Object _propertyValue, com.sun.star.uno.Type type) throws com.sun.star.beans.UnknownPropertyException 84cdf0e10cSrcweir { 85cdf0e10cSrcweir return _propertyValue; 86cdf0e10cSrcweir } 87cdf0e10cSrcweir convertToPropertyValue(String _propertyName, Object _controlValue)88cdf0e10cSrcweir public Object convertToPropertyValue(String _propertyName, Object _controlValue) throws com.sun.star.beans.UnknownPropertyException 89cdf0e10cSrcweir { 90cdf0e10cSrcweir return _controlValue; 91cdf0e10cSrcweir } 92cdf0e10cSrcweir describePropertyLine(String _propertyName, com.sun.star.inspection.XPropertyControlFactory _propertyControlFactory)93cdf0e10cSrcweir 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 94cdf0e10cSrcweir { 95cdf0e10cSrcweir com.sun.star.inspection.LineDescriptor descriptor = new com.sun.star.inspection.LineDescriptor(); 96cdf0e10cSrcweir 97cdf0e10cSrcweir descriptor = new LineDescriptor(); 98cdf0e10cSrcweir descriptor.Category = "Methods"; 99cdf0e10cSrcweir descriptor.DisplayName = "has method"; 100cdf0e10cSrcweir descriptor.HasPrimaryButton = descriptor.HasSecondaryButton = false; 101cdf0e10cSrcweir descriptor.IndentLevel = 0; 102cdf0e10cSrcweir try 103cdf0e10cSrcweir { 104cdf0e10cSrcweir XPropertyControl control = (XPropertyControl)UnoRuntime.queryInterface( 105cdf0e10cSrcweir XPropertyControl.class, _propertyControlFactory.createPropertyControl( 106cdf0e10cSrcweir PropertyControlType.TextField, true ) ); 107cdf0e10cSrcweir 108cdf0e10cSrcweir descriptor.Control = control; 109cdf0e10cSrcweir } 110cdf0e10cSrcweir catch( com.sun.star.lang.IllegalArgumentException e ) 111cdf0e10cSrcweir { 112cdf0e10cSrcweir } 113cdf0e10cSrcweir return descriptor; 114cdf0e10cSrcweir } 115cdf0e10cSrcweir dispose()116cdf0e10cSrcweir public void dispose() 117cdf0e10cSrcweir { 118cdf0e10cSrcweir // nothing to do 119cdf0e10cSrcweir } 120cdf0e10cSrcweir getActuatingProperties()121cdf0e10cSrcweir public String[] getActuatingProperties() 122cdf0e10cSrcweir { 123cdf0e10cSrcweir // none 124cdf0e10cSrcweir return new String[] { }; 125cdf0e10cSrcweir } 126cdf0e10cSrcweir getPropertyState(String _propertyName)127cdf0e10cSrcweir public com.sun.star.beans.PropertyState getPropertyState(String _propertyName) throws com.sun.star.beans.UnknownPropertyException 128cdf0e10cSrcweir { 129cdf0e10cSrcweir return com.sun.star.beans.PropertyState.DIRECT_VALUE; 130cdf0e10cSrcweir } 131cdf0e10cSrcweir getPropertyValue(String _propertyName)132cdf0e10cSrcweir public Object getPropertyValue(String _propertyName) throws com.sun.star.beans.UnknownPropertyException 133cdf0e10cSrcweir { 134cdf0e10cSrcweir XIdlMethod method = impl_getMethod( _propertyName ); 135cdf0e10cSrcweir 136cdf0e10cSrcweir String signature = new String(); 137cdf0e10cSrcweir signature += method.getReturnType().getName(); 138cdf0e10cSrcweir signature += " "; 139cdf0e10cSrcweir signature += method.getName(); 140cdf0e10cSrcweir 141cdf0e10cSrcweir signature += "("; 142cdf0e10cSrcweir 143cdf0e10cSrcweir XIdlClass[] parameterTypes = method.getParameterTypes(); 144cdf0e10cSrcweir for ( int param = 0; param<parameterTypes.length; ++param ) 145cdf0e10cSrcweir { 146cdf0e10cSrcweir signature += ( param == 0 ) ? " " : ", "; 147cdf0e10cSrcweir signature += parameterTypes[param].getName(); 148cdf0e10cSrcweir } 149cdf0e10cSrcweir 150cdf0e10cSrcweir signature += " )"; 151cdf0e10cSrcweir return signature; 152cdf0e10cSrcweir } 153cdf0e10cSrcweir getSupersededProperties()154cdf0e10cSrcweir public String[] getSupersededProperties() 155cdf0e10cSrcweir { 156cdf0e10cSrcweir return new String[] { }; 157cdf0e10cSrcweir } 158cdf0e10cSrcweir getSupportedProperties()159cdf0e10cSrcweir public com.sun.star.beans.Property[] getSupportedProperties() 160cdf0e10cSrcweir { 161cdf0e10cSrcweir Property[] properties = new Property[] { }; 162cdf0e10cSrcweir if ( m_methods != null ) 163cdf0e10cSrcweir { 164cdf0e10cSrcweir properties = new Property[ m_methods.length ]; 165cdf0e10cSrcweir for ( int i=0; i<m_methods.length; ++i ) 166cdf0e10cSrcweir { 167cdf0e10cSrcweir properties[i] = new Property( m_methods[i].getName(), 0, new Type( String.class ), (short)0 ); 168cdf0e10cSrcweir m_methodsHash.put( m_methods[i].getName(), m_methods[i] ); 169cdf0e10cSrcweir } 170cdf0e10cSrcweir } 171cdf0e10cSrcweir return properties; 172cdf0e10cSrcweir } 173cdf0e10cSrcweir inspect(Object _component)174cdf0e10cSrcweir public void inspect(Object _component) throws com.sun.star.lang.NullPointerException 175cdf0e10cSrcweir { 176cdf0e10cSrcweir if ( m_introspection == null ) 177cdf0e10cSrcweir return; 178cdf0e10cSrcweir 179cdf0e10cSrcweir m_introspectionAccess = null; 180cdf0e10cSrcweir m_methods = null; 181cdf0e10cSrcweir m_methodsHash = new java.util.HashMap(); 182cdf0e10cSrcweir 183cdf0e10cSrcweir m_introspectionAccess = m_introspection.inspect( _component ); 184cdf0e10cSrcweir if ( m_introspectionAccess == null ) 185cdf0e10cSrcweir return; 186cdf0e10cSrcweir 187cdf0e10cSrcweir m_methods = m_introspectionAccess.getMethods( MethodConcept.ALL ); 188cdf0e10cSrcweir } 189cdf0e10cSrcweir isComposable(String _propertyName)190cdf0e10cSrcweir public boolean isComposable(String _propertyName) throws com.sun.star.beans.UnknownPropertyException 191cdf0e10cSrcweir { 192cdf0e10cSrcweir return true; 193cdf0e10cSrcweir } 194cdf0e10cSrcweir onInteractivePropertySelection(String str, boolean param, Object[] obj, com.sun.star.inspection.XObjectInspectorUI xObjectInspectorUI)195cdf0e10cSrcweir 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 196cdf0e10cSrcweir { 197cdf0e10cSrcweir return InteractiveSelectionResult.Cancelled; 198cdf0e10cSrcweir } 199cdf0e10cSrcweir removeEventListener(com.sun.star.lang.XEventListener _eventListener)200cdf0e10cSrcweir public void removeEventListener(com.sun.star.lang.XEventListener _eventListener) 201cdf0e10cSrcweir { 202cdf0e10cSrcweir // ignoring this 203cdf0e10cSrcweir } 204cdf0e10cSrcweir removePropertyChangeListener(com.sun.star.beans.XPropertyChangeListener _propertyChangeListener)205cdf0e10cSrcweir public void removePropertyChangeListener(com.sun.star.beans.XPropertyChangeListener _propertyChangeListener) 206cdf0e10cSrcweir { 207cdf0e10cSrcweir // ignoring this 208cdf0e10cSrcweir } 209cdf0e10cSrcweir setPropertyValue(String str, Object obj)210cdf0e10cSrcweir public void setPropertyValue(String str, Object obj) throws com.sun.star.beans.UnknownPropertyException 211cdf0e10cSrcweir { 212cdf0e10cSrcweir // we declared our properties as readonly 213cdf0e10cSrcweir throw new java.lang.RuntimeException(); 214cdf0e10cSrcweir } 215cdf0e10cSrcweir suspend(boolean param)216cdf0e10cSrcweir public boolean suspend(boolean param) 217cdf0e10cSrcweir { 218cdf0e10cSrcweir return true; 219cdf0e10cSrcweir } 220cdf0e10cSrcweir 221cdf0e10cSrcweir /** returns the descriptor for the method with the given name 222cdf0e10cSrcweir * @param _propertyName 223cdf0e10cSrcweir * the name of the method whose descriptor should be obtained 224cdf0e10cSrcweir * @throws com.sun.star.beans.UnknownPropertyException 225cdf0e10cSrcweir * if we don't have a method hash, or the given property name does not denote a method of our inspectee 226cdf0e10cSrcweir */ impl_getMethod( String _methodName )227cdf0e10cSrcweir private XIdlMethod impl_getMethod( String _methodName ) throws UnknownPropertyException 228cdf0e10cSrcweir { 229cdf0e10cSrcweir XIdlMethod method = (XIdlMethod)m_methodsHash.get( _methodName ); 230cdf0e10cSrcweir if ( method == null ) 231cdf0e10cSrcweir throw new com.sun.star.beans.UnknownPropertyException(); 232cdf0e10cSrcweir 233cdf0e10cSrcweir return method; 234cdf0e10cSrcweir } 235cdf0e10cSrcweir } 236