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 private long standaloneWindow; 46 private volatile boolean quit; 47 48 GtkFileDialogPeer(FileDialog fd) { 49 super(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); 79 } else { 80 // Fix 6987233: add the trailing slash if it's absent 81 String with_separator = directory; 82 if (directory != null) { 83 with_separator = directory.endsWith(File.separator) ? 84 directory : (directory + File.separator); 85 } 86 accessor.setDirectory(fd, with_separator); 87 accessor.setFile(fd, filenames[0]); 88 89 int filesNumber = (filenames != null) ? filenames.length : 0; 90 File[] files = new File[filesNumber]; 91 for (int i = 0; i < filesNumber; i++) { 92 files[i] = new File(directory, filenames[i]); 93 } 94 accessor.setFiles(fd, files); 95 } 96 } 97 98 /** 99 * Called exclusively by the native C code. 100 */ 101 private boolean filenameFilterCallback(String fullname) { 102 if (fd.getFilenameFilter() == null) { 103 // no filter, accept all. 104 return true; 105 } 106 107 File filen = new File(fullname); 108 return fd.getFilenameFilter().accept(new File(filen.getParent()), 109 filen.getName()); 110 } 111 112 @Override 113 public void setVisible(boolean b) { 114 XToolkit.awtLock(); 115 try { 116 quit = !b; 117 if (b) { 118 Runnable task = () -> { 119 showNativeDialog(); 120 standaloneWindow = 0; 121 fd.setVisible(false); 122 }; 123 new ManagedLocalsThread(task).start(); 124 } else { 125 quit(); 126 fd.setVisible(false); 127 } 128 } finally { 129 XToolkit.awtUnlock(); 130 } 131 } 132 133 @Override 134 public void dispose() { 135 XToolkit.awtLock(); 136 try { 137 quit = true; 138 quit(); 139 } 140 finally { 141 XToolkit.awtUnlock(); 142 } 143 super.dispose(); 144 } 145 146 @Override 147 public void setDirectory(String dir) { 148 // We do not implement this method because we 149 // have delegated to FileDialog#setDirectory 150 } 151 152 @Override 153 public void setFile(String file) { 154 // We do not implement this method because we 155 // have delegated to FileDialog#setFile 156 } 157 158 protected void requestXFocus(long time, boolean timeProvided) { 159 if(standaloneWindow == 0) { 160 super.requestXFocus(time, timeProvided); 161 return; 162 } 163 XNETProtocol net_protocol = XWM.getWM().getNETProtocol(); 164 if (net_protocol != null) { 165 net_protocol.setActiveWindow(standaloneWindow); 166 } 167 } 168 169 @Override 170 public void setFilenameFilter(FilenameFilter filter) { 171 // We do not implement this method because we 172 // have delegated to FileDialog#setFilenameFilter 173 } 174 175 private void showNativeDialog() { 176 String dirname = fd.getDirectory(); 177 // File path has a priority against directory path. 178 String filename = fd.getFile(); 179 if (filename != null) { 180 final File file = new File(filename); 181 if (fd.getMode() == FileDialog.LOAD 182 && dirname != null 183 && file.getParent() == null) { 184 // File path for gtk_file_chooser_set_filename. 185 filename = dirname + (dirname.endsWith(File.separator) ? "" : 186 File.separator) + filename; 187 } 188 if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) { 189 // Filename for gtk_file_chooser_set_current_name. 190 filename = file.getName(); 191 // Directory path for gtk_file_chooser_set_current_folder. 192 dirname = file.getParent(); 193 } 194 } 195 if (!quit) { 196 run(fd.getTitle(), fd.getMode(), dirname, filename, 197 fd.getFilenameFilter(), fd.isMultipleMode(), fd.getX(), fd.getY()); 198 } 199 } 200 201 /** 202 * Called by native code when GTK dialog is created. 203 */ 204 boolean setWindow(long xid) { 205 if (!quit && widget != 0) { 206 standaloneWindow = xid; 207 requestXFocus(); 208 return true; 209 } 210 return false; 211 } 212 }