From 4239a304becba56aef9ce945a03a57416d9a4237 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Fri, 15 Nov 2002 03:05:11 +0000 Subject: * native/jni/classpath/native_state.c (add_node): Set `c_state' field even when moving node. * native/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkCheckboxPeer.c (Java_gnu_java_awt_peer_gtk_GtkCheckboxGroupPeer_dispose): New function. (Java_gnu_java_awt_peer_gtk_GtkCheckboxGroupPeer_remove): New function. Include GtkComponentPeer header. * gnu/java/awt/peer/gtk/GtkComponentPeer.java (dispose): Removed. * native/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkComponentPeer.c (Java_gnu_java_awt_peer_gtk_GtkGenericPeer_dispose): Renamed. * gnu/java/awt/peer/gtk/Makefile.am (EXTRA_DIST): Added GtkCheckboxGroupPeer.java. * gnu/java/awt/peer/gtk/GtkCheckboxPeer.java (old_group): Now a GtkCheckboxGroupPeer. (nativeCreate): Argument now a GtkCheckboxGroupPeer. (nativeSetCheckboxGroup): Likewise. Removed `old_group' argument. (create): Find the GtkCheckboxGroupPeer. (setCheckboxGroup): Likewise. (dispose): New method. * gnu/java/awt/peer/gtk/GtkCheckboxGroupPeer.java: New file. * gnu/java/awt/peer/gtk/GtkGenericPeer.java (next_native_state): New global. (getUniqueInteger): New method. (native_state): Use it. (dispose): New native method. --- gnu/java/awt/peer/gtk/GtkCheckboxGroupPeer.java | 86 +++++++++++++++++++++++++ gnu/java/awt/peer/gtk/GtkCheckboxPeer.java | 30 +++++++-- gnu/java/awt/peer/gtk/GtkComponentPeer.java | 2 - gnu/java/awt/peer/gtk/GtkGenericPeer.java | 26 ++++++-- gnu/java/awt/peer/gtk/Makefile.am | 1 + 5 files changed, 130 insertions(+), 15 deletions(-) create mode 100644 gnu/java/awt/peer/gtk/GtkCheckboxGroupPeer.java (limited to 'gnu') diff --git a/gnu/java/awt/peer/gtk/GtkCheckboxGroupPeer.java b/gnu/java/awt/peer/gtk/GtkCheckboxGroupPeer.java new file mode 100644 index 000000000..9903c3104 --- /dev/null +++ b/gnu/java/awt/peer/gtk/GtkCheckboxGroupPeer.java @@ -0,0 +1,86 @@ +/* GtkCheckboxGroupPeer.java - Wrap a CheckboxGroup + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.peer.gtk; +import java.awt.*; +import java.awt.event.*; +import java.util.*; + +// Note that there is no peer interface for a CheckboxGroup. We +// introduce our own in order to make it easier to keep a piece of +// native state for each one. +public class GtkCheckboxGroupPeer extends GtkGenericPeer +{ + // This maps from a CheckboxGroup to the native peer. + private static WeakHashMap map = new WeakHashMap (); + + // Find the native peer corresponding to a CheckboxGroup. + public static synchronized GtkCheckboxGroupPeer + getCheckboxGroupPeer (CheckboxGroup group) + { + if (group == null) + return null; + GtkCheckboxGroupPeer nat = (GtkCheckboxGroupPeer) map.get (group); + if (nat == null) + { + nat = new GtkCheckboxGroupPeer (); + map.put (group, nat); + } + return nat; + } + + private GtkCheckboxGroupPeer () + { + // We don't need any special state here. Note that we can't store + // a reference to the java-side CheckboxGroup. That would mean + // they could never be collected. + super (null); + } + + // Dispose of our native resources. + public native void dispose (); + + // Remove a given checkbox from this group. + public native void remove (GtkCheckboxPeer box); + + // When collected, clean up the native state. + protected void finalize () + { + dispose (); + } +} diff --git a/gnu/java/awt/peer/gtk/GtkCheckboxPeer.java b/gnu/java/awt/peer/gtk/GtkCheckboxPeer.java index aee2b22a4..48f880400 100644 --- a/gnu/java/awt/peer/gtk/GtkCheckboxPeer.java +++ b/gnu/java/awt/peer/gtk/GtkCheckboxPeer.java @@ -44,11 +44,10 @@ public class GtkCheckboxPeer extends GtkComponentPeer implements CheckboxPeer { // Group from last time it was set. - public CheckboxGroup old_group; + public GtkCheckboxGroupPeer old_group; - public native void nativeCreate (CheckboxGroup group); - public native void nativeSetCheckboxGroup (CheckboxGroup group, - CheckboxGroup old_group); + public native void nativeCreate (GtkCheckboxGroupPeer group); + public native void nativeSetCheckboxGroup (GtkCheckboxGroupPeer group); public native void connectHooks (); public GtkCheckboxPeer (Checkbox c) @@ -63,7 +62,8 @@ public class GtkCheckboxPeer extends GtkComponentPeer public void create () { CheckboxGroup g = ((Checkbox) awtComponent).getCheckboxGroup (); - nativeCreate (g); + old_group = GtkCheckboxGroupPeer.getCheckboxGroupPeer (g); + nativeCreate (old_group); } public void setState (boolean state) @@ -78,8 +78,15 @@ public class GtkCheckboxPeer extends GtkComponentPeer public void setCheckboxGroup (CheckboxGroup group) { - nativeSetCheckboxGroup (group, old_group); - old_group = group; + GtkCheckboxGroupPeer gp + = GtkCheckboxGroupPeer.getCheckboxGroupPeer (group); + if (gp != old_group) + { + if (old_group != null) + old_group.remove (this); + nativeSetCheckboxGroup (gp); + old_group = gp; + } } public void getArgs (Component component, GtkArgList args) @@ -95,4 +102,13 @@ public class GtkCheckboxPeer extends GtkComponentPeer { super.postItemEvent (awtComponent, stateChange); } + + public void dispose () + { + // Notify the group so that the native state can be cleaned up + // appropriately. + if (old_group != null) + old_group.remove (this); + super.dispose (); + } } diff --git a/gnu/java/awt/peer/gtk/GtkComponentPeer.java b/gnu/java/awt/peer/gtk/GtkComponentPeer.java index 7c97484b9..6e3b25290 100644 --- a/gnu/java/awt/peer/gtk/GtkComponentPeer.java +++ b/gnu/java/awt/peer/gtk/GtkComponentPeer.java @@ -121,8 +121,6 @@ public class GtkComponentPeer extends GtkGenericPeer setEnabled (false); } - native public void dispose (); - public void enable () { setEnabled (true); diff --git a/gnu/java/awt/peer/gtk/GtkGenericPeer.java b/gnu/java/awt/peer/gtk/GtkGenericPeer.java index 0991e6082..464f71157 100644 --- a/gnu/java/awt/peer/gtk/GtkGenericPeer.java +++ b/gnu/java/awt/peer/gtk/GtkGenericPeer.java @@ -40,17 +40,22 @@ package gnu.java.awt.peer.gtk; import java.awt.*; import java.awt.event.*; -/* This class will go away with Japhar integration. For use with Sun's JDK - this may be required, unless another method of associating Java objects - with GTK objects is used. */ - public class GtkGenericPeer { - // FIXME: this isn't guaranteed to give unique numbers. - final int native_state = java.lang.System.identityHashCode(this); + final int native_state = getUniqueInteger (); + + // Next native state value we will assign. + private static int next_native_state = 0; + + // The widget or other java-side object we wrap. protected Object awtWidget; + + // Global event queue. protected static EventQueue q = null; + // Dispose of our native state. + public native void dispose (); + protected GtkGenericPeer (Object awtWidget) { this.awtWidget = awtWidget; @@ -67,4 +72,13 @@ public class GtkGenericPeer q.postEvent (new ActionEvent (awtWidget, ActionEvent.ACTION_PERFORMED, command, mods)); } + + // Return a unique integer for use in the native state mapping + // code. We can't use a hash code since that is not guaranteed to + // be unique. + private static synchronized int getUniqueInteger () + { + // Let's assume this will never wrap. + return next_native_state++; + } } diff --git a/gnu/java/awt/peer/gtk/Makefile.am b/gnu/java/awt/peer/gtk/Makefile.am index 565d00109..1f95dc721 100644 --- a/gnu/java/awt/peer/gtk/Makefile.am +++ b/gnu/java/awt/peer/gtk/Makefile.am @@ -7,6 +7,7 @@ EXTRA_DIST = \ GtkArgList.java \ GtkButtonPeer.java \ GtkCanvasPeer.java \ + GtkCheckboxGroupPeer.java \ GtkCheckboxMenuItemPeer.java \ GtkCheckboxPeer.java \ GtkChoicePeer.java \ -- cgit v1.2.1