1 /*
   2  * Copyright 1995-2006 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 
  35 /**
  36  * FileDialogPeer for the GtkFileChooser.
  37  * 
  38  * @author Costantino Cerbo (c.cerbo@gmail.com)
  39  */
  40 class GtkFileDialogPeer extends XDialogPeer implements FileDialogPeer {
  41 
  42     private FileDialog fd;
  43 
  44     public GtkFileDialogPeer(FileDialog fd) {
  45         super((Dialog) fd);
  46         this.fd = fd;
  47     }
  48 
  49     private native void run(String title, int mode, String dir, String file,
  50             FilenameFilter filter);
  51 
  52     private native void quit();
  53 
  54     /**
  55      * Called exclusively by the native C code.
  56      */
  57     private void setFileInternal(String filename) {
  58         if (filename == null || filename.trim().isEmpty()) {
  59             fd.setFile(null);
  60             fd.setDirectory(null);
  61         } else {
  62             File filen = new File(filename);
  63             fd.setFile(filen.getName());
  64             fd.setDirectory(filen.getParent() + File.separator);
  65         }
  66     }
  67 
  68     /**
  69      * Called exclusively by the native C code.
  70      */
  71     private boolean filenameFilterCallback(String fullname) {
  72         if (fd.getFilenameFilter() == null) {
  73             // no filter, accept all.
  74             return true;
  75         }
  76 
  77         File filen = new File(fullname);
  78         return fd.getFilenameFilter().accept(new File(filen.getParent()),
  79                 filen.getName());
  80     }
  81 
  82     @Override
  83     public void setVisible(boolean b) {
  84         XToolkit.awtLock();
  85         try {
  86             if (b) {
  87                 run(fd.getTitle(), fd.getMode(), fd.getDirectory(), 
  88                         fd.getFile(), fd.getFilenameFilter());
  89 
  90                 fd.setVisible(false);
  91             } else {
  92                 quit();
  93                 fd.setVisible(false);
  94             }
  95         } finally {
  96             XToolkit.awtUnlock();
  97         }
  98     }
  99 
 100     @Override
 101     public void dispose() {
 102         quit();
 103         super.dispose();
 104     }
 105 
 106     @Override
 107     public void setDirectory(String dir) {
 108         // We do not implement this method because we
 109         // have delegated to FileDialog#setDirectory
 110     }
 111 
 112     @Override
 113     public void setFile(String file) {
 114         // We do not implement this method because we
 115         // have delegated to FileDialog#setFile
 116     }
 117 
 118     @Override
 119     public void setFilenameFilter(FilenameFilter filter) {
 120         // We do not implement this method because we
 121         // have delegated to FileDialog#setFilenameFilter
 122     }
 123 }