JNIQSwingWrapper.h

00001 /*
00002  * @(#)JNIQSwingWrapper.h  0.2.0 / 2007-05-02
00003  *
00004  * Copyright (c) 2007, ETF and contributors. All rights reserved.
00005  *
00006  * This software is licensed under the LGPL, version 2.1, license.
00007  * A copy of the license is included in the file LICENSE-LGPL.txt.
00008  */
00009 #ifndef JNIQSWINGWRAPPER_H
00010 #define JNIQSWINGWRAPPER_H
00011 
00012 #include "JavaQxCore.h"
00013 
00014 /*^**************************************************************************^*/
00015 /*- JNIQSwingWrapper Class.                                                  -*/
00016 /*^**************************************************************************^*/
00017 
00024 class JAVAQX_EXPORT JNIQSwingWrapper
00025 {
00026 public:
00027   inline static JNIEnv *env();
00028   inline static bool isQSwingThread(JNIEnv *env);
00029   inline static void checkGUIThread(JNIEnv *env);
00030 public:
00031   static void checkException();
00032   static bool testException();
00033   static void fatalError(const char *errorMessage);
00034   static void throwInternalError(const char *errorMessage = 0);
00035   static void throwOutOfMemoryError(const char *errorMessage = 0);
00036   static void throwRuntimeExcpetion(const char *exceptionMessage = 0);
00037   static jfieldID getFieldID(jclass cls, const char *name, const char *sig);
00038   static jfieldID getStaticFieldID(jclass cls, const char *name,
00039                                    const char *sig);
00040   static jmethodID getMethodID(jclass cls, const char *name, const char *sig);
00041   static jmethodID getStaticMethodID(jclass cls, const char *name,
00042                                      const char *sig);
00043   static jobject getObjectField(jobject obj, jfieldID fieldID);
00044   static void setObjectField(jobject obj, jfieldID fieldID, jobject val);
00045   static jboolean getBooleanField(jobject obj, jfieldID fieldID);
00046   static void setBooleanField(jobject obj, jfieldID fieldID, jboolean val);
00047   static jbyte getByteField(jobject obj, jfieldID fieldID);
00048   static void setByteField(jobject obj, jfieldID fieldID, jbyte val);
00049   static jchar getCharField(jobject obj, jfieldID fieldID);
00050   static void setCharField(jobject obj, jfieldID fieldID, jchar val);
00051   static jshort getShortField(jobject obj, jfieldID fieldID);
00052   static void setShortField(jobject obj, jfieldID fieldID, jshort val);
00053   static jint getIntField(jobject obj, jfieldID fieldID);
00054   static void setIntField(jobject obj, jfieldID fieldID, jint val);
00055   static jlong getLongField(jobject obj, jfieldID fieldID);
00056   static void setLongField(jobject obj, jfieldID fieldID, jlong val);
00057   static jfloat getFloatField(jobject obj, jfieldID fieldID);
00058   static void setFloatField(jobject obj, jfieldID fieldID, jfloat val);
00059   static jdouble getDoubleField(jobject obj, jfieldID fieldID);
00060   static void setDoubleField(jobject obj, jfieldID fieldID, jdouble val);
00061   static bool callObjectMethod(jobject &result, jobject obj, jmethodID methodID,
00062                                va_list &args);
00063   static bool callObjectMethod(jobject &result, jobject obj, jmethodID methodID,
00064                                ...);
00065   static bool callBooleanMethod(jboolean &result, jobject obj,
00066                                 jmethodID methodID, va_list &args);
00067   static bool callBooleanMethod(jboolean &result, jobject obj,
00068                                 jmethodID methodID, ...);
00069   static bool callByteMethod(jbyte &result, jobject obj, jmethodID methodID,
00070                              va_list &args);
00071   static bool callByteMethod(jbyte &result, jobject obj, jmethodID methodID,
00072                              ...);
00073   static bool callCharMethod(jchar &result, jobject obj, jmethodID methodID,
00074                              va_list &args);
00075   static bool callCharMethod(jchar &result, jobject obj, jmethodID methodID,
00076                              ...);
00077   static bool callShortMethod(jshort &result, jobject obj, jmethodID methodID,
00078                               va_list &args);
00079   static bool callShortMethod(jshort &result, jobject obj, jmethodID methodID,
00080                               ...);
00081   static bool callIntMethod(jint &result, jobject obj, jmethodID methodID,
00082                             va_list &args);
00083   static bool callIntMethod(jint &result, jobject obj, jmethodID methodID, ...);
00084   static bool callLongMethod(jlong &result, jobject obj, jmethodID methodID,
00085                              va_list &args);
00086   static bool callLongMethod(jlong &result, jobject obj, jmethodID methodID,
00087                              ...);
00088   static bool callFloatMethod(jfloat &result, jobject obj, jmethodID methodID,
00089                               va_list &args);
00090   static bool callFloatMethod(jfloat &result, jobject obj, jmethodID methodID,
00091                               ...);
00092   static bool callDoubleMethod(jdouble &result, jobject obj, jmethodID methodID,
00093                                va_list &args);
00094   static bool callDoubleMethod(jdouble &result, jobject obj, jmethodID methodID,
00095                                ...);
00096   static bool callVoidMethod(jobject obj, jmethodID methodID, va_list &args);
00097   static bool callVoidMethod(jobject obj, jmethodID methodID, ...);
00098   static jstring toString(jobject obj);
00099   static jstring newString(const jchar *uchars, jsize len);
00100   static jstring newStringUTF(const char *bytes);
00101   static bool callStaticVoidMethod(jclass cls, jmethodID methodID,
00102                                    va_list &args);
00103   static bool callStaticVoidMethod(jclass cls, jmethodID methodID, ...);
00104   static jobject newObject(jclass cls, jmethodID constructor, va_list &args);
00105   static jobject newObject(jclass cls, jmethodID constructor, ...);
00106   static jobjectArray newObjectArray(jclass cls, jsize len);
00107   static jbyteArray newByteArray(jsize len);
00108   static jobject newGlobalRef(jobject obj);
00109   static jweak newWeakGlobalRef(jobject obj);
00110   static void deleteGlobalRef(jobject &objref);
00111   static void deleteWeakGlobalRef(jweak &wobjref);
00112   static bool isLiveRef(jweak wobjref);
00113 private:
00114   friend class QSwingBridge; // uses initialize(JNIEnv *env) and sm_disposed
00115   static void initialize(JNIEnv *env);
00116 private:
00117   static JNIEnv *sm_env;
00118   static bool sm_disposed;
00119 };
00120 
00121 /*^**************************************************************************^*/
00122 /*- JNIQSwingWrapper :: Public Static Inline Member Functions.               -*/
00123 /*^**************************************************************************^*/
00124 
00130 inline JNIEnv *JNIQSwingWrapper::env()
00131 {
00132   return sm_env;
00133 }
00134 
00142 inline bool JNIQSwingWrapper::isQSwingThread(JNIEnv *env)
00143 {
00144   if (sm_disposed) {
00145     JNILogger::logError("QSwing disposed", "JNIQSwingWrapper", __LINE__);
00146     env->FatalError("QSwing disposed"); // does not return
00147   }
00148   return (sm_env == env);
00149 }
00150 
00154 inline void JNIQSwingWrapper::checkGUIThread(JNIEnv *env)
00155 {
00156   if (sm_disposed) {
00157     JNILogger::logError("QSwing disposed", "JNIQSwingWrapper", __LINE__);
00158     env->FatalError("QSwing disposed"); // does not return
00159   }
00160   if (sm_env != env) {
00161     JNIQSwingWrapper::fatalError("GUI object outside main thread");
00162   }
00163 }
00164 
00165 /*^**************************************************************************^*/
00166 /*- Macro Definition.                                                        -*/
00167 /*^**************************************************************************^*/
00168 
00169 #define QSWING_ENV JNIQSwingWrapper::env()
00170 
00171 #endif // JNIQSWINGWRAPPER_H
00172 
00173 /*^*****************************************************************************
00174   File History:
00175 
00176  - 2007-05-02 09:16:18 Slobodan
00177    Initial version
00178 
00179 *****************************************************************************^*/

Copyright © 2007 ETF and contributors. All Rights Reserved.