1 /*
   2  * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 package sun.awt.X11;
  26 
  27 import java.awt.Dialog;
  28 import java.awt.FileDialog;
  29 import java.awt.peer.FileDialogPeer;
  30 import java.io.File;
  31 import java.io.FilenameFilter;
  32 import javax.swing.SwingUtilities;
  33 import javax.swing.SwingWorker;
  34 import sun.awt.AWTAccessor;
  35 
  36 /**
  37  * FileDialogPeer for the GtkFileChooser.
  38  * 
  39  * @author Costantino Cerbo (c.cerbo@gmail.com)
  40  */
  41 class GtkFileDialogPeer extends XDialogPeer implements FileDialogPeer {
  42 
  43     private FileDialog fd;
  44 
  45     public GtkFileDialogPeer(FileDialog fd) {
  46         super((Dialog) fd);
  47         this.fd = fd;
  48     }
  49 
  50     private native void run(String title, int mode, String dir, String file,
  51             FilenameFilter filter, boolean isMultipleMode);
  52 
  53     private native void quit();
  54 
  55     /**
  56      * Called exclusively by the native C code.
  57      */
  58     private void setFileInternal(String directory, String[] filenames) {
  59         AWTAccessor.FileDialogAccessor accessor = AWTAccessor
  60                 .getFileDialogAccessor();
  61 
  62         if (filenames == null) {
  63             accessor.setDirectory(fd, null);
  64             accessor.setFile(fd, null);
  65             accessor.setFiles(fd, null, null);
  66         } else {
  67             accessor.setDirectory(fd, directory);
  68             accessor.setFile(fd, filenames[0]);
  69             accessor.setFiles(fd, directory, filenames);
  70         }
  71     }
  72 
  73     /**
  74      * Called exclusively by the native C code.
  75      */
  76     private boolean filenameFilterCallback(String fullname) {
  77         if (fd.getFilenameFilter() == null) {
  78             // no filter, accept all.
  79             return true;
  80         }
  81 
  82         File filen = new File(fullname);
  83         return fd.getFilenameFilter().accept(new File(filen.getParent()),
  84                 filen.getName());
  85     }
  86 
  87     @Override
  88     public void setVisible(boolean b) {
  89         XToolkit.awtLock();
  90         try {
  91             if (b) {
  92                 Thread t = new Thread() {
  93                     public void run() {
  94                         GtkFileDialogPeer.this.run(fd.getTitle(), fd.getMode(),
  95                                 fd.getDirectory(), fd.getFile(), fd
  96                                         .getFilenameFilter(), fd
  97                                         .isMultipleMode());
  98                         fd.setVisible(false);
  99                     }
 100                 };
 101                 t.start();
 102             } else {
 103                 quit();
 104                 fd.setVisible(false);
 105             }
 106         } finally {
 107             XToolkit.awtUnlock();
 108         }
 109     }
 110 
 111     @Override
 112     public void dispose() {
 113         quit();
 114         super.dispose();
 115     }
 116 
 117     @Override
 118     public void setDirectory(String dir) {
 119         // We do not implement this method because we
 120         // have delegated to FileDialog#setDirectory
 121     }
 122 
 123     @Override
 124     public void setFile(String file) {
 125         // We do not implement this method because we
 126         // have delegated to FileDialog#setFile
 127     }
 128 
 129     @Override
 130     public void setFilenameFilter(FilenameFilter filter) {
 131         // We do not implement this method because we
 132         // have delegated to FileDialog#setFilenameFilter
 133     }
 134 }