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]

[gui] [PATCH] Implement GtkFileDialogPeer.setFilenameFilter()


Hello,

I committed the following patch to the java-gui-branch.  It implements
GtkFileDialogPeer.setFilenameFilter(), using gtk+ 2.4's new
gtk_file_filter_add_custom() interface.  The patch also includes other
small improvements to the FileDialog peer.

-David Jee

2004-07-20  David Jee  <djee@redhat.com>

        * gnu/java/awt/peer/gtk/GtkFileDialogPeer.java:
        Collect all native method declaration at the top.
        (create): Set the filename filter if necessary.
        (setDirectory): Call nativeSetDirectory().
        (setFilenameFilter): Implement.
        (filenameFilterCallback): New method.
        * jni/gtk-peer/gnu_java_awt_peer_gtk_GtkFileDialogPeer.c
        (create): Configure dialog to show hidden files.
        (filenameFilterCallback): New function.
        (nativeSetFilenameFilter): New function.
        (nativeSetDirectory): New function.


Index: gnu/java/awt/peer/gtk/GtkFileDialogPeer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/awt/peer/gtk/GtkFileDialogPeer.java,v
retrieving revision 1.6.4.1
diff -u -r1.6.4.1 GtkFileDialogPeer.java
--- gnu/java/awt/peer/gtk/GtkFileDialogPeer.java	15 Jul 2004 21:07:25 -0000	1.6.4.1
+++ gnu/java/awt/peer/gtk/GtkFileDialogPeer.java	20 Jul 2004 15:35:12 -0000
@@ -45,6 +45,7 @@
 import java.awt.event.WindowEvent;
 import java.awt.peer.FileDialogPeer;
 import java.io.FilenameFilter;
+import java.io.File;
 
 public class GtkFileDialogPeer extends GtkDialogPeer implements FileDialogPeer
 {
@@ -52,13 +53,27 @@
   
   private String currentFile = null;
   private String currentDirectory = null;
+  private FilenameFilter filter;
 
   native void create (GtkContainerPeer parent);
+  native void connectJObject ();
+  native void connectSignals ();
+  native void nativeSetFile (String file);
+  native public String nativeGetDirectory();
+  native public void nativeSetDirectory(String directory);
+  native void nativeSetFilenameFilter (FilenameFilter filter);
 
   public void create() {
     create((GtkContainerPeer) awtComponent.getParent().getPeer());
-    setDirectory(((FileDialog) awtComponent).getDirectory());
-    setFile(((FileDialog) awtComponent).getFile());
+
+    FileDialog fd = (FileDialog) awtComponent;
+
+    setDirectory(fd.getDirectory());
+    setFile(fd.getFile());
+
+    FilenameFilter filter = fd.getFilenameFilter();
+    if (filter != null)
+      setFilenameFilter(filter);
   }
 
   public GtkFileDialogPeer (FileDialog fd)
@@ -66,10 +81,6 @@
     super (fd);
   }
 
-  native void connectJObject ();
-  native void connectSignals ();
-  native void nativeSetFile (String file);
-
   public void setFile (String fileName)
   {
     /* If nothing changed do nothing.  This usually happens because
@@ -99,8 +110,6 @@
       }
   }
 
-  native public String nativeGetDirectory();
-
   public void setDirectory (String directory)
   {
     /* If nothing changed so nothing.  This usually happens because
@@ -118,20 +127,25 @@
       }
       
     currentDirectory = directory;
-
-    // Gtk expects the directory to end with a file separator
-    if (directory.substring (directory.length () - 1).equals (FS))
-      nativeSetFile (directory);
-    else
-      nativeSetFile (directory + FS);
+    nativeSetDirectory (directory);
   }
 
   public void setFilenameFilter (FilenameFilter filter)
   {
-    /* GTK has no filter callbacks yet.  It works by setting a pattern
-     * (see gtk_file_selection_complete), which we can't convert
-     * to the callback paradigm. With GTK-2.4 there will be a
-     * gtk_file_filter_add_custom function that we can use. */
+    this.filter = filter;
+    nativeSetFilenameFilter(filter);
+  }
+
+  /* This method interacts with the native callback function of the
+     same name.  The native function will extract the filename from the
+     GtkFileFilterInfo object and send it to this method, which will
+     in turn call the filter's accept() method and give back the return
+     value. */
+  boolean filenameFilterCallback (String fullname) {
+    String filename = fullname.substring(fullname.lastIndexOf(FS) + 1);
+    String dirname = fullname.substring(0, fullname.lastIndexOf(FS));
+    File dir = new File(dirname);
+    return filter.accept(dir, filename);
   }
 
   public Graphics getGraphics ()
