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 import com.sun.star.reflection.ParamInfo;
25 import com.sun.star.reflection.ParamMode;
26 import com.sun.star.reflection.XIdlClass;
27 import com.sun.star.reflection.XIdlMethod;
28 import com.sun.star.uno.TypeClass;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.util.Vector;
32 
33 public class UnoMethodNode extends UnoNode{
34     XIdlMethod m_xIdlMethod = null;
35     Object[] m_oParamObjects = null;
36     Object m_oUnoReturnObject = null;
37     boolean m_bisInvoked = false;
38     XUnoMethodNode m_xUnoMethodNode = null;
39 
40 
41     /** Creates a new instance of UnoMethodNode */
UnoMethodNode(XIdlMethod _xIdlMethod, Object _oUnoObject, XUnoMethodNode _xUnoMethodNode)42     public UnoMethodNode(XIdlMethod _xIdlMethod, Object _oUnoObject, XUnoMethodNode _xUnoMethodNode) {
43         super(_oUnoObject);
44         m_xIdlMethod = _xIdlMethod;
45         m_oParamObjects = new Object[m_xIdlMethod.getParameterInfos().length];
46         m_xUnoMethodNode = _xUnoMethodNode;
47     }
48 
isFoldable()49     protected boolean isFoldable(){
50         return ((!this.isPrimitive()) &&  (getTypeClass().getValue() != TypeClass.VOID_value));
51     }
52 
isInvokable()53     protected boolean isInvokable(){
54         boolean bisFoldable = true;
55         XIdlClass[] xIdlClasses = m_xIdlMethod.getParameterTypes();
56         for (int i = 0; i < xIdlClasses.length; i++){
57             bisFoldable = Introspector.isPrimitive(xIdlClasses[i].getTypeClass());
58             if (!bisFoldable){
59                 return false;
60             }
61         }
62         return bisFoldable;
63     }
64 
getXIdlMethod()65     public XIdlMethod getXIdlMethod(){
66         return m_xIdlMethod;
67     }
68 
69 
getAnchor()70     public String getAnchor(){
71         return getXIdlMethod().getName();
72     }
73 
74 
getName()75     public String getName(){
76         return getXIdlMethod().getName();
77     }
78 
79 
invoke()80     public Object invoke(){
81         Object oUnoReturnObject = null;
82         if (!hasParameters()){
83             oUnoReturnObject = invokeParameterlessMethod();
84             m_bisInvoked = true;
85         }
86         else{
87             Vector oUnoMethodObjects = m_xUnoMethodNode.getMethodObjects();
88             if (oUnoMethodObjects != null){
89                 for (int i = 0; i < getXIdlMethod().getParameterInfos().length; i++){
90                     this.m_oParamObjects[i] = oUnoMethodObjects.get(i);
91                 }
92                 if (oUnoMethodObjects.size() == m_oParamObjects.length + 1){
93                     oUnoReturnObject = oUnoMethodObjects.get(oUnoMethodObjects.size()-1);
94                 }
95                 m_bisInvoked = (oUnoReturnObject != null);
96             }
97         }
98         m_oUnoReturnObject = oUnoReturnObject;
99         return oUnoReturnObject;
100     }
101 
102 
isInvoked()103     public boolean isInvoked(){
104         return m_bisInvoked;
105     }
106 
107 
getNodeDescription()108     protected String getNodeDescription(){
109         String sNodeDescription = "";
110         String sParameters = getParameterDescription();
111         if (m_xIdlMethod.getParameterInfos().length > 0){
112             sNodeDescription = getStandardMethodDescription();
113         }
114         else{
115             TypeClass typeClass = getTypeClass();
116             if (typeClass != TypeClass.VOID){
117                 sNodeDescription = getStandardMethodDescription();
118             }
119             else{
120                 sNodeDescription = getStandardMethodDescription();
121             }
122         }
123         return sNodeDescription;
124     }
125 
126 
getStandardMethodDescription()127     public String getStandardMethodDescription(){
128         String sNodeDescription = m_xIdlMethod.getReturnType().getName() + " " + m_xIdlMethod.getName() + " (" + getParameterDescription() + " )";
129         if (isPrimitive()){
130             sNodeDescription += "";
131         }
132         return sNodeDescription;
133     }
134 
135 
hasParameters()136     public boolean hasParameters(){
137         return (m_xIdlMethod.getParameterInfos().length > 0);
138     }
139 
140 
getLastParameterObjects()141     public Object[] getLastParameterObjects(){
142         return m_oParamObjects;
143     }
144 
145 
getLastUnoReturnObject()146     public Object getLastUnoReturnObject(){
147         return m_oUnoReturnObject;
148     }
149 
150 
getParameterDescription()151     public String getParameterDescription(){
152         ParamInfo[] paramInfo = m_xIdlMethod.getParameterInfos();
153         String sParameters = "";
154         String sStandardMethodDisplayText = m_xIdlMethod.getReturnType().getName() + " " + m_xIdlMethod.getName() + " (" + sParameters + " )";
155         if (Introspector.isValid(paramInfo)) {
156             //  get all parameters with type and mode
157             for ( int i = 0; i < paramInfo.length; i++ ) {
158                 XIdlClass xIdlClass = paramInfo[ i ].aType;
159                 if ( i == 0 ) {
160                     //  the first parameter has no leading comma
161                     sParameters += "[" + getParamMode(paramInfo[ i ].aMode ) + "] " + xIdlClass.getName();
162                 }
163                 else {
164                     //  all other parameters are separated with comma
165                     sParameters += ", [" + getParamMode(paramInfo[ i ].aMode ) + "] " + xIdlClass.getName();
166                 }
167             }
168         }
169         return sParameters;
170     }
171 
172 
173     //  return the parameter mode (IN, OUT, INOUT)
getParamMode(ParamMode paramMode)174     private static String getParamMode(ParamMode paramMode) {
175         String toReturn = "";
176         if ( paramMode == ParamMode.IN ) {
177             toReturn = "IN";
178         }
179         if ( paramMode == ParamMode.OUT ) {
180             toReturn = "OUT";
181         }
182         if ( paramMode == ParamMode.INOUT ) {
183             toReturn = "INOUT";
184         }
185         return( toReturn );
186     }
187 
getTypeClass()188     public TypeClass getTypeClass(){
189         XIdlClass xIdlClass = m_xIdlMethod.getReturnType();
190         return xIdlClass.getTypeClass();
191     }
192 
193 
invokeParameterlessMethod()194     private Object invokeParameterlessMethod(){
195     try {
196         Object[][] aParamInfo = new Object[1][];
197         aParamInfo[0] = new Object[] {};
198         return getXIdlMethod().invoke(getUnoObject(), aParamInfo);
199     } catch (Exception ex) {
200         ex.printStackTrace(System.out);
201         return null;
202     }}
203 
204 
isPrimitive()205     public boolean isPrimitive(){
206         return Introspector.isObjectPrimitive(m_xIdlMethod.getClass(), getTypeClass());
207     }
208 
209 
invoke(Object _oUnoObject, Object[] oParameters)210     protected Object invoke(Object _oUnoObject, Object[] oParameters) throws com.sun.star.uno.Exception{
211         Object[][] aParams = new Object[1][oParameters.length];
212         for ( int i = 0; i < oParameters.length; i++ ) {
213             aParams[0][i] = oParameters[i];
214         }
215         return m_xIdlMethod.invoke(_oUnoObject, aParams);
216     }
217 }
218