summaryrefslogtreecommitdiff
path: root/modules/jvm/src/c/createMainScilabObject.c
blob: 9466f96bb9621a7a7e21e8f7a8947e9d87152046 (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
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) INRIA - Allan CORNET
 * Copyright (C) DIGITEO - 2010 - Allan CORNET
 *
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at
 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 *
 */

/*--------------------------------------------------------------------------*/
#include "createMainScilabObject.h"
#include "getScilabObject.h"
#include "getScilabJNIEnv.h"
#include "getScilabJavaVM.h"
#include "catchIfJavaException.h"
#include "scilabmode.h"
#include "localization.h"
/*--------------------------------------------------------------------------*/
static jobject ScilabObject = NULL;
/*--------------------------------------------------------------------------*/
BOOL createMainScilabObject(void)
{
    BOOL bOK = FALSE;
    JNIEnv * currentENV = getScilabJNIEnv();

    if (currentENV)
    {
        jclass cls = NULL;
        cls = (*currentENV)->FindClass(currentENV, "org/scilab/modules/core/Scilab");
        bOK = catchIfJavaException(_("Could not access to the Main Scilab Class:\n"));
        if (cls)
        {
            jmethodID mid = NULL;
            mid = (*currentENV)->GetMethodID(currentENV, cls, "<init>", "(I)V");
            bOK = catchIfJavaException(_("Could not access to the constructor of the Main Scilab Class:\n"));
            if (mid)
            {
                jint ScilabMode = getScilabMode();
                jobject localScilabObject = (*currentENV)->NewObject(currentENV, cls, mid, ScilabMode);
                bOK = catchIfJavaException(_("Could not create a Scilab main class. Error:\n"));
                if (bOK == TRUE)
                {
                    ScilabObject = (*currentENV)->NewGlobalRef(currentENV, localScilabObject);
                    /* Catch the exception and display an human-reading error message */
                    bOK = catchIfJavaException(_("Could not create a Scilab main class. Error:\n"));
                }
            }
        }
    }

    return bOK;
}
/*--------------------------------------------------------------------------*/
jobject getScilabObject(void)
{
    return ScilabObject;
}
/*--------------------------------------------------------------------------*/
BOOL finishMainScilabObject(void)
{
    BOOL retValue = FALSE;
    jint result = 1;
    JNIEnv * currentENV = getScilabJNIEnv();
    JavaVM * currentJVM = getScilabJavaVM();

    JavaVMAttachArgs args;
    args.version = (*currentENV)->GetVersion(currentENV);
    args.name = (char *) "Scilab - Finish";
    args.group = NULL;

    result = (*currentJVM)->AttachCurrentThread(currentJVM, (void **) &currentENV, (void*) &args) ;
    if (result == 0)
    {
        jclass cls = NULL;
        cls = (*currentENV)->FindClass(currentENV, "org/scilab/modules/core/Scilab");
        catchIfJavaException(_("Could not access to the Main Scilab Class:\n"));
        if (cls)
        {
            jmethodID mid = NULL;
            mid = (*currentENV)->GetStaticMethodID(currentENV, cls, "executeFinalHooks", "()V");
            if (mid)
            {
                (*currentENV)->CallStaticVoidMethod(currentENV, cls, mid);
            }
            catchIfJavaException(_("Cannot execute final hooks. Error:\n"));

            (*currentENV)->DeleteGlobalRef(currentENV, ScilabObject);
            ScilabObject = NULL;
            retValue = TRUE;
        }

        (*currentJVM)->DetachCurrentThread(currentJVM);
    }
    return retValue;
}
/*--------------------------------------------------------------------------*/
BOOL canCloseMainScilabObject(void)
{
    BOOL retValue = FALSE;
    JNIEnv * currentENV = getScilabJNIEnv();
    JavaVM * currentJVM = getScilabJavaVM();
    jint result = 1;

    JavaVMAttachArgs args;
    args.version = (*currentENV)->GetVersion(currentENV);
    args.name = (char *) "Scilab - Try finish";
    args.group = NULL;

    result = (*currentJVM)->AttachCurrentThread(currentJVM, (void **) &currentENV, (void*) &args) ;
    if (result == 0)
    {
        jclass cls = NULL;
        cls = (*currentENV)->FindClass(currentENV, "org/scilab/modules/core/Scilab");
        catchIfJavaException(_("Could not access to the Main Scilab Class:\n"));
        if (cls)
        {
            jmethodID mid = NULL;
            mid = (*currentENV)->GetStaticMethodID(currentENV, cls, "canClose", "()Z");
            if (mid)
            {
                retValue = (*currentENV)->CallStaticBooleanMethod(currentENV, cls, mid);
            }
            catchIfJavaException(_("Error with Scilab.canClose():\n"));
        }

        (*currentJVM)->DetachCurrentThread(currentJVM);
    }

    return retValue;
}
/*--------------------------------------------------------------------------*/
void forceCloseMainScilabObject(void)
{
    JNIEnv * currentENV = getScilabJNIEnv();
    JavaVM * currentJVM = getScilabJavaVM();
    jint result = 1;

    JavaVMAttachArgs args;
    args.version = (*currentENV)->GetVersion(currentENV);
    args.name = (char *) "Scilab - Force finish";
    args.group = NULL;

    result = (*currentJVM)->AttachCurrentThread(currentJVM, (void **) &currentENV, (void*) &args) ;
    if (result == 0)
    {
        jclass cls = NULL;
        cls = (*currentENV)->FindClass(currentENV, "org/scilab/modules/core/Scilab");
        catchIfJavaException(_("Could not access to the Main Scilab Class:\n"));
        if (cls)
        {
            jmethodID mid = NULL;
            mid = (*currentENV)->GetStaticMethodID(currentENV, cls, "forceClose", "()V");
            if (mid)
            {
                (*currentENV)->CallStaticVoidMethod(currentENV, cls, mid);
            }
            catchIfJavaException(_("Error with Scilab.forceClose():\n"));
        }
    }
}
/*--------------------------------------------------------------------------*/