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