summaryrefslogtreecommitdiff
path: root/sj2/source/jscpp/sjapplet_impl.cxx
blob: 62eb485a7222bf4eba834eb83b3e2172b8c6f35b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: sjapplet_impl.cxx,v $
 *
 *  $Revision: 1.31 $
 *
 *  last change: $Author: vg $ $Date: 2007-12-07 11:46:48 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#include "sjapplet_impl.hxx"

#define HWND int
#define HMENU int
#define HDC int

#include "rtl/ustring.hxx"
#include <rtl/process.h>
#include <osl/mutex.hxx>

#include <tools/urlobj.hxx>
#include <tools/debug.hxx>

#include <svtools/ownlist.hxx>

#ifdef QUARTZ
#include "premac.h"
#include <Cocoa/Cocoa.h>
#include "postmac.h"
#endif

#include <vcl/svapp.hxx>
#include <vcl/window.hxx>
#include <vcl/wrkwin.hxx>
#include <vcl/syschild.hxx>
#include <vcl/sysdata.hxx>
#include <com/sun/star/java/XJavaVM.hpp>
#include "com/sun/star/lang/XMultiComponentFactory.hpp"
#include "com/sun/star/uno/XComponentContext.hpp"
#include "jvmaccess/classpath.hxx"

#if defined(UNX) && !defined(QUARTZ)
#define Time xlib_time
#define Window xlib_window
#define Font xlib_font
#define Cursor xlib_cursor
#define KeyCode xlib_keycode

#include "X11/Xlib.h"
#undef Time 
#undef Window 
#undef Font 
#undef Cursor 
#undef KeyCode 
#endif

using namespace ::rtl;
using namespace ::osl;
#ifdef SOLAR_JAVA
using namespace ::com::sun::star::java;
#endif // SOLAR_JAVA
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::uno;

namespace css = ::com::sun::star;
#ifdef SOLAR_JAVA

static void testJavaException(JNIEnv * pEnv)  throw(com::sun::star::uno::RuntimeException)
{
    jthrowable jtThrowable = pEnv->ExceptionOccurred();
    if(jtThrowable) { // is it a java exception ?
#if OSL_DEBUG_LEVEL > 1
        pEnv->ExceptionDescribe();
#endif
        pEnv->ExceptionClear();

          jclass jcThrowable = pEnv->FindClass("java/lang/Throwable");
          jmethodID jmThrowable_getMessage = pEnv->GetMethodID(jcThrowable, "getMessage", "()Ljava/lang/String;");

          jstring jsMessage = (jstring)pEnv->CallObjectMethod(jtThrowable, jmThrowable_getMessage);

          rtl::OUString ouMessage;
        if(jsMessage) {
            const jchar * jcMessage = pEnv->GetStringChars(jsMessage, NULL);
            ouMessage = rtl::OUString(jcMessage);
            pEnv->ReleaseStringChars(jsMessage, jcMessage);
        }

          throw css::uno::RuntimeException(ouMessage, css::uno::Reference<XInterface>());
    }
}
#else // !SOLAR_JAVA
static void throwException() throw(com::sun::star::uno::RuntimeException)
{
    throw css::uno::RuntimeException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Java applets not supported.")), css::uno::Reference<XInterface>());
}
#endif

#ifdef SOLAR_JAVA
#if defined (UNX) && !defined(QUARTZ)
struct EmbeddedWindow {
    jobject _joWindow;

    EmbeddedWindow(JNIEnv * pEnv, SystemEnvData const * pEnvData) throw(com::sun::star::uno::RuntimeException);
    void dispose(JNIEnv * pEnv);
};