Index: jni/gtk-peer/gnu_java_awt_peer_gtk_GtkFileDialogPeer.c
===================================================================
RCS file: /cvs/gcc/gcc/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkFileDialogPeer.c,v
retrieving revision 1.4.8.3
diff -u -r1.4.8.3 gnu_java_awt_peer_gtk_GtkFileDialogPeer.c
--- jni/gtk-peer/gnu_java_awt_peer_gtk_GtkFileDialogPeer.c	15 Jul 2004 21:07:25 -0000	1.4.8.3
+++ jni/gtk-peer/gnu_java_awt_peer_gtk_GtkFileDialogPeer.c	20 Jul 2004 15:35:12 -0000
@@ -69,6 +69,9 @@
                                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                        NULL);
 
+  /* GtkFileChooser doesn't show hidden files by default. */
+  g_object_set(GTK_FILE_CHOOSER(widget), "show_hidden", TRUE);
+
   /* GtkFileSelect is not modal by default */
   gtk_window_set_modal (GTK_WINDOW (widget), TRUE);
 
@@ -136,9 +139,72 @@
   return (*env)->NewStringUTF(env, str);
 }
 
-/*
- * Set the filename in the file selection dialog.
- */
+
+/* This function interfaces with the Java callback method of the same name.
+   This function extracts the filename from the GtkFileFilterInfo object,
+   and passes it to the Java method.  The Java method will call the filter's
+   accept() method and will give back the return value. */
+gboolean filenameFilterCallback (const GtkFileFilterInfo *filter_info,
+                                 gpointer obj)
+{
+  gchar* dirname;
+  jclass cx;
+  jmethodID id;
+  jstring *filename;
+
+  cx = (*gdk_env)->GetObjectClass (gdk_env, (jobject) obj);
+  id = (*gdk_env)->GetMethodID (gdk_env, cx, "filenameFilterCallback",
+                                             "(Ljava/lang/String;)Z");
+
+  filename = (*gdk_env)->NewStringUTF(gdk_env, filter_info->filename);
+
+  gdk_threads_leave();
+  gboolean accepted = (*gdk_env)->CallBooleanMethod(gdk_env, obj, id, filename);
+  gdk_threads_enter();
+
+  return accepted;
+}
+
+JNIEXPORT void JNICALL 
+Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_nativeSetFilenameFilter
+    (JNIEnv *env, jobject obj, jobject filter_obj)
+{
+  void *ptr;
+  GtkFileFilter *filter;
+
+  ptr = NSA_GET_PTR (env, obj);
+
+  gdk_threads_enter ();
+
+  filter = gtk_file_filter_new();
+  gtk_file_filter_add_custom(filter,
+                             GTK_FILE_FILTER_FILENAME,
+                             G_CALLBACK(filenameFilterCallback),
+                             obj,
+                             NULL);
+
+  gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(ptr), filter);
+
+  gdk_threads_leave ();
+}
+
+JNIEXPORT void JNICALL 
+Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_nativeSetDirectory
+    (JNIEnv *env, jobject obj, jstring directory)
+{
+  void *ptr;
+  const char *str;
+
+  ptr = NSA_GET_PTR (env, obj);
+
+  str = (*env)->GetStringUTFChars (env, directory, 0);
+
+  gdk_threads_enter ();
+  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(ptr), str);
+  gdk_threads_leave ();
+
+  (*env)->ReleaseStringUTFChars (env, directory, str);
+}
 
 JNIEXPORT void JNICALL 
 Java_gnu_java_awt_peer_gtk_GtkFileDialogPeer_nativeSetFile 

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