1 /* 2 * Copyright (c) 2010, 2015, 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.FileDialog; 28 import java.awt.peer.FileDialogPeer; 29 import java.io.File; 30 import java.io.FilenameFilter; 31 import sun.awt.AWTAccessor; 32 import sun.misc.ManagedLocalsThread; 33 34 /** 35 * FileDialogPeer for the GtkFileChooser. 36 * 37 * @author Costantino Cerbo (c.cerbo@gmail.com) 38 */ 39 final class GtkFileDialogPeer extends XDialogPeer implements FileDialogPeer { 40 41 private final FileDialog fd; 42 43 // A pointer to the native GTK FileChooser widget 44 private volatile long widget = 0L; 45 46 GtkFileDialogPeer(FileDialog fd) { 47 super(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); 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 87 int filesNumber = (filenames != null) ? filenames.length : 0; 88 File[] files = new File[filesNumber]; 89 for (int i = 0; i < filesNumber; i++) { 90 files[i] = new File(directory, filenames[i]); 91 } 92 accessor.setFiles(fd, files); 93 } 94 } 95 96 /** 97 * Called exclusively by the native C code. 98 */ 99 private boolean filenameFilterCallback(String fullname) { 100 if (fd.getFilenameFilter() == null) { 101 // no filter, accept all. 102 return true; 103 } 104 105 File filen = new File(fullname); 106 return fd.getFilenameFilter().accept(new File(filen.getParent()), 107 filen.getName()); 108 } 109 110 @Override 111 public void setVisible(boolean b) { 112 XToolkit.awtLock(); 113 try { 114 if (b) { 115 Runnable task = () -> { 116 showNativeDialog(); 117 fd.setVisible(false); 118 }; 119 new ManagedLocalsThread(task).start(); 120 } else { 121 quit(); 122 fd.setVisible(false); 123 } 124 } finally { 125 XToolkit.awtUnlock(); 126 } 127 } 128 129 @Override 130 public void dispose() { 131 quit(); 132 super.dispose(); 133 } 134 135 @Override 136 public void setDirectory(String dir) { 137 // We do not implement this method because we 138 // have delegated to FileDialog#setDirectory 139 } 140 141 @Override 142 public void setFile(String file) { 143 // We do not implement this method because we 144 // have delegated to FileDialog#setFile 145 } 146 147 @Override 148 public void setFilenameFilter(FilenameFilter filter) { 149 // We do not implement this method because we 150 // have delegated to FileDialog#setFilenameFilter 151 } 152 153 private void showNativeDialog() { 154 String dirname = fd.getDirectory(); 155 // File path has a priority against directory path. 156 String filename = fd.getFile(); 157 if (filename != null) { 158 final File file = new File(filename); 159 if (fd.getMode() == FileDialog.LOAD 160 && dirname != null 161 && file.getParent() == null) { 162 // File path for gtk_file_chooser_set_filename. 163 filename = dirname + (dirname.endsWith(File.separator) ? "" : 164 File.separator) + filename; 165 } 166 if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) { 167 // Filename for gtk_file_chooser_set_current_name. 168 filename = file.getName(); 169 // Directory path for gtk_file_chooser_set_current_folder. 170 dirname = file.getParent(); 171 } 172 } 173 run(fd.getTitle(), fd.getMode(), dirname, filename, 174 fd.getFilenameFilter(), fd.isMultipleMode(), fd.getX(), fd.getY()); 175 } 176 }