1 /*
   2  * Copyright (c) 2010, 2011, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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     // A pointer to the native GTK FileChooser widget
  46     private volatile long widget = 0L;
  47 
  48     public GtkFileDialogPeer(FileDialog fd) {
  49         super((Dialog) fd);
  50         this.fd = fd;
  51     }
  52 
  53     private static native void initIDs();
  54     static {
  55         initIDs();
  56     }
  57 
  58     private native void run(String title, int mode, String dir, String file,
  59                             FilenameFilter filter, boolean isMultipleMode, int x, int y);
  60     private native void quit();
  61 
  62     @Override
  63     public native void toFront();
  64 
  65     @Override
  66     public native void setBounds(int x, int y, int width, int height, int op);
  67 
  68     /**
  69      * Called exclusively by the native C code.
  70      */
  71     private void setFileInternal(String directory, String[] filenames) {
  72         AWTAccessor.FileDialogAccessor accessor = AWTAccessor
  73                 .getFileDialogAccessor();
  74 
  75         if (filenames == null) {
  76             accessor.setDirectory(fd, null);
  77             accessor.setFile(fd, null);
  78             accessor.setFiles(fd, null, null);
  79         } else {
  80             // Fix 6987233: add the trailing slash if it's absent
  81             accessor.setDirectory(fd, directory +
  82                     (directory.endsWith(File.separator) ?
  83                      "" : File.separator));
  84             accessor.setFile(fd, filenames[0]);
  85             accessor.setFiles(fd, directory, filenames);
  86         }
  87     }
  88 
  89     /**
  90      * Called exclusively by the native C code.
  91      */
  92     private boolean filenameFilterCallback(String fullname) {
  93         if (fd.getFilenameFilter() == null) {
  94             // no filter, accept all.
  95             return true;
  96         }
  97 
  98         File filen = new File(fullname);
  99         return fd.getFilenameFilter().accept(new File(filen.getParent()),
 100                 filen.getName());
 101     }
 102 
 103     @Override
 104     public void setVisible(boolean b) {
 105         XToolkit.awtLock();
 106         try {
 107             if (b) {
 108                 Thread t = new Thread() {
 109                     public void run() {
 110                         GtkFileDialogPeer.this.run(fd.getTitle(), fd.getMode(),
 111                                    fd.getDirectory(), fd.getFile(), fd.getFilenameFilter(), fd.isMultipleMode(),
 112                                    fd.getX(), fd.getY());
 113                         fd.setVisible(false);
 114                     }
 115                 };
 116                 t.start();
 117             } else {
 118                 quit();
 119                 fd.setVisible(false);
 120             }
 121         } finally {
 122             XToolkit.awtUnlock();
 123         }
 124     }
 125 
 126     @Override
 127     public void dispose() {
 128         quit();
 129         super.dispose();
 130     }
 131 
 132     @Override
 133     public void setDirectory(String dir) {
 134         // We do not implement this method because we
 135         // have delegated to FileDialog#setDirectory
 136     }
 137 
 138     @Override
 139     public void setFile(String file) {
 140         // We do not implement this method because we
 141         // have delegated to FileDialog#setFile
 142     }
 143 
 144     @Override
 145     public void setFilenameFilter(FilenameFilter filter) {
 146         // We do not implement this method because we
 147         // have delegated to FileDialog#setFilenameFilter
 148     }
 149 }