EmbeddedWindow::EmbeddedWindow(JNIEnv * pEnv, SystemEnvData const * pEnvData)
    throw(com::sun::star::uno::RuntimeException) : _joWindow(0)
{
    // ensure that all operations for the window contained in pEnvData
    // have been processed by the Xserver since java will access that
    // window by a separate connection to the Xserver
    XSync( (Display*)pEnvData->pDisplay, False);
    try
    {   //java < 1.5
        jclass jcToolkit = pEnv->FindClass("java/awt/Toolkit");                         testJavaException(pEnv);
        jmethodID jmToolkit_getDefaultToolkit = pEnv->GetStaticMethodID(
            jcToolkit, "getDefaultToolkit", "()Ljava/awt/Toolkit;" );   testJavaException(pEnv);
        pEnv->CallStaticObjectMethod(jcToolkit, jmToolkit_getDefaultToolkit);           testJavaException(pEnv);
        
        jclass jcMotifAppletViewer = pEnv->FindClass(
            "sun/plugin/navig/motif/MotifAppletViewer");
        if(pEnv->ExceptionOccurred())
        {
            pEnv->ExceptionClear();
            jcMotifAppletViewer = pEnv->FindClass(
                "sun/plugin/viewer/MNetscapePluginContext");                testJavaException(pEnv);
        }
        
        jclass jcClassLoader = pEnv->FindClass("java/lang/ClassLoader");                testJavaException(pEnv);
        jmethodID jmClassLoader_loadLibrary = pEnv->GetStaticMethodID(
            jcClassLoader, "loadLibrary",
            "(Ljava/lang/Class;Ljava/lang/String;Z)V" );                testJavaException(pEnv);
        jstring jsplugin = pEnv->NewStringUTF("javaplugin_jni");                        testJavaException(pEnv);
        pEnv->CallStaticVoidMethod(jcClassLoader, jmClassLoader_loadLibrary,
                                   jcMotifAppletViewer, jsplugin, JNI_FALSE);                  testJavaException(pEnv);
        
        jmethodID jmMotifAppletViewer_getWidget = pEnv->GetStaticMethodID(
            jcMotifAppletViewer, "getWidget", "(IIIII)I" );             testJavaException(pEnv);
        jint ji_widget = pEnv->CallStaticIntMethod(jcMotifAppletViewer,
                        jmMotifAppletViewer_getWidget, pEnvData->aWindow,
                        0, 0, 1, 1);                                                testJavaException(pEnv);
        jclass jcFrame  = pEnv->FindClass("sun/awt/motif/MEmbeddedFrame");              testJavaException(pEnv);
        jmethodID jmFrame_rinit = pEnv->GetMethodID(jcFrame, "<init>", "(J)V" );        testJavaException(pEnv);
        jobject joFrame = pEnv->AllocObject(jcFrame);                                   testJavaException(pEnv);
        pEnv->CallVoidMethod(joFrame, jmFrame_rinit, (jlong)ji_widget);                 testJavaException(pEnv);
        _joWindow = pEnv->NewGlobalRef(joFrame);
    }
    catch (RuntimeException & )
    {
    }

    if (_joWindow)
        return;
    //try Java > 1.5
#ifdef SOLARIS
    jclass jcFrame  = pEnv->FindClass("sun/awt/motif/MEmbeddedFrame"); testJavaException(pEnv);
    jobject joFrame = pEnv->AllocObject(jcFrame); testJavaException(pEnv);
    jmethodID jmFrame_rinit = pEnv->GetMethodID(jcFrame, "<init>", "(JZ)V" ); testJavaException(pEnv);
    pEnv->CallVoidMethod(joFrame, jmFrame_rinit, (jlong) pEnvData->aWindow, JNI_FALSE); testJavaException(pEnv);
    _joWindow = pEnv->NewGlobalRef(joFrame);
#elif LINUX
    jclass jcFrame  = pEnv->FindClass("sun/awt/X11/XEmbeddedFrame"); testJavaException(pEnv);
    jobject joFrame = pEnv->AllocObject(jcFrame); testJavaException(pEnv);
    jmethodID jmFrame_rinit = pEnv->GetMethodID(jcFrame, "<init>", "(J)V" ); testJavaException(pEnv);

    pEnv->CallVoidMethod(joFrame, jmFrame_rinit, (jlong) pEnvData->aWindow); testJavaException(pEnv);
    _joWindow = pEnv->NewGlobalRef(joFrame);
#endif
}

