This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] More List fixes


Tom and Bryce,

Trying to avoid changes to ListPeer.java (and so dodging the question
of compatibility) I used reflection to check if the method is available.
I've optimized to check just once.

Should I check this in instead of the original patch?

Regards to all,
Fernando


2003-12-12 Fernando Nasser <fnasser@redhat.com>


        * java/awt/List.java (replaceItem): Use direct implementation
        if available.
        * gnu/java/awt/peer/gtk/GtkListPeer.java: Declare replaceItem as
        native.
        (handleEvent): Only generate ActionEvent on double-click if
        something is selected.
        * jni/gtk-peer/gnu_java_awt_peer_gtk_GtkListPeer.c
        (Java_gnu_java_awt_peer_gtk_GtkListPeer_replaceItem): New function.
        Native implementation of replaceItem.

--
Fernando Nasser
Red Hat Canada Ltd.                     E-Mail:  fnasser@redhat.com
2323 Yonge Street, Suite #300
Toronto, Ontario   M4P 2C9
Index: gnu/java/awt/peer/gtk/GtkListPeer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/awt/peer/gtk/GtkListPeer.java,v
retrieving revision 1.5
diff -c -p -r1.5 GtkListPeer.java
*** gnu/java/awt/peer/gtk/GtkListPeer.java	11 Dec 2003 13:50:50 -0000	1.5
--- gnu/java/awt/peer/gtk/GtkListPeer.java	15 Dec 2003 20:30:41 -0000
*************** public class GtkListPeer extends GtkComp
*** 81,86 ****
--- 81,87 ----
    }
    
    public native void delItems (int start, int end);
+   public native void replaceItem(String item, int index);
    public native void deselect (int index);
    
    public Dimension getMinimumSize (int rows)
*************** public class GtkListPeer extends GtkComp
*** 135,142 ****
  	if (!me.isConsumed ()
  	    && (me.getModifiers () & MouseEvent.BUTTON1_MASK) != 0
  	    && me.getClickCount() == 2)
! 	  postActionEvent (((List)awtComponent).getSelectedItem (), 
! 			   me.getModifiers ());
        }
  
      if (e.getID () == KeyEvent.KEY_PRESSED)
--- 136,150 ----
  	if (!me.isConsumed ()
  	    && (me.getModifiers () & MouseEvent.BUTTON1_MASK) != 0
  	    && me.getClickCount() == 2)
! 	  {
!             String selectedItem = ((List)awtComponent).getSelectedItem ();
! 
!             /* Double-click only generates an Action event
! 	       if something is selected */
!             if (selectedItem != null)
! 	      postActionEvent (((List)awtComponent).getSelectedItem (), 
! 			       me.getModifiers ());
! 	  }
        }
  
      if (e.getID () == KeyEvent.KEY_PRESSED)
Index: java/awt/List.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/awt/List.java,v
retrieving revision 1.15
diff -c -p -r1.15 List.java
*** java/awt/List.java	2 Dec 2003 16:19:28 -0000	1.15
--- java/awt/List.java	15 Dec 2003 20:30:41 -0000
*************** import java.awt.event.ActionListener;
*** 43,48 ****
--- 43,49 ----
  import java.awt.event.ItemEvent;
  import java.awt.event.ItemListener;
  import java.awt.peer.ListPeer;
+ import java.lang.reflect.Method;
  import java.util.EventListener;
  import java.util.Vector;
  import javax.accessibility.Accessible;
*************** private ItemListener item_listeners;
*** 105,110 ****
--- 106,114 ----
  // The list of ActionListeners for this object.
  private ActionListener action_listeners;
  
+ // Whether the ListPeer implementation contains a replaceItem method
+ private Boolean hasReplaceMethod = null;
+ 
  /*************************************************************************/
  
  /*
*************** clear()
*** 647,654 ****
  public synchronized void
  replaceItem(String item, int index) throws IllegalArgumentException
  {
!   remove(index);
!   addItem(item, index);
  }
  
  /*************************************************************************/
--- 651,712 ----
  public synchronized void
  replaceItem(String item, int index) throws IllegalArgumentException
  {
!   Method replace = null;
! 
!   items.removeElementAt (index);
!   if ((index == -1) || (index >= items.size()))
!     items.addElement(item);
!   else
!     items.insertElementAt(item, index);
! 
!   if (peer != null)
!     {
!       ListPeer l = (ListPeer) peer;
! 
!       // Check if the implementation class has a method for replaceItem      
!       if (null == hasReplaceMethod)
!         {
!           Class lclass = l.getClass();
!           //System.out.println("l is a " + lclass.getName() +
! 	  //                   " extends " + lclass.getSuperclass().getName());
!           Class[] parms = {String.class, int.class};
! 
!           try
!             {
! 	      replace = lclass.getMethod ("replaceItem", parms);
! 	      hasReplaceMethod = Boolean.TRUE;
! 	    }
!           catch (NoSuchMethodException e)
! 	    {
! 	      hasReplaceMethod = Boolean.FALSE;
! 	    }
! 	}
! 
!       /* Gtk implements a replaceItem method which
!          allows us to directly replace an entry */
!       if (hasReplaceMethod.booleanValue())
! 	{
! 	  Object[] args = {item, new Integer(index)};
! 
!           try
! 	    {
! 	      replace.invoke (l, args);
! 	    }
! 	  catch (Exception e)
! 	    {
! 	       // Doesn't work; fall back to brute force
! 	       hasReplaceMethod = Boolean.FALSE;
! 	    }
! 	}
! 
!       if (!hasReplaceMethod.booleanValue())
!         {
! 	  /* In the general case we have to remove and re-add as
! 	     the ListPeer interface does not have a replaceItem */
!           l.delItems (index, index);
!           l.add (item, index);
! 	}
!     }
  }
  
  /*************************************************************************/
Index: jni/gtk-peer/gnu_java_awt_peer_gtk_GtkListPeer.c
===================================================================
RCS file: /cvs/gcc/gcc/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkListPeer.c,v
retrieving revision 1.7
diff -c -p -r1.7 gnu_java_awt_peer_gtk_GtkListPeer.c
*** jni/gtk-peer/gnu_java_awt_peer_gtk_GtkListPeer.c	13 Dec 2003 01:15:47 -0000	1.7
--- jni/gtk-peer/gnu_java_awt_peer_gtk_GtkListPeer.c	15 Dec 2003 20:30:41 -0000
*************** Java_gnu_java_awt_peer_gtk_GtkListPeer_a
*** 173,178 ****
--- 173,197 ----
    (*env)->ReleaseStringUTFChars (env, text, str);
  }
  
+ JNIEXPORT void JNICALL
+ Java_gnu_java_awt_peer_gtk_GtkListPeer_replaceItem
+   (JNIEnv *env, jobject obj, jstring item, jint index)
+ {
+   void *ptr;
+   GtkCList *list;
+   const gchar *str;
+     
+   ptr = NSA_GET_PTR (env, obj);
+   str = (*env)->GetStringUTFChars (env, item, NULL);
+ 
+   gdk_threads_enter ();
+ 
+   list = CLIST_FROM_SW (ptr);
+ 
+   gtk_clist_set_text (list, index, 0, str);
+ 
+   gdk_threads_leave ();
+ }
  
  JNIEXPORT void JNICALL
  Java_gnu_java_awt_peer_gtk_GtkListPeer_delItems

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]