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 accessor.setDirectory(fd, directory + 80 (directory.endsWith(File.separator) ? 81 "" : File.separator)); 82 accessor.setFile(fd, filenames[0]); 83 accessor.setFiles(fd, directory, filenames); 84 } 85 } 86 87 /** 88 * Called exclusively by the native C code. 89 */ 90 private boolean filenameFilterCallback(String fullname) { 91 if (fd.getFilenameFilter() == null) { 92 // no filter, accept all. 93 return true; 94 } 95 96 File filen = new File(fullname); 97 return fd.getFilenameFilter().accept(new File(filen.getParent()), 98 filen.getName()); 99 } 100 101 @Override 102 public void setVisible(boolean b) { 103 XToolkit.awtLock(); 104 try { 105 if (b) { 106 Thread t = new Thread() { 107 public void run() { 108 showNativeDialog(); 109 fd.setVisible(false); 110 } 111 }; 112 t.start(); 113 } else { 114 quit(); 115 fd.setVisible(false); 116 } 117 } finally { 118 XToolkit.awtUnlock(); 119 } 120 } 121 122 @Override 123 public void dispose() { 124 quit(); 125 super.dispose(); 126 } 127 128 @Override 129 public void setDirectory(String dir) { 130 // We do not implement this method because we 131 // have delegated to FileDialog#setDirectory 132 } 133 134 @Override 135 public void setFile(String file) { 136 // We do not implement this method because we 137 // have delegated to FileDialog#setFile 138 } 139 140 @Override 141 public void setFilenameFilter(FilenameFilter filter) { 142 // We do not implement this method because we 143 // have delegated to FileDialog#setFilenameFilter 144 } 145 146 private void showNativeDialog() { 147 String dirname = fd.getDirectory(); 148 // File path has a priority against directory path. 149 String filename = fd.getFile(); 150 if (filename != null) { 151 final File file = new File(filename); 152 if (fd.getMode() == FileDialog.LOAD 153 && dirname != null 154 && file.getParent() == null) { 155 // File path for gtk_file_chooser_set_filename. 156 filename = dirname + (dirname.endsWith(File.separator) ? "" : 157 File.separator) + filename; 158 } 159 if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) { 160 // Filename for gtk_file_chooser_set_current_name. 161 filename = file.getName(); 162 // Directory path for gtk_file_chooser_set_current_folder. 163 dirname = file.getParent(); 164 } 165 } 166 GtkFileDialogPeer.this.run(fd.getTitle(), fd.getMode(), dirname, 167 filename, fd.getFilenameFilter(), 168 fd.isMultipleMode(), fd.getX(), fd.getY()); 169 } 170 171 }