#else // UNX && !QUARTZ

struct EmbeddedWindow {
jobject _joWindow;

EmbeddedWindow(JNIEnv * pEnv, SystemEnvData const * pEnvData) throw(com::sun::star::uno::RuntimeException);
void dispose(JNIEnv * pEnv);
};


#ifdef WNT
EmbeddedWindow::EmbeddedWindow(JNIEnv * pEnv, SystemEnvData const * pEnvData) throw(com::sun::star::uno::RuntimeException)
{
jclass jcFrame = pEnv->FindClass("sun/awt/windows/WEmbeddedFrame");     testJavaException(pEnv);
jmethodID jmFrame_rinit = pEnv->GetMethodID(jcFrame, "<init>", "(I)V"); testJavaException(pEnv);

jobject joFrame = pEnv->AllocObject(jcFrame);                           testJavaException(pEnv);
pEnv->CallVoidMethod(joFrame, jmFrame_rinit, (jint)pEnvData->hWnd);     testJavaException(pEnv);

_joWindow = pEnv->NewGlobalRef(joFrame);
}

#elif defined QUARTZ
EmbeddedWindow::EmbeddedWindow(JNIEnv * pEnv, SystemEnvData const * pEnvData) throw(com::sun::star::uno::RuntimeException)
{
  /* The WNT code (above) that this code derives from, may be using quite old
     ways of interacting with native windows. More modern approaches seems to
     point towards JAWT_* and com.apple.eawt */
#if 0
// FIXME: this is not going to work on cocoa
jclass jcFrame = pEnv->FindClass("apple/awt/CEmbeddedFrame");     testJavaException(pEnv);
jmethodID jmFrame_rinit = pEnv->GetMethodID(jcFrame, "<init>", "(I)V"); testJavaException(pEnv);

jobject joFrame = pEnv->AllocObject(jcFrame);                           testJavaException(pEnv);
pEnv->CallVoidMethod(joFrame, jmFrame_rinit, (jint)pEnvData->pView);     testJavaException(pEnv);

_joWindow = pEnv->NewGlobalRef(joFrame);
#endif
}

#else

EmbeddedWindow::EmbeddedWindow(JNIEnv * pEnv, SystemEnvData const * pEnvData) throw(com::sun::star::uno::RuntimeException)
{
jclass jcFrame = pEnv->FindClass("java/awt/Frame");                     testJavaException(pEnv);
jmethodID jmFrame_rinit = pEnv->GetMethodID(jcFrame, "<init>", "()V");  testJavaException(pEnv);
jobject joFrame = pEnv->AllocObject(jcFrame);                                   testJavaException(pEnv);
pEnv->CallVoidMethod(joFrame, jmFrame_rinit);                           testJavaException(pEnv);

_joWindow = pEnv->NewGlobalRef(joFrame);
}

#endif

#endif // UNX && !QUARTZ

void EmbeddedWindow::dispose(JNIEnv * pEnv)
{
jclass jcWindow = pEnv->FindClass("java/awt/Window");                        testJavaException(pEnv);
jmethodID jmWindow_dispose = pEnv->GetMethodID(jcWindow, "dispose", "()V" ); testJavaException(pEnv);

pEnv->CallVoidMethod(_joWindow, jmWindow_dispose);                           testJavaException(pEnv);
pEnv->DeleteGlobalRef(_joWindow);

_joWindow = 0;
}
#endif // SOLAR_JAVA


#ifdef SOLAR_JAVA
SjApplet2_Impl::SjApplet2_Impl()  throw(com::sun::star::uno::RuntimeException)
    :
    _joAppletExecutionContext(0),
    _jcAppletExecutionContext(0)

{}
#else // !SOLAR_JAVA
SjApplet2_Impl::SjApplet2_Impl()  throw(com::sun::star::uno::RuntimeException)
{}
#endif


