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