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.getFileDialogAccessor();
  60   
  61         if (filenames == null) {
  62             accessor.setDirectory(fd, null);
  63             accessor.setFile(fd, null);
  64             accessor.setFiles(fd, null, null);
  65         } else {
  66             accessor.setDirectory(fd, directory);
  67             accessor.setFile(fd, filenames[0]);
  68             accessor.setFiles(fd, directory, filenames);
  69         }
  70     }
  71 
  72     /**
  73      * Called exclusively by the native C code.
  74      */
  75     private boolean filenameFilterCallback(String fullname) {
  76         if (fd.getFilenameFilter() == null) {
  77             // no filter, accept all.
  78             return true;
  79         }
  80 
  81         File filen = new File(fullname);
  82         return fd.getFilenameFilter().accept(new File(filen.getParent()),
  83                 filen.getName());
  84     }
  85 
  86     @Override
  87     public void setVisible(boolean b) {
  88         XToolkit.awtLock();
  89         try {
  90             if (b) {
  91                 Thread t = new Thread() {
  92                     public void run() {
  93                         GtkFileDialogPeer.this.run(fd.getTitle(), fd.getMode(), 
  94                                 fd.getDirectory(), fd.getFile(), 
  95                                 fd.getFilenameFilter(), fd.isMultipleMode());
  96                         fd.setVisible(false);
  97                     }
  98                 };
  99                 t.start();
 100             } else {
 101                 quit();
 102                 fd.setVisible(false);
 103             }
 104         } finally {
 105             XToolkit.awtUnlock();
 106         }
 107     }
 108 
 109     @Override
 110     public void dispose() {
 111         quit();
 112         super.dispose();
 113     }
 114 
 115     @Override
 116     public void setDirectory(String dir) {
 117         // We do not implement this method because we
 118         // have delegated to FileDialog#setDirectory
 119     }
 120 
 121     @Override
 122     public void setFile(String file) {
 123         // We do not implement this method because we
 124         // have delegated to FileDialog#setFile
 125     }
 126 
 127     @Override
 128     public void setFilenameFilter(FilenameFilter filter) {
 129         // We do not implement this method because we
 130         // have delegated to FileDialog#setFilenameFilter
 131     }
 132 }