Update ooo320-m1
[ooovba.git] / odk / examples / java / Inspector / UnoMethodNode.java
blob5c671cd2339c0b873befc24f3d54689ca68ffe7b
1 /*************************************************************************
3 * $RCSfile: UnoMethodNode.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2007-04-04 09:22:53 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.reflection.ParamInfo;
42 import com.sun.star.reflection.ParamMode;
43 import com.sun.star.reflection.XIdlClass;
44 import com.sun.star.reflection.XIdlMethod;
45 import com.sun.star.uno.TypeClass;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.util.Vector;
50 public class UnoMethodNode extends UnoNode{
51 XIdlMethod m_xIdlMethod = null;
52 Object[] m_oParamObjects = null;
53 Object m_oUnoReturnObject = null;
54 boolean m_bisInvoked = false;
55 XUnoMethodNode m_xUnoMethodNode = null;
58 /** Creates a new instance of UnoMethodNode */
59 public UnoMethodNode(XIdlMethod _xIdlMethod, Object _oUnoObject, XUnoMethodNode _xUnoMethodNode) {
60 super(_oUnoObject);
61 m_xIdlMethod = _xIdlMethod;
62 m_oParamObjects = new Object[m_xIdlMethod.getParameterInfos().length];
63 m_xUnoMethodNode = _xUnoMethodNode;
66 protected boolean isFoldable(){
67 return ((!this.isPrimitive()) && (getTypeClass().getValue() != TypeClass.VOID_value));
70 protected boolean isInvokable(){
71 boolean bisFoldable = true;
72 XIdlClass[] xIdlClasses = m_xIdlMethod.getParameterTypes();
73 for (int i = 0; i < xIdlClasses.length; i++){
74 bisFoldable = Introspector.isPrimitive(xIdlClasses[i].getTypeClass());
75 if (!bisFoldable){
76 return false;
79 return bisFoldable;
82 public XIdlMethod getXIdlMethod(){
83 return m_xIdlMethod;
87 public String getAnchor(){
88 return getXIdlMethod().getName();
92 public String getName(){
93 return getXIdlMethod().getName();
97 public Object invoke(){
98 Object oUnoReturnObject = null;
99 if (!hasParameters()){
100 oUnoReturnObject = invokeParameterlessMethod();
101 m_bisInvoked = true;
103 else{
104 Vector oUnoMethodObjects = m_xUnoMethodNode.getMethodObjects();
105 if (oUnoMethodObjects != null){
106 for (int i = 0; i < getXIdlMethod().getParameterInfos().length; i++){
107 this.m_oParamObjects[i] = oUnoMethodObjects.get(i);
109 if (oUnoMethodObjects.size() == m_oParamObjects.length + 1){
110 oUnoReturnObject = oUnoMethodObjects.get(oUnoMethodObjects.size()-1);
112 m_bisInvoked = (oUnoReturnObject != null);
115 m_oUnoReturnObject = oUnoReturnObject;
116 return oUnoReturnObject;
120 public boolean isInvoked(){
121 return m_bisInvoked;
125 protected String getNodeDescription(){
126 String sNodeDescription = "";
127 String sParameters = getParameterDescription();
128 if (m_xIdlMethod.getParameterInfos().length > 0){
129 sNodeDescription = getStandardMethodDescription();
131 else{
132 TypeClass typeClass = getTypeClass();
133 if (typeClass != TypeClass.VOID){
134 sNodeDescription = getStandardMethodDescription();
136 else{
137 sNodeDescription = getStandardMethodDescription();
140 return sNodeDescription;
144 public String getStandardMethodDescription(){
145 String sNodeDescription = m_xIdlMethod.getReturnType().getName() + " " + m_xIdlMethod.getName() + " (" + getParameterDescription() + " )";
146 if (isPrimitive()){
147 sNodeDescription += "";
149 return sNodeDescription;
153 public boolean hasParameters(){
154 return (m_xIdlMethod.getParameterInfos().length > 0);
158 public Object[] getLastParameterObjects(){
159 return m_oParamObjects;
163 public Object getLastUnoReturnObject(){
164 return m_oUnoReturnObject;
168 public String getParameterDescription(){
169 ParamInfo[] paramInfo = m_xIdlMethod.getParameterInfos();
170 String sParameters = "";
171 String sStandardMethodDisplayText = m_xIdlMethod.getReturnType().getName() + " " + m_xIdlMethod.getName() + " (" + sParameters + " )";
172 if (Introspector.isValid(paramInfo)) {
173 // get all parameters with type and mode
174 for ( int i = 0; i < paramInfo.length; i++ ) {
175 XIdlClass xIdlClass = paramInfo[ i ].aType;
176 if ( i == 0 ) {
177 // the first parameter has no leading comma
178 sParameters += "[" + getParamMode(paramInfo[ i ].aMode ) + "] " + xIdlClass.getName();
180 else {
181 // all other parameters are separated with comma
182 sParameters += ", [" + getParamMode(paramInfo[ i ].aMode ) + "] " + xIdlClass.getName();
186 return sParameters;
190 // return the parameter mode (IN, OUT, INOUT)
191 private static String getParamMode(ParamMode paramMode) {
192 String toReturn = "";
193 if ( paramMode == ParamMode.IN ) {
194 toReturn = "IN";
196 if ( paramMode == ParamMode.OUT ) {
197 toReturn = "OUT";
199 if ( paramMode == ParamMode.INOUT ) {
200 toReturn = "INOUT";
202 return( toReturn );
205 public TypeClass getTypeClass(){
206 XIdlClass xIdlClass = m_xIdlMethod.getReturnType();
207 return xIdlClass.getTypeClass();
211 private Object invokeParameterlessMethod(){
212 try {
213 Object[][] aParamInfo = new Object[1][];
214 aParamInfo[0] = new Object[] {};
215 return getXIdlMethod().invoke(getUnoObject(), aParamInfo);
216 } catch (Exception ex) {
217 ex.printStackTrace(System.out);
218 return null;
222 public boolean isPrimitive(){
223 return Introspector.isObjectPrimitive(m_xIdlMethod.getClass(), getTypeClass());
227 protected Object invoke(Object _oUnoObject, Object[] oParameters) throws com.sun.star.uno.Exception{
228 Object[][] aParams = new Object[1][oParameters.length];
229 for ( int i = 0; i < oParameters.length; i++ ) {
230 aParams[0][i] = oParameters[i];
232 return m_xIdlMethod.invoke(_oUnoObject, aParams);