SjApplet2_Impl::~SjApplet2_Impl() throw()
{
#ifdef SOLAR_JAVA
    if (_joAppletExecutionContext)
    { 
        try
        {
            if ( ! _virtualMachine.is())
                return;
            jvmaccess::VirtualMachine::AttachGuard vmAttachGuard(_virtualMachine);
            JNIEnv * pEnv = vmAttachGuard.getEnvironment();
            _pEmbeddedWindow->dispose(pEnv);
            delete _pEmbeddedWindow;
            
            pEnv->DeleteGlobalRef(_joAppletExecutionContext);
            pEnv->DeleteGlobalRef(_jcAppletExecutionContext);
        }
        catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
        {
        }
    }
#endif        
}

void SjApplet2_Impl::init(
    Window * pParentWin,
    css::uno::Reference< css::uno::XComponentContext > const & context,
    const INetURLObject & rDocBase, const SvCommandList & rCmdList)
    throw(com::sun::star::uno::RuntimeException)
{
#ifdef SOLAR_JAVA
    _pParentWin = pParentWin;


    // Java URL erzeugen
    rtl::OUString url = rDocBase.GetMainURL(INetURLObject::DECODE_TO_IURI);

    if(!url.getLength())
        url = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("file:///"));

    if (url.getLength()) {
        //WorkAround, weil Java mit dem | nicht zurecht kommt
        if(rDocBase.GetProtocol() == INET_PROT_FILE && url.pData->buffer[9] == INET_ENC_DELIM_TOKEN) {
            rtl::OUString tmp = url.copy(0, 9);
            tmp += String(INET_DELIM_TOKEN);
            tmp += url.copy(10);

            url = tmp;
        }
    }


#if OSL_DEBUG_LEVEL > 1
    rtl::OString tmp = rtl::OUStringToOString(url, RTL_TEXTENCODING_ASCII_US);
    OSL_TRACE("SjApplet2_Impl::init - mainUrl: %s\n", tmp.getStr());
