1 /* 2 * Copyright (c) 2010, 2014, 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.*; 28 import java.awt.event.ComponentEvent; 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 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(long ownerWindow, String title, int mode, String dir, String file, 57 FilenameFilter filter, boolean isMultipleMode, int x, int y); 58 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 void notifyConfigure(long window, int x, int y, int width, int height) { 101 XToolkit.awtLock(); 102 try { 103 Insets insets = XWM.getInsetsFromExtents(window); 104 if (insets == null) { 105 insets = new Insets(0,0,0,0); 106 } 107 108 int resX = x - insets.left; 109 int resY = y - insets.top; 110 int resWidth = width + insets.left + insets.right; 111 int resHeight = height + insets.top + insets.bottom; 112 113 Rectangle bounds = target.getBounds(); 114 115 if (bounds.x != resX || bounds.y != resY) { 116 AWTAccessor.getComponentAccessor().setLocation(target, resX, resY); 117 postEvent(new ComponentEvent(target, ComponentEvent.COMPONENT_MOVED)); 118 } 119 120 if (bounds.width != resWidth || bounds.height != resHeight) { 121 AWTAccessor.getComponentAccessor().setSize(target, resWidth, resHeight); 122 postEvent(new ComponentEvent(target, ComponentEvent.COMPONENT_RESIZED)); 123 } 124 } finally { 125 XToolkit.awtUnlock(); 126 } 127 } 128 129 /** 130 * Called exclusively by the native C code. 131 */ 132 private boolean filenameFilterCallback(String fullname) { 133 if (fd.getFilenameFilter() == null) { 134 // no filter, accept all. 135 return true; 136 } 137 138 File filen = new File(fullname); 139 return fd.getFilenameFilter().accept(new File(filen.getParent()), 140 filen.getName()); 141 } 142 143 @Override 144 public void setVisible(boolean b) { 145 XToolkit.awtLock(); 146 try { 147 if (b) { 148 Thread t = new Thread() { 149 public void run() { 150 showNativeDialog(); 151 fd.setVisible(false); 152 } 153 }; 154 t.start(); 155 } else { 156 quit(); 157 fd.setVisible(false); 158 } 159 } finally { 160 XToolkit.awtUnlock(); 161 } 162 } 163 164 @Override 165 public void dispose() { 166 quit(); 167 super.dispose(); 168 } 169 170 @Override 171 public void setDirectory(String dir) { 172 // We do not implement this method because we 173 // have delegated to FileDialog#setDirectory 174 } 175 176 @Override 177 public void setFile(String file) { 178 // We do not implement this method because we 179 // have delegated to FileDialog#setFile 180 } 181 182 @Override 183 public void setFilenameFilter(FilenameFilter filter) { 184 // We do not implement this method because we 185 // have delegated to FileDialog#setFilenameFilter 186 } 187 188 private void showNativeDialog() { 189 long ownerWindow = 0L; 190 191 XWindowPeer ownerPeer = getOwnerPeer(); 192 if (ownerPeer != null) { 193 ownerWindow = ownerPeer.getWindow(); 194 } 195 196 String dirname = fd.getDirectory(); 197 // File path has a priority against directory path. 198 String filename = fd.getFile(); 199 if (filename != null) { 200 final File file = new File(filename); 201 if (fd.getMode() == FileDialog.LOAD 202 && dirname != null 203 && file.getParent() == null) { 204 // File path for gtk_file_chooser_set_filename. 205 filename = dirname + (dirname.endsWith(File.separator) ? "" : 206 File.separator) + filename; 207 } 208 if (fd.getMode() == FileDialog.SAVE && file.getParent() != null) { 209 // Filename for gtk_file_chooser_set_current_name. 210 filename = file.getName(); 211 // Directory path for gtk_file_chooser_set_current_folder. 212 dirname = file.getParent(); 213 } 214 } 215 216 run(ownerWindow, fd.getTitle(), fd.getMode(), dirname, filename, 217 fd.getFilenameFilter(), fd.isMultipleMode(), fd.getX(), fd.getY()); 218 } 219 }