Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / OfficeDev / DesktopEnvironment / OnewayExecutor.java
blobaf002fa6b7fc4f6bf826310fc658295088a340dd
1 /*************************************************************************
3 * $RCSfile: OnewayExecutor.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:39:52 $
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 java.util.Vector;
43 // __________ Implementation __________
45 /**
46 * It's not allowed to call synchronoues back inside an oneway interface call.
47 * (see IOnewayLink too). So we start a thread (implemented by this class), which
48 * gets all neccessary parameters from the original called object and
49 * call it back later inside his run() method. So the execution of such oneway call
50 * will be asynchronous. It works in a generic way and can be used or any type
51 * of oneway request. Because the source and the target of this call-link knows,
52 * which method was used and which parameters must be handled.
54 * @author Andreas Schlüns
55 * @created 17.07.2002 08:18
57 class OnewayExecutor extends Thread
59 // _______________________________
61 /**
62 * const
63 * We define some request for some well known oneway interface
64 * calls here too. So they mustn't be declared more then ones.
65 * Of course it's not necessary to use it ... but why not :-)
68 public static final int REQUEST_FRAMEACTION = 1 ;
69 public static final int REQUEST_STATUSCHANGED = 2 ;
70 public static final int REQUEST_ADDSTATUSLISTENER = 3 ;
71 public static final int REQUEST_REMOVESTATUSLISTENER = 4 ;
72 public static final int REQUEST_DISPATCH = 5 ;
74 public static final boolean ENCODE_PARAMS = true ;
75 public static final boolean DECODE_PARAMS = false ;
77 // _______________________________
79 /**
80 * @member m_rLink the object, which wish to be called back by this thread
81 * @member m_nRequest describes the type of the original request (means the
82 * called oneyway method)
83 * @member m_lParams list of parameters of the original request
85 private IOnewayLink m_rLink ;
86 private int m_nRequest ;
87 private Vector m_lParams ;
89 // _______________________________
91 /**
92 * ctor
93 * It's initialize this thread with all neccessary parameters.
94 * It gets the object, which wish to be called back and the type
95 * and parameters of the original request.
97 * @param nRequest
98 * The two user of this callback can define an unique number,
99 * which identify the type of original interface method.
100 * So the called interface object can decide, which action will be
101 * neccessary.
103 * @param lParams
104 * If the original method used parameters, they will be coded here in
105 * a generic way. Only the called interface object know (it depends
106 * from the original request - see nRequest too), how this list must
107 * be interpreted.
108 * Note: Atomic types (e.g. int, long) will be transported as objects
109 * too (Integer, Long)!
111 public OnewayExecutor( IOnewayLink rLink ,
112 int nRequest ,
113 Vector lParams )
115 m_rLink = rLink ;
116 m_nRequest = nRequest;
117 m_lParams = lParams ;
119 if (m_rLink==null)
120 System.out.println("ctor ... m_rLink == null");
121 if (m_lParams==null)
122 System.out.println("ctor ... m_lParams == null");
125 // _______________________________
128 * implements the thread function
129 * Here we call the internal set link object back and
130 * give him all neccessary parameters.
131 * After that we die by ouerself ...
133 public void run()
135 if (m_rLink==null)
136 System.out.println("run ... m_rLink == null");
137 if (m_lParams==null)
138 System.out.println("run ... m_lParams == null");
140 if (m_rLink!=null)
141 m_rLink.execOneway( m_nRequest, m_lParams );
144 // _______________________________
147 * static helper!
148 * To make convertion of the generic parameter list to the original
149 * one easier - you can use this helper methods. They know how suchlist
150 * must be coded. It's not a must to use it - but you can ...
152 public static void codeFrameAction(
153 boolean bEncode, Vector[] lParams,
154 com.sun.star.frame.FrameActionEvent[] aAction)
156 if (bEncode)
158 lParams[0] = new Vector(1);
159 lParams[0].add( (Object)(aAction[0]) );
161 else
163 aAction[0] = (com.sun.star.frame.FrameActionEvent)
164 (lParams[0].elementAt(0));
168 // _______________________________
170 public static void codeStatusChanged(
171 boolean bEncode, Vector[] lParams,
172 com.sun.star.frame.FeatureStateEvent[] aStatus)
174 if (bEncode)
176 lParams[0] = new Vector(1);
177 lParams[0].add( (Object)aStatus[0] );
179 else
181 aStatus[0] = (com.sun.star.frame.FeatureStateEvent)
182 (lParams[0].elementAt(0));
186 // _______________________________
188 public static void codeAddOrRemoveStatusListener(
189 boolean bEncode, Vector[] lParams,
190 com.sun.star.frame.XStatusListener[] xListener,
191 com.sun.star.util.URL[] aURL)
193 if (bEncode)
195 lParams[0] = new Vector(2);
196 lParams[0].add( (Object)xListener[0] );
197 lParams[0].add( (Object)aURL[0] );
199 else
201 xListener[0] = (com.sun.star.frame.XStatusListener)
202 (lParams[0].elementAt(0));
203 aURL[0] = (com.sun.star.util.URL)(lParams[0].elementAt(1));
207 // _______________________________
209 public static void codeDispatch(
210 boolean bEncode, Vector[] lParams,
211 com.sun.star.util.URL[] aURL,
212 com.sun.star.beans.PropertyValue[][] lArgs)
214 if (bEncode)
216 int nLength = lArgs.length+1;
217 int nPos = 0;
218 lParams[0] = new Vector(nLength);
220 lParams[0].add( (Object)aURL[0] );
221 --nLength;
223 while (nLength>0)
225 lParams[0].add( (Object)lArgs[0][nPos] );
226 --nLength;
227 ++nPos ;
230 else
232 int nLength = lParams[0].size()-1;
233 int nPos = 0;
235 lArgs[0] = new com.sun.star.beans.PropertyValue[nLength];
236 aURL[0] = (com.sun.star.util.URL)(lParams[0].elementAt(0));
238 while (nPos<nLength)
240 lArgs[0][nPos] = (com.sun.star.beans.PropertyValue)
241 (lParams[0].elementAt(nPos+1));
242 ++nPos;