#endif

    css::uno::Reference<XJavaVM> _xJavaVM = css::uno::Reference<XJavaVM>(
        (css::uno::Reference< css::lang::XMultiComponentFactory >(
            context->getServiceManager(), css::uno::UNO_QUERY_THROW)->
         createInstanceWithContext(
             rtl::OUString(
                 RTL_CONSTASCII_USTRINGPARAM(
                     "com.sun.star.java.JavaVirtualMachine")),
             context)),
        UNO_QUERY);
    Sequence<sal_Int8> processID(17);
    rtl_getGlobalProcessId((sal_uInt8 *)processID.getArray());
    processID[16] = 0;
    
    OSL_ENSURE(sizeof (sal_Int64)
                    >= sizeof (jvmaccess::VirtualMachine *),
                "Pointer cannot be represented as sal_Int64");
    sal_Int64 nPointer = reinterpret_cast< sal_Int64 >(
        static_cast< jvmaccess::VirtualMachine * >(0));
    _xJavaVM->getJavaVM(processID) >>= nPointer;
    _virtualMachine = reinterpret_cast< jvmaccess::VirtualMachine * >(nPointer);
    if (!_virtualMachine.is())
        return;
 
    try
    {
        jvmaccess::VirtualMachine::AttachGuard vmAttachGuard(_virtualMachine);

        JNIEnv * pEnv = vmAttachGuard.getEnvironment();

        _jcAppletExecutionContext = jvmaccess::ClassPath::loadClass(
            context, pEnv, 
            rtl::OUString(
                RTL_CONSTASCII_USTRINGPARAM(
                    "vnd.sun.star.expand:$ORIGIN/classes/classes.jar")),
            rtl::OUString(
                RTL_CONSTASCII_USTRINGPARAM(
                    "stardiv.applet.AppletExecutionContext")));
        testJavaException(pEnv);
        _jcAppletExecutionContext = (jclass) pEnv->NewGlobalRef(_jcAppletExecutionContext );                               testJavaException(pEnv);

        jclass jcURL = pEnv->FindClass("java/net/URL");                                                                   testJavaException(pEnv);
        jmethodID jmURL_rinit = pEnv->GetMethodID(jcURL, "<init>", "(Ljava/lang/String;)V");                              testJavaException(pEnv);
        jobject joDocBase = pEnv->AllocObject(jcURL);                                                                     testJavaException(pEnv);
        jstring jsURL = pEnv->NewString(url.getStr(), url.getLength());                                                   testJavaException(pEnv);
        pEnv->CallVoidMethod(joDocBase, jmURL_rinit, jsURL);                                                              testJavaException(pEnv);

        jclass jcHashtable = pEnv->FindClass("java/util/Hashtable");                                                      testJavaException(pEnv);
        jmethodID jmHashtable_rinit = pEnv->GetMethodID(jcHashtable, "<init>", "()V");                                    testJavaException(pEnv);
        jmethodID jmHashtable_put = pEnv->GetMethodID(jcHashtable, "put",
                                                      "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");        testJavaException(pEnv);
        jobject joParameters = pEnv->AllocObject(jcHashtable);                                                            testJavaException(pEnv);
        pEnv->CallVoidMethod(joParameters, jmHashtable_rinit);                                                            testJavaException(pEnv);

        for(sal_uInt32 i = 0; i < rCmdList.Count(); ++i) {
            const SvCommand & rCmd = rCmdList[i];
            String aCmd = rCmd.GetCommand();
            String aLoweredCmd = aCmd.ToLowerAscii();

#if OSL_DEBUG_LEVEL > 1
            rtl::OUString command = aCmd;
            rtl::OUString value = rCmd.GetArgument();
            rtl::OString cmd_tmp = rtl::OUStringToOString(command, RTL_TEXTENCODING_ASCII_US);
            rtl::OString value_tmp = rtl::OUStringToOString(value, RTL_TEXTENCODING_ASCII_US);
            OSL_TRACE("command: %s=%s", cmd_tmp.getStr(), value_tmp.getStr());
#endif
            jstring jsCommand = pEnv->NewString(aLoweredCmd.GetBuffer(), aLoweredCmd.Len());                              testJavaException(pEnv);
            jstring jsArg = pEnv->NewString(rCmd.GetArgument().GetBuffer(), rCmd.GetArgument().Len());                    testJavaException(pEnv);
            pEnv->CallObjectMethod(joParameters, jmHashtable_put, jsCommand, jsArg);                                      testJavaException(pEnv);
        }

        SystemEnvData const * pEnvData  = ((SystemChildWindow *)pParentWin)->GetSystemData();
        _pEmbeddedWindow = new EmbeddedWindow(pEnv, pEnvData);

        jmethodID jmAppletExecutionContext_rinit = pEnv->GetMethodID(_jcAppletExecutionContext, "<init>",
                                                                     "(Ljava/net/URL;Ljava/util/Hashtable;Ljava/awt/Container;J)V");  testJavaException(pEnv);
        jmethodID jmAppletExecutionContext_init = pEnv->GetMethodID(_jcAppletExecutionContext, "init", "()V");            testJavaException(pEnv);
        jmethodID jmAppletExecutionContext_startUp = pEnv->GetMethodID(_jcAppletExecutionContext, "startUp", "()V");      testJavaException(pEnv);

        _joAppletExecutionContext = pEnv->AllocObject(_jcAppletExecutionContext);                                         testJavaException(pEnv);
        _joAppletExecutionContext = pEnv->NewGlobalRef(_joAppletExecutionContext);                                        testJavaException(pEnv);
        pEnv->CallVoidMethod(_joAppletExecutionContext, jmAppletExecutionContext_rinit,
                             joDocBase, joParameters, _pEmbeddedWindow->_joWindow, (jlong)0);                             testJavaException(pEnv);
        pEnv->CallVoidMethod(_joAppletExecutionContext, jmAppletExecutionContext_init);                                   testJavaException(pEnv);
        pEnv->CallVoidMethod(_joAppletExecutionContext, jmAppletExecutionContext_startUp);                                testJavaException(pEnv);

    }
    catch(jvmaccess::VirtualMachine::AttachGuard::CreationException & )
    {
        throw RuntimeException(
            rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
                              "sjapplet_impl.cxx: Could not create jvmaccess::"
                              "VirtualMachine::AttachGuard!")), 0);
    }

