[Java-gnome-developer] Final patch and a question
Brought to you by:
afcowie
|
From: Julian F. <jul...@be...> - 2001-03-31 05:12:24
|
Ok, I grabbed the newest version out of CVS this afternoon and it now works=
with only my patches to the LibGlade stuff which I will include at the=
bottom of this email. Hopefully I will now be able to use a version=
straight out of the repository. Changes are as follows:
- The biggest change in there is changing all the longs to ints. This was=
necessary to make it work on Solaris. I would appreciate if someone could=
test this on linux or whatever else to see if it breaks it there or if it=
still works.
- I added a parameter to the LibGlade constructor which takes a widget name=
and beings loading the file from that point in the tree. This is a=
parameter that the C libglade library already takes, but Avi added it in a=
version of the code after you grabbed it from him.
- I added some exception handling and error checking code. I suspect this=
suffers from the same problem of rethrowing the exception before the=
exceptions are cleared or something? In trying to come up with a way to=
allow the Java user to decide whether the inability to bind a signal=
should kill the file loading (I definitely do not want it to do this) I=
came up with the idea of having a method in the Java side of LibGlade=
that was called. The user can subclass LibGlade to make the method=
display an error message however the user likes. The return value=
determines whether the C code throws an exception or not. You can nix=
this if you like, it was just the best I could come up with.
- And finally, I reenabled some code you had commented out regarding the=
target parameter of the signal connection. This functionality is needed=
to allow the Gtk objects to send messages to each other (this is allowed=
for in Glade).
Let me know if anything is unclear or if you'd like me to change something.
And finally, you wrote:
>4) Subject: parent missing from GtkWidget -
>> But what I can't quite figure out is why the java objects are
>> created as GtkWidgets instead of being just returned as GtkWidgets.
>This is a very good point. I have been meaning to make this change
>but it hasn't been my highest priority. I have just added a method
>to BaseObject called makeBaseObjectClass. This method detects the
>correct Gtk type and builds a peer for it much the same way as
>the makeBaseObjectSubclass. I will be updating the code generator
>to use this method where appropriate over the next few days.
What I don't understand is why you would ever want to use=
makeBaseObjectSubclass(). There still dozens of methods using it in the=
code and I just can't think of any reason why you would not want to user=
to be able to typecast the object back to its proper type.
The patch follows immediately below.
Thanks,
Julian
Index: LibGlade.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/java-gnome/java-gnome/src/other/LibGlade.java,v
retrieving revision 1.2
diff -u -r1.2 LibGlade.java
--- LibGlade.java 2001/02/15 09:02:54 1.2
+++ LibGlade.java 2001/03/31 04:56:04
@@ -16,6 +16,7 @@
*
* @author Avi Bryant
* @author Jean van Wyk
+ * @author Julian Fitzell
*
*/
@@ -31,11 +32,16 @@
protected Map widgets =3D new Hashtable();
protected Object owner;
-
- public LibGlade(String file, Object owner)
+
+ public LibGlade(Object owner, String file, String root)
{
this.owner =3D owner;
- glade =3D construct(file);
+ try {
+ glade =3D construct(file, root);
+ }
+ catch(Exception e) {
+ System.err.println("LibGlade error: " + e);
+ }
}
public GtkWidget getWidget(String name)
@@ -44,7 +50,7 @@
if(widget =3D=3D null)
{
- long nativepeer =3D getNativeWidget(name);
+ int nativepeer =3D getNativeWidget(name);
if(nativepeer !=3D 0)
widget =3D getWidget(nativepeer);
}
@@ -58,20 +64,20 @@
if(widget =3D=3D null)
{
- long nativepeer =3D getNativeWidgetByLongName(name);
+ int nativepeer =3D getNativeWidgetByLongName(name);
if(nativepeer !=3D 0)
widget =3D getWidget(nativepeer);
}
return widget;
}
-
- protected native long construct(String file);
+
+ protected native long construct(String file, String root);
- protected native long getNativeWidget(String name);
- protected native long getNativeWidgetByLongName(String longName);
- protected native String getWidgetName(long nativepeer);
- protected native String getWidgetLongName(long nativepeer);
+ protected native int getNativeWidget(String name);
+ protected native int getNativeWidgetByLongName(String longName);
+ protected native String getWidgetName(int nativepeer);
+ protected native String getWidgetLongName(int nativepeer);
protected void connect(String handler, String signal, String data,
GtkWidget source, Object target)
@@ -85,10 +91,21 @@
source.signalConnect(signal, handler, target);
}
- protected GtkWidget getWidget(long nativepeer)
+ protected boolean connectError(Throwable exc)
{
+ System.err.println("Error connecting Glade signal: " +=
exc.getMessage());
+
+ //by default we don't want to abort the LibGlade construction
+ return false;
+ }
+
+ protected GtkWidget getWidget(int nativepeer)
+ {
String widgetLongName =3D getWidgetLongName(nativepeer);
+ if (widgetLongName =3D=3D null)
+ return null;
+
GtkWidget widget =3D (GtkWidget) widgets.get(widgetLongName);
if(widget =3D=3D null)
{
@@ -103,5 +120,5 @@
return widget;
}
- protected native GtkWidget makeWidget(long nativepeer);
+ protected native GtkWidget makeWidget(int nativepeer);
}
Index: gnu_glade_LibGlade.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/java-gnome/java-gnome/src/other/gnu_glade_LibGlade.c,v
retrieving revision 1.2
diff -u -r1.2 gnu_glade_LibGlade.c
--- gnu_glade_LibGlade.c 2001/02/15 09:02:54 1.2
+++ gnu_glade_LibGlade.c 2001/03/31 04:56:04
@@ -33,6 +33,9 @@
jclass cls;
jmethodID getWidgetMID;
jmethodID connectMID;
+ jthrowable exc; //exception
+ jmethodID errorMID;
+ jboolean errorRet;
jstring handler, signal, data;
jobject source, target;
@@ -51,68 +54,120 @@
data =3D 0L;
cls =3D (*env)->GetObjectClass(env, obj);
+ if (cls =3D=3D 0)
+ return;
+
getWidgetMID =3D (*env)->GetMethodID(env, cls, "getWidget",
- "(J)Lgnu/gtk/GtkWidget;");
+ "(I)Lgnu/gtk/GtkWidget;");
connectMID =3D (*env)->GetMethodID(env, cls, "connect",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lgnu/gtk/Gtk=
Widget;Ljava/lang/Object;)V");
+ if (getWidgetMID =3D=3D 0 || connectMID =3D=3D 0)
+ return;
+
+ errorMID =3D (*env)->GetMethodID(env, cls, "connectError",=
"(Ljava/lang/Throwable;)Z");
+ //no need to return if 0... we just don't use it later
source =3D (*env)->CallObjectMethod(env, obj, getWidgetMID, _source);
-// We want the class that called the XML file to handle it therefore it
-// should be 0L regardless.
-// if(_target)
-// target =3D (*env)->CallObjectMethod(env, obj, getWidgetMID,=
_target);
-// else
- target =3D NULL;
+ exc =3D (*env)->ExceptionOccurred(env);
+ if (exc)
+ {
+ if (errorMID)
+ {
+ errorRet =3D (*env)->CallBooleanMethod(env, obj, errorMID,=
exc);
+ if (errorRet =3D=3D JNI_TRUE)
+ (*env)->Throw(env, exc);
+ }
+ (*env)->ExceptionClear(env);
+ }
+
+ if(_target)
+ {
+ target =3D (*env)->CallObjectMethod(env, obj, getWidgetMID,=
_target);
+ exc =3D (*env)->ExceptionOccurred(env);
+ if (exc)
+ {
+ if (errorMID)
+ {
+ errorRet =3D (*env)->CallBooleanMethod(env, obj, errorMID,=
exc);
+ if (errorRet =3D=3D JNI_TRUE)
+ (*env)->Throw(env, exc);
+ }
+ (*env)->ExceptionClear(env);
+ }
+ }
+ else
+ target =3D 0L;
(*env)->CallVoidMethod(env, obj, connectMID, handler, signal, data,=
source, target);
+
+ exc =3D (*env)->ExceptionOccurred(env);
+ if (exc)
+ {
+ if (errorMID)
+ {
+ errorRet =3D (*env)->CallBooleanMethod(env, obj, errorMID,=
exc);
+ if (errorRet =3D=3D JNI_TRUE)
+ (*env)->Throw(env, exc);
+ }
+ (*env)->ExceptionClear(env);
+ }
}
JNIEXPORT jlong JNICALL
Java_gnu_glade_LibGlade_construct
- (JNIEnv *env, jobject o, jstring file)
+ (JNIEnv *env, jobject o, jstring file, jstring root)
{
GladeXML *xml;
struct jglade_info i;
char *filename;
+ char *rootname;
i.env =3D env;
i.o =3D o;
filename =3D (*env)->GetStringUTFChars(env, file, 0);
+ if(root)
+ rootname =3D (*env)->GetStringUTFChars(env, root, 0);
+ else
+ rootname =3D 0L;
+
glade_init();
- xml =3D glade_xml_new(filename, 0L);
+ xml =3D glade_xml_new(filename, rootname);
if(xml)
{
glade_xml_signal_autoconnect_full(xml, xml_connect, &i);
}
(*env)->ReleaseStringUTFChars(env, file, filename);
+ if(rootname)
+ (*env)->ReleaseStringUTFChars(env, root, rootname);
return (jlong) xml;
}
+
JNIEXPORT jstring JNICALL Java_gnu_glade_LibGlade_getWidgetName
- (JNIEnv *env, jobject o, jlong nativepeer)
+ (JNIEnv *env, jobject o, jint nativepeer)
{
- GtkObject *gtk =3D (GtkObject *)nativepeer;
+ GtkObject *gtk =3D (GtkObject *)(nativepeer);
return (*env)->NewStringUTF(env, glade_get_widget_name(gtk));
}
JNIEXPORT jstring JNICALL Java_gnu_glade_LibGlade_getWidgetLongName
- (JNIEnv *env, jobject o, jlong nativepeer)
+ (JNIEnv *env, jobject o, jint nativepeer)
{
- GtkObject *gtk =3D (GtkObject *)nativepeer;
+ GtkObject *gtk =3D (GtkObject *)(nativepeer);
return (*env)->NewStringUTF(env, glade_get_widget_long_name(gtk));
}
-JNIEXPORT jlong JNICALL Java_gnu_glade_LibGlade_getNativeWidget
+JNIEXPORT jint JNICALL Java_gnu_glade_LibGlade_getNativeWidget
(JNIEnv *env, jobject o, jstring namestr)
{
char *name =3D (*env)->GetStringUTFChars(env, namestr, 0);
- jlong result;
+ jint result;
jclass cls;
jfieldID fid;
GladeXML* xml;
@@ -121,18 +176,18 @@
fid =3D (*env)->GetFieldID(env, cls, "glade", "J");
xml =3D (*env)->GetLongField(env, o, fid);
- result =3D (jlong) glade_xml_get_widget(xml, name);
+ result =3D (jint) glade_xml_get_widget(xml, name);
(*env)->ReleaseStringUTFChars(env, namestr, name);
return result;
}
-JNIEXPORT jlong JNICALL Java_gnu_glade_LibGlade_getNativeWidgetByLongName
+JNIEXPORT jint JNICALL Java_gnu_glade_LibGlade_getNativeWidgetByLongName
(JNIEnv *env, jobject o, jstring longnamestr)
{
char *longname =3D (*env)->GetStringUTFChars(env, longnamestr, 0);
- jlong result;
+ jint result;
jclass cls;
jfieldID fid;
GladeXML* xml;
@@ -141,7 +196,7 @@
fid =3D (*env)->GetFieldID(env, cls, "glade", "J");
xml =3D (*env)->GetLongField(env, o, fid);
- result =3D (jlong) glade_xml_get_widget_by_long_name(xml, longname);
+ result =3D (jint) glade_xml_get_widget_by_long_name(xml, longname);
(*env)->ReleaseStringUTFChars(env, longnamestr, longname);
@@ -149,9 +204,9 @@
}
JNIEXPORT jobject JNICALL Java_gnu_glade_LibGlade_makeWidget
- (JNIEnv *env, jobject o, jlong nativepeer)
+ (JNIEnv *env, jobject o, jint nativepeer)
{
- GtkObject *gtk =3D (GtkObject *)nativepeer;
+ GtkObject *gtk =3D (GtkObject *)(nativepeer);
char classname[100];
jclass cls;
|