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 sun.awt.AWTAccessor; 33 34 /** 35 * FileDialogPeer for the GtkFileChooser. 36 * 37 * @author Costantino Cerbo (c.cerbo@gmail.com) 38 */ 39 class GtkFileDialogPeer extends XDialogPeer implements FileDialogPeer { 40 41 private FileDialog fd; 42 43 // A pointer to the native GTK FileChooser widget 44 private volatile long widget = 0L; 45 46 public GtkFileDialogPeer(FileDialog fd) { 47 super((Dialog) fd); 48 this.fd = fd; 49 } 50 51 private static native void initIDs(); 52 static { 53 initIDs(); 54 } 55 56 private native void run(String title, int mode, String dir, String file, 57 FilenameFilter filter, boolean isMultipleMode, int x, int y); 58 private native void quit(); 59 60 @Override 61 public native void toFront(); 62 63 @Override 64 public native void setBounds(int x, int y, int width, int height, int op); 65 66 /** 67 * Called exclusively by the native C code. 68 */ 69 private void setFileInternal(String directory, String[] filenames) { 70 AWTAccessor.FileDialogAccessor accessor = AWTAccessor 71 .getFileDialogAccessor(); 72 73 if (filenames == null) { 74 accessor.setDirectory(fd, null); 75 accessor.setFile(fd, null); 76 accessor.setFiles(fd, null, null); 77 } else { 78 // Fix 6987233: add the trailing slash if it's absent 79 String with_separator = directory; 80 if (directory != null) { 81 with_separator = directory.endsWith(File.separator) ? 82 directory : (directory + File.separator); 83 } 84 accessor.setDirectory(fd, with_separator); 85 accessor.setFile(fd, filenames[0]); 86 accessor.setFiles(fd, directory, filenames); 87 } 88 } 89 90 /** 91 * Called exclusively by the native C code. 92 */ 93 private boolean filenameFilterCallback(String fullname) { 94 if (fd.getFilenameFilter() == null) { 95 // no filter, accept all. 96 return true; 97 } 98 99 File filen = new File(fullname); 100 return fd.getFilenameFilter().accept(new File(filen.getParent()), 101 filen.getName()); 102 } 103 104 @Override 105 public void setVisible(boolean b) { 106 XToolkit.awtLock(); 107 try { 108 if (b) { 109 Thread t = new Thread() { 110 public void run() { 111 showNativeDialog(); 112 fd.setVisible(false); 113 } 114 }; 115 t.start(); 116 } else { 117 quit(); 118 fd.setVisible(false); 119 } 120 } finally { 121 XToolkit.awtUnlock(); 122 } 123 } 124 125 @Override 126 public void dispose() { 127 quit(); 128 super.dispose(); 129 } 130 131 @Override 132 public void setDirectory(String dir) { 133 // We do not implement this method because we 134 // have delegated to FileDialog#setDirectory 135 } 136 137 @Override 138 public void setFile(String file) { 139 // We do not implement this method because we 140 // have delegated to FileDialog#setFile 141 } 142 143 @Override 144 public void setFilenameFilter(FilenameFilter filter) { 145 // We do not implement this method because we 146 // have delegated to FileDialog#setFilenameFilter 147 } 148 149 private void showNativeDialog() { 150 String dirname = fd.getDirectory(); 151 // File path has a priority against directory path. 152 String filename = fd.getFile(); 153 if (filename != null) { 154 final File file = new File(filename); 155 if (fd.getMode() == FileDialog.LOAD 156 && dirname != null 157 && file.getParent() == null) { 158 // File path for gtk_file_chooser_set_filename. 159 filename = dirname + (dirname.endsWith(File.separator) ? "" : 160 File.separator) + filename; 161 } 162 if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) { 163 // Filename for gtk_file_chooser_set_current_name. 164 filename = file.getName(); 165 // Directory path for gtk_file_chooser_set_current_folder. 166 dirname = file.getParent(); 167 } 168 } 169 GtkFileDialogPeer.this.run(fd.getTitle(), fd.getMode(), dirname, 170 filename, fd.getFilenameFilter(), 171 fd.isMultipleMode(), fd.getX(), fd.getY()); 172 } 173 174 }