#else // !SOLAR_JAVA
    throwException();
#endif
}

void SjApplet2_Impl::setSize(const Size & rSize) throw(com::sun::star::uno::RuntimeException)
{

#ifdef SOLAR_JAVA
    try
    {
        if ( ! _virtualMachine.is())
            return;
        jvmaccess::VirtualMachine::AttachGuard vmAttachGuard(_virtualMachine);
        JNIEnv * pEnv = vmAttachGuard.getEnvironment();
        
        _pParentWin->SetSizePixel(rSize);

        jmethodID jmAppletExecutionContext_resize = pEnv->GetMethodID(
            _jcAppletExecutionContext, "appletResize", "(II)V");
        testJavaException(pEnv);
        pEnv->CallVoidMethod(_joAppletExecutionContext,
                             jmAppletExecutionContext_resize, (jint)rSize.Width(),
                             (jint)rSize.Height());
        testJavaException(pEnv);
    }
    catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
    {
        throw RuntimeException(
            rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
                              "SjApplet2_Impl::setSize, Could not create jvmaccess::"
                              "VirtualMachine::AttachGuard!")), 0);
    }
#else // !SOLAR_JAVA
    throwException();
#endif
}

void SjApplet2_Impl::restart() throw(com::sun::star::uno::RuntimeException)
{
#ifdef SOLAR_JAVA
    try
    {
        if ( ! _virtualMachine.is())
            return;
        jvmaccess::VirtualMachine::AttachGuard vmAttachGuard(_virtualMachine);
        JNIEnv * pEnv = vmAttachGuard.getEnvironment();
        jmethodID jmAppletExecutionContext_restart = pEnv->GetMethodID(
            _jcAppletExecutionContext, "restart", "()V");
        testJavaException(pEnv);
        pEnv->CallVoidMethod(_joAppletExecutionContext, jmAppletExecutionContext_restart);
        testJavaException(pEnv);
    }
    catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
    {
        throw RuntimeException(
            rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
                              "SjApplet2_Impl::restart, Could not create jvmaccess::"
                              "VirtualMachine::AttachGuard!")), 0);
    }
#else // !SOLAR_JAVA
    throwException();
#endif
}

void SjApplet2_Impl::reload() throw(com::sun::star::uno::RuntimeException)
{
#ifdef SOLAR_JAVA
    try
    {
        if ( ! _virtualMachine.is())
            return;
        jvmaccess::VirtualMachine::AttachGuard vmAttachGuard(_virtualMachine);
        JNIEnv * pEnv = vmAttachGuard.getEnvironment();
        jmethodID jmAppletExecutionContext_reload = pEnv->GetMethodID(
            _jcAppletExecutionContext, "reload", "()V");
        testJavaException(pEnv);
        pEnv->CallVoidMethod(_joAppletExecutionContext, jmAppletExecutionContext_reload);
        testJavaException(pEnv);
    }
    catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
    {
        throw RuntimeException(
            rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
                              "SjApplet2_Impl::reload, Could not create jvmaccess::"
                              "VirtualMachine::AttachGuard!")), 0);
    }
#else // !SOLAR_JAVA
    throwException();
#endif
}

