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