void SjApplet2_Impl::start() throw(com::sun::star::uno::RuntimeException)
{
#ifdef SOLAR_JAVA
    try
    {
        if ( ! _virtualMachine.is())
            return;
        jvmaccess::VirtualMachine::AttachGuard vmAttachGuard(_virtualMachine);
        JNIEnv * pEnv = vmAttachGuard.getEnvironment();
        jmethodID jmAppletExecutionContext_sendStart = pEnv->GetMethodID(
            _jcAppletExecutionContext, "sendStart", "()V");
        testJavaException(pEnv);
        pEnv->CallVoidMethod(_joAppletExecutionContext, jmAppletExecutionContext_sendStart);
        testJavaException(pEnv);
    }
    catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
    {
        throw RuntimeException(
            rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
                              "SjApplet2_Impl::restart, Could not create jvmaccess::"
                              "VirtualMachine::AttachGuard!")), 0);
    }
#else // !SOLAR_JAVA
    throwException();
#endif
}

void SjApplet2_Impl::stop() throw(com::sun::star::uno::RuntimeException)
{
#ifdef SOLAR_JAVA
    try
    {
        if ( ! _virtualMachine.is())
            return;
        jvmaccess::VirtualMachine::AttachGuard vmAttachGuard(_virtualMachine);
        JNIEnv * pEnv = vmAttachGuard.getEnvironment();
        jmethodID jmAppletExecutionContext_sendStop = pEnv->GetMethodID(
            _jcAppletExecutionContext, "sendStop", "()V");
        testJavaException(pEnv);
        pEnv->CallVoidMethod(_joAppletExecutionContext, jmAppletExecutionContext_sendStop);
        testJavaException(pEnv);
    }
    catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
    {
        throw RuntimeException(
            rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
                              "SjApplet2_Impl::restart, Could not create jvmaccess::"
                              "VirtualMachine::AttachGuard!")), 0);
    }        
#else // !SOLAR_JAVA
    throwException();
#endif
}

void SjApplet2_Impl::close() throw(com::sun::star::uno::RuntimeException)
{
#ifdef SOLAR_JAVA
    try
    {
        if ( ! _virtualMachine.is())
            return;
        jvmaccess::VirtualMachine::AttachGuard vmAttachGuard(_virtualMachine);
        JNIEnv * pEnv = vmAttachGuard.getEnvironment();
    
        jmethodID jmAppletExecutionContext_shutdown  = pEnv->GetMethodID(
            _jcAppletExecutionContext, "shutdown", "()V");
        testJavaException(pEnv);
        pEnv->CallVoidMethod(_joAppletExecutionContext, jmAppletExecutionContext_shutdown);
        testJavaException(pEnv);

        jmethodID jmWaitForDispose= pEnv->GetMethodID(
            _jcAppletExecutionContext,"waitForDispose","()V");
        testJavaException(pEnv);
        //blocks until the applet has destroyed itself and the container was disposed
        //(stardiv.applet.AppletExecutionContext.dispose)
        pEnv->CallVoidMethod(_joAppletExecutionContext, jmWaitForDispose);

        // now that the applet is disposed, we dispose the AppletExecutionContext, that will end the thread
        // which dispatches the applet methods, such as init, start, stop , destroy.
        jmethodID jmAppletExecutionContext_dispose= pEnv->GetMethodID(
            _jcAppletExecutionContext,"dispose", "()V");
        testJavaException(pEnv);
        pEnv->CallVoidMethod(_joAppletExecutionContext, jmAppletExecutionContext_dispose);
        testJavaException(pEnv);
        
        if( _pParentWin )
        {
            WorkWindow*	pAppWin	= Application::GetAppWindow();
            if(pAppWin)
            {
                while(_pParentWin->GetChildCount())
                {
                    Window* pChild = _pParentWin->GetChild(0);
                    pChild->Show( FALSE );
                    pChild->SetParent( pAppWin );
                }
            }
        }
    }
    catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
    {
        throw RuntimeException(
            rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
                              "SjApplet2_Impl::close, Could not create jvmaccess::"
                              "VirtualMachine::AttachGuard!")), 0);
    }
#else // !SOLAR_JAVA
    throwException();